import { createRouter, createWebHistory } from 'vue-router' import Layout from '@/layout/default.vue' import { getToken, getLang } from '@/utils/auth' import { deepFind, isEmpty } from '@/utils/datas' import store from '../store' import i18n from '../lang/i18n' import { Modal } from '@arco-design/web-vue' export const constantRoutes = [{ path: '/redirect', component: Layout, name: 'redirect', children: [{ path: '/redirect/:path(.*)', component: () => import('@/views/Redirect.vue') }] }, { path: '/auth/login', // 授权登录 name: 'auth-login', component: () => import('@/views/authLogin.vue') }, { path: '/403', name: 'forbidden', component: () => import('@/views/error/403.vue') }, { path: '/:catchAll(.*)', name: 'not-found', component: () => import('@/views/error/404.vue') }, { path: '/', component: Layout, redirect: { name: 'index' }, children: [{ path: 'index', name: 'index', component: () => import('@/views/Index.vue'), meta: { title: 'index', menuItem: true, permission: "pass", icon: 'btn_home' } }, { path: 'image-to-image', name: 'image-to-image', component: () => import('@/views/Image.vue'), meta: { title: 'imageToImage', menuItem: true, permission: "pass", icon: 'btn_tst' } }, { path: 'image-to-image2', name: 'image-to-image2', component: () => import('@/views/Image.vue'), meta: { title: 'imageToImage2', menuItem: true, permission: "pass", icon: 'btn_tst' } }, { path: 'change-face', name: 'change-face', component: () => import('@/views/ChangeFace.vue'), meta: { title: 'changeFace', menuItem: true, permission: "pass", icon: 'btn_yjhl' } }, { path: 'change-face-video', name: 'change-face-video', component: () => import('@/views/ChangeFace.vue'), meta: { title: 'changeFaceVideo', menuItem: true, permission: "pass", icon: 'btn_yjhl' } }, { path: 'fast-image', name: 'fast-image', component: () => import('@/views/FastImage.vue'), meta: { title: 'fastImage', menuItem: true, permission: "pass", icon: 'btn_kjst' } }, { path: 'fast-video', name: 'fast-video', component: () => import('@/views/FastVideo.vue'), meta: { title: 'fastVideo', menuItem: true, permission: "pass", icon: 'btn_kjsp' } }, { path: 'generated-images', name: 'generated-images', component: () => import('@/views/GeneratedImages.vue'), meta: { title: 'AI文生图', menuItem: true, permission: "pass", icon: 'btn_kjst' } }, { path: 'recharge', name: 'recharge', component: () => import('@/views/Recharge.vue'), meta: { title: 'recharge', menuItem: true, permission: "pass", icon: 'btn_kscz' } }, { path: 'help', name: 'help', component: () => import('@/views/Help.vue'), meta: { title: 'help', menuItem: true, permission: "pass", icon: 'btn_help' } }, { path: 'invite', name: 'invite', component: () => import('@/views/Invite.vue'), meta: { title: 'moneyInvite', menuItem: true, permission: "pass", icon: 'btn_kscz' } }] } ] const routes = constantRoutes let routerConfig = { history: createWebHistory(), scrollBehavior: () => ({ y: 0 }), routes } const router = createRouter(routerConfig) export function resetRouter() { const newRouter = createRouter(routerConfig) router.matcher = newRouter.matcher // reset router } // 路由白名单 const whiteList = [ 'redirect', 'login', 'set-password', 'reset-password', 'forbidden', 'not-found', ] // 是否有权限 function hasPermission({ name, path, meta = {} }) { if (whiteList.includes(name) || path.startsWith('/redirect')) return true let permission_routes = store.getters.permission_routes // 要访问的路由必须在权限路由中 let route = deepFind(permission_routes, (item) => item.name === name, { childrenKey: 'children' }) return !isEmpty(route) } // 权限控制 router.beforeEach(async (to = {}, from, next) => { if (from.path != '/fast-video') { next() } else { const lang = getLang() const messages = i18n.global.messages[lang] let isPrevent = store.getters.showPrevent; if (isPrevent) { try { await new Promise((resolve, reject) => { Modal.confirm({ title: messages.common.notice, content: messages.common.switchPageTip, okText: messages.common.confirm, cancelText: messages.common.cancel, onOk: () => { resolve(); }, onCancel: () => { reject(); } }) }) next() } catch (error) { next(false) } } else { next() } } }) export default router