import { getToken } from '@/utils/auth' import { baseUrl } from "@/config.js" //只要是未登录状态,想要跳转到名单内的路径时,直接跳到登录页 const loginPage = "/subLoginPages/pages/login" // 页面白名单,不受拦截 // const whiteList = [ // '/subLoginPages/pages/login', // '/pages/tabBar/mine/mine' // ] // 拦截名单,受拦截 const blockedList = [ // '/subCarPages/index/index' '/subCarPages/buy/buy', // 求购车辆 '/subCarPages/sell/sell', // 发布车辆 '/subCarPages/userPost/userPost', //我的发布 '/subSchoolPages/registration/registration', //学车报名 '/subInspectionPages/details/details', // 检测商品 '/subRepairPages/details/details', // 汽修商品 '/subSchoolPages/appointment/appointment', // 预约报名 '/pages/tabBar/order/order', // 消费记录 '/pages/tabBar/mine/Recharge', // 充值 '/subCouponPages/index/index', // 我的优惠券 '/pages/tabBar/mine/mine' // 个人中心 ] function hasPermission(url) { let islogin = getToken(); // Get the token (e.g., from a cookie or local storage) // console.log(1, islogin); islogin = Boolean(islogin); // Convert to a boolean value // console.log(2, islogin); const hasParams = url.indexOf('?') !== -1; const isBlockedURL = blockedList.some((blockedURL) => { return hasParams ? blockedURL === url.split('?')[0] : blockedURL === url; }); if (isBlockedURL && !islogin) { console.log('未登录'); return false; } return true; } uni.addInterceptor('navigateTo', { // 页面跳转前进行拦截, invoke根据返回值进行判断是否继续执行跳转 invoke(e) { if (!hasPermission(e.url)) { uni.navigateTo({ url: loginPage + '?url=' + e.url }) return false } return true }, success(e) {} }) uni.addInterceptor('switchTab', { // tabbar页面跳转前进行拦截 invoke(e) { if (!hasPermission(e.url)) { uni.navigateTo({ url: loginPage + '?url=' + e.url }) return false } return true }, success(e) {} }) uni.addInterceptor('reLaunch', { // tabbar页面跳转前进行拦截 invoke(e) { if (!hasPermission(e.url)) { uni.navigateTo({ url: loginPage + '?url=' + e.url }) return false } return true }, success(e) {} }) uni.addInterceptor('redirectTo', { // tabbar页面跳转前进行拦截 invoke(e) { if (!hasPermission(e.url)) { uni.navigateTo({ url: loginPage + '?url=' + e.url }) return false } return true }, success(e) {} })