import axios from "axios"; import axiosMiniprogramAdapter from "axios-miniprogram-adapter"; import utils from "@/utils/encode"; let env; env = "test"; // env = "production"; let testUrl = "http://192.168.110.230:38080"; let productUrl = "http://uat.xingoil.com/guest-api"; // let productUrl = "https://www.xingoil.com/adminapi"; // const instance = axios.create({ baseURL: env == "production" ? productUrl : testUrl, adapter: axiosMiniprogramAdapter, timeout: 5000, }); instance.interceptors.request.use( (config) => { if (!config.HIDELODING) { uni.showLoading({ title: "加载中...", }); } const token = uni.getStorageSync("Authorization"); const unionId = uni.getStorageSync("unionid"); const openId = uni.getStorageSync("openid"); config.headers["imei"] = unionId; config.headers["openId"] = openId; config.headers["Authorization"] = token; config.headers["dataSources"] = "MP"; config.headers["loginSystem"] = "MINI_APP"; config.headers["loginDevice"] = "LOGIN_MP_WECHAT"; config.headers["accountSources"] = "LOGIN_MP_WECHAT"; 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; } return config; }, (error) => { uni.hideLoading(); uni.showToast({ title: "网络超时", icon: "none", }); return Promise.reject(error); } ); instance.interceptors.response.use( (response) => { uni.hideLoading(); 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; res.data = dataParam; } } if (res.code === 42011) { uni.showModal({ title: "提示", content: "登录信息过期,请重新登录哦", confirmText: "去登录", success: function (res) { if (res.confirm) { uni.navigateTo({ url: "/subPackages/login/login/index", }); } }, }); return Promise.reject(); } if (res.code != 20000) { uni.showToast({ title: res.msg, icon: "none", }); return Promise.reject(); } return res; }, (error) => { uni.hideLoading(); uni.getNetworkType({ success: function (res) { uni.showToast({ title: res.networkType === "none" ? "没有网啦!" : "请求失败!", icon: "none", }); }, }); return Promise.reject(error); } ); export default instance;