import { to as tos } from 'await-to-js'; import router from './router'; import NProgress from 'nprogress'; import 'nprogress/nprogress.css'; import { getToken } from '@/utils/auth'; import { isPathMatch } from '@/utils/validate'; import { useUserStore } from '@/store/modules/user'; import { ElMessage } from 'element-plus/es'; import { getCurrentSite, SITE_ROUTES, onPath } from '@/utils/siteConfig'; import { cartStore } from '@/store/modules/cart'; import { stationStore } from '@/store/modules/station'; import Cookies from 'js-cookie'; NProgress.configure({ showSpinner: false }); const whiteList = [ '/login', '/register', '/social-callback', '/register*', '/register/*', '/index', '/', '/indexB', '/indexMro', '/indexFuli', '/reg', '/search', '/item', '/breg', '/greg', '/theme', '/indexDiy', '/indexMroDiy', '/indexFuliDiy', '/indexDataDiy' ]; const isWhiteList = (path: string) => { return whiteList.some((pattern) => isPathMatch(pattern, path)); }; // 根据配置决定首页路径 function getDomainHomePath(site: string, station: ReturnType, toPath: string) { const allowedPaths = SITE_ROUTES[site] || []; // 原有的 /indexData 和 /indexDataDiy 逻辑保持不变 const hasIndexData = allowedPaths.includes('/indexData'); const hasIndexDataDiy = allowedPaths.includes('/indexDataDiy'); if (station.stationData && Object.keys(station.stationData).length > 0) { const data = station.stationData.data || station.stationData; const isDiy = data?.isDiy; if (isDiy === 0 || isDiy === '0') { if (hasIndexData) { return '/indexData'; } } else if (isDiy !== undefined && isDiy !== null) { if (hasIndexDataDiy) { return '/indexDataDiy'; } } } // 新增:当 userSonType 为 '3' 时,优先跳转到 /indexEnterprise(如果允许) const userStore = useUserStore(); if (userStore.userInfo?.user?.userSonType == '3' && allowedPaths.includes('/indexEnterprise')) { console.log('????????????????????????????????????') return '/indexEnterprise'; } // 新增:根据 diyConfig 配置判断 /index、/indexMro、/indexFuli const diyConfig = station.diyState || []; // 处理 / 或 /index if (toPath === '/' || toPath === '/index') { const platformDiy = diyConfig.find((item: any) => item.configKey === 'platormDiy'); if (platformDiy?.value === '1' && allowedPaths.includes('/indexDiy')) { return '/indexDiy'; } if (allowedPaths.includes('/index')) { return '/index'; } } // 处理 /indexMro if (toPath === '/indexMro') { const industrialDiy = diyConfig.find((item: any) => item.configKey === 'industrialDiy'); if (industrialDiy?.value === '1' && allowedPaths.includes('/indexMroDiy')) { return '/indexMroDiy'; } return '/indexMro'; } // 处理 /indexFuli if (toPath === '/indexFuli') { const fuliDiy = diyConfig.find((item: any) => item.configKey === 'fuliDiy'); if (fuliDiy?.value === '1' && allowedPaths.includes('/indexFuliDiy')) { return '/indexFuliDiy'; } return '/indexFuli'; } return allowedPaths[0] || '/'; } // 统一跳转处理 function doRedirect(path: string, next: any, isMultiDomain: boolean) { if (isMultiDomain) { onPath(path); next(false); } else { next(path); } } router.beforeEach(async (to, from, next) => { NProgress.start(); const site = getCurrentSite(); const allowedPaths = SITE_ROUTES[site]; const token = Cookies.get('Authorization'); const isMultiDomain = import.meta.env.VITE_DOMAIN_NAME == 'true'; const station = stationStore(); const cart = cartStore(); const userStore = useUserStore(); // 需求一:判断是否需要调用 getInfo(有 token 或 不在白名单) // const needGetInfo = token || !isWhiteList(to.path); // console.log(!isWhiteList(to.path), '8888888888888888') if (token) { const [err] = await tos(userStore.getInfo()); // 需求五:getInfo 失败跳转/login if (err) { await useUserStore().logout(); ElMessage.error(err); doRedirect('/login', next, isMultiDomain); NProgress.done(); return; } // 有 token 时请求购物车和站点数据 if (token) { try { cart.onCartCount(); await station.onstation(); } catch (error) { console.error('获取站点数据失败:', error); } } } // 无论是否有 token,都获取 DIY 配置数据(新接口无需登录) try { await station.onDiyState(); } catch (error) { console.error('获取 DIY 配置失败:', error); } // 需求四:多域名下检查路径权限 if (isMultiDomain) { const homePaths = ['/', '/index', '/indexData', '/indexDataDiy', '/indexMro', '/indexFuli']; if (homePaths.includes(to.path)) { const homePath = getDomainHomePath(site, station, to.path); if (to.path !== homePath) { doRedirect(homePath, next, isMultiDomain); NProgress.done(); return; } } if (!allowedPaths.includes(to.path)) { console.warn(`[${site}] 禁止访问 ${to.path}`); const homePath = getDomainHomePath(site, station, to.path); doRedirect(homePath, next, isMultiDomain); NProgress.done(); return; } } else { // 单域名下首页处理 const homePaths = ['/', '/index', '/indexData', '/indexDataDiy', '/indexMro', '/indexFuli']; if (homePaths.includes(to.path)) { const homePath = getDomainHomePath(site, station, to.path); if (to.path !== homePath) { doRedirect(homePath, next, isMultiDomain); NProgress.done(); return; } } } // 需求三 & 需求六:正常放行 next(); NProgress.done(); }); router.afterEach(() => { NProgress.done(); });