permission.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { to as tos } from 'await-to-js';
  2. import router from './router';
  3. import NProgress from 'nprogress';
  4. import 'nprogress/nprogress.css';
  5. import { getToken } from '@/utils/auth';
  6. import { isPathMatch } from '@/utils/validate';
  7. import { useUserStore } from '@/store/modules/user';
  8. import { ElMessage } from 'element-plus/es';
  9. import { getCurrentSite, SITE_ROUTES, onPath } from '@/utils/siteConfig';
  10. import { cartStore } from '@/store/modules/cart';
  11. import { stationStore } from '@/store/modules/station';
  12. import Cookies from 'js-cookie';
  13. NProgress.configure({ showSpinner: false });
  14. const whiteList = [
  15. '/login',
  16. '/register',
  17. '/social-callback',
  18. '/register*',
  19. '/register/*',
  20. '/index',
  21. '/',
  22. '/indexB',
  23. '/indexMro',
  24. '/indexFuli',
  25. '/reg',
  26. '/search',
  27. '/item',
  28. '/breg',
  29. '/greg',
  30. '/diy',
  31. '/theme'
  32. ];
  33. const isWhiteList = (path: string) => {
  34. return whiteList.some((pattern) => isPathMatch(pattern, path));
  35. };
  36. function getMainSiteUrl(path: string) {
  37. if (import.meta.env.PROD) {
  38. return `https://index.xiaoluwebsite.xyz${path}`;
  39. } else {
  40. const devPort = window.location.port || import.meta.env.VITE_APP_PORT;
  41. return `https://index.xiaoluwebsite.xyz:${devPort}${path}`;
  42. }
  43. }
  44. // 根据 isDiy 字段决定首页路径
  45. function getDomainHomePath(site: string, station: ReturnType<typeof stationStore>) {
  46. const allowedPaths = SITE_ROUTES[site] || [];
  47. const hasIndexData = allowedPaths.includes('/indexData');
  48. const hasDiy = allowedPaths.includes('/diy');
  49. const hasIndex = allowedPaths.includes('/index');
  50. if (station.stationData && Object.keys(station.stationData).length > 0) {
  51. // 兼容:stationData.data.isDiy 或 stationData.isDiy
  52. const data = station.stationData.data || station.stationData;
  53. const isDiy = data?.isDiy;
  54. if (isDiy === 0 || isDiy === '0') {
  55. if (hasIndexData) {
  56. return '/indexData';
  57. }
  58. } else if (isDiy !== undefined && isDiy !== null) {
  59. if (hasDiy) {
  60. return '/diy';
  61. }
  62. }
  63. }
  64. if (hasIndex) {
  65. return '/index';
  66. }
  67. return allowedPaths[0] || '/';
  68. }
  69. // 统一跳转处理
  70. function doRedirect(path: string, next: any, isMultiDomain: boolean) {
  71. if (isMultiDomain) {
  72. onPath(path);
  73. next(false);
  74. } else {
  75. next(path);
  76. }
  77. }
  78. router.beforeEach(async (to, from, next) => {
  79. NProgress.start();
  80. const site = getCurrentSite();
  81. const allowedPaths = SITE_ROUTES[site];
  82. const token = Cookies.get('Authorization');
  83. const isMultiDomain = import.meta.env.VITE_DOMAIN_NAME == 'true';
  84. const station = stationStore();
  85. const cart = cartStore();
  86. const userStore = useUserStore();
  87. // 需求一:判断是否需要调用 getInfo(有 token 或 不在白名单)
  88. const needGetInfo = token || !isWhiteList(to.path);
  89. if (needGetInfo) {
  90. const [err] = await tos(userStore.getInfo());
  91. // 需求五:getInfo 失败跳转/login
  92. if (err) {
  93. await userStore.logout();
  94. ElMessage.error(err);
  95. doRedirect('/login', next, isMultiDomain);
  96. NProgress.done();
  97. return;
  98. }
  99. // 修复:有 token 时才请求购物车和站点数据
  100. if (token) {
  101. try {
  102. // 获取购物车数量
  103. cart.onCartCount();
  104. // 获取站点数据
  105. await station.onstation();
  106. } catch (error) {
  107. console.error('获取站点数据失败:', error);
  108. }
  109. }
  110. }
  111. // 需求四:多域名下检查路径权限
  112. if (isMultiDomain) {
  113. const homePaths = ['/', '/index', '/indexData', '/diy'];
  114. if (homePaths.includes(to.path)) {
  115. const homePath = getDomainHomePath(site, station);
  116. if (to.path !== homePath) {
  117. doRedirect(homePath, next, isMultiDomain);
  118. NProgress.done();
  119. return;
  120. }
  121. }
  122. if (!allowedPaths.includes(to.path)) {
  123. console.warn(`[${site}] 禁止访问 ${to.path}`);
  124. const homePath = getDomainHomePath(site, station);
  125. doRedirect(homePath, next, isMultiDomain);
  126. NProgress.done();
  127. return;
  128. }
  129. } else {
  130. // 单域名下首页处理
  131. const homePaths = ['/', '/index', '/indexData', '/diy'];
  132. if (homePaths.includes(to.path)) {
  133. const homePath = getDomainHomePath(site, station);
  134. if (to.path !== homePath) {
  135. doRedirect(homePath, next, isMultiDomain);
  136. NProgress.done();
  137. return;
  138. }
  139. }
  140. }
  141. // 需求三 & 需求六:正常放行
  142. next();
  143. NProgress.done();
  144. });
  145. router.afterEach(() => {
  146. NProgress.done();
  147. });