zuozhengqing
2023-12-20 15bcbd44b6a3b2ec34092bc5ed3e213fa9c55de4
src/router/index.js
@@ -6,6 +6,8 @@
import operateRouter from "./operate/index.js" // 操作
import warehouseManageRouter from "./warehouseManage/index.js" // 仓库管理
import reportRouter from "./report/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)
@@ -19,8 +21,8 @@
export const routes = [
  // 无权限数据页面
  {
    path: "/",
    name: "NoData",
    path: "noData",
    name: "noData",
    meta: {
      title: "",
      auth: true,
@@ -118,9 +120,54 @@
  })
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 === 3);
    if (foundObject) {
      // 存储进vuex
      store.commit('setMenus', foundObject.menus);
      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) => {
  try {
    const result = await hasPermission(to.path);
    console.log(result,"result")
    next();
    if (!isSkip) {
      if(to.path==="/overview/overviewList"||to.path==="/overview/previewExcel"||to.path==="/operate/inventoryAdjustmentHistory"){
        next();
      }else{
        next('/noData')
      }
    }
  } catch (error) {
    console.error('Error in navigation guard:', error);
    next();
  }
});
router.afterEach((to, from, next) => {
  console.log(to, "to")
  if ((to.path === "/overview/overviewList" || to.path === "/productManage/productList") && to.params.name) {
    console.log(from, next)
    to.meta.title = to.params.name