// 从环境变量获取域名配置 export const DOMAIN_MAP: Record = { www: import.meta.env.VITE_DOMAIN_WWW || 'index.xiaoluwebsite.xyz', b: import.meta.env.VITE_DOMAIN_B || 'b.xiaoluwebsite.xyz', mro: import.meta.env.VITE_DOMAIN_MRO || 'mro.xiaoluwebsite.xyz', fuli: import.meta.env.VITE_DOMAIN_FULI || 'fuli.xiaoluwebsite.xyz', reg: import.meta.env.VITE_DOMAIN_REG || 'reg.xiaoluwebsite.xyz', breg: import.meta.env.VITE_DOMAIN_BREG || 'breg.xiaoluwebsite.xyz', greg: import.meta.env.VITE_DOMAIN_GREG || 'greg.xiaoluwebsite.xyz', passport: import.meta.env.VITE_DOMAIN_PASSPORT || 'pass.xiaoluwebsite.xyz', search: import.meta.env.VITE_DOMAIN_SEARCH || 'search.xiaoluwebsite.xyz', item: import.meta.env.VITE_DOMAIN_ITEM || 'item.xiaoluwebsite.xyz', cart: import.meta.env.VITE_DOMAIN_CART || 'cart.xiaoluwebsite.xyz', trad: import.meta.env.VITE_DOMAIN_TRAD || 'trad.xiaoluwebsite.xyz', payc: import.meta.env.VITE_DOMAIN_PAYC || 'payc.xiaoluwebsite.xyz', order: import.meta.env.VITE_DOMAIN_ORDER || 'order.xiaoluwebsite.xyz', plan: import.meta.env.VITE_DOMAIN_PLAN || 'plan.xiaoluwebsite.xyz', plan_info: import.meta.env.VITE_DOMAIN_PLAN_INFO || 'planinfo.xiaoluwebsite.xyz', i: import.meta.env.VITE_DOMAIN_I || 'i.xiaoluwebsite.xyz', easybuv: import.meta.env.VITE_DOMAIN_EASYBUV || 'easybuv.xiaoluwebsite.xyz' }; // 反向域名映射 (域名 -> 站点名) const REVERSE_DOMAIN_MAP: Record = Object.entries(DOMAIN_MAP).reduce( (acc, [site, domain]) => { acc[domain] = site; return acc; }, {} as Record ); // 每个站点允许的路由 (保持不变) export const SITE_ROUTES: Record = { www: ['/', '/index', '/indexDiy', '/indexData', '/indexDataDiy', '/theme', '/indexEnterprise'], //优易365主站 b: ['/indexB'], //企业购商城 mro: ['/indexMro', '/indexMroDiy'], //工业品商城 fuli: ['/indexFuli', '/indexFuliDiy'], //福礼商城 reg: ['/reg'], //个人注册 breg: ['/breg'], //企业注册 greg: ['/greg'], //供应商注册 passport: ['/login'], //登录页 search: ['/search', '/search/special', '/search/brand'], //搜索 item: ['/item'], //商品详情, cart: ['/cart'], //商品详情 trad: ['/trad'], //确认订单信息 payc: ['/payc'], //支付订单 plan: ['/plan', '/plan/procure', '/plan/guide', '/plan/project'], //解决方案 plan_info: ['/plan_info', '/plan_info/procure', '/plan_info/guide', '/plan_info/project', '/solve/real'], //信息展示 order: [ '/order/orderManage', '/order/orderManage/detail', '/order/orderAudit', '/order/afterSale', '/order/batchOrder', '/order/orderEvaluation', '/enterprise/companyInfo', '/enterprise/companyInfo/edit', '/enterprise/purchaseHabit', '/enterprise/invoiceManage', '/enterprise/agreementSupply', '/enterprise/companyInfo', '/enterprise/invoiceManage', '/enterprise/myCollection', '/enterprise/myFootprint', '/enterprise/purchaseHabit', '/enterprise/purchasePlan', '/enterprise/purchaseHistory', '/reconciliation/billManage', '/reconciliation/billManage/detail', '/reconciliation/invoiceManage', '/reconciliation/invoiceManage/detail', '/organization/deptManage', '/organization/staffManage', '/organization/roleManage', '/valueAdded/maintenance', '/valueAdded/complaint', '/cost/itemExpense', '/cost/quotaControl', '/cost/manageDetail', '/cost/quotaControl/apply', '/enterprise/purchasePlan', '/organization/approvalFlow', '/organization/approvalFlow/create', '/order/orderManage/detail/:orderNo', '/order/orderManage/applyAfter', '/valueAdded/maintenanceApply', '/enterprise/messageNotice', '/enterprise/securitySetting', '/enterprise/securitySetting/resetPassword', '/enterprise/securitySetting/changePhone', '/enterprise/changePerson', '/enterprise/messageNoticeAdd', '/order/orderEvaluation/evaluation' ], //订单列表 i: ['/i'], //个人信息 easybuv: ['/easybuv'] //地址管理 }; // 获取当前站点 export function getCurrentSite(): any { // 无论是生产还是开发,现在都统一通过域名判断 const host = window.location.hostname; // 使用环境变量配置的域名映射 if (host === 'localhost') return 'www'; // 兼容未配hosts的情况 // 从反向映射中查找站点 const site = REVERSE_DOMAIN_MAP[host]; if (site) return site; // 生产环境逻辑 (保持不变,或者合并到上面的判断中) if (import.meta.env.PROD) { // 如果上面没匹配到,且是生产环境,可以尝试原有的逻辑或默认返回 www // 这里建议直接复用上面的 hostname 判断,因为生产环境也是域名 return 'www'; } return 'www'; // 默认 fallback } // ... PATH_TO_SITE_MAP 和 getSiteByPath 保持不变 ... const PATH_TO_SITE_MAP: Record = {}; for (const [site, paths] of Object.entries(SITE_ROUTES)) { for (const path of paths) { PATH_TO_SITE_MAP[path] = site as any; } } export function getSiteByPath(path: string): any | null { const cleanPath = path.split('?')[0]; return PATH_TO_SITE_MAP[cleanPath] || null; } // 跨站跳转逻辑 import router from '@/router'; // ... 前面的 DOMAIN_MAP, SITE_ROUTES, getCurrentSite, getSiteByPath 等代码保持不变 ... /** * 智能跳转函数 * @param path 跳转路径、相对路径或完整URL (可能包含 openType 参数) */ export function onPath(path: string) { if (!path) return; console.log('[跨站跳转] 原始输入:', path); // --- 辅助函数:解析并移除 openType --- const parseAndCleanUrl = (url: string) => { let cleanPath = url; let extractedOpenType: number | undefined = undefined; try { // 构造 URL 对象以便解析参数 // 如果是相对路径,补充临时协议和域名以便解析 const baseUrl = url.startsWith('http') ? url : `http://temp${url}`; const urlObj = new URL(baseUrl); // 1. 提取 openType const otParam = urlObj.searchParams.get('openType'); if (otParam !== null) { extractedOpenType = Number(otParam); // 2. 移除 openType 参数 urlObj.searchParams.delete('openType'); } // 3. 生成清洗后的路径 if (url.startsWith('http')) { // 绝对路径:返回完整 URL cleanPath = urlObj.toString(); } else { // 相对路径:返回 path + search + hash cleanPath = urlObj.pathname + urlObj.search + urlObj.hash; } } catch (e) { // 降级处理:如果 URL 解析失败,使用正则移除 openType console.warn('[跨站跳转] URL 解析失败,使用正则降级处理', e); // 提取 openType (简单匹配 ?openType=x 或 &openType=x) const match = url.match(/[?&]openType=(\d+)/); if (match) { extractedOpenType = Number(match[1]); } // 移除 openType 参数 cleanPath = url .replace(/[?&]openType=\d+/, (match, offset, string) => { // 如果是第一个参数 (?openType=...),替换为 ? // 如果是后续参数 (&openType=...),替换为空 // 这里简化处理:直接去掉,最后统一清理多余的 ? 或 & return ''; }) .replace(/([?&])$/, '') // 去除末尾残留的 ? 或 & .replace(/\?&/, '?'); // 修复 ?& 变成 ? } return { cleanPath, openType: extractedOpenType }; }; // 1. 解析路径,获取清洗后的路径和 openType const { cleanPath, openType } = parseAndCleanUrl(path); console.log('[跨站跳转] 清洗后路径:', cleanPath, '提取的 openType:', openType); // 2. 判断是否为全路径 (http:// 或 https://) if (/^https?:\/\//i.test(cleanPath)) { if (openType === 0) { // openType 为 0,当前窗口打开 window.location.href = cleanPath; } else { // openType 为 1 或 undefined,新窗口打开 window.open(cleanPath, '_blank'); } return; } // 3. 获取目标站点 (使用清洗后的路径) const targetSite = getSiteByPath(cleanPath); if (!targetSite) { console.error(`[跨站跳转失败] 路径 "${cleanPath}" 未关联任何站点`); return; } // 4. 获取当前站点 const currentSite = getCurrentSite(); // 5. 构建目标 URL (用于跨域跳转) const baseDomain = DOMAIN_MAP[targetSite]; let finalUrl = ''; if (import.meta.env.PROD) { // 正式环境:不加端口 finalUrl = `https://${baseDomain}${cleanPath}`; } else { // 开发环境:加上端口 const devPort = window.location.port || import.meta.env.VITE_APP_PORT; finalUrl = `https://${baseDomain}:${devPort}${cleanPath}`; } // 6. 判断是否同域名 const isSameSite = currentSite === targetSite; // 7. 执行跳转逻辑 // 【优先级最高】如果 path 中包含了 openType,严格按照 openType 执行 if (openType !== undefined) { if (openType === 1) { // 新窗口打开 window.open(finalUrl, '_blank'); } else { // 当前窗口打开 (openType === 0) if (isSameSite) { // 同域且强制当前窗口:使用 router.push router.push(cleanPath); } else { // 跨域且强制当前窗口:使用 location.href window.location.href = finalUrl; } } return; } // 【默认行为】未指定 openType,根据是否同域名自动判断 if (isSameSite) { // 同域名:使用 Vue Router 内部跳转 router.push(cleanPath); } else { // 跨域名:使用新窗口打开 window.open(finalUrl, '_blank'); } }