import { config } from '@/common/config.js'; import store from '@/store/index.js' let baseUrl = config.baseUrl // vm指向this const install = (Vue, vm) => { uni.$u.http.setConfig((config) => { config.baseURL = baseUrl return config }); // 请求拦截,配置Token等参数 uni.$u.http.interceptors.request.use((config) => { if (vm.vuex_token) { config.header.Authorization = `Bearer ${vm.vuex_token}`; } // url加时间戳 config.url = config.url + '?t=' + Date.now() return config; }, config => { return Promise.reject(config) }) // 响应拦截,判断状态码是否通过 uni.$u.http.interceptors.response.use((response) => { let res = response.data if (res.code == 200) { return res; } else if (res.msg == "令牌不能为空" || res.code == 401) { uni.showModal({ title: '提示', content: '登录信息已失效, 请重新登录', showCancel: false, success: function(res) { if (res.confirm) { uni.$u.vuex('vuex_token', ''); uni.$u.vuex('vuex_user', {}); uni.$u.vuex('vuex_isLogin', false); uni.redirectTo({ url: "/pages/login/login" }) } } }); } else { uni.showToast({ title: res.msg, icon: 'none', duration: 2000 }) } }, (response) => { return Promise.reject(response) }) } export default { install }