import env from '@/config/env' /* * config * baseUrl: 请求域名 * url: 请求路径 * method: 请求方法 * header: 请求头 * token: 请求token * data: 请求参数 * */ const request = (config) => { return new Promise((resolve) => { const baseConfig = env[getApp().globalData.getEnvConfig()] const token = config?.token ? config.token : uni.getStorageSync('token') const headerDefault = { appid: getApp().globalData.appid, version: baseConfig.version + '+' + baseConfig.buildNumber, authorization: `Bearer ${token}` } const URL = config.baseUrl ? config.baseUrl + config.url : baseConfig.baseUrl + config.url const method = config.method || 'POST' const header = config.header || headerDefault const data = config.data || {} const timestamp = new Date().getTime() uni.request({ url: URL, method, header, data, timeout: 7000, async success(res) { const { statusCode, data } = res if (statusCode === 200) { const code = data.errorCode const message = data.errorMsg if(code === 403) { uni.removeStorageSync('token') getApp().globalData.updateIsLogin(false) resolve({ code: 403, data, message: '登录已过期' }) uni.showModal({ title: '提示', content: '登录已过期,请重新登录', showCancel: false, success() { uni.reLaunch({ url: '/pages/home/home' }) } }) } else { resolve({ code, data: data.data, message }) } } else { resolve({ code: -1, data, message: '网络不太好哦,请稍后再试' }) } }, async fail() { resolve({ code: -1, data, message: '网络不太好哦,请稍后再试' }) }, async complete(res) { console.log(URL.substring(baseConfig.baseUrl.length + 1), { env: getApp().globalData.getEnvConfig(), url: URL.substring(baseConfig.baseUrl.length + 1), req: config?.data || {}, code: res?.data?.errorCode, res: res?.data?.data, token: header?.authorization || '', message: res?.data?.errorMsg, duration: new Date().getTime() - timestamp }) } }) }) } export default request