router.ts 3.05 KB
// 路由:/login 登录;/m/* 手机免登录页;其余都在后台布局里(需登录)。
// meta.admin = 仅管理员可见:普通账号(客户)的菜单里不显示,直接输网址也会跳回工作台。
import { createRouter, createWebHistory } from 'vue-router'
import { hooks } from './api'
import { loadMe, session } from './session'

export const router = createRouter({
  history: createWebHistory(),
  scrollBehavior: () => ({ top: 0 }),
  routes: [
    { path: '/login', component: () => import('./pages/Login.vue'), meta: { public: true, title: '登录' } },
    { path: '/m/restock/order/:token', component: () => import('./mobile/OrderPage.vue'), meta: { public: true, title: '补货单' } },
    {
      path: '/',
      component: () => import('./layouts/Console.vue'),
      children: [
        { path: '', component: () => import('./pages/Dashboard.vue'), meta: { title: '工作台' } },
        { path: 'runs', component: () => import('./pages/restock/Runs.vue'), meta: { title: '补货单' } },
        { path: 'runs/:id', component: () => import('./pages/restock/RunDetail.vue'), meta: { title: '补货单详情', nav: '/runs' } },
        { path: 'categories', component: () => import('./pages/restock/Categories.vue'), meta: { title: '在库分类' } },
        { path: 'so', component: () => import('./pages/restock/SoSync.vue'), meta: { title: 'SO 单同步' } },
        { path: 'rules', component: () => import('./pages/restock/Rules.vue'), meta: { title: '补货规则', admin: true } },
        { path: 'exports', component: () => import('./pages/restock/Exports.vue'), meta: { title: '数据导出', admin: true } },
        { path: 'assistant', component: () => import('./pages/assistant/Assistant.vue'), meta: { title: '智能助手', admin: true } },
        { path: 'wecom', component: () => import('./pages/system/Wecom.vue'), meta: { title: '消息通道', admin: true } },
        { path: 'client', component: () => import('./pages/system/Client.vue'), meta: { title: '取数端', admin: true } },
        { path: 'alerts', component: () => import('./pages/system/Alerts.vue'), meta: { title: '告警' } },
        { path: 'audit', component: () => import('./pages/system/Audit.vue'), meta: { title: '操作日志', admin: true } },
        { path: 'settings', component: () => import('./pages/system/Settings.vue'), meta: { title: '系统设置', admin: true } },
      ],
    },
    { path: '/:pathMatch(.*)*', component: () => import('./pages/NotFound.vue'), meta: { public: true, title: '页面不存在' } },
  ],
})

hooks.onUnauthorized = () => {
  session.user = null
  if (!router.currentRoute.value.meta.public) router.replace({ path: '/login', query: { next: router.currentRoute.value.fullPath } })
}

router.beforeEach(async (to) => {
  document.title = `${to.meta.title ? to.meta.title + ' · ' : ''}库存补货助手`
  if (to.meta.public) return true
  if (!session.user && !(await loadMe())) return { path: '/login', query: to.fullPath !== '/' ? { next: to.fullPath } : {} }
  if (to.meta.admin && !session.isAdmin) return { path: '/' }
  return true
})