// 路由 import { RouterMount, createRouter } from './uni-simple-router.js' import store from '@/core/store' import { getToken, removeToken, isWorkbenchSession } from '@/core/utils/auth' const router = createRouter({ platform: process.env.VUE_APP_PLATFORM, applet: { animationDuration: 0 //默认 300ms }, routerErrorEach: ({ type, msg }) => { switch (type) { case 3: // APP退出应用 // #ifdef APP-PLUS router.$lockStatus = false; uni.showModal({ title: '提示', content: '您确定要退出应用吗?', success: function(res) { if (res.confirm) { plus.runtime.quit(); } } }); // #endif break; case 2: router.$lockStatus = false; break; case 0: router.$lockStatus = false; break; default: break; } //console.log("异常路由",type); }, // 通配符,非定义页面,跳转404 routes: [...ROUTES, { path: '*', redirect: (to) => { return { path: '/pages/404' } } }, ] }); //全局路由前置守卫 router.beforeEach(async (to, from, next) => { const hasToken = getToken(); // 工作台会话(token = workbenchToken):不做用户态校验,不调用 user.info if (hasToken && isWorkbenchSession()) { console.log('工作台会话放行:', to.path); next(); return; } if (hasToken) { console.log("去哪里",to); if (to.path === '/pages/login/index') { // 已登录时访问登录页,重定向到首页 next({ path: '/pages/index/index', replace: true }); } else { const hasGetUserInfo = store.getters.user.employees_name; console.log("有没有用户信息",hasGetUserInfo); if (hasGetUserInfo) { try { store.dispatch('floatWindowManager/is_visible').catch(error => { console.log('浮窗显示检查失败:', error); }); if(to.meta && to.meta.auth){ console.log(to.path); const hasPermiss =await store.dispatch('checkPath',to.path); if(!hasPermiss){ next({path:"/pages/403"}) return; } } const hasFromPath = !!from.path; const isSameRoute = hasFromPath && to.path === from.path && to.fullPath === from.fullPath; if(isSameRoute){ console.log('路由重复,取消跳转:', to.path); next(false); }else{ console.log('路由放行:', from.path, '->', to.path); next(); } } catch (error) { console.log('已登录分支路由守卫异常:', error); next(); } } else { console.log("没有用户信息"); try { await store.dispatch('getUserInfo'); console.log("获取完成,去哪里",to) store.dispatch('floatWindowManager/is_visible').catch(error => { console.log('浮窗显示检查失败:', error); }); if (to.path === '/pages/index/index') { console.log('路由放行:', from.path, '->', to.path); next(); } else { console.log('路由重定向:', from.path, '->', to.fullPath); next({ path: to.fullPath, NAVTYPE: 'SwitchTab' }) } } catch (error) { await store.dispatch('logout'); next({ path: '/pages/login/index', replace: true }); } } } } else { // 没有Token的情况 if (to.path === '/pages/login/index') { next(); } else { uni.hideTabBar(); next({ path: '/pages/login/index', query: { redirect: to.fullPath || to.path }, replace:true }); } } }); export { router, RouterMount }