PixelAI-mobile/common/routerInterceptor.js

78 lines
2.0 KiB
JavaScript
Raw Normal View History

2024-12-03 09:34:24 +08:00
// 需要登录的界面
let needLogin = [
"/pages/my/mySetting"
];
// 多端通用路由
let filterRoute = [
"/uview-ui/components/u-avatar-cropper/u-avatar-cropper",
2024-12-05 11:46:54 +08:00
"/pages/index/rich-detail",
"/pages/index/webview",
2024-12-03 09:34:24 +08:00
];
// 拦截类型
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];
const install = (vm) => {
// #ifdef H5
// H5路由拦截,用于拦截用户地址栏输入地址
const token = vm.$store.state.vuex_token;
let locationUrl = window.location.href.split("/#")[1];
if(needLogin.includes(locationUrl)&&!token){
uni.showToast({
title: '该页面需要登录才能访问,请先登录',
icon: 'none'
});
uni.navigateTo({
url: "/pages/my/wxLogin?nextUrl="+locationUrl
});
} else if(!filterRoute.includes(locationUrl)&&locationUrl!=='/'&&vm.$store.state.current_platform&&
locationUrl.indexOf(vm.$store.state.current_platform)==-1){
uni.navigateTo({
url: `/pages/${vm.$store.state.current_platform}/index/index`,
complete() {
setTimeout(()=>{
uni.showToast({
title: '该设备端无法访问对应页面!',
icon: 'none',
});
},500)
}
});
}
// #endif
// uniapp跳转页面路由拦截器
list.forEach(item => {
uni.addInterceptor(item, {
invoke(e) {
const token = vm.$store.state.vuex_token;
// console.log(vm.$store.state.current_platform)
const url = e.url.split('?')[0];
if (needLogin.includes(url) && !token) {
console.log(url)
uni.showToast({
title: '该页面需要登录才能访问,请先登录',
icon: 'none'
});
uni.navigateTo({
url: "/pages/my/wxLogin?nextUrl="+e.url
});
return false;
}
// #ifndef APP
else if(!filterRoute.includes(url)&&url.indexOf(vm.$store.state.current_platform)==-1){
uni.showToast({
title: '该设备端无法访问对应页面!',
icon: 'none'
});
return false;
}
// #endif
return true;
}
});
});
}
export default {
install
}