const BASE_URL = 'http://127.0.0.1:8088' import store from '@/store/index.js' import api from './api.js' var num = 1 export const myRequest = (options) => { // uni.showLoading({ // title:'拼命加载中...' // }) return new Promise((resolve, reject) => { uni.request({ url: BASE_URL + options.url, method: options.method || 'GET', data: options.data, header: options.noToken ? {} : { AuthorizationToken: 'WX ' + uni.getStorageSync('token') }, success: async (res) => { if (res.data.code == 401) { if (num <= 3) { num++ res = await doRequest(options) }; } resolve(res) }, fail: (err) => { uni.showToast({ title: "请求接口失败" }) reject(JSON.stringify(err)) } // complete:()=>{ // uni.hideLoading() // } }) }) async function doRequest(response) { var datas = { url: '/refreshToken/' + uni.getStorageSync('union_id'), method: 'get', noToken: true } const res = await myRequest(datas) if (res.data.code === 200) { uni.setStorageSync('token', res.data.data.token) let onset = await myRequest(response) console.log(datas) return onset } else { uni.removeStorageSync('union_id') uni.showToast({ title: '登陆过期请重新登陆!', icon: 'none', duration: 1500, success() { setTimeout(() => { uni.navigateTo({ url: '/pages/login/login' }); }, 1500) } }) } } }