Blame view

addons/kuerp/uniapp/utils/request.js 1.24 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  /**

   * 网络请求

   */

  import baseURL from "../static/config/index"

  

  class Http {

    constructor(params) {

      this.baseURL = params.baseURL

    }

  

    get(url, data) {

      return this.http('GET', url, data)

    }

  

    post(url, data) {

      return this.http('POST', url, data)

    }

  

    http(method, url, data) {

      return new Promise((resolve, reject) => {

        uni.request({

          url: this.baseURL + url,

          data,

          header: {

            Accept: 'application/json',

            'Content-Type': 'application/json',

            'token': uni.getStorageSync('token')

          },

          method,

          success: ({ data: res }) => {

            if (res.msg === '请先登陆') {

              uni.clearStorageSync()

              uni.reLaunch({ url: '/pages/login/index' })

              return

            }

            if (res.code != 200 && res.msg !== '购物车空空如也' && res.msg !== '查询失败') {

              uni.showToast({

                title: res.msg,

                icon: 'none',

              })

            }

            resolve(res)

          },

          fail: (error) => {

            reject(error)

          },

          complete: () => { }

        })

      })

    }

  }

  

  const http = new Http({ baseURL })

  

  export default http