k94314517
2024-07-22 8d48f2f55453635bf60e5e00d1e4746288e9da67
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { baseUrl } from "./config"
import { wxLoginCustomer } from '../api/index'
 
export function request(options){
  let loading = options.loading || '1' // 默认值1显示,否则不显示
  if(loading == '1'){
    wx.showLoading()
  }
  return new Promise(resolve => {
    wx.request({
      url: baseUrl + options.url,
      data: options.data || {},
      method: options.method || "GET",
      header: {
        'content-type': 'application/json;charset=utf-8',
        'token': wx.getStorageSync('token') || ''
      },
      timeout: 12000,
      success: (res) => {
        if (res.data.code === 200) {
          resolve(res.data || true)
        } else {
          // if(res.data.code == 5112){
          //   wx.navigateTo({
          //     url: '/pages/auth/auth ',
          //   })
          // }
          if(res.data.code == 5111 || res.data.code == 5112){
            wx.login({
              timeout: 5000,
              success(res) {
                if (res.code) {
                  //发起网络请求
                  wxLoginCustomer({
                    code: res.code
                  }).then(res => {
                    console.log(res)
                    if (res && res.data.member) {
                      wx.switchTab({
                        url: '/pages/index/index',
                      })
                      wx.setStorageSync('member', res.data.member)
                      wx.setStorageSync('openid', res.data.member.openid)
                      wx.setStorageSync('sessionKey', res.data.sessionKey)
                      wx.setStorageSync('token', res.data.token)
                    } else {
                      console.log('登录失败!')
                    }
                  })
                } else {
                  console.log('登录失败!' + res)
                }
              },
              fail(err) {
                console.log(err)
              }
            })
          }
          res.data.message && wx.showToast({
            title: res.data.message || '发生错误',
            icon: 'none'
          })
        }
      },
      fail(err) {
        console.log('err', err);
        err.message && wx.showToast({
          title: err.message || '发生错误',
          icon: 'none'
        })
      },
      complete() {
        if(loading == '1'){
          wx.hideLoading()
        }
      }
    })
  } )
}