From 9d3f1aa7571cd532c0c27bf8376ebf4b2178a252 Mon Sep 17 00:00:00 2001 From: charles <981744753@qq.com> Date: 星期三, 24 七月 2024 17:45:45 +0800 Subject: [PATCH] fix:解决srm之前改了域名,处理各种异常状态,跳转也会出现异常等问题。 --- src/router/index.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 57 insertions(+), 1 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index 4834a64..64070cf 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -5,6 +5,8 @@ import supplierRouter from "./supplier/index.js" import purchaseRouter from "./purchase/index.js" import productRouter from "./product/index.js" +import {getMenuTreeByRole} from "@/api/menus/index" +import store from '@/store/index.js'; Vue.use(Router) const login = (resolve) => require(["@/views/other/login/index"], resolve) @@ -12,8 +14,20 @@ const purchaseManage = (resolve) => require(["@/views/purchaseManage/index"], resolve) const productManage = (resolve) => require(["@/views/productManage/index"], resolve) const PreviewFile = (resolve) => require(["@/views/supplierManage/supplier/PreviewFile"], resolve) +const noData = (resolve) => require(["@/views/NoData/index"], resolve) + export const routes = [ + // 鏃犳暟鎹〉闈� + { + path: "noData", + name: "noData", + meta: { + title: "", + auth: true, + }, + component: noData, + }, { path: "supplierManage", // 渚涘簲鍟嗙鐞� name: "supplierManage", @@ -87,7 +101,7 @@ // 瀵煎嚭璺敱 鍦� main.js 閲屼娇鐢� const createRouter = () => new Router({ - mode: "history", + mode: "hash", // base: window.getServerJson.context, scrollBehavior: () => ({ y: 0 }), routes: constantRoutes @@ -95,6 +109,48 @@ const router = createRouter() +let isSkip = false; +async function hasPermission(routePath) { + isSkip = false; + try { + const res = await getMenuTreeByRole(); + const newPath = { + path: "/noData" + }; + const foundObject = res.data.list.find(obj => obj.systemType === 4); + if (foundObject) { + // 瀛樺偍杩泇uex + store.commit('setMenus', foundObject); + foundObject.menus.forEach(item => { + const nextPath = item.children.find(obj => obj.path === routePath); + if (nextPath) { + newPath.path = nextPath.path; + isSkip = true; + } + }); + } else { + newPath.path = '/noData'; + } + return newPath; + } catch (error) { + return { path: "/noData" }; + } +} +router.beforeEach(async (to, from, next) => { + console.log(to,from,"鐪嬬湅") + try { + const result = await hasPermission(to.path); + console.log(result,"result") + next(); + if (!isSkip) { + next('/noData') + } + } catch (error) { + console.error('Error in navigation guard:', error); + next(); + } +}); + // router.beforeEach((to, from, next) => { // must call `next` // console.log(to, from) -- Gitblit v1.8.0