import config from '@/config/request' let logouted = 0 const request = options => { const header = Object.assign({}, config.headers, { token: uni.getStorageSync('token') }) const otherUrl = ['queryPartnerAccount', 'getSessionKey', 'loginByOpenid', 'partnerAccountApplication', 'checkWorkNumOrAccount'] return new Promise((resolve, reject)=>{ const { url } = options uni.request({ header, // url: (otherUrl.find(e => url.includes(e)) ? 'http://192.168.31.116:9000/' : config.baseURL) + url, url: config.baseURL + url, method: options.method || 'GET', // 请求类型,默认为GET data: options.data || {}, // 请求参数,默认空对象 success: ({ data }) => { const { status, message, code } = data // 状态判断,根据后台定义并提示 if (status === 200) { resolve(data) } else if (status == 401 || code === 401) { if (!logouted) { // 登录过期 uni.clearStorageSync() uni.showToast({ title: message || '登录过期,请重新登录', icon: 'none' }) setTimeout(() => { logouted = 0 uni.reLaunch({ url: '/pages/login/login' }) }, 1500) reject(data) logouted = 1 } } else if (status == 10028 || status == 10014) { // 用户不存在 resolve(data) } else if (!status) { resolve(data) } else { uni.showToast({ title: message, icon: 'none' }) reject(data) } }, fail: err => { uni.showToast({ title: '请求失败!', icon: 'none' }) reject(err) }, }) }) } const get = (url, data, options = {}) => { options.method = 'GET' options.data = data options.url = url return request(options) } const post = (url, data, options = {}) => { options.method = 'POST' options.data = data options.url = url return request(options) } export default { request, get, post }