WMS_PDA/src/core/router/index.js
liufumingren 5ae92e0f63 chore(cli): 由 HBuilderX 工程迁移为 uni-app Vue2 CLI 脚手架工程
- 业务源码统一迁入 src/,H5 模板移到 public/index.html
- 新增 @vue/cli 4.5 + @dcloudio 2.0.2-5020620260917001 全套依赖与 scripts
- 保留 vue.config.js 的 ROUTES 注入(uni-simple-router 运行时依赖)
- 新增 scripts/node-compat.cjs 兼容 Node 23+ 移除的 util.isRegExp 等 API
- README 补充构建/运行/回退说明
2026-10-02 19:45:18 +08:00

156 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 路由
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
}