/** * 手太欠 * 愿这世界都如故事里一样 美好而动人~ */ import store from '@/store' // 基础配置 // https://lifetest.ysd-bs.com //测试地址 // https://card.ysd-bs.com const config = { // apiUrl : 'https://lifetest.ysd-bs.com/api/', apiUrl : 'https://card.ysd-bs.com/api/', timeout: 60000 } let loginHintState = false // 网络请求 const request = (parameter) => { // 检查url配置 if(parameter.url === 'undefined' || parameter.url === '') { uni.showToast({ title: '请求地址不能为空', icon : 'none' }) return } // 注入header config.header = { 'Accept': 'application/json', 'Authorization': store.getters.getToken || '' } // 请求实例 return new Promise((resolve, reject) => { uni.request({ url : config.apiUrl + parameter.url, timeout : config.timeout, header : config.header || {}, data : parameter.data || {}, method : parameter.method || 'GET', success: res => { if (res.header.Authorization) { updateToken('token', res.header.Authorization) } if (res.statusCode === 200) { const resolveData = res.data if(resolveData.status_code === 200) { resolve(resolveData.data) return } if (resolveData.status_code === 401) { loginHint() return } reject(resolveData) return } errToast(res.statusCode) } }) }) } // 处理一些http请求错误提示 const errToast = (code) => { switch (code){ case 404: uni,uni.showToast({ title: code + '接口不存在,请联系系统管理员', icon : none }) break; case 405: uni.showToast({ title: code + '请检查接口请求方式错误', icon :'none' }) break; case 500: uni.showToast({ title: code + '服务端错误,请检查服务器信息', icon : 'none' }) break; } } // 更新token const updateToken = (token) => { store.commit('setToken', token) } // 处理登录提示 const loginHint = () => { if(loginHintState) return if(!loginHintState) loginHintState = true updateToken('') uni.showModal({ title:'登录提示', content:'您的登录信息已过期,请重新登录', confirmColor:'#33f800', showCancel:false, success: res => { console.log(getApp().globalData.envType) loginHintState = false if(res.confirm){ if(getApp().globalData.envType == 'campusEnv') { uni.reLaunch({ url: '/pages/campus/signin' }) } else if(getApp().globalData.envType == 'oilEnv') { uni.reLaunch({ url: '/pages/oil/signin' }) }else if(getApp().globalData.envType == 'unicomEnv') { uni.reLaunch({ url: '/pages/unicom/signin' }) }else if(getApp().globalData.envType == 'drawEnv') { uni.reLaunch({ url: '/pages/draw/signin' }) }else if(getApp().globalData.envType == 'giftPEnv') { uni.reLaunch({ url: '/pages/giftPack/signin' }) }else { uni.reLaunch({ url: '/pages/auth/login' }) } } } }) } export default request