2024-12-03 09:34:24 +08:00
|
|
|
|
import configService from '@/common/config.service.js';
|
|
|
|
|
//免登录接口
|
|
|
|
|
// let noLoginUrl = [
|
|
|
|
|
|
|
|
|
|
// ];
|
|
|
|
|
|
|
|
|
|
// 这里的vm,就是我们在vue文件里面的this,所以我们能在这里获取vuex的变量,比如存放在里面的token
|
|
|
|
|
// 同时,我们也可以在此使用getApp().globalData,如果你把token放在getApp().globalData的话,也是可以使用的
|
|
|
|
|
const install = (Vue, vm) => {
|
|
|
|
|
let url = ''
|
|
|
|
|
// 此处仅作调试新接口的开发环境,会在开发环境时影响旧接口的请求,不影响上线使用
|
2024-12-06 23:58:58 +08:00
|
|
|
|
// if (process.env.NODE_ENV === 'development') {
|
|
|
|
|
// url = configService.apiUrl;
|
|
|
|
|
// }
|
|
|
|
|
url = configService.apiUrl;
|
2024-12-03 09:34:24 +08:00
|
|
|
|
// #ifdef H5
|
|
|
|
|
typeof window.fastUrl !== 'undefined' ? url = window.fastUrl : '',
|
|
|
|
|
// #endif
|
|
|
|
|
Vue.prototype.$u.http.setConfig({
|
|
|
|
|
baseUrl: url,
|
|
|
|
|
header: {
|
|
|
|
|
'content-type': 'application/json; charset=utf-8'
|
|
|
|
|
},
|
|
|
|
|
originalData: true
|
|
|
|
|
});
|
|
|
|
|
// 请求拦截,配置Token等参数
|
|
|
|
|
Vue.prototype.$u.http.interceptor.request = (config) => {
|
|
|
|
|
//在需要登录的接口,请求前判断token 是否存在,不存在则到登录
|
|
|
|
|
// if (noLoginUrl.indexOf(config.url) == -1 && !vm.$store.state.vuex_token) {
|
|
|
|
|
// // vm.$u.route('/pages/login/mobilelogin');
|
|
|
|
|
// uni.navigateTo({'url':'/pages/my/compontents/login'});
|
|
|
|
|
// uni.showToast({'title':'请先登录', 'icon':'none'});
|
|
|
|
|
// return false;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
config.header.token = vm.$store.state.vuex_token;
|
|
|
|
|
// if (config.method == 'POST') {
|
|
|
|
|
// config.data['__token__'] = vm.vuex__token__;
|
|
|
|
|
// }
|
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
// 响应拦截,判断状态码是否通过
|
|
|
|
|
Vue.prototype.$u.http.interceptor.response = (res) => {
|
|
|
|
|
//返回__token__,设置
|
|
|
|
|
// if (res.header.__token__) {
|
|
|
|
|
// vm.$u.vuex('vuex__token__', res.header.__token__);
|
|
|
|
|
// }
|
|
|
|
|
let result = res.data;
|
|
|
|
|
switch (result.code) {
|
|
|
|
|
case 1:
|
|
|
|
|
case 0:
|
|
|
|
|
return result;
|
|
|
|
|
break;
|
|
|
|
|
case 401:
|
|
|
|
|
//需要登录的接口,当token 过期时,到登录页面
|
|
|
|
|
vm.$u.vuex('vuex_token', '');
|
|
|
|
|
// vm.$u.route('/pages/login/mobilelogin');
|
|
|
|
|
uni.navigateTo({url:'/pages/login/login'});
|
|
|
|
|
return result;
|
|
|
|
|
break;
|
|
|
|
|
case 403: //没有权限访问
|
|
|
|
|
uni.showToast({
|
|
|
|
|
icon: "none",
|
|
|
|
|
title: result.msg
|
|
|
|
|
})
|
|
|
|
|
return result;
|
|
|
|
|
break;
|
2024-12-29 16:09:08 +08:00
|
|
|
|
case 502: //错误判断
|
|
|
|
|
if(result.errorMsg === '只有登录成功后才可访问'){
|
2025-01-06 00:54:54 +08:00
|
|
|
|
if(vm.$store.state.current_platform === 'mobile_web'){
|
|
|
|
|
vm.$u.vuex('vuex_token', '');
|
|
|
|
|
vm.$u.vuex('user_message', {});
|
2024-12-29 16:09:08 +08:00
|
|
|
|
uni.navigateTo({url:`/pages/mobile_web/my/child_pages/login?nextUrl=/${getCurrentPages()[0].route}`,complete() {
|
|
|
|
|
setTimeout(()=>{
|
|
|
|
|
uni.showToast({title: vm.$store.state.vuex_token?'当前登录过期,请重新登录!':
|
|
|
|
|
'只有登录成功后才可访问',icon: 'none'});
|
|
|
|
|
},300);
|
|
|
|
|
}});
|
2025-01-06 00:54:54 +08:00
|
|
|
|
}else{
|
|
|
|
|
const status = vm.$store.state.vuex_token;
|
|
|
|
|
vm.$u.vuex('vuex_token', '');
|
|
|
|
|
vm.$u.vuex('user_message', {});
|
|
|
|
|
vm.$router.go(0);
|
|
|
|
|
if(status){
|
|
|
|
|
setTimeout(()=>{
|
|
|
|
|
uni.showToast({title: '当前登录过期,请重新登录!',icon: 'none'});
|
|
|
|
|
},500);
|
|
|
|
|
}
|
|
|
|
|
// uni.reLaunch({
|
|
|
|
|
// url:getCurrentPages()[0].route,
|
|
|
|
|
// complete() {
|
|
|
|
|
// if(status){
|
|
|
|
|
// setTimeout(()=>{
|
|
|
|
|
// uni.showToast({title: '当前登录过期,请重新登录!',icon: 'none'});
|
|
|
|
|
// },300);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// })
|
|
|
|
|
}
|
2024-12-29 16:09:08 +08:00
|
|
|
|
return result;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2024-12-03 09:34:24 +08:00
|
|
|
|
default:
|
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
|
return res.data;
|
|
|
|
|
} else {
|
|
|
|
|
console.error(res)
|
|
|
|
|
vm.$u.toast('网络请求错误!');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
install
|
|
|
|
|
}
|