293 lines
8.2 KiB
JavaScript
293 lines
8.2 KiB
JavaScript
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
|
||
import { useAuthStore } from '@/stores/auth'
|
||
import { nextTick } from 'vue'
|
||
import NProgress from 'nprogress'
|
||
const ModernHome = () => import('../views/ModernHome/ModernHome.vue')
|
||
const List = () => import('../views/List.vue')
|
||
const Login = () => import('../views/Login/Login.vue')
|
||
const Register = () => import('../views/Register.vue')
|
||
const ForgotPassword = () => import('../views/ForgotPassword.vue')
|
||
const CreationWorkspace = () => import('../views/CreationWorkspace/CreationWorkspace.vue')
|
||
const ProjectGallery = () => import('../views/ProjectGallery.vue')
|
||
const OrderManagement = () => import('../views/OrderManagement/OrderManagement.vue')
|
||
const OrderDetail = () => import('../views/OrderDetail.vue')
|
||
const DeviceSettings = () => import('../views/DeviceSettings.vue')
|
||
const AgentManagement = () => import('../views/AgentManagement.vue')
|
||
const AddAgent = () => import('../views/AddAgent.vue')
|
||
const DeviceList = () => import('../views/DeviceList.vue')
|
||
const UiTest = () => import('../views/UiTest.vue')
|
||
const PointsRecharge = () => import('../views/PointsRecharge/PointsRecharge.vue')
|
||
const UserCenter = () => import('../views/user/index.vue')
|
||
const NotFound = () => import('../views/NotFound.vue')
|
||
const Waitlist = () => import('../views/Waitlist.vue')
|
||
const KefuReduce = () => import('../views/kefuReduce.vue')
|
||
const isPortraitMobile = () => {
|
||
return window.innerWidth < 768 && window.innerHeight > window.innerWidth
|
||
}
|
||
const isWeChatBrowser = () => {
|
||
const ua = navigator.userAgent.toLowerCase()
|
||
return ua.indexOf('micromessenger') !== -1
|
||
}
|
||
const CreateProject = () => {
|
||
if (isPortraitMobile()) {
|
||
return import('../views/Project/CreateProjectShu/CreateProjectShu.vue')
|
||
}
|
||
return import('../views/Project/CreateProject.vue')
|
||
}
|
||
const home = () => {
|
||
return import('../views/home/index.vue')
|
||
// if (isWeChatBrowser()) {
|
||
// return import('../views/ModernHome/ModernHome.vue')
|
||
// }
|
||
}
|
||
NProgress.configure({
|
||
showSpinner: false,
|
||
})// 开启轻量模式(顶部细线)
|
||
// 路由配置
|
||
const routes = [
|
||
{
|
||
path: '/',
|
||
name: 'home',
|
||
component: home,
|
||
meta: { requiresAuth: false, keepAlive: false, fullScreen: true }
|
||
}, {
|
||
path: '/login',
|
||
name: 'login',
|
||
component: Login,
|
||
meta: { requiresGuest: true }
|
||
},
|
||
{
|
||
path: '/login/phone',
|
||
name: 'phone-login',
|
||
component: () => import('@/views/Login/PhoneLogin.vue'),
|
||
meta: { requiresGuest: true, fullScreen: true }
|
||
},
|
||
{
|
||
path: '/czhome',
|
||
name: 'czhome',
|
||
component: ModernHome,
|
||
meta: { requiresAuth: true, keepAlive: false }
|
||
},
|
||
{
|
||
path: '/register',
|
||
name: 'register',
|
||
component: Register,
|
||
meta: { requiresGuest: true, fullScreen: true }
|
||
},
|
||
{
|
||
path: '/forgot-password',
|
||
name: 'forgot-password',
|
||
component: ForgotPassword,
|
||
meta: { requiresGuest: true, fullScreen: true }
|
||
},
|
||
{
|
||
path: '/Waitlist',
|
||
name: 'Waitlist',
|
||
component: Waitlist, // 升级页
|
||
meta: { requiresAuth: true, keepAlive: false, fullScreen: true }
|
||
},
|
||
{
|
||
path: '/kefu-reduce',
|
||
name: 'kefu-reduce',
|
||
component: KefuReduce, // 客服跳转页
|
||
meta: { requiresAuth: false, keepAlive: false, fullScreen: true }
|
||
},
|
||
{
|
||
path: '/:pathMatch(.*)*',
|
||
name: 'NotFound',
|
||
component: NotFound, // 显示404页面组件
|
||
meta: { requiresAuth: false, keepAlive: false, fullScreen: true }
|
||
}
|
||
]
|
||
|
||
//免费会员/达人会员动态路由
|
||
export const freeRoutes = [
|
||
{
|
||
path: '/ui-test',
|
||
name: 'ui-test',
|
||
component: UiTest,
|
||
meta: { requiresAuth: false, keepAlive: false }
|
||
},
|
||
{
|
||
path: '/creation-workspace',
|
||
name: 'creation-workspace',
|
||
component: CreationWorkspace,
|
||
meta: { requiresAuth: true, keepAlive: false }
|
||
},
|
||
{
|
||
path: '/project-gallery',
|
||
name: 'project-gallery',
|
||
component: ProjectGallery,
|
||
meta: { requiresAuth: true, keepAlive: false }
|
||
},
|
||
{
|
||
path: '/order-management',
|
||
name: 'order-management',
|
||
component: OrderManagement,
|
||
meta: { requiresAuth: true, keepAlive: false }
|
||
},
|
||
{
|
||
path: '/order-management/:orderId',
|
||
name: 'order-detail',
|
||
component: OrderDetail,
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/agent-management',
|
||
name: 'agent-management',
|
||
component: AgentManagement,
|
||
meta: { requiresAuth: true, keepAlive: false }
|
||
},
|
||
{
|
||
path: '/user-center',
|
||
name: 'user-center',
|
||
component: UserCenter,
|
||
meta: { requiresAuth: true, keepAlive: false }
|
||
},
|
||
{
|
||
path: '/add-agent',
|
||
name: 'add-agent',
|
||
component: AddAgent,
|
||
meta: { requiresAuth: true, fullScreen: false }
|
||
},
|
||
{
|
||
path: '/device-settings',
|
||
name: 'device-settings',
|
||
component: DeviceSettings,
|
||
meta: { requiresAuth: true, keepAlive: false }
|
||
},
|
||
{
|
||
path: '/device-list/:agentId',
|
||
name: 'device-list',
|
||
component: DeviceList,
|
||
meta: { requiresAuth: true, keepAlive: false }
|
||
},
|
||
{
|
||
path: '/project/:id/:series',
|
||
name: 'project',
|
||
component:CreateProject,
|
||
meta: { requiresAuth: true, fullScreen: true }
|
||
},
|
||
{
|
||
path: '/purchase',
|
||
name: 'model-purchase',
|
||
component: () => import('../views/ModelPurchase.vue'),
|
||
meta: { requiresAuth: true, fullScreen: true }
|
||
},
|
||
{
|
||
path: '/points-recharge',
|
||
name: 'points-recharge',
|
||
component: PointsRecharge,
|
||
meta: { requiresAuth: true, fullScreen: true }
|
||
},
|
||
{
|
||
path: '/list',
|
||
name: 'list',
|
||
component: List,
|
||
meta: { requiresAuth: true }
|
||
},
|
||
{
|
||
path: '/:pathMatch(.*)*',
|
||
name: 'NotFound',
|
||
component: NotFound, // 显示404页面组件
|
||
meta: { requiresAuth: false, keepAlive: false, fullScreen: true }
|
||
},
|
||
]
|
||
const router = createRouter({
|
||
// history: createWebHistory(),
|
||
history: createWebHashHistory(),
|
||
routes,
|
||
})
|
||
// 路由守卫
|
||
router.beforeEach(async (to, from, next) => {
|
||
NProgress.start()
|
||
// if(window.location.hostname=='localhost'){
|
||
// window.localStorage.setItem('token','123')
|
||
// return next()
|
||
// }
|
||
if(to.path=='/'){
|
||
if(isWeChatBrowser()){//如果是微信浏览器,跳转到现代首页
|
||
return next('/czhome')
|
||
}
|
||
}
|
||
if (to.path == '/login' || to.path == '/login/phone' || to.path == '/register' || to.path == '/forgot-password') {
|
||
const token = localStorage.getItem('token')
|
||
// 如果有 token,跳转到首页
|
||
if (token) {
|
||
next('/czhome')
|
||
return
|
||
}
|
||
}
|
||
const findRoutes = [...freeRoutes, ...routes]
|
||
let newto = findRoutes.find(route => route.path == to.path)
|
||
if (newto?.meta?.requiresAuth) {
|
||
const token = localStorage.getItem('token')
|
||
// 如果没有 token,跳转到登录页
|
||
if (!token) {
|
||
next('/login')
|
||
return
|
||
}
|
||
}
|
||
// 检查是否需要添加动态路由
|
||
const authStore = useAuthStore();
|
||
// const info = await authStore.updateUserInfo();
|
||
// console.log(info,'infoinfo');
|
||
const user_role = authStore.user?.userRole;
|
||
if ((user_role == 1 || user_role == 2) && router.getRoutes().length == routes.length) {
|
||
// 添加动态路由
|
||
addDynamicRoutes();
|
||
if (isDynamicRoute(to.path)) {
|
||
next('/czhome')
|
||
setTimeout(() => {
|
||
router.push(to.path)
|
||
}, 20);
|
||
return
|
||
}
|
||
}
|
||
// else if (user_role == 0) {
|
||
// // 恢复默认路由
|
||
// removeDynamicRoutes()
|
||
// }
|
||
if (user_role == 0&&to.path!='/Waitlist'&&to.path!='/') {
|
||
// 跳转升级页
|
||
return next('/Waitlist')
|
||
}
|
||
next()
|
||
})
|
||
|
||
// 添加动态路由的函数
|
||
function addDynamicRoutes() {
|
||
freeRoutes.forEach(route => {
|
||
router.addRoute(route)
|
||
})
|
||
}
|
||
//恢复默认路由
|
||
function removeDynamicRoutes() {
|
||
if (router.getRoutes().length == routes.length) {
|
||
return
|
||
}
|
||
router.getRoutes().forEach(route => {
|
||
if (route.name && !routes.some(r => r.name === route.name)) {
|
||
router.removeRoute(route.name)
|
||
}
|
||
})
|
||
}
|
||
// 检查是否是动态路由
|
||
function isDynamicRoute(path) {
|
||
return freeRoutes.some(route => {
|
||
// 简单匹配,不考虑动态参数
|
||
const routePath = route.path.replace(/:[^/]+/g, '[^/]+')
|
||
const regex = new RegExp(`^${routePath}$`)
|
||
return regex.test(path)
|
||
})
|
||
}
|
||
window.Redirectlogin = () => {
|
||
localStorage.removeItem('token')
|
||
// 恢复默认路由
|
||
removeDynamicRoutes()
|
||
router.push('/login')
|
||
}
|
||
router.afterEach(() => {
|
||
NProgress.done()
|
||
})
|
||
export default router |