You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.4 KiB
95 lines
2.4 KiB
import { createRouter, createWebHistory } from 'vue-router'
|
|
|
|
// 设备检测函数
|
|
function isMobileDevice() {
|
|
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
|
|
}
|
|
|
|
// 检查登录状态
|
|
function isLoggedIn() {
|
|
const token = localStorage.getItem('token')
|
|
return !!token && token !== 'null' && token !== 'undefined'
|
|
}
|
|
|
|
// 获取用户信息
|
|
function getUserInfo() {
|
|
try {
|
|
return JSON.parse(localStorage.getItem('userInfo') || '{}')
|
|
} catch {
|
|
return {}
|
|
}
|
|
}
|
|
|
|
const mobileRoutes = [
|
|
{
|
|
path: '/mobile/login',
|
|
name: 'MobileLogin',
|
|
component: () => import('/@/views/mobile/login.vue')
|
|
},
|
|
{
|
|
path: '/mobile/agreement',
|
|
name: 'MobileAgreement',
|
|
component: () => import('/@/views/mobile/agreement.vue'),
|
|
meta: { requiresAuth: true, mobileOnly: true }
|
|
},
|
|
{
|
|
path: '/mobile/service',
|
|
name: 'MobileService',
|
|
component: () => import('/@/views/mobile/index.vue'),
|
|
meta: { requiresAuth: true, mobileOnly: true }
|
|
},
|
|
{
|
|
path: '/mobile/service/create',
|
|
name: 'MobileServiceCreate',
|
|
component: () => import('/@/views/mobile/service/create.vue'),
|
|
meta: { requiresAuth: true, mobileOnly: true }
|
|
},
|
|
{
|
|
path: '/mobile/service/detail',
|
|
name: 'MobileServiceDetail',
|
|
component: () => import('/@/views/mobile/service/detail.vue'),
|
|
meta: { requiresAuth: true, mobileOnly: true }
|
|
},
|
|
{
|
|
path: '/mobile',
|
|
redirect: '/mobile/login'
|
|
}
|
|
]
|
|
|
|
// 移动端路由守卫函数
|
|
function mobileRouteGuard(to, from, next) {
|
|
if (to.path.startsWith('/mobile')) {
|
|
// 设备检测(开发环境暂时跳过)
|
|
if (process.env.NODE_ENV === 'development') {
|
|
// 开发环境跳过设备检测,方便测试
|
|
} else if (!isMobileDevice()) {
|
|
next('/')
|
|
return
|
|
}
|
|
|
|
// 登录检查
|
|
if (to.meta.requiresAuth && !isLoggedIn()) {
|
|
next('/mobile/login')
|
|
return
|
|
}
|
|
|
|
// 已登录用户的智能跳转
|
|
if (isLoggedIn() && to.path === '/mobile/login') {
|
|
const userInfo = getUserInfo()
|
|
const isUserOrCto = ['user', 'cto'].includes((userInfo.roleCode || '').toLowerCase())
|
|
const isSigned = userInfo.agreementSignFlag === true
|
|
|
|
if (isUserOrCto && !isSigned) {
|
|
next('/mobile/agreement')
|
|
} else {
|
|
next('/mobile/service')
|
|
}
|
|
return
|
|
}
|
|
}
|
|
|
|
next()
|
|
}
|
|
// 导出移动端路由数组和守卫函数
|
|
export const mobileRouters = mobileRoutes
|
|
export { mobileRouteGuard }
|
|
|