Bläddra i källkod

Merge remote-tracking branch 'origin/master'

chillqi 2 år sedan
förälder
incheckning
a503dcbdb6
3 ändrade filer med 50 tillägg och 4 borttagningar
  1. 37 4
      src/router/index.ts
  2. 12 0
      src/types/router-meta.d.ts
  3. 1 0
      tsconfig.json

+ 37 - 4
src/router/index.ts

@@ -33,11 +33,18 @@ const router = createRouter({
                 path: '/storeDetail/:storeId',
                 name: 'storeDetail',
                 component: () => import('../views/portal/StoreDetail.vue'),
+                meta: {
+                    title: '店铺详情'
+                }
             },
             {
                 path: '/staff/createProduct/:storeId',
                 name: 'createProduct',
                 component: () => import('../views/staff/CreateProduct.vue'),
+                meta: {
+                    title: '创建商品',
+                    permission: ['STAFF', 'MANAGER']
+                }
             },
             {
                 path: '/customer/coupon/',
@@ -53,30 +60,56 @@ const router = createRouter({
                 path: '/productDetail/:productId',
                 name: 'productDetail',
                 component: () => import('../views/portal/ProductDetail.vue'),
+                meta: {
+                    title: '商品详情'
+                }
             },
             {
                 path: '/customer/buyProduct/:productId',
                 name: 'buyProduct',
                 component: () => import('../views/customer/BuyProduct.vue'),
+                meta: {
+                    title: '购买商品',
+                    permission: ['CUSTOMER']
+                }
             },
             {
                 path: '/customer/allOrder',
-                name: 'bugProduct',
+                name: 'allOrder',
                 component: () => import('../views/customer/AllOrder.vue'),
+                meta: {
+                    title: '全部订单',
+                    permission: ['CUSTOMER']
+                }
             },
             {
                 path: '/manager/createStore',
+                name: 'createStore',
                 component: () => import('../views/manager/CreateStore.vue'),
+                meta: {
+                    title: '创建店铺',
+                    permission: ['MANAGER']
+                }
             },
         ]
     }]
 })
 
 router.beforeEach((to, from, next) => {
-    const role = sessionStorage.getItem('token');
+    const token:string|null = sessionStorage.getItem('token');
+    const role:string|null = sessionStorage.getItem('role')
 
-    if (role) {
-        next();
+    if (token) {
+        if(to.meta.permission) {
+            if(to.meta.permission.includes(role!)) {
+                next();
+            }
+            else {
+                next('/allStore');
+            }
+        } else {
+            next()
+        }
     } else {
         if(to.path === '/login') {
             next();

+ 12 - 0
src/types/router-meta.d.ts

@@ -0,0 +1,12 @@
+/**
+ * @description 扩展Vue-Router-Meta的类型,防止Typescript报错
+ */
+
+declare module 'vue-router' {
+    interface RouteMeta {
+        permission?: Array<string>
+        title?: string
+    }
+}
+
+export {}

+ 1 - 0
tsconfig.json

@@ -27,6 +27,7 @@
     "src/**/*.ts",
     "src/**/*.tsx",
     "src/**/*.vue",
+    "src/types/**/*.d.ts",
     "auto-imports.d.ts"
   ],
   "references": [{ "path": "./tsconfig.node.json" }]