闫增涛
2024-12-23 c5ee0e9d22473f3fd96f442724058dd297fec97c
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
80
81
82
83
84
85
86
87
88
89
90
91
92
import config from "../js/config"
import {
  loginInfo
} from '../js/login';
const app = getApp()
let clearStorage = false;
let timer = null;
let backUrl = null
const request = (params) => {
  let fullUrl = `${config.requestCtx}${params.url}`
  let token = wx.getStorageSync(config.tokenKey) ? wx.getStorageSync(config.tokenKey) : ''
  return new Promise((resolve, reject) => {
    wx.request({
      url: fullUrl,
      method: params.method,
      data: params.data ? params.data : {},
      header: {
        'content-type': 'application/json',
        'Authorization': `bearer ${token}`
      },
      success: (res) => {
        if (res.statusCode == 200) {
          return resolve(res.data.data)
        } else if (res.statusCode == 401) {
          const pages = getCurrentPages()
          const currentPage = pages[pages.length - 1]
          const url = currentPage.route
          let options = currentPage.options
          let urlargs = `/${url}?`
          // wx.clearStorageSync();
          if (!clearStorage) {
            clearStorage = true;
            if (!timer) {
              timer = setTimeout(() => {
                clearStorage = false
              }, 4000);
            }
            // urlargs = JSON.stringify(urlargs)
            console.log()
            let strOpt = JSON.stringify(options)
            let currentUrl = {
              options: options,
              backUrl: urlargs
            }
            // currentUrl = JSON.stringify(currentUrl)
            // 不去首页登录,直接调用公共登录方法
            // wx.reLaunch({
            //   url: urlargs ? '/pages/home/home?backUrl=' + encodeURIComponent(currentUrl) : '/pages/home/home'
            // })
            console.log(currentUrl, 123)
            loginInfo(app, (data) => {
              if (data) {
                if (currentUrl.options) {
                  for (let key in options) {
                    const value = currentUrl.options[key]
                    currentUrl.backUrl += `${key}=${value}&`
                  }
                  backUrl = currentUrl.backUrl
                  console.log(backUrl, "url")
                  if (backUrl) {
                    wx.reLaunch({
                      url: backUrl
                    })
                  } else {
                    wx.reLaunch({
                      url: '/pages/home/home'
                    })
                  }
                }
              } else {}
            })
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          reject(res.data.message)
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '接口请求错误',
          icon: 'none'
        })
        reject('接口请求错误')
      },
      complete: () => {}
    })
  })
}
export default request