permission copy.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 } from '@/utils/siteConfig';
  10. import { cartStore } from '@/store/modules/cart';
  11. NProgress.configure({ showSpinner: false });
  12. const whiteList = [
  13. '/login',
  14. '/register',
  15. '/social-callback',
  16. '/register*',
  17. '/register/*',
  18. '/index',
  19. '/',
  20. '/indexB',
  21. '/indexMro',
  22. '/indexFuli',
  23. '/reg',
  24. '/search',
  25. '/item',
  26. '/breg',
  27. '/greg',
  28. '/diy'
  29. ];
  30. const isWhiteList = (path: string) => {
  31. return whiteList.some((pattern) => isPathMatch(pattern, path));
  32. };
  33. // 获取主站完整 URL(用于跨域跳转)
  34. function getMainSiteUrl(path: string) {
  35. if (import.meta.env.PROD) {
  36. return `https://www.xiaoluwebsite.xyz${path}`;
  37. } else {
  38. // 本地开发:指向 www.xiaoluwebsite.xyz 加上当前运行的端口
  39. // 假设你启动 vite 后访问的是 http://www.xiaoluwebsite.xyz
  40. const devPort = window.location.port || import.meta.env.VITE_APP_PORT;
  41. return `http://www.xiaoluwebsite.xyz:${devPort}${path}`;
  42. }
  43. }
  44. router.beforeEach(async (to, from, next) => {
  45. NProgress.start();
  46. const site = getCurrentSite();
  47. const allowedPaths = SITE_ROUTES[site];
  48. if (getToken()) {
  49. const [err] = await tos(useUserStore().getInfo());
  50. if (err) {
  51. await useUserStore().logout();
  52. ElMessage.error(err);
  53. if (import.meta.env.VITE_DOMAIN_NAME == 'true') {
  54. window.location.href = getMainSiteUrl('/login');
  55. } else {
  56. next('/login');
  57. }
  58. NProgress.done();
  59. } else {
  60. cartStore().onCartCount();
  61. // 是否开启多域名
  62. if (import.meta.env.VITE_DOMAIN_NAME == 'true') {
  63. if (!allowedPaths.includes(to.path)) {
  64. console.warn(`[${site}] 禁止访问 ${to.path}`);
  65. window.location.href = getMainSiteUrl('/index');
  66. NProgress.done();
  67. } else {
  68. next();
  69. NProgress.done();
  70. }
  71. } else {
  72. next();
  73. NProgress.done();
  74. }
  75. }
  76. } else {
  77. // 没有 token
  78. if (isWhiteList(to.path)) {
  79. next();
  80. NProgress.done();
  81. } else {
  82. // 非白名单且无 token,强制去登录
  83. if (import.meta.env.VITE_DOMAIN_NAME == 'true') {
  84. window.location.href = getMainSiteUrl('/login');
  85. } else {
  86. next('/login');
  87. }
  88. // 或者 '/login' 根据你的白名单设置
  89. NProgress.done();
  90. }
  91. }
  92. });
  93. router.afterEach(() => {
  94. NProgress.done();
  95. });