| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- import Vue from 'vue'
- import VueRouter from 'vue-router'
- import Layout from '@/views/Layout.vue'
- import axios from 'axios'
- import { getEffectivePermissions, hasPermission } from '@/utils/permissions'
- Vue.use(VueRouter)
- const routes = [
- {
- path: '/login',
- name: 'Login',
- component: () => import('@/views/Login.vue')
- },
- {
- path: '/403',
- name: 'Forbidden',
- component: { template: '<div style="text-align:center;padding:100px"><h1>403</h1><p>权限不足</p><router-link to="/">返回首页</router-link></div>' },
- meta: { title: '权限不足' }
- },
- {
- path: '/',
- component: Layout,
- redirect: '/dashboard',
- children: [
- {
- path: 'dashboard',
- name: 'Dashboard',
- component: () => import('@/views/Dashboard.vue'),
- meta: { title: '首页', perm: 'dashboard' }
- },
- // 家庭管理
- {
- path: 'families',
- name: 'Families',
- component: () => import('@/views/Families.vue'),
- meta: { title: '家庭列表', perm: 'family:list' }
- },
- {
- path: 'children',
- name: 'Children',
- component: () => import('@/views/Children.vue'),
- meta: { title: '孩子管理', perm: 'family:children' }
- },
- {
- path: 'points',
- name: 'Points',
- component: () => import('@/views/Points.vue'),
- meta: { title: '积分管理', perm: 'family:points' }
- },
- // 任务管理
- {
- path: 'tasks',
- name: 'Tasks',
- component: () => import('@/views/Tasks.vue'),
- meta: { title: '任务列表', perm: 'task:list' }
- },
- {
- path: 'task-templates',
- name: 'TaskTemplates',
- component: () => import('@/views/TaskTemplates.vue'),
- meta: { title: '任务模板', perm: 'task:templates' }
- },
- // 奖励管理
- {
- path: 'rewards',
- name: 'Rewards',
- component: () => import('@/views/Rewards.vue'),
- meta: { title: '奖励列表', perm: 'reward:list' }
- },
- // 成长规划师管理
- {
- path: 'guide-packages',
- name: 'GuidePackages',
- component: () => import('@/views/GuidePackages.vue'),
- meta: { title: '成长规划师套餐', requiresTeacher: true, perm: 'biz:packages' }
- },
- {
- path: 'guide-family-task',
- name: 'GuideFamilyTask',
- component: () => import('@/views/GuideFamilyTask.vue'),
- meta: { title: '家庭任务管理', requiresTeacher: true, perm: 'service:families' }
- },
- {
- path: 'teacher-consult',
- name: 'TeacherConsult',
- component: () => import('@/views/TeacherConsult.vue'),
- meta: { title: '家长咨询', requiresTeacher: true, perm: 'assessment:consult' }
- },
- // 新增成长规划师模块
- {
- path: 'teacher-dashboard',
- name: 'TeacherDashboard',
- component: () => import('@/views/teacher/TeacherDashboard.vue'),
- meta: { title: '成长规划师工作台', requiresTeacher: true, perm: 'dashboard' }
- },
- {
- path: 'teacher-families',
- name: 'TeacherFamilies',
- component: () => import('@/views/teacher/TeacherFamilies.vue'),
- meta: { title: '我的家庭', requiresTeacher: true, perm: 'service:families' }
- },
- {
- path: 'teacher-team',
- name: 'TeacherTeam',
- component: () => import('@/views/teacher/TeacherTeam.vue'),
- meta: { title: '我的团队', requiresTeacher: true, perm: 'service:team' }
- },
- {
- path: 'teacher-packages',
- name: 'TeacherPackages',
- component: () => import('@/views/teacher/TeacherPackages.vue'),
- meta: { title: '套餐管理', requiresTeacher: true, perm: 'biz:packages' }
- },
- {
- path: 'teacher-orders',
- name: 'TeacherOrders',
- component: () => import('@/views/teacher/TeacherOrders.vue'),
- meta: { title: '订单与佣金', requiresTeacher: true, perm: 'biz:orders' }
- },
- // 系统管理
- {
- path: 'users',
- name: 'Users',
- component: () => import('@/views/Users.vue'),
- meta: { title: '用户管理', perm: 'system:users' }
- },
- {
- path: 'package-audit',
- name: 'PackageAudit',
- component: () => import('@/views/PackageAudit.vue'),
- meta: { title: '套餐审核', perm: 'system:audit' }
- },
- {
- path: 'guide-audit',
- name: 'GuideAudit',
- component: () => import('@/views/GuideAudit.vue'),
- meta: { title: '成长规划师审核', perm: 'system:audit' }
- },
- {
- path: 'operation-logs',
- name: 'OperationLogs',
- component: () => import('@/views/OperationLogs.vue'),
- meta: { title: '操作日志', perm: 'system:logs' }
- },
- // 服务管理(管理员)
- {
- path: 'service-types',
- name: 'ServiceTypes',
- component: () => import('@/views/admin/ServiceTypes.vue'),
- meta: { title: '服务类型管理', perm: 'system:config' }
- },
- {
- path: 'service-contents',
- name: 'ServiceContents',
- component: () => import('@/views/admin/ServiceContents.vue'),
- meta: { title: '服务内容管理', perm: 'system:config' }
- },
- {
- path: 'package-templates',
- name: 'PackageTemplates',
- component: () => import('@/views/admin/PackageTemplates.vue'),
- meta: { title: '套餐模板管理', perm: 'system:config' }
- },
- {
- path: 'teacher-messages',
- name: 'TeacherMessages',
- component: () => import('@/views/teacher/TeacherMessages.vue'),
- meta: { title: '消息中心', requiresTeacher: true, perm: 'messages' }
- },
- {
- path: 'teacher-assessment',
- name: 'TeacherAssessment',
- component: () => import('@/views/teacher/TeacherAssessment.vue'),
- meta: { title: 'DAN测评管理', requiresTeacher: true, perm: 'assessment:dan' }
- },
- {
- path: 'assessment-admin',
- name: 'AssessmentAdmin',
- component: () => import('@/views/admin/Assessment.vue'),
- meta: { title: '测评管理', perm: 'assessment:dan' }
- },
- {
- path: 'vendor-review',
- name: 'VendorReview',
- component: () => import('@/views/admin/VendorReview.vue'),
- meta: { title: '服务商审核', perm: 'system:audit' }
- },
- {
- path: 'product-manage',
- name: 'ProductManage',
- component: () => import('@/views/admin/ProductManage.vue'),
- meta: { title: '商品管理', perm: 'system:products' }
- },
- {
- path: 'order-manage',
- name: 'OrderManage',
- component: () => import('@/views/admin/OrderManage.vue'),
- meta: { title: '订单管理', perm: 'system:orders' }
- },
- {
- path: 'sys-config',
- name: 'SysConfig',
- component: () => import('@/views/admin/SysConfig.vue'),
- meta: { title: '系统配置', perm: 'system:config' }
- },
- {
- path: 'energy-sandbox',
- name: 'EnergySandbox',
- component: () => import('@/views/admin/EnergySandbox.vue'),
- meta: { title: '五维能量', perm: 'system:energy' }
- },
- // ========== 文章管理 ==========
- {
- path: 'article-categories',
- name: 'ArticleCategory',
- component: () => import('@/views/admin/ArticleCategory.vue'),
- meta: { title: '文章分类', perm: 'articles:categories' }
- },
- {
- path: 'article-manage',
- name: 'ArticleManage',
- component: () => import('@/views/admin/ArticleManage.vue'),
- meta: { title: '文章管理', perm: 'articles:manage' }
- },
- {
- path: 'article-edit',
- name: 'ArticleEdit',
- component: () => import('@/views/admin/ArticleEdit.vue'),
- meta: { title: '文章编辑', perm: 'articles:manage' }
- },
- // ========== 分佣裂变系统 ==========
- {
- path: 'product-profit-rate',
- name: 'ProductProfitRate',
- component: () => import('@/views/admin/ProductProfitRate.vue'),
- meta: { title: '产品利润率', perm: 'system:products' }
- },
- {
- path: 'withdraw-audit',
- name: 'WithdrawAudit',
- component: () => import('@/views/admin/WithdrawAudit.vue'),
- meta: { title: '提现审核', perm: 'system:audit' }
- },
- {
- path: 'education-systems',
- name: 'EducationSystems',
- component: () => import('@/views/admin/EducationSystems.vue'),
- meta: { title: '教育体系管理', perm: 'system:education' }
- },
- {
- path: 'zodiac-configs',
- name: 'ZodiacConfigs',
- component: () => import('@/views/admin/ZodiacConfigs.vue'),
- meta: { title: '星座配置', perm: 'system:config' }
- },
- {
- path: 'bazi-configs',
- name: 'BaziConfigs',
- component: () => import('@/views/admin/BaziConfigs.vue'),
- meta: { title: '八字配置', perm: 'system:config' }
- },
- {
- path: 'blood-type-configs',
- name: 'BloodTypeConfigs',
- component: () => import('@/views/admin/BloodTypeConfigs.vue'),
- meta: { title: '血型配置', perm: 'system:config' }
- },
- // ========== DanShop 电商管理 ==========
- {
- path: 'danshop-category',
- name: 'DanShopCategory',
- component: () => import('@/views/admin/DanShopCategoryManage'),
- meta: { title: '商品分类管理', perm: 'system:danshop' }
- },
- {
- path: 'danshop-profit-rule',
- name: 'DanShopProfitRule',
- component: () => import('@/views/admin/DanShopProfitRuleConfig'),
- meta: { title: '分润规则配置', perm: 'system:danshop' }
- },
- {
- path: 'danshop-logistics',
- name: 'DanShopLogistics',
- component: () => import('@/views/admin/DanShopLogisticsManage'),
- meta: { title: '物流管理', perm: 'system:danshop' }
- },
- {
- path: 'danshop-product',
- name: 'DanShopProduct',
- component: () => import('@/views/admin/ProductManage.vue'),
- meta: { title: 'DanShop商品', perm: 'system:products' }
- },
- {
- path: 'danshop-order',
- name: 'DanShopOrder',
- component: () => import('@/views/admin/OrderManage.vue'),
- meta: { title: 'DanShop订单', perm: 'system:orders' }
- },
- {
- path: 'my-families',
- name: 'MyFamilies',
- component: () => import('@/views/teacher/FamilyList.vue'),
- meta: { title: '我的家庭', perm: 'service:family' }
- },
- {
- path: 'growth-records',
- name: 'GrowthRecords',
- component: () => import('@/views/teacher/GrowthRecords.vue'),
- meta: { title: '成长记录', perm: 'growth:records' }
- },
- {
- path: 'growth-plans',
- name: 'GrowthPlans',
- component: () => import('@/views/teacher/GrowthPlans.vue'),
- meta: { title: '成长计划', perm: 'growth:plans' }
- },
- {
- path: 'health-reports',
- name: 'HealthReports',
- component: () => import('@/views/nutritionist/HealthReports.vue'),
- meta: { title: '健康报告', perm: 'health:reports' }
- },
- {
- path: 'health-indicators',
- name: 'HealthIndicators',
- component: () => import('@/views/nutritionist/HealthIndicators.vue'),
- meta: { title: '健康指标', perm: 'health:indicators' }
- },
- {
- path: 'health-checkins',
- name: 'HealthCheckins',
- component: () => import('@/views/nutritionist/HealthCheckins.vue'),
- meta: { title: '健康打卡', perm: 'health:checkins' }
- },
- {
- path: 'activities',
- name: 'Activities',
- component: () => import('@/views/admin/Activities.vue'),
- meta: { title: '活动列表', perm: 'activity:list' }
- },
- {
- path: 'activity-review',
- name: 'ActivityReview',
- component: () => import('@/views/admin/ActivityReview.vue'),
- meta: { title: '活动审核', perm: 'activity:review' }
- },
- // ========== 维度配置 + 知识库管理 ==========
- {
- path: 'dimension-config',
- name: 'DimensionConfig',
- component: () => import('@/views/admin/dimension'),
- meta: { title: '维度配置', perm: 'system:config' }
- },
- {
- path: 'knowledge-base',
- name: 'KnowledgeBase',
- component: () => import('@/views/admin/knowledge'),
- meta: { title: '知识库管理', perm: 'system:config' }
- }
- ]
- }
- ]
- const router = new VueRouter({
- mode: 'history',
- base: process.env.BASE_URL,
- routes
- })
- // Token 验证缓存:避免 redirect 链中重复调用 /api/admin-auth/info
- let tokenValidationCache = { token: '', timestamp: 0 }
- const TOKEN_CACHE_TTL = 30000 // 30秒内不重复验证
- async function validateToken(token) {
- try {
- const baseURL = process.env.VUE_APP_BASE_API || ''
- const res = await axios.post(`${baseURL}/api/admin-auth/info`, {}, {
- headers: { Authorization: `Bearer ${token}` },
- validateStatus: () => true
- })
- return res.status === 200 && res.data?.code === 200
- } catch (e) {
- return false
- }
- }
- router.beforeEach(async (to, from, next) => {
- const token = localStorage.getItem('token')
- const role = localStorage.getItem('role')
- // 1. 登录页允许访问
- if (to.path === '/login') {
- // 如果已有token和role,直接跳转到对应首页
- if (token && role === 'teacher') {
- return next('/teacher-dashboard')
- }
- return next()
- }
- // 2. 无Token强制跳转
- if (!token) {
- return next('/login')
- }
- // 3. Token有效性验证 (使用缓存避免 redirect 链重复调用)
- const now = Date.now()
- let isValid = false
- if (tokenValidationCache.token === token && (now - tokenValidationCache.timestamp) < TOKEN_CACHE_TTL) {
- isValid = true
- } else {
- isValid = await validateToken(token)
- if (isValid) {
- tokenValidationCache = { token, timestamp: now }
- }
- }
- if (!isValid) {
- tokenValidationCache = { token: '', timestamp: 0 }
- localStorage.clear()
- return next('/login')
- }
- // 4. 登录后首页重定向(多角色)
- const roleHomeMap = {
- 'teacher': '/teacher-dashboard',
- 'nutritionist': '/dashboard',
- 'activity_admin': '/dashboard',
- 'article_admin': '/dashboard'
- }
- if (to.path === '/dashboard' && roleHomeMap[role]) {
- return next({ path: roleHomeMap[role], replace: true })
- }
- // 5. 权限检查:如果路由有 perm 要求,检查用户权限
- const requiredPerm = to.meta && to.meta.perm
- if (requiredPerm) {
- const rolesStr = localStorage.getItem('roles')
- const roles = rolesStr ? JSON.parse(rolesStr) : [localStorage.getItem('role')]
- const perms = getEffectivePermissions(roles)
- if (!hasPermission(perms, requiredPerm)) {
- console.warn('Access denied: insufficient permissions for', to.name)
- return next({ path: roleHomeMap[role] || '/dashboard', query: { accessDenied: 'no-permission' }, replace: true })
- }
- }
- return next()
- })
- // Suppress navigation guard redirect errors (teacher/admin route cross-access)
- const originalPush = VueRouter.prototype.push
- VueRouter.prototype.push = function push(location) {
- return originalPush.call(this, location).catch(err => err)
- }
- export default router
|