import { ROOTPATH as baseUrl, WECHAT_APPID } from "@/common/config.js" // 这里的Vue为Vue对象(非创建出来的实例),vm为main.js中“Vue.use(httpInterceptor, app)”这一句的第二个参数, // 为一个Vue的实例,也即每个页面的"this" // 如果需要了解这个install方法是什么,请移步:https://uviewui.com/components/vueUse.html const install = (Vue, vm) => { // 此为自定义配置参数,具体参数见上方说明 Vue.prototype.$u.http.setConfig({ baseUrl, showLoading: false, // 是否显示请求中的loading loadingMask: false, // 展示loading的时候,是否给一个透明的蒙层,防止触摸穿透 loadingText: '', // 请求loading中的文字提示 loadingTime: 800, originalData: true, // 是否在拦截器中返回服务端的原始数据 // 设置自定义头部content-type header: { 'content-type': 'application/json;charset=UTF-8' } }); // 请求拦截部分,如配置,每次请求前都会执行 Vue.prototype.$u.http.interceptor.request = (config) => { console.log('config-http', config) config.data.activity_tag = 'walksz' config.data.activity_list_id = 13 // 优先从storage获取token,因为storage更新更及时 let walksz_lifeData = uni.getStorageSync('walksz_lifeData') || {} let vuex_token = walksz_lifeData.vuex_token || vm.vuex_token || ''; if (vuex_token) { config.header['Authorization'] = `Bearer ${vuex_token}`; console.log('请求携带token:', vuex_token.substring(0, 20) + '...') } else { console.log('请求未携带token') } return config; } // 响应拦截,如配置,每次请求结束都会执行本方法 Vue.prototype.$u.http.interceptor.response = (res) => { console.log('res-http', res) if (res.statusCode === 200) { if (res.data.hasOwnProperty("errcode")) { if (res.data?.errcode === 40001) { uni.showModal({ title: '用户信息已失效', confirmText: '重新获取', success: function(res) { if (res.confirm) { uni.removeStorageSync('walksz_lifeData') // #ifndef H5 uni.login({ provider: 'weixin', success: (res) => { let url = baseUrl + '/api/mobile/user/wechat-login' uni.request({ url: url, data:{ code:res.code, activity_tag : 'walksz', activity_list_id : 13 }, method: 'GET', success: result => { console.log("result",result) uni.setStorageSync("walksz_lifeData",{'vuex_token':result.data.token}) const currentPage = getCurrentPages()[ getCurrentPages() .length - 1]; const currentPagePath = "/" + currentPage.route; const currentPageOptions = currentPage.options; const queryString = Object .keys( currentPageOptions) .map(key => `${key}=${currentPageOptions[key]}` ) .join('&'); // 使用uni.reLaunch方法刷新当前页面 uni.reLaunch({ url: `${currentPagePath}?${queryString}` }); console.log("reLaunch", `${currentPagePath}?${queryString}` ) } }); } }); // #endif // #ifdef H5 // H5端跳转到微信授权页面 const redirectUri = encodeURIComponent(window.location.href.split('?')[0]) const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${WECHAT_APPID}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect` window.location.href = authUrl // #endif // login end } } }) // showConfirm end return } else if (res.data?.errcode === 40002 || res.data?.errcode === 40003) { // TOKEN鉴权失败,清除token并重新登录 console.log('TOKEN鉴权失败,清除token') uni.removeStorageSync('walksz_lifeData') vm.vuex_token = '' // #ifndef H5 uni.showModal({ title: '登录已失效', content: '请重新登录', showCancel: false, success: () => { // 重新登录 uni.login({ provider: 'weixin', success: (loginRes) => { let url = baseUrl + '/api/mobile/user/wechat-login' uni.request({ url: url, data: { code: loginRes.code, activity_tag: 'walksz', activity_list_id: 13 }, method: 'GET', success: result => { uni.setStorageSync("walksz_lifeData", {'vuex_token': result.data.token}) vm.vuex_token = result.data.token // 重新加载当前页面 const currentPage = getCurrentPages()[getCurrentPages().length - 1] const currentPagePath = "/" + currentPage.route const currentPageOptions = currentPage.options const queryString = Object.keys(currentPageOptions).map(key => `${key}=${currentPageOptions[key]}`).join('&') uni.reLaunch({ url: `${currentPagePath}?${queryString}` }) } }) } }) } }) // #endif // #ifdef H5 // H5端跳转到微信授权页面 const redirectUri = encodeURIComponent(window.location.href.split('?')[0]) const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${WECHAT_APPID}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect` window.location.href = authUrl // #endif return false } else { uni.showToast({ icon: "none", title: res?.data?.errmsg || '请求失败' }) } return false; } else { return res.data; } } else { // uni.showToast({ // icon: "none", // title: res.statusCode // }) return false; } } } export default { install }