import { createRouter, createWebHashHistory } from 'vue-router' import type { RouteRecordRaw } from 'vue-router' import { getAuthToken } from '@/api' import { isInUniWebView } from '@/utils/uniapp-bridge' const routes: RouteRecordRaw[] = [ { path: '/', name: 'AvatarHome', redirect: '/avatar/manage', meta: { title: '数字分身管理', requiresAuth: true } }, { path: '/avatar/create', name: 'AvatarCreate', component: () => import('@/views/AvatarCreate.vue'), meta: { title: '创建数字分身', requiresAuth: true } }, { path: '/avatar/manage', name: 'AvatarManage', component: () => import('@/views/AvatarManage.vue'), meta: { title: '数字分身管理', requiresAuth: true } }, { path: '/avatar/edit/:id', name: 'AvatarEdit', component: () => import('@/views/AvatarEdit.vue'), meta: { title: '分身微调', requiresAuth: true } }, { path: '/avatar/chat/:id', name: 'AvatarChat', component: () => import('@/views/AvatarChat.vue'), meta: { title: '和分身对话', requiresAuth: true } }, { path: '/share/:shareToken', name: 'AvatarPublicChat', component: () => import('@/views/AvatarChat.vue'), meta: { title: '和我聊聊' } }, { path: '/authorization', name: 'AuthorizationManage', component: () => import('@/views/AuthorizationManage.vue'), meta: { title: '授权管理', requiresAuth: true } }, { path: '/avatar/:avatarId/authorization', name: 'AvatarAuthorizationManage', component: () => import('@/views/AuthorizationManage.vue'), meta: { title: '授权管理', requiresAuth: true } }, { path: '/token/charge', name: 'TokenCharge', component: () => import('@/views/TokenCharge.vue'), meta: { title: '积分充值', requiresAuth: true, requiresUniWebView: true } }, { path: '/avatar/card', name: 'AvatarCard', component: () => import('@/views/AvatarCard.vue'), meta: { title: '分身名片', requiresAuth: true } }, { path: '/avatar/contacts', name: 'AvatarContacts', component: () => import('@/views/AvatarContacts.vue'), meta: { title: '分身人脉', requiresAuth: true } }, { path: '/avatar/projects', name: 'MyProjects', component: () => import('@/views/MyProjects.vue'), meta: { title: '我的项目', requiresAuth: true } }, { path: '/avatar/org/create', name: 'CreateOrg', component: () => import('@/views/CreateOrg.vue'), meta: { title: '创建组织', requiresAuth: true } }, { path: '/knowledge', name: 'KnowledgeManage', component: () => import('@/views/KnowledgeManage.vue'), meta: { title: '知识库管理', requiresAuth: true } }, { path: '/avatar/:avatarId/knowledge', name: 'AvatarKnowledgeManage', component: () => import('@/views/KnowledgeManage.vue'), meta: { title: '知识库管理', requiresAuth: true } }, { path: '/knowledge/qa/create', name: 'QaPairCreate', component: () => import('@/views/QaPairEdit.vue'), meta: { title: '添加问答对', requiresAuth: true } }, { path: '/knowledge/qa/:qaId/edit', name: 'QaPairEdit', component: () => import('@/views/QaPairEdit.vue'), meta: { title: '编辑问答对', requiresAuth: true } }, { path: '/avatar/:avatarId/knowledge/qa/create', name: 'AvatarQaPairCreate', component: () => import('@/views/QaPairEdit.vue'), meta: { title: '添加问答对', requiresAuth: true } }, { path: '/avatar/:avatarId/knowledge/qa/:qaId/edit', name: 'AvatarQaPairEdit', component: () => import('@/views/QaPairEdit.vue'), meta: { title: '编辑问答对', requiresAuth: true } }, { path: '/login/sms', name: 'SmsLogin', component: () => import('@/views/SmsLogin.vue'), meta: { title: '会会数字分身登录' } } ] const router = createRouter({ // 混合架构下 web-view 内用 hash 模式,原生返回键与深链更稳 history: createWebHashHistory(), routes }) router.beforeEach((to, from, next) => { document.title = to.meta.title as string || '会会数字分身' if (to.meta.requiresUniWebView && !isInUniWebView()) { next({ path: '/avatar/manage' }) return } const hasLocalSession = Boolean(localStorage.getItem('hh_app_token')) const hasInjectedSession = Boolean(getAuthToken()) if (to.meta.requiresAuth && !hasLocalSession && !hasInjectedSession) { next({ path: '/login/sms', query: { redirect: to.fullPath } }) return } next() }) export default router