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.

143 lines
4.4 KiB

3 years ago
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,
2 years ago
showLoading: false, // 是否显示请求中的loading
loadingMask: false, // 展示loading的时候是否给一个透明的蒙层防止触摸穿透
loadingText: '', // 请求loading中的文字提示
3 years ago
loadingTime: 800,
3 years ago
originalData: true, // 是否在拦截器中返回服务端的原始数据
3 years ago
// 设置自定义头部content-type
header: {
'content-type': 'application/json;charset=UTF-8'
}
});
// 请求拦截部分,如配置,每次请求前都会执行
Vue.prototype.$u.http.interceptor.request = (config) => {
console.log('config-http', config)
2 years ago
config.data.activity_tag = 'xtykt'
config.data.activity_list_id = 12
2 years ago
2 years ago
let xtykt_lifeData = uni.getStorageSync('xtykt_lifeData')
let vuex_token = xtykt_lifeData.vuex_token;
3 years ago
if (vuex_token || vm.vuex_token) {
config.header['Authorization'] = `Bearer ${vuex_token || vm.vuex_token}`;
}
3 years ago
return config;
}
// 响应拦截,如配置,每次请求结束都会执行本方法
Vue.prototype.$u.http.interceptor.response = (res) => {
3 years ago
console.log('res-http', res)
if (res.statusCode === 200) {
if (res.data.hasOwnProperty("errcode")) {
if (res.data?.errcode === 40001) {
2 years ago
uni.showModal({
title:'用户信息已失效',
confirmText:'重新获取',
success:function(res){
if(res.confirm){
console.log("res",res)
uni.removeStorageSync('xtykt_lifeData')
uni.login({
provider: 'weixin',
success: (res1) => {
uni.request({
url:baseUrl+'/api/mobile/user/wechat-login',
data:{
code:res1.code,
activity_tag : 'xtykt',
activity_list_id : 12
},
method: 'GET',
success: res2 => {uni.setStorageSync('xtykt_lifeData',{'vuex_token':res2.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}`)
},
fail: (res3) => {
console.log("err",res3)
}
});
},
fail: (res4) => {
console.log("err",res4)
}
});
// login end
}
}
}) // showConfirm end
return
// setTimeout(() => {
// let origin = window.location.origin;
// let pathname = window.location.pathname;
// window.location.href = origin + pathname;
// }, 1500)
3 years ago
} else {
uni.showToast({
icon: "none",
title: res?.data?.errmsg
})
3 years ago
}
return false;
} else {
3 years ago
3 years ago
return res.data;
}
3 years ago
} else {
3 years ago
// uni.showToast({
// icon: "none",
// title: res.statusCode
// })
3 years ago
3 years ago
return false;
}
}
}
2 years ago
// const customParamsSerializer = (params)=>{
// console.log("params",params)
// let result = "";
// for (let key in params) {
// if (params.hasOwnProperty(key)) {
// if (Array.isArray(params[key])) {
// if (typeof params[key] === "object") {
// params[key].forEach((item, index) => {
// if (typeof item === "object") {
// key[index]['key'] = item.key
// key[index]['op'] = item.op
// key[index]['value'] = item.value
// // result += `${key}[${index}][key]=${item.key}&${key}[${index}][op]=${item.op}&${key}[${index}][value]=${item.value}&`;
// }
// });
// }
// }
// }
// }
// console.log("result",result)
// return result.slice(0, -1);
// }
3 years ago
export default {
install
}