You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.2 KiB

import {
ROOTPATH as baseUrl
} 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)
1 year ago
let xtt_lifeData3 = uni.getStorageSync('xtt_lifeData3')
let vuex_token = xtt_lifeData3.vuex_token;
if (vuex_token || vm.vuex_token) {
config.header['Authorization'] = `Bearer ${vuex_token || vm.vuex_token}`;
}
return config;
}
// 响应拦截,如配置,每次请求结束都会执行本方法
Vue.prototype.$u.http.interceptor.response = (res) => {
console.log('res-http', res)
if (res.statusCode === 200) {
if (res.data.hasOwnProperty("errcode")) {
1 year ago
if (res.data?.errcode === 40001) {
console.log("调用用户信息失效")
uni.showModal({
title: '用户信息已失效',
confirmText: '重新获取',
success: function(res) {
1 year ago
if (res.confirm) {
console.log("调用用户信息失效code",res)
1 year ago
uni.removeStorageSync()
uni.login({
provider: 'weixin',
1 year ago
success: (res) => {
console.log("调用用户信息失效code12",res)
let url = `${baseUrl}/api/mobile/user/login`
console.log("调用用户信息失效url",url)
uni.request({
url: url,
data:{
2 years ago
code:res.code
},
method: 'GET',
success: result => {
console.log("result",result)
1 year ago
uni.setStorageSync("xtt_lifeData3",{'vuex_token':result.data.token})
uni.reLaunch({
url: '/pages/home/home'
1 year ago
});
},
fail:function(err2){
console.log("err2",err2)
},
});
1 year ago
},
fail:function(err){
console.log("err",err)
}
});
// login end
}
}
}) // showConfirm end
return
} else {
uni.showToast({
icon: "none",
2 years ago
title: res?.data?.errmsg,
duration:1500
})
}
return false;
} else {
return res.data;
}
2 years ago
} else {
console.log("noneeee",res.statusCode)
uni.showToast({
icon: "none",
title: ''+res.statusCode,
duration:1500
})
return false;
}
}
}
export default {
install
}