src/router/index.js
@@ -5,14 +5,29 @@
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)
const supplierManage = (resolve) => require(["@/views/supplierManage/index"], resolve)
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)
const commonSet = (resolve) => require(["@/views/systemSet/commonSet/index"], resolve) //系統設置
export const routes = [
  // 无数据页面
  {
    path: "noData",
    name: "noData",
    meta: {
      title: "",
      auth: true,
    },
    component: noData,
  },
  {
    path: "supplierManage", // 供应商管理
    name: "supplierManage",
@@ -42,6 +57,14 @@
      title: "产品管理",
      isAllways: true
    }
  },
  {
    path: "/systemSet/commonSet",
    name: "commonSet",
    component: commonSet,
    meta: {
      title: "通用设置"
    }
  }
]
export const constantRoutes = [
@@ -68,6 +91,14 @@
    }
  },
  {
    path: "/PreviewFile",
    component: PreviewFile,
    meta: {
      isLogin: true,
      title: "预览"
    }
  },
  {
    path: "*",
    component: () => import("@/views/other/error/404"),
    meta: {
@@ -86,6 +117,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) {
      // 存储进vuex
      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)