|
@@ -6,11 +6,13 @@ import { getToken } from '@/utils/auth';
|
|
|
import { isPathMatch } from '@/utils/validate';
|
|
import { isPathMatch } from '@/utils/validate';
|
|
|
import { useUserStore } from '@/store/modules/user';
|
|
import { useUserStore } from '@/store/modules/user';
|
|
|
import { ElMessage } from 'element-plus/es';
|
|
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 { cartStore } from '@/store/modules/cart';
|
|
|
|
|
+import { stationStore } from '@/store/modules/station';
|
|
|
import Cookies from 'js-cookie';
|
|
import Cookies from 'js-cookie';
|
|
|
|
|
|
|
|
NProgress.configure({ showSpinner: false });
|
|
NProgress.configure({ showSpinner: false });
|
|
|
|
|
+
|
|
|
const whiteList = [
|
|
const whiteList = [
|
|
|
'/login',
|
|
'/login',
|
|
|
'/register',
|
|
'/register',
|
|
@@ -35,58 +37,128 @@ const isWhiteList = (path: string) => {
|
|
|
return whiteList.some((pattern) => isPathMatch(pattern, path));
|
|
return whiteList.some((pattern) => isPathMatch(pattern, path));
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-// 获取主站完整 URL(用于跨域跳转)
|
|
|
|
|
function getMainSiteUrl(path: string) {
|
|
function getMainSiteUrl(path: string) {
|
|
|
if (import.meta.env.PROD) {
|
|
if (import.meta.env.PROD) {
|
|
|
return `https://index.xiaoluwebsite.xyz${path}`;
|
|
return `https://index.xiaoluwebsite.xyz${path}`;
|
|
|
} else {
|
|
} else {
|
|
|
- // 本地开发:指向 index.xiaoluwebsite.xyz 加上当前运行的端口
|
|
|
|
|
- // 假设你启动 vite 后访问的是 http://index.xiaoluwebsite.xyz
|
|
|
|
|
const devPort = window.location.port || import.meta.env.VITE_APP_PORT;
|
|
const devPort = window.location.port || import.meta.env.VITE_APP_PORT;
|
|
|
return `https://index.xiaoluwebsite.xyz:${devPort}${path}`;
|
|
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) => {
|
|
router.beforeEach(async (to, from, next) => {
|
|
|
NProgress.start();
|
|
NProgress.start();
|
|
|
const site = getCurrentSite();
|
|
const site = getCurrentSite();
|
|
|
const allowedPaths = SITE_ROUTES[site];
|
|
const allowedPaths = SITE_ROUTES[site];
|
|
|
const token = Cookies.get('Authorization');
|
|
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 (err) {
|
|
|
- // if (false) {
|
|
|
|
|
- await useUserStore().logout();
|
|
|
|
|
|
|
+ await userStore.logout();
|
|
|
ElMessage.error(err);
|
|
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();
|
|
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();
|
|
NProgress.done();
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // 需求三 & 需求六:正常放行
|
|
|
|
|
+ next();
|
|
|
|
|
+ NProgress.done();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
router.afterEach(() => {
|
|
router.afterEach(() => {
|