import Interceptor from "./core/interceptor"; import Request from "./index"; import TokenApi from ".././api/token"; export const globalInterceptor = { request: new Interceptor(), response: new Interceptor(), }; /** * 全局配置 * 只能配置 静态数据 * `content-type` 默认为 application/json * header 中`content-type`设置特殊参数 或 配置其他会导致触发 跨域 问题,出现跨域会直接进入响应拦截器的catch函数中 */ export const config = { // baseURL: "/gyhl", baseURL:"http://36.137.58.70:52/gyhl/", // baseURL: 'http://120.132.17.220:8888', // baseURL: 'http://192.168.1.50:8888', // baseURL: "http://36.212.152.5:32/gyhl", // baseURL: 'http://192.168.1.56:21925', // imgURL:'http://120.132.17.220:18090/', imgURL: window.location.href.split("//")[0] + "//" + window.location.href.split("//")[1].split("/")[0] + "/", dataType: "json", responseType: "text", method: "POST", header: { "content-type": "application/json", // 'content-type': 'application/x-www-form-urlencoded' }, }; /** * 全局 请求拦截器 * 例如: 配置token * * `return config` 继续发送请求 * `return false` 会停止发送请求,不会进入错误数据拦截,也不会进入请求对象中的catch函数中 * `return Promise.reject('xxxxx')` 停止发送请求, 会错误数据拦截,也会进入catch函数中 * * @param {Object} config 发送请求的配置数据 */ globalInterceptor.request.use( (config) => { // 显示加载中 效果 uni.showLoading({ mask: true, }); config.header.Authorization = getToken(); return config; // return false; // return Promise.reject('is error') }, (err) => { console.error("is global fail request interceptor: ", err); return false; } ); // 支持添加多个请求、响应拦截器 // globalInterceptor.request.use(config => { // console.log('is global request interceptor 2'); // return config; // }, err => { // console.error('global request: ', err); // return false; // }); /** * 全局 响应拦截器 * 例如: 根据状态码选择性拦截、过滤转换数据 * * `return res` 继续返回数据 * `return false` 停止返回数据,不会进入错误数据拦截,也不会进入catch函数中 * `return Promise.reject('xxxxx')` 返回错误信息, 会错误数据拦截,也会进入catch函数中 * * @param {Object} res 请求返回的数据 * @param {Object} config 发送请求的配置数据 * @return {Object|Boolean|Promise} */ globalInterceptor.response.use( (res, config) => { uni.hideLoading(); if (res.data && res.data.code == 98) { // const automatic_logon = uni.getStorageSync('automatic_logon'); // const loginInfo = uni.getStorageSync('admin_info'); // if(automatic_logon){ // //调用接口获取最新的token // uni.request({ // url: config.baseURL+'/apis/user/login', //仅为示例,并非真实接口地址。 // data: loginInfo, // method:'POST', // success: (res) => { // console.log(res.data); // // uni.setStorageSync('userInfo', res.data.userInfo); // } // }); // this.$http.request({ // url: '/apis/user/login', // params: loginInfo, // }).then(res=>{ // if(res.data.code == 0){ // uni.setStorageSync('userInfo', res.data.userInfo); // }else{ // uni.showToast({ // icon: 'none', // title: res.data.msg, // }); // } // }).catch(err=>{ // uni.showToast({ // icon: 'none', // title: '重新登陆失败', // }); // }); // }else{ //令牌到期,需要重新登陆 uni.showToast({ title: res.data.msg, duration: 1000, icon: "none", }); setTimeout(function () { uni.navigateTo({ url: "/pages/login/login", }); }, 1000); // } } else { return res; } // 回传数据中没有携带 code // if (!(res.data && res.data.code)) { // return res; // } // return Promise.reject('is error') }, (err, config) => { uni.showToast({ title: "连接服务器失败", duration: 1000, icon: "none", }); const { errMsg, data } = err; return Promise.reject({ errMsg, data, config, }); } ); // 重新请求更新获取 token async function getApiToken(uid) { const res = await TokenApi.getMockToken(uid); const { token } = res.data; return token; } // 获取 localStorage 中的 token function getToken() { try { const value = uni.getStorageSync("userInfo"); if (value) { return value.token; } } catch (e) { uni.showToast({ title: "请先登陆", duration: 2000, icon: "none", }); setTimeout(function () { uni.navigateTo({ url: "/pages/login/login", }); }, 2000); } } // 保存 token 到 localStorage function saveToken(token) { uni.setStorageSync("token", token); }