permission.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. '/theme',
  31. '/indexDiy',
  32. '/indexMroDiy',
  33. '/indexFuliDiy',
  34. '/indexDataDiy'
  35. ];
  36. const isWhiteList = (path: string) => {
  37. return whiteList.some((pattern) => isPathMatch(pattern, path));
  38. };
  39. // 根据配置决定首页路径
  40. function getDomainHomePath(site: string, station: ReturnType<typeof stationStore>, toPath: string) {
  41. const allowedPaths = SITE_ROUTES[site] || [];
  42. // 原有的 /indexData 和 /indexDataDiy 逻辑保持不变
  43. const hasIndexData = allowedPaths.includes('/indexData');
  44. const hasIndexDataDiy = allowedPaths.includes('/indexDataDiy');
  45. if (station.stationData && Object.keys(station.stationData).length > 0) {
  46. const data = station.stationData.data || station.stationData;
  47. const isDiy = data?.isDiy;
  48. if (isDiy === 0 || isDiy === '0') {
  49. if (hasIndexData) {
  50. return '/indexData';
  51. }
  52. } else if (isDiy !== undefined && isDiy !== null) {
  53. if (hasIndexDataDiy) {
  54. return '/indexDataDiy';
  55. }
  56. }
  57. }
  58. // 新增:当 userSonType 为 '3' 时,优先跳转到 /indexEnterprise(如果允许)
  59. const userStore = useUserStore();
  60. if (userStore.userInfo?.user?.userSonType == '3' && allowedPaths.includes('/indexEnterprise')) {
  61. console.log('????????????????????????????????????')
  62. return '/indexEnterprise';
  63. }
  64. // 新增:根据 diyConfig 配置判断 /index、/indexMro、/indexFuli
  65. const diyConfig = station.diyState || [];
  66. // 处理 / 或 /index
  67. if (toPath === '/' || toPath === '/index') {
  68. const platformDiy = diyConfig.find((item: any) => item.configKey === 'platormDiy');
  69. if (platformDiy?.value === '1' && allowedPaths.includes('/indexDiy')) {
  70. return '/indexDiy';
  71. }
  72. if (allowedPaths.includes('/index')) {
  73. return '/index';
  74. }
  75. }
  76. // 处理 /indexMro
  77. if (toPath === '/indexMro') {
  78. const industrialDiy = diyConfig.find((item: any) => item.configKey === 'industrialDiy');
  79. if (industrialDiy?.value === '1' && allowedPaths.includes('/indexMroDiy')) {
  80. return '/indexMroDiy';
  81. }
  82. return '/indexMro';
  83. }
  84. // 处理 /indexFuli
  85. if (toPath === '/indexFuli') {
  86. const fuliDiy = diyConfig.find((item: any) => item.configKey === 'fuliDiy');
  87. if (fuliDiy?.value === '1' && allowedPaths.includes('/indexFuliDiy')) {
  88. return '/indexFuliDiy';
  89. }
  90. return '/indexFuli';
  91. }
  92. return allowedPaths[0] || '/';
  93. }
  94. // 统一跳转处理
  95. function doRedirect(path: string, next: any, isMultiDomain: boolean) {
  96. if (isMultiDomain) {
  97. onPath(path);
  98. next(false);
  99. } else {
  100. next(path);
  101. }
  102. }
  103. router.beforeEach(async (to, from, next) => {
  104. NProgress.start();
  105. const site = getCurrentSite();
  106. const allowedPaths = SITE_ROUTES[site];
  107. const token = Cookies.get('Authorization');
  108. const isMultiDomain = import.meta.env.VITE_DOMAIN_NAME == 'true';
  109. const station = stationStore();
  110. const cart = cartStore();
  111. const userStore = useUserStore();
  112. // 需求一:判断是否需要调用 getInfo(有 token 或 不在白名单)
  113. // const needGetInfo = token || !isWhiteList(to.path);
  114. // console.log(!isWhiteList(to.path), '8888888888888888')
  115. if (token) {
  116. const [err] = await tos(userStore.getInfo());
  117. // 需求五:getInfo 失败跳转/login
  118. if (err) {
  119. await useUserStore().logout();
  120. ElMessage.error(err);
  121. doRedirect('/login', next, isMultiDomain);
  122. NProgress.done();
  123. return;
  124. }
  125. // 有 token 时请求购物车和站点数据
  126. if (token) {
  127. try {
  128. cart.onCartCount();
  129. await station.onstation();
  130. } catch (error) {
  131. console.error('获取站点数据失败:', error);
  132. }
  133. }
  134. }
  135. // 无论是否有 token,都获取 DIY 配置数据(新接口无需登录)
  136. try {
  137. await station.onDiyState();
  138. } catch (error) {
  139. console.error('获取 DIY 配置失败:', error);
  140. }
  141. // 需求四:多域名下检查路径权限
  142. if (isMultiDomain) {
  143. const homePaths = ['/', '/index', '/indexData', '/indexDataDiy', '/indexMro', '/indexFuli'];
  144. if (homePaths.includes(to.path)) {
  145. const homePath = getDomainHomePath(site, station, to.path);
  146. if (to.path !== homePath) {
  147. doRedirect(homePath, next, isMultiDomain);
  148. NProgress.done();
  149. return;
  150. }
  151. }
  152. if (!allowedPaths.includes(to.path)) {
  153. console.warn(`[${site}] 禁止访问 ${to.path}`);
  154. const homePath = getDomainHomePath(site, station, to.path);
  155. doRedirect(homePath, next, isMultiDomain);
  156. NProgress.done();
  157. return;
  158. }
  159. } else {
  160. // 单域名下首页处理
  161. const homePaths = ['/', '/index', '/indexData', '/indexDataDiy', '/indexMro', '/indexFuli'];
  162. if (homePaths.includes(to.path)) {
  163. const homePath = getDomainHomePath(site, station, to.path);
  164. if (to.path !== homePath) {
  165. doRedirect(homePath, next, isMultiDomain);
  166. NProgress.done();
  167. return;
  168. }
  169. }
  170. }
  171. // 需求三 & 需求六:正常放行
  172. next();
  173. NProgress.done();
  174. });
  175. router.afterEach(() => {
  176. NProgress.done();
  177. });