/**
* router路由拦截器
*/
export function routeInterceptor() {
const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'];
list.forEach((item) => {
uni.addInterceptor(item, {
invoke(args) {
const fullPath = args.url;
// console.log('router-invoke', args, fullPath);
return routeAuth(fullPath);
},
success(args) {
// console.log("router-success", args);
},
fail(err) {
// console.log("router-fail", err);
},
complete(res) {
// console.log("router-complete", res);
}
});
});
// [TODO] 第一个页面加载处理
setTimeout(() => {
const currentPath = getCurrentFullPath();
if (currentPath) routeAuth(currentPath);
}, 500);
}
/**
* 路由权限判断
* @param {string} fullPath 路由全路径
* @returns
*/
export function routeAuth(fullPath) {
let res = true;
let url = fullPath.split('?')[0];
let page = pages.find((item) => '/' + item.path == url);
if (page?.auth) {
const user = useStoreUser();
if (user.getToken == null) {
goLogin();
res = false;
}
}
return res;
}