YingXingAI/static/common/js/router.js

56 lines
1.9 KiB
JavaScript
Raw Normal View History

2025-06-30 14:43:02 +08:00
import config from "./config.js"
const initApp = function(vm) {
/**
* 页面跳转拦截器
*/
let that = vm;
let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"];
list.forEach(item => { //用遍历的方式分别为,uni.navigateTo,uni.redirectTo,uni.reLaunch,uni.switchTab这4个路由方法添加拦截器
uni.addInterceptor(item, {
invoke(e) { // 调用前拦截
//获取用户的token
// console.log(e)
const token = that.vuex_token,
//获取要跳转的页面路径url去掉"?"和"?"后的参数)
url = e.url.split('?')[0];
let notNeed = config.whiteList.includes(url)
// 如果在whiteList里面就不需要登录
// console.log(notNeed)
if (notNeed) {
return e
} else {
//需要登录
if (token == '') {
// uni.showToast({
// title: '请先登录',
// icon: 'none'
// })
// uni.navigateTo({
// url: config.loginPage
// })
2025-06-30 14:43:02 +08:00
uni.navigateTo({
url: '/'
2025-06-30 14:43:02 +08:00
})
return false
} else {
return e
}
}
},
fail(err) { // 失败回调拦截
console.log(err)
// if (Debug) {
// uni.showModal({
// content: JSON.stringify(err),
// showCancel: false
// });
// }
}
})
})
}
export default {
initApp: initApp
}