import axios from 'axios' import utils from '@/utils/encode' // const env = 'test' const env = 'production' const testUrl = 'http://192.168.110.77:38080' // const productUrl = 'http://uat.xingoil.com/adminapi' const productUrl = 'https://www.xingoil.com/adminapi' let whiteList = ['/oil-site/oilSiteOrderInfo/getOrderQrCode'] const service = axios.create({ baseURL: env == 'production' ? productUrl : testUrl, // baseURL: testUrl, timeout: 5000 }) let closeShowLoading var url = '' service.interceptors.request.use( config => { closeShowLoading = config?.closeShowLoading || false; if (!closeShowLoading) { uni.showLoading({ title: '加载中...', mask: true }) } const token = uni.getStorageSync('Authorization') const unionId = uni.getStorageSync('unionid') const openId = uni.getStorageSync('openid') // const accountSources = 'WE_CHAT_PROGRAM' const accountSources = 'LOGIN_MP_WECHAT' // console.log('取出来缓存', token, openid) config.headers['openId'] = openId config.headers['Authorization'] = token config.headers['accountSources'] = accountSources config.headers['imei'] = unionId config.headers['dataSources'] = 'MP' if (env === 'production') { const JSESSIONID = utils.uuid() config.headers['JSESSIONID'] = JSESSIONID config.headers['token'] = utils.md5Salt(JSESSIONID) const data = { params: '' } data.params = utils.encrypt(JSON.stringify(config.data)) config.data = data // } } else { url = config.url } return config }, error => { if (!closeShowLoading) { uni.hideLoading() } uni.showToast({ title: '网络超时', icon: "none" }) return Promise.reject(error) } ) service.interceptors.response.use( response => { const res = response.data if (env === 'production') { if (res.encrypt == 1) { const dataParam = JSON.parse(utils.decrypt(res.data)) res.data = JSON.stringify(dataParam) === '{}' ? null : dataParam } } if (!closeShowLoading) { uni.hideLoading() } if (!whiteList.includes(response.config.url)) { if (res.code === 42011) { uni.showModal({ title: "提示", content: "登录信息过期,请重新登录哦", confirmText: "去登录", success: function(res) { if (res.confirm) { uni.navigateTo({ url: "/BagAuth/pages/login/login", }); } }, }); return Promise.reject(); } if (res.code != 20000) { uni.showToast({ title: res.msg, icon: "none", }); return Promise.reject(); } } return res }, error => { if (!closeShowLoading) { uni.hideLoading() } uni.getNetworkType({ success: (res) => { if (res.networkType === 'none') { uni.showToast({ title: '没有网啦!', icon: "none" }) } else { uni.showToast({ title: '请求失败!', icon: "none" }) } } }); return Promise.reject(error) } ) // #ifndef H5 service.defaults.adapter = function(config) { return new Promise((resolve, reject) => { var settle = require('axios/lib/core/settle'); var buildURL = require('axios/lib/helpers/buildURL'); uni.request({ method: config.method.toUpperCase(), url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer), header: config.headers, data: config.data, dataType: config.dataType, responseType: config.responseType, sslVerify: config.sslVerify, complete: function complete(response) { response = { data: response.data, status: response.statusCode, errMsg: response.errMsg, header: response.header, config: config }; settle(resolve, reject, response); } }) }) } // #endif export default service