siteConfig.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // 从环境变量获取域名配置
  2. export const DOMAIN_MAP: Record<string, string> = {
  3. www: import.meta.env.VITE_DOMAIN_WWW || 'index.xiaoluwebsite.xyz',
  4. b: import.meta.env.VITE_DOMAIN_B || 'b.xiaoluwebsite.xyz',
  5. mro: import.meta.env.VITE_DOMAIN_MRO || 'mro.xiaoluwebsite.xyz',
  6. fuli: import.meta.env.VITE_DOMAIN_FULI || 'fuli.xiaoluwebsite.xyz',
  7. reg: import.meta.env.VITE_DOMAIN_REG || 'reg.xiaoluwebsite.xyz',
  8. breg: import.meta.env.VITE_DOMAIN_BREG || 'breg.xiaoluwebsite.xyz',
  9. greg: import.meta.env.VITE_DOMAIN_GREG || 'greg.xiaoluwebsite.xyz',
  10. passport: import.meta.env.VITE_DOMAIN_PASSPORT || 'pass.xiaoluwebsite.xyz',
  11. search: import.meta.env.VITE_DOMAIN_SEARCH || 'search.xiaoluwebsite.xyz',
  12. item: import.meta.env.VITE_DOMAIN_ITEM || 'item.xiaoluwebsite.xyz',
  13. cart: import.meta.env.VITE_DOMAIN_CART || 'cart.xiaoluwebsite.xyz',
  14. trad: import.meta.env.VITE_DOMAIN_TRAD || 'trad.xiaoluwebsite.xyz',
  15. payc: import.meta.env.VITE_DOMAIN_PAYC || 'payc.xiaoluwebsite.xyz',
  16. order: import.meta.env.VITE_DOMAIN_ORDER || 'order.xiaoluwebsite.xyz',
  17. plan: import.meta.env.VITE_DOMAIN_PLAN || 'plan.xiaoluwebsite.xyz',
  18. plan_info: import.meta.env.VITE_DOMAIN_PLAN_INFO || 'planinfo.xiaoluwebsite.xyz',
  19. i: import.meta.env.VITE_DOMAIN_I || 'i.xiaoluwebsite.xyz',
  20. easybuv: import.meta.env.VITE_DOMAIN_EASYBUV || 'easybuv.xiaoluwebsite.xyz'
  21. };
  22. // 反向域名映射 (域名 -> 站点名)
  23. const REVERSE_DOMAIN_MAP: Record<string, string> = Object.entries(DOMAIN_MAP).reduce(
  24. (acc, [site, domain]) => {
  25. acc[domain] = site;
  26. return acc;
  27. },
  28. {} as Record<string, string>
  29. );
  30. // 每个站点允许的路由 (保持不变)
  31. export const SITE_ROUTES: Record<any, string[]> = {
  32. www: ['/', '/index', '/indexDiy', '/indexData', '/indexDataDiy', '/theme', '/indexEnterprise'], //优易365主站
  33. b: ['/indexB'], //企业购商城
  34. mro: ['/indexMro', '/indexMroDiy'], //工业品商城
  35. fuli: ['/indexFuli', '/indexFuliDiy'], //福礼商城
  36. reg: ['/reg'], //个人注册
  37. breg: ['/breg'], //企业注册
  38. greg: ['/greg'], //供应商注册
  39. passport: ['/login'], //登录页
  40. search: ['/search', '/search/special', '/search/brand'], //搜索
  41. item: ['/item', '/itemPreview'], //商品详情,
  42. cart: ['/cart'], //商品详情
  43. trad: ['/trad'], //确认订单信息
  44. payc: ['/payc'], //支付订单
  45. plan: ['/plan', '/plan/procure', '/plan/guide', '/plan/project'], //解决方案
  46. plan_info: ['/plan_info', '/plan_info/procure', '/plan_info/guide', '/plan_info/project', '/solve/real'], //信息展示
  47. order: [
  48. '/order/orderManage',
  49. '/order/orderManage/detail',
  50. '/order/orderAudit',
  51. '/order/afterSale',
  52. '/order/batchOrder',
  53. '/order/orderEvaluation',
  54. '/enterprise/companyInfo',
  55. '/enterprise/companyInfo/edit',
  56. '/enterprise/purchaseHabit',
  57. '/enterprise/invoiceManage',
  58. '/enterprise/agreementSupply',
  59. '/enterprise/companyInfo',
  60. '/enterprise/invoiceManage',
  61. '/enterprise/myCollection',
  62. '/enterprise/myFootprint',
  63. '/enterprise/purchaseHabit',
  64. '/enterprise/purchasePlan',
  65. '/enterprise/purchaseHistory',
  66. '/reconciliation/billManage',
  67. '/reconciliation/billManage/detail',
  68. '/reconciliation/invoiceManage',
  69. '/reconciliation/invoiceManage/detail',
  70. '/organization/deptManage',
  71. '/organization/staffManage',
  72. '/organization/roleManage',
  73. '/valueAdded/maintenance',
  74. '/valueAdded/complaint',
  75. '/cost/itemExpense',
  76. '/cost/quotaControl',
  77. '/cost/manageDetail',
  78. '/cost/quotaControl/apply',
  79. '/enterprise/purchasePlan',
  80. '/organization/approvalFlow',
  81. '/organization/approvalFlow/create',
  82. '/order/orderManage/detail/:orderNo',
  83. '/order/orderManage/applyAfter',
  84. '/valueAdded/maintenanceApply',
  85. '/enterprise/messageNotice',
  86. '/enterprise/securitySetting',
  87. '/enterprise/securitySetting/resetPassword',
  88. '/enterprise/securitySetting/changePhone',
  89. '/enterprise/changePerson',
  90. '/enterprise/messageNoticeAdd',
  91. '/order/orderEvaluation/evaluation',
  92. '/analysis/orderAnalysis',
  93. '/analysis/purchaseDetail',
  94. '/analysis/orderStatus',
  95. '/analysis/settlementStatus',
  96. '/analysis/deptPurchase'
  97. ], //订单列表
  98. i: ['/i'], //个人信息
  99. easybuv: ['/easybuv'] //地址管理
  100. };
  101. // 获取当前站点
  102. export function getCurrentSite(): any {
  103. // 无论是生产还是开发,现在都统一通过域名判断
  104. const host = window.location.hostname;
  105. // 使用环境变量配置的域名映射
  106. if (host === 'localhost') return 'www'; // 兼容未配hosts的情况
  107. // 从反向映射中查找站点
  108. const site = REVERSE_DOMAIN_MAP[host];
  109. if (site) return site;
  110. // 生产环境逻辑 (保持不变,或者合并到上面的判断中)
  111. if (import.meta.env.PROD) {
  112. // 如果上面没匹配到,且是生产环境,可以尝试原有的逻辑或默认返回 www
  113. // 这里建议直接复用上面的 hostname 判断,因为生产环境也是域名
  114. return 'www';
  115. }
  116. return 'www'; // 默认 fallback
  117. }
  118. // ... PATH_TO_SITE_MAP 和 getSiteByPath 保持不变 ...
  119. const PATH_TO_SITE_MAP: Record<string, any> = {};
  120. for (const [site, paths] of Object.entries(SITE_ROUTES)) {
  121. for (const path of paths) {
  122. PATH_TO_SITE_MAP[path] = site as any;
  123. }
  124. }
  125. export function getSiteByPath(path: string): any | null {
  126. const cleanPath = path.split('?')[0];
  127. return PATH_TO_SITE_MAP[cleanPath] || null;
  128. }
  129. // 跨站跳转逻辑
  130. import router from '@/router';
  131. // ... 前面的 DOMAIN_MAP, SITE_ROUTES, getCurrentSite, getSiteByPath 等代码保持不变 ...
  132. /**
  133. * 智能跳转函数
  134. * @param path 跳转路径、相对路径或完整URL (可能包含 openType 参数)
  135. */
  136. export function onPath(path: string) {
  137. if (!path) return;
  138. console.log('[跨站跳转] 原始输入:', path);
  139. // --- 辅助函数:解析并移除 openType ---
  140. const parseAndCleanUrl = (url: string) => {
  141. let cleanPath = url;
  142. let extractedOpenType: number | undefined = undefined;
  143. try {
  144. // 构造 URL 对象以便解析参数
  145. // 如果是相对路径,补充临时协议和域名以便解析
  146. const baseUrl = url.startsWith('http') ? url : `http://temp${url}`;
  147. const urlObj = new URL(baseUrl);
  148. // 1. 提取 openType
  149. const otParam = urlObj.searchParams.get('openType');
  150. if (otParam !== null) {
  151. extractedOpenType = Number(otParam);
  152. // 2. 移除 openType 参数
  153. urlObj.searchParams.delete('openType');
  154. }
  155. // 3. 生成清洗后的路径
  156. if (url.startsWith('http')) {
  157. // 绝对路径:返回完整 URL
  158. cleanPath = urlObj.toString();
  159. } else {
  160. // 相对路径:返回 path + search + hash
  161. cleanPath = urlObj.pathname + urlObj.search + urlObj.hash;
  162. }
  163. } catch (e) {
  164. // 降级处理:如果 URL 解析失败,使用正则移除 openType
  165. console.warn('[跨站跳转] URL 解析失败,使用正则降级处理', e);
  166. // 提取 openType (简单匹配 ?openType=x 或 &openType=x)
  167. const match = url.match(/[?&]openType=(\d+)/);
  168. if (match) {
  169. extractedOpenType = Number(match[1]);
  170. }
  171. // 移除 openType 参数
  172. cleanPath = url
  173. .replace(/[?&]openType=\d+/, (match, offset, string) => {
  174. // 如果是第一个参数 (?openType=...),替换为 ?
  175. // 如果是后续参数 (&openType=...),替换为空
  176. // 这里简化处理:直接去掉,最后统一清理多余的 ? 或 &
  177. return '';
  178. })
  179. .replace(/([?&])$/, '') // 去除末尾残留的 ? 或 &
  180. .replace(/\?&/, '?'); // 修复 ?& 变成 ?
  181. }
  182. return {
  183. cleanPath,
  184. openType: extractedOpenType
  185. };
  186. };
  187. // 1. 解析路径,获取清洗后的路径和 openType
  188. const { cleanPath, openType } = parseAndCleanUrl(path);
  189. console.log('[跨站跳转] 清洗后路径:', cleanPath, '提取的 openType:', openType);
  190. // 2. 判断是否为全路径 (http:// 或 https://)
  191. if (/^https?:\/\//i.test(cleanPath)) {
  192. if (openType === 0) {
  193. // openType 为 0,当前窗口打开
  194. window.location.href = cleanPath;
  195. } else {
  196. // openType 为 1 或 undefined,新窗口打开
  197. window.open(cleanPath, '_blank');
  198. }
  199. return;
  200. }
  201. // 3. 获取目标站点 (使用清洗后的路径)
  202. const targetSite = getSiteByPath(cleanPath);
  203. if (!targetSite) {
  204. console.error(`[跨站跳转失败] 路径 "${cleanPath}" 未关联任何站点`);
  205. return;
  206. }
  207. // 4. 获取当前站点
  208. const currentSite = getCurrentSite();
  209. // 5. 构建目标 URL (用于跨域跳转)
  210. const baseDomain = DOMAIN_MAP[targetSite];
  211. let finalUrl = '';
  212. if (import.meta.env.PROD) {
  213. // 正式环境:不加端口
  214. finalUrl = `https://${baseDomain}${cleanPath}`;
  215. } else {
  216. // 开发环境:加上端口
  217. const devPort = window.location.port || import.meta.env.VITE_APP_PORT;
  218. finalUrl = `https://${baseDomain}:${devPort}${cleanPath}`;
  219. }
  220. // 6. 判断是否同域名
  221. const isSameSite = currentSite === targetSite;
  222. // 7. 执行跳转逻辑
  223. // 【优先级最高】如果 path 中包含了 openType,严格按照 openType 执行
  224. if (openType !== undefined) {
  225. if (openType === 1) {
  226. // 新窗口打开
  227. window.open(finalUrl, '_blank');
  228. } else {
  229. // 当前窗口打开 (openType === 0)
  230. if (isSameSite) {
  231. // 同域且强制当前窗口:使用 router.push
  232. router.push(cleanPath);
  233. } else {
  234. // 跨域且强制当前窗口:使用 location.href
  235. window.location.href = finalUrl;
  236. }
  237. }
  238. return;
  239. }
  240. // 【默认行为】未指定 openType,根据是否同域名自动判断
  241. if (isSameSite) {
  242. // 同域名:使用 Vue Router 内部跳转
  243. router.push(cleanPath);
  244. } else {
  245. // 跨域名:使用新窗口打开
  246. window.open(finalUrl, '_blank');
  247. }
  248. }