2 次代码提交 9704bece70 ... dbbfa70733

作者 SHA1 备注 提交日期
  牛奶 dbbfa70733 Merge branch 'master' of http://8.152.4.3:3000/yp_web/yoe-shop-web into master 1 月之前
  牛奶 da6f0e6207 冲突修改 1 月之前
共有 4 个文件被更改,包括 105 次插入134 次删除
  1. 0 102
      src/permission copy.ts
  2. 102 30
      src/permission.ts
  3. 1 1
      src/utils/siteConfig.ts
  4. 2 1
      vite.config.ts

+ 0 - 102
src/permission copy.ts

@@ -1,102 +0,0 @@
-import { to as tos } from 'await-to-js';
-import router from './router';
-import NProgress from 'nprogress';
-import 'nprogress/nprogress.css';
-import { getToken } from '@/utils/auth';
-import { isPathMatch } from '@/utils/validate';
-import { useUserStore } from '@/store/modules/user';
-import { ElMessage } from 'element-plus/es';
-import { getCurrentSite, SITE_ROUTES } from '@/utils/siteConfig';
-import { cartStore } from '@/store/modules/cart';
-
-NProgress.configure({ showSpinner: false });
-const whiteList = [
-  '/login',
-  '/register',
-  '/social-callback',
-  '/register*',
-  '/register/*',
-  '/index',
-  '/',
-  '/indexB',
-  '/indexMro',
-  '/indexFuli',
-  '/reg',
-  '/search',
-  '/item',
-  '/breg',
-  '/greg',
-  '/diy'
-];
-
-
-const isWhiteList = (path: string) => {
-  return whiteList.some((pattern) => isPathMatch(pattern, path));
-};
-
-// 获取主站完整 URL(用于跨域跳转)
-function getMainSiteUrl(path: string) {
-  if (import.meta.env.PROD) {
-    return `https://index.xiaoluwebsite.xyz${path}`;
-  } else {
-    // 本地开发:指向 index.xiaoluwebsite.xyz 加上当前运行的端口
-    // 假设你启动 vite 后访问的是 http://index.xiaoluwebsite.xyz
-    const devPort = window.location.port || import.meta.env.VITE_APP_PORT;
-    return `http://index.xiaoluwebsite.xyz:${devPort}${path}`;
-  }
-}
-
-router.beforeEach(async (to, from, next) => {
-  NProgress.start();
-  const site = getCurrentSite();
-  const allowedPaths = SITE_ROUTES[site];
-
-  if (getToken()) {
-    const [err] = await tos(useUserStore().getInfo());
-    if (err) {
-      await useUserStore().logout();
-      ElMessage.error(err);
-      if (import.meta.env.VITE_DOMAIN_NAME == 'true') {
-        window.location.href = getMainSiteUrl('/login');
-      } else {
-        next('/login');
-      }
-      NProgress.done();
-    } else {
-      cartStore().onCartCount();
-      // 是否开启多域名
-      if (import.meta.env.VITE_DOMAIN_NAME == 'true') {
-        if (!allowedPaths.includes(to.path)) {
-          console.warn(`[${site}] 禁止访问 ${to.path}`);
-          window.location.href = getMainSiteUrl('/index');
-          NProgress.done();
-        } else {
-          next();
-          NProgress.done();
-        }
-      } else {
-        next();
-        NProgress.done();
-      }
-    }
-  } else {
-    // 没有 token
-    if (isWhiteList(to.path)) {
-      next();
-      NProgress.done();
-    } else {
-      // 非白名单且无 token,强制去登录
-      if (import.meta.env.VITE_DOMAIN_NAME == 'true') {
-        window.location.href = getMainSiteUrl('/login');
-      } else {
-        next('/login');
-      }
-      // 或者 '/login' 根据你的白名单设置
-      NProgress.done();
-    }
-  }
-});
-
-router.afterEach(() => {
-  NProgress.done();
-});

+ 102 - 30
src/permission.ts

@@ -6,11 +6,13 @@ import { getToken } from '@/utils/auth';
 import { isPathMatch } from '@/utils/validate';
 import { useUserStore } from '@/store/modules/user';
 import { ElMessage } from 'element-plus/es';
-import { getCurrentSite, SITE_ROUTES } from '@/utils/siteConfig';
+import { getCurrentSite, SITE_ROUTES, onPath } from '@/utils/siteConfig';
 import { cartStore } from '@/store/modules/cart';
+import { stationStore } from '@/store/modules/station';
 import Cookies from 'js-cookie';
 
 NProgress.configure({ showSpinner: false });
+
 const whiteList = [
   '/login',
   '/register',
@@ -35,58 +37,128 @@ const isWhiteList = (path: string) => {
   return whiteList.some((pattern) => isPathMatch(pattern, path));
 };
 
-// 获取主站完整 URL(用于跨域跳转)
 function getMainSiteUrl(path: string) {
   if (import.meta.env.PROD) {
     return `https://index.xiaoluwebsite.xyz${path}`;
   } else {
-    // 本地开发:指向 index.xiaoluwebsite.xyz 加上当前运行的端口
-    // 假设你启动 vite 后访问的是 http://index.xiaoluwebsite.xyz
     const devPort = window.location.port || import.meta.env.VITE_APP_PORT;
     return `https://index.xiaoluwebsite.xyz:${devPort}${path}`;
   }
 }
 
+// 根据 isDiy 字段决定首页路径
+function getDomainHomePath(site: string, station: ReturnType<typeof stationStore>) {
+  const allowedPaths = SITE_ROUTES[site] || [];
+  const hasIndexData = allowedPaths.includes('/indexData');
+  const hasDiy = allowedPaths.includes('/diy');
+  const hasIndex = allowedPaths.includes('/index');
+
+  if (station.stationData && Object.keys(station.stationData).length > 0) {
+    // 兼容:stationData.data.isDiy 或 stationData.isDiy
+    const data = station.stationData.data || station.stationData;
+    const isDiy = data?.isDiy;
+
+    if (isDiy === 0 || isDiy === '0') {
+      if (hasIndexData) {
+        return '/indexData';
+      }
+    } else if (isDiy !== undefined && isDiy !== null) {
+      if (hasDiy) {
+        return '/diy';
+      }
+    }
+  }
+
+  if (hasIndex) {
+    return '/index';
+  }
+
+  return allowedPaths[0] || '/';
+}
+
+// 统一跳转处理
+function doRedirect(path: string, next: any, isMultiDomain: boolean) {
+  if (isMultiDomain) {
+    onPath(path);
+    next(false);
+  } else {
+    next(path);
+  }
+}
+
 router.beforeEach(async (to, from, next) => {
   NProgress.start();
   const site = getCurrentSite();
   const allowedPaths = SITE_ROUTES[site];
   const token = Cookies.get('Authorization');
+  const isMultiDomain = import.meta.env.VITE_DOMAIN_NAME == 'true';
+  const station = stationStore();
+  const cart = cartStore();
+  const userStore = useUserStore();
 
-  //不需要请求登录信息
-  if (isWhiteList(to.path) && !token) {
-    next();
-    NProgress.done();
-  } else {
-    const [err] = await tos(useUserStore().getInfo());
+  // 需求一:判断是否需要调用 getInfo(有 token 或 不在白名单)
+  const needGetInfo = token || !isWhiteList(to.path);
+
+  if (needGetInfo) {
+    const [err] = await tos(userStore.getInfo());
+
+    // 需求五:getInfo 失败跳转/login
     if (err) {
-      // if (false) {
-      await useUserStore().logout();
+      await userStore.logout();
       ElMessage.error(err);
-      if (import.meta.env.VITE_DOMAIN_NAME == 'true') {
-        window.location.href = getMainSiteUrl('/login');
-      } else {
-        next('/login');
+      doRedirect('/login', next, isMultiDomain);
+      NProgress.done();
+      return;
+    }
+
+    // 修复:有 token 时才请求购物车和站点数据
+    if (token) {
+      try {
+        // 获取购物车数量
+        cart.onCartCount();
+        // 获取站点数据
+        await station.onstation();
+      } catch (error) {
+        console.error('获取站点数据失败:', error);
+      }
+    }
+  }
+
+  // 需求四:多域名下检查路径权限
+  if (isMultiDomain) {
+    const homePaths = ['/', '/index', '/indexData', '/diy'];
+    if (homePaths.includes(to.path)) {
+      const homePath = getDomainHomePath(site, station);
+      if (to.path !== homePath) {
+        doRedirect(homePath, next, isMultiDomain);
+        NProgress.done();
+        return;
       }
+    }
+
+    if (!allowedPaths.includes(to.path)) {
+      console.warn(`[${site}] 禁止访问 ${to.path}`);
+      const homePath = getDomainHomePath(site, station);
+      doRedirect(homePath, next, isMultiDomain);
       NProgress.done();
-    } else {
-      cartStore().onCartCount();
-      // 是否开启多域名
-      if (import.meta.env.VITE_DOMAIN_NAME == 'true') {
-        if (!allowedPaths.includes(to.path)) {
-          console.warn(`[${site}] 禁止访问 ${to.path}`);
-          window.location.href = getMainSiteUrl('/index');
-          NProgress.done();
-        } else {
-          next();
-          NProgress.done();
-        }
-      } else {
-        next();
+      return;
+    }
+  } else {
+    // 单域名下首页处理
+    const homePaths = ['/', '/index', '/indexData', '/diy'];
+    if (homePaths.includes(to.path)) {
+      const homePath = getDomainHomePath(site, station);
+      if (to.path !== homePath) {
+        doRedirect(homePath, next, isMultiDomain);
         NProgress.done();
+        return;
       }
     }
   }
+
+  // 需求三 & 需求六:正常放行
+  next();
+  NProgress.done();
 });
 
 router.afterEach(() => {

+ 1 - 1
src/utils/siteConfig.ts

@@ -1,6 +1,6 @@
 // 每个站点允许的路由 (保持不变)
 export const SITE_ROUTES: Record<any, string[]> = {
-  www: ['/', '/index', '/indexData', '/theme'], //优易365主站
+  www: ['/', '/index', '/indexData', '/theme', '/diy'], //优易365主站
   b: ['/indexB'], //企业购商城
   mro: ['/indexMro'], //工业品商城
   fuli: ['/indexFuli'], //福礼商城

+ 2 - 1
vite.config.ts

@@ -47,9 +47,10 @@ export default defineConfig(({ mode, command }) => {
         [env.VITE_APP_BASE_API]: {
           // target: 'http://localhost:8080',
           // target: 'http://yp1.yingpaipay.com:9026',
-          target: 'https://one.xiaoluwebsite.xyz',
+          target: 'https://jingyang.xiaoluwebsite.xyz',
           // target: 'https://one.yoe365.com',
           // target: 'http://192.168.1.52:8080',
+          // target: 'https://apiyouyida.xiaoluwebsite.xyz',//线上
           changeOrigin: true,
           ws: true,
           rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')