Some checks failed
Build and Deploy Front / deploy (push) Failing after 15s
提问卡改为可透点阵的薄玻璃并重排;运营端摄像头扫码改到顶部;登录不再展示领域异常码。 Co-authored-by: Cursor <cursoragent@cursor.com>
306 lines
8.9 KiB
TypeScript
306 lines
8.9 KiB
TypeScript
import type { Router, LocationQueryRaw } from 'vue-router';
|
||
import NProgress from 'nprogress'; // progress bar
|
||
|
||
import { useUserStore } from '@/store';
|
||
import { useTabBarStore } from '@/store';
|
||
import { useRbacUserStore } from '@/store/modules/rbac-user';
|
||
import { OmsLoginType } from '@/api/rbac';
|
||
import { isLogin, setToken } from '@/utils/auth';
|
||
import {
|
||
ADMIN_H5_HOME_PATH,
|
||
ADMIN_H5_LOGIN_PATH,
|
||
CUSTOMER_H5_HOME_PATH,
|
||
getDefaultHomePathByType,
|
||
isAdminH5Path,
|
||
isAgentPortalPath,
|
||
isLoginRouteName,
|
||
resolveCustomerEntryPath,
|
||
resolvePostLogoutLogin,
|
||
shouldRedirectCustomerToH5,
|
||
} from '@/utils/login-portal';
|
||
import { preferH5Portal } from '@/utils/device';
|
||
import { applyRbacUserToUserStore } from '@/utils/enter-system';
|
||
import { shouldPortalIris } from '@/utils/page-transition-flag';
|
||
|
||
function buildLoginRedirect(to: {
|
||
name?: string | symbol | null;
|
||
path: string;
|
||
fullPath?: string;
|
||
query: LocationQueryRaw;
|
||
}) {
|
||
const rbacUserStore = useRbacUserStore();
|
||
const target = resolvePostLogoutLogin({
|
||
loginType: rbacUserStore.resolveLoginTypeForRedirect(),
|
||
path: to.fullPath || to.path,
|
||
});
|
||
if (target.path) {
|
||
return {
|
||
path: target.path,
|
||
query: {
|
||
...(target.query || {}),
|
||
} as LocationQueryRaw,
|
||
};
|
||
}
|
||
return {
|
||
name: target.name,
|
||
query: {
|
||
...(target.query || {}),
|
||
...to.query,
|
||
} as LocationQueryRaw,
|
||
};
|
||
}
|
||
|
||
export default function setupUserLoginInfoGuard(router: Router) {
|
||
let hasEnsuredUser = false;
|
||
|
||
router.beforeEach(async (to, from, next) => {
|
||
const tabBarStore = useTabBarStore();
|
||
// 已缓存的 Tab 来回切:跳过进度条,避免顶栏重绘造成顿挫
|
||
const toName = to.name ? String(to.name) : '';
|
||
const fromName = from.name ? String(from.name) : '';
|
||
const isCachedTabSwitch =
|
||
!!toName &&
|
||
!!fromName &&
|
||
toName !== fromName &&
|
||
!isLoginRouteName(toName) &&
|
||
!isLoginRouteName(fromName) &&
|
||
tabBarStore.getCacheList.includes(toName);
|
||
// 门户翻页:跳过顶栏横条,等待感交给翻页动画
|
||
if (
|
||
!shouldPortalIris(to, from) &&
|
||
!isCachedTabSwitch &&
|
||
!String(to.path || '').startsWith('/h5') &&
|
||
to.path !== '/'
|
||
) {
|
||
NProgress.start();
|
||
}
|
||
const userStore = useUserStore();
|
||
const rbacUserStore = useRbacUserStore();
|
||
|
||
if (isLoginRouteName(to.name as string)) {
|
||
NProgress.done();
|
||
hasEnsuredUser = false;
|
||
|
||
// 管理端登录页在手机/微信:直达管理端 H5 登录
|
||
if (to.name === 'adminLogin' && preferH5Portal()) {
|
||
const redirect =
|
||
typeof to.query.redirect === 'string' ? to.query.redirect : '';
|
||
if (isLogin()) {
|
||
next({
|
||
path:
|
||
rbacUserStore.loginPortalType === OmsLoginType.User
|
||
? getDefaultHomePathByType(OmsLoginType.User)
|
||
: ADMIN_H5_LOGIN_PATH,
|
||
replace: true,
|
||
});
|
||
return;
|
||
}
|
||
next({
|
||
path: ADMIN_H5_LOGIN_PATH,
|
||
query: redirect
|
||
? { redirect: isAdminH5Path(redirect) ? redirect : ADMIN_H5_HOME_PATH }
|
||
: {},
|
||
replace: true,
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 客户登录页在手机/微信:直达 H5
|
||
if (to.name === 'login' && preferH5Portal()) {
|
||
const fromOfficialSite = to.query.from === 'official-site';
|
||
const redirect =
|
||
typeof to.query.redirect === 'string' ? to.query.redirect : '';
|
||
const target = resolveCustomerEntryPath(redirect || CUSTOMER_H5_HOME_PATH);
|
||
if (isLogin() && fromOfficialSite) {
|
||
if (rbacUserStore.loginPortalType === OmsLoginType.Customer) {
|
||
next({
|
||
path: target,
|
||
replace: true,
|
||
});
|
||
return;
|
||
}
|
||
next({
|
||
path: '/h5/login',
|
||
replace: true,
|
||
});
|
||
return;
|
||
}
|
||
next({
|
||
path: isLogin() ? target : '/h5/login',
|
||
query: !isLogin() && redirect ? { redirect: target } : {},
|
||
replace: true,
|
||
});
|
||
return;
|
||
}
|
||
|
||
const ssoToken =
|
||
typeof to.query.accessToken === 'string' ? to.query.accessToken : '';
|
||
if (to.query.sso === '1' && ssoToken) {
|
||
try {
|
||
const refreshToken =
|
||
typeof to.query.refreshToken === 'string'
|
||
? to.query.refreshToken
|
||
: undefined;
|
||
const ok = await rbacUserStore.loginBySsoResult({
|
||
accessToken: ssoToken,
|
||
refreshToken: refreshToken || null,
|
||
});
|
||
if (ok && rbacUserStore.userInfo) {
|
||
applyRbacUserToUserStore(rbacUserStore.userInfo);
|
||
if (rbacUserStore.token) {
|
||
setToken(rbacUserStore.token, true);
|
||
}
|
||
next({
|
||
path: getDefaultHomePathByType(OmsLoginType.Customer),
|
||
query: {},
|
||
replace: true,
|
||
});
|
||
return;
|
||
}
|
||
} catch (error) {
|
||
console.error('客户门户 SSO 登录失败', error);
|
||
}
|
||
next({
|
||
path: preferH5Portal() ? '/h5/login' : '/login',
|
||
query: {},
|
||
replace: true,
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 官网「去发货」:客户已登录进工作台;管理端账号展示客户登录页,避免误进后台
|
||
const fromOfficialSite = to.query.from === 'official-site';
|
||
if (isLogin() && fromOfficialSite && to.name === 'login') {
|
||
if (rbacUserStore.loginPortalType === OmsLoginType.Customer) {
|
||
next({
|
||
path: resolveCustomerEntryPath(getDefaultHomePathByType(OmsLoginType.Customer)),
|
||
replace: true,
|
||
});
|
||
return;
|
||
}
|
||
next();
|
||
return;
|
||
}
|
||
|
||
if (isLogin()) {
|
||
next({
|
||
path: getDefaultHomePathByType(rbacUserStore.loginPortalType),
|
||
});
|
||
return;
|
||
}
|
||
next();
|
||
return;
|
||
}
|
||
|
||
// 已登录客户 + 手机/微信:访问 PC 页时切到 H5
|
||
if (
|
||
isLogin() &&
|
||
rbacUserStore.loginPortalType === OmsLoginType.Customer &&
|
||
shouldRedirectCustomerToH5(String(to.path || ''))
|
||
) {
|
||
NProgress.done();
|
||
next({
|
||
path: resolveCustomerEntryPath(to.path),
|
||
replace: true,
|
||
});
|
||
return;
|
||
}
|
||
|
||
if (to.meta?.requiresAuth === false) {
|
||
NProgress.done();
|
||
next();
|
||
return;
|
||
}
|
||
|
||
// H5:未登录 — 客户四 Tab 可逛;深链仍引导登录
|
||
if (!isLogin() && String(to.path || '').startsWith('/h5')) {
|
||
const path = String(to.path || '');
|
||
if (isAdminH5Path(path) && path !== ADMIN_H5_LOGIN_PATH) {
|
||
NProgress.done();
|
||
next({
|
||
path: ADMIN_H5_LOGIN_PATH,
|
||
query: { redirect: to.fullPath },
|
||
replace: true,
|
||
});
|
||
return;
|
||
}
|
||
if (!isAdminH5Path(path) && path !== '/h5/login') {
|
||
const guestOk =
|
||
to.meta?.h5GuestOk === true ||
|
||
path === '/h5' ||
|
||
path === '/h5/' ||
|
||
path === '/h5/price' ||
|
||
path === '/h5/orders' ||
|
||
path === '/h5/assistant' ||
|
||
path === '/h5/me';
|
||
if (guestOk) {
|
||
NProgress.done();
|
||
next();
|
||
return;
|
||
}
|
||
NProgress.done();
|
||
next({
|
||
path: '/h5/login',
|
||
query: { redirect: to.fullPath },
|
||
replace: true,
|
||
});
|
||
return;
|
||
}
|
||
}
|
||
|
||
if (isLogin()) {
|
||
// F5:先用瘦身 localStorage / session menus 灌 userStore,避免空等接口
|
||
if (
|
||
rbacUserStore.userInfo?.id &&
|
||
!(userStore.name || userStore.accountId)
|
||
) {
|
||
applyRbacUserToUserStore(rbacUserStore.userInfo);
|
||
}
|
||
|
||
// 代理门户不依赖运营菜单,有用户信息即可放行
|
||
if (
|
||
rbacUserStore.loginPortalType === OmsLoginType.Agent &&
|
||
isAgentPortalPath(String(to.path || ''))
|
||
) {
|
||
if (rbacUserStore.userInfo) {
|
||
applyRbacUserToUserStore(rbacUserStore.userInfo);
|
||
}
|
||
next();
|
||
return;
|
||
}
|
||
|
||
const needMenus = !(rbacUserStore.menus?.length > 0);
|
||
if (!needMenus && (userStore.name || userStore.accountId)) {
|
||
next();
|
||
return;
|
||
}
|
||
|
||
if (!hasEnsuredUser) {
|
||
hasEnsuredUser = true;
|
||
try {
|
||
if (needMenus) {
|
||
await rbacUserStore.ensureCurrentUser();
|
||
if (rbacUserStore.userInfo) {
|
||
applyRbacUserToUserStore(rbacUserStore.userInfo);
|
||
}
|
||
}
|
||
if (!rbacUserStore.userInfo?.id && !(userStore.name || userStore.accountId)) {
|
||
throw new Error('用户信息不存在');
|
||
}
|
||
next();
|
||
} catch (error) {
|
||
hasEnsuredUser = false;
|
||
console.error('获取用户信息失败', error);
|
||
await userStore.logout();
|
||
next(buildLoginRedirect(to));
|
||
}
|
||
} else {
|
||
next();
|
||
}
|
||
} else {
|
||
hasEnsuredUser = false;
|
||
next(buildLoginRedirect(to));
|
||
}
|
||
});
|
||
}
|