import router, {resetRouter} from "@/router"; import store from "./store"; import {Message} from "element-ui"; import NProgress from "nprogress"; // progress bar import "nprogress/nprogress.css"; // progress bar style import {getToken} from "@/utils/auth"; // get token from cookie import getPageTitle from "@/utils/get-page-title"; import {asyncRoutes, constantRoutes} from "@/router"; import Layout from "@/layout"; import {getHomePath} from "@/views/setting/tools"; NProgress.configure({showSpinner: false}); // NProgress Configuration const whiteList = ["/login", "/homePage", "/registrationPage", "/shoppingCart", "/homePageImage"]; // no redirect whitelist // 获取用户代理字符串 const userAgent = window.navigator.userAgent; // 判断是否为移动设备 const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent); router.beforeEach(async (to, from, next) => { // start progress bar NProgress.start(); // set page title document.title = getPageTitle(to.meta.title); // determine whether the user has logged in // const hasToken = getToken() // if (hasToken) { // if (to.path === '/login') { // // if is logged in, redirect to the home page // next({ path: '/' }) // NProgress.done() // } else { // // determine whether the user has obtained his permission roles through getInfo // const hasRoles = store.getters.roles && store.getters.roles.length > 0 // if (hasRoles) { // next() // } else { // try { // // get user info // // note: roles must be a object array! such as: ['admin'] or ,['developer','editor'] // const { roles } = await store.dispatch('user/getInfo') // // generate accessible routes map based on roles // const accessRoutes = await store.dispatch('permission/generateRoutes', roles) // // dynamically add accessible routes // router.addRoutes(accessRoutes) // // hack method to ensure that addRoutes is complete // // set the replace: true, so the navigation will not leave a history record // next({ ...to, replace: true }) // } catch (error) { // // remove token and go to login page to re-login // await store.dispatch('user/resetToken') // Message.error(error || 'Has Error') // next(`/login?redirect=${to.path}`) // NProgress.done() // } // } // } // } else { // /* has no token*/ // if (whiteList.indexOf(to.path) !== -1) { // // in the free login whitelist, go directly // next() // } else { // // other pages that do not have permission to access are redirected to the login page. // next(`/login?redirect=${to.path}`) // NProgress.done() // } // } const userid = sessionStorage.getItem("userId"); const user = sessionStorage.getItem("user"); const auths = sessionStorage.getItem("auths"); const homePath =getHomePath() if (to.path === homePath) { next(); // 如果已经是目标路径,直接放行 NProgress.done(); return; } console.log("homePath",homePath); // const homePath ='/homePage/index'; // const homePath ='/ncmatchHome/index'; const hasRoutes = JSON.parse(sessionStorage.getItem("routes")) && JSON.parse(sessionStorage.getItem("routes")).length ? true : false; console.log("topath", to.path) if (to.path.includes('/floatingBox') || to.path.includes('/gpu/gpuIndex')) { console.log("放行了") next() NProgress.done(); return } if (to.path === '/homePage'||to.path === '/ncmatchHome') { next(homePath); NProgress.done(); return } // if (to.path === '/') { // next('/product/productHome'); // NProgress.done(); // return // } if (to.path.includes("/ncmatchHome")||to.path.includes("/kyyForm") || to.path.includes("/screen") || to.path.includes("/beforeLogin") || to.path.includes("/wxDetailPage") || to.path.includes("/wxPage") || to.path.includes("/login") || to.path.includes("/homePage") || to.path.includes("/registrationPage") || to.path.includes("/payPage") || to.path.includes("/paySuccess") || to.path.includes("/homePageImage")) { console.log("to", to) try { if (to.path.includes("/beforeLogin") || to.path.includes("/registrationPage")) { if (isMobile && to.path.indexOf('/mobile') == -1) { const mobilePath = to.path + '/mobile'; next(mobilePath); } else { next(); } } else { next(); } } catch (error) { console.log(error); } } else { try { if (!userid || !user) { for (let item of whiteList) { if (to.path.includes(item)) { next(); NProgress.done(); return; } } // next(`/login?redirect=${to.path}`); console.log("路由执行了2@@@") next(homePath) // next(`/beforeLogin`); // console.log("111行被打印了") NProgress.done(); } else { if (store.getters.permission_routes.length === 0) { const accessRoutes = await store.dispatch( "permission/generateRoutes", { user: user, auths: JSON.parse(auths), } ); resetRouter(); router.addRoutes(accessRoutes); // if (isMobile && to.path.indexOf('/mobile') == -1) { // to.path = to.path + '/mobile' // } next({...to, replace: true}); // next(path) } else { next(); NProgress.done(); } } } catch (error) { // console.log(error); Message.error(error || "Has Error"); // next(`/login?redirect=${to.path}`); next(homePath) // console.log("137行被打印了") NProgress.done(); } } }); // function filterAsyncRouter(asyncRouterMap, lastRouter = false) { // console.log(asyncRouterMap) // return asyncRouterMap.filter((route) => { // console.log(route) // if (route.component) { // // Layout ParentView 组件特殊处理 // if (route.component === "Layout") { // route.component = Layout; // } else { // route.component = loadView(route.component); // } // } // if (route.children != null && route.children && route.children.length) { // route.children = filterAsyncRouter(route.children, route); // } else { // delete route["children"]; // delete route["redirect"]; // } // return true; // }); // } // const loadView = (view) => { // if (process.env.NODE_ENV === "development") { // return (resolve) => require([`@/views/${view}`], resolve); // } else { // // 使用 import 实现生产环境的路由懒加载 // return () => import(`@/views/${view}`); // } // }; router.afterEach(() => { // finish progress bar NProgress.done(); });