// xuchao: 请求/响应拦截器 —— baseUrl 统一走 config.js import config from '@/common/config.js' const install = (Vue, vm) => { Vue.prototype.$u.http.setConfig({ baseUrl: config.apiBaseUrl }); // 请求拦截:带上登录凭证 Vue.prototype.$u.http.interceptor.request = (config) => { config.header.Authorization = uni.getStorageSync('token'); return config; } // 响应拦截:errcode==0 才算成功;status==600 视为登录失效,跳登录页 Vue.prototype.$u.http.interceptor.response = (res) => { if (res.errcode == 0) { return res; } else if (res.status == 600) { uni.removeStorageSync('phone') uni.reLaunch({ url: '/pages/index/authorization' }) } else { uni.showToast({ icon: 'none', title: res.errmsg, duration: 5000 }) }; } } export default { install }