front/src/utils/login-portal.ts
zzh 02fb9c4f28
All checks were successful
Build and Deploy Front / deploy (push) Successful in 6m37s
feat: 切换 bcdship 域名并收敛 deployTargets,完善微信支付充值与 AI 助手顶栏
将站点/API 默认值收敛到配置,避免再次全局改域名;同步 H5/PC 支付充值与助手浮层交互调整。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-12-01 18:31:30 +08:00

301 lines
9.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { OmsLoginType } from '@/api/rbac'
import { preferH5Portal } from '@/utils/device'
/** 管理端(内部用户)登录路由 */
export const ADMIN_LOGIN_ROUTE_NAME = 'adminLogin'
/** 客户端(客户)登录路径 */
export const CUSTOMER_LOGIN_PATH = '/login'
/** 管理端(内部用户)登录路径 */
export const ADMIN_LOGIN_PATH = '/admin'
/** 渠道代理登录路径 / 路由名 */
export const AGENT_LOGIN_PATH = '/agent'
export const AGENT_LOGIN_ROUTE_NAME = 'agentLogin'
export const AGENT_HOME_PATH = '/agent/workplace'
/** 客户端(客户)登录路由 */
export const CUSTOMER_LOGIN_ROUTE_NAME = 'login'
/** 客户 H5 默认落地 */
export const CUSTOMER_H5_HOME_PATH = '/h5'
/** 管理端 H5 默认落地 */
export const ADMIN_H5_HOME_PATH = '/h5/admin'
/** 管理端 H5 登录 */
export const ADMIN_H5_LOGIN_PATH = '/h5/admin/login'
/** 客户 PC / 管理端 PC 默认落地 */
export const CUSTOMER_PC_HOME_PATH = '/home/workplace'
/** 管理端 PC 默认落地 */
export const ADMIN_PC_HOME_PATH = '/home/workplace'
export function isLoginRouteName(name?: string | null): boolean {
return (
name === ADMIN_LOGIN_ROUTE_NAME
|| name === CUSTOMER_LOGIN_ROUTE_NAME
|| name === AGENT_LOGIN_ROUTE_NAME
)
}
export function getLoginTypeFromRouteName(name?: string | null): OmsLoginType {
if (name === CUSTOMER_LOGIN_ROUTE_NAME) return OmsLoginType.Customer
if (name === AGENT_LOGIN_ROUTE_NAME) return OmsLoginType.Agent
return OmsLoginType.User
}
export function getLoginRouteNameByType(type: OmsLoginType): string {
if (type === OmsLoginType.Customer) return CUSTOMER_LOGIN_ROUTE_NAME
if (type === OmsLoginType.Agent) return AGENT_LOGIN_ROUTE_NAME
return ADMIN_LOGIN_ROUTE_NAME
}
export function getDefaultHomePathByType(type: OmsLoginType): string {
if (type === OmsLoginType.Customer) {
return preferH5Portal() ? CUSTOMER_H5_HOME_PATH : CUSTOMER_PC_HOME_PATH
}
if (type === OmsLoginType.Agent) {
return AGENT_HOME_PATH
}
return preferH5Portal() ? ADMIN_H5_HOME_PATH : ADMIN_PC_HOME_PATH
}
/** 是否管理端 H5 路径 */
export function isAdminH5Path(path: string): boolean {
const p = path.trim() || '/'
return p === ADMIN_H5_HOME_PATH || p.startsWith(`${ADMIN_H5_HOME_PATH}/`)
}
/** H5 专属路径:不做 PC→H5 回跳 */
export function isH5ExemptPath(path: string): boolean {
const p = path.trim() || '/'
return (
p === '/' ||
p.startsWith('/h5') ||
p.startsWith('/wechat/') ||
p.startsWith('/track/') ||
p.startsWith('/agent') ||
p.startsWith('/redirect')
)
}
/**
* 将客户 PC 深链映射到 H5;非手机端原样返回。
* 无对应 H5 页时落到 `/h5`。
*/
export function resolveCustomerEntryPath(path?: string | null): string {
const raw = (path || '').trim()
if (!preferH5Portal()) {
return raw || CUSTOMER_PC_HOME_PATH
}
if (!raw || raw === '/' || raw === CUSTOMER_PC_HOME_PATH) {
return CUSTOMER_H5_HOME_PATH
}
if (raw.startsWith('/h5')) return raw
if (raw.startsWith('/wechat/') || raw.startsWith('/track/')) return raw
if (raw.startsWith('/order/myOrders') || raw.startsWith('/order/placeOrder')) {
return '/h5/orders'
}
if (raw.startsWith('/order/priceCheck')) return '/h5/price'
if (raw === '/assistant' || raw.startsWith('/assistant/')) return '/h5/assistant'
if (raw.startsWith('/user/invoice')) return '/h5/invoice'
if (
raw.startsWith('/user/profile') ||
raw.startsWith('/user/recharge') ||
raw.startsWith('/user/marketing') ||
raw.startsWith('/user/sub-account') ||
raw.startsWith('/finance/')
) {
return '/h5/me'
}
// 其它客户 PC 菜单在手机上统一进 H5 壳
return CUSTOMER_H5_HOME_PATH
}
/** 手机端客户访问 PC 页时是否应切到 H5 */
export function shouldRedirectCustomerToH5(path: string): boolean {
if (!preferH5Portal()) return false
if (isH5ExemptPath(path)) return false
if (path === ADMIN_LOGIN_PATH || path.startsWith(`${ADMIN_LOGIN_PATH}/`)) return false
return true
}
/** 仅客户门户可访问的路由路径前缀(/h5/admin 除外,见 isCustomerPortalOnlyPath) */
const CUSTOMER_PORTAL_ONLY_PATH_PREFIXES = [
'/user/profile',
'/user/marketing',
'/user/recharge',
'/user/invoice',
'/user/accountFlow',
'/h5',
] as const
/** 仅管理端可访问的路由路径前缀 */
const ADMIN_PORTAL_ONLY_PATH_PREFIXES = ['/order/waybillManage', '/h5/admin'] as const
export function isCustomerPortalOnlyPath(path: string): boolean {
const normalized = path.trim() || '/'
// 管理端 H5 与客户 /h5 同前缀,需显式排除
if (isAdminH5Path(normalized)) return false
return CUSTOMER_PORTAL_ONLY_PATH_PREFIXES.some(
(prefix) => normalized === prefix || normalized.startsWith(`${prefix}/`),
)
}
export function isAdminPortalOnlyPath(path: string): boolean {
return ADMIN_PORTAL_ONLY_PATH_PREFIXES.some((prefix) => path.startsWith(prefix))
}
/** 当前登录主体是否可访问该路由(客户专属 / 管理端专属互斥) */
export function canAccessCustomerPortalRoute(
loginType: OmsLoginType,
options?: { path?: string; customerPortalOnly?: boolean; adminPortalOnly?: boolean }
): boolean {
const isCustomer = loginType === OmsLoginType.Customer
const path = options?.path || ''
if (isCustomer) {
if (options?.adminPortalOnly) return false
if (path && isAdminPortalOnlyPath(path)) return false
return true
}
// 管理端:拦截客户专属页
if (options?.customerPortalOnly) return false
if (path && isCustomerPortalOnlyPath(path)) return false
return true
}
/** 仅客户会访问的路径(管理端几乎不会落在这些页) */
function isCustomerExclusivePath(path: string): boolean {
const p = path.trim() || '/'
if (isAdminH5Path(p)) return false
return (
isCustomerPortalOnlyPath(p) ||
p.startsWith('/h5') ||
p.startsWith('/wechat/') ||
p.startsWith('/track/') ||
p.startsWith('/finance/') ||
p.startsWith('/assistant') ||
p.startsWith('/marketing/portal') ||
p.startsWith('/order/myOrders') ||
p.startsWith('/order/placeOrder') ||
p.startsWith('/order/priceCheck') ||
p.startsWith('/order/guide')
)
}
/** 是否渠道代理门户路径(不含 /agentAdmin 运营端) */
export function isAgentPortalPath(path: string): boolean {
const p = (path || '').trim() || '/'
return p === AGENT_LOGIN_PATH || p.startsWith(`${AGENT_LOGIN_PATH}/`)
}
/** 未登录时按目标路径选择应跳转的登录页(无主体类型时的兜底) */
export function resolveLoginRouteNameFromPath(path: string): string {
const p = (path || '').trim() || '/'
if (p === CUSTOMER_LOGIN_PATH || p === '/' || p.startsWith(`${CUSTOMER_LOGIN_PATH}/`)) {
return CUSTOMER_LOGIN_ROUTE_NAME
}
if (isAgentPortalPath(p)) {
return AGENT_LOGIN_ROUTE_NAME
}
if (p === ADMIN_LOGIN_PATH || p.startsWith(`${ADMIN_LOGIN_PATH}/`)) {
return ADMIN_LOGIN_ROUTE_NAME
}
if (isAdminPortalOnlyPath(p)) {
return ADMIN_LOGIN_ROUTE_NAME
}
// 仅客户专属路径回客户登录;/home 等共享工作台默认管理端,避免管理员过期落到客户入口
if (isCustomerExclusivePath(p)) {
return CUSTOMER_LOGIN_ROUTE_NAME
}
return ADMIN_LOGIN_ROUTE_NAME
}
/**
* 登出/过期后应回到的登录入口。
* 锁死:User → 管理端登录,Customer → 客户端登录,绝不互串。
*/
export function resolvePostLogoutLogin(options?: {
loginType?: OmsLoginType | null
path?: string | null
}): { name: string; path?: string; query?: Record<string, string> } {
const path = String(options?.path || '').trim() || '/'
const type = options?.loginType
let routeName: string
if (type === OmsLoginType.Customer) {
routeName = CUSTOMER_LOGIN_ROUTE_NAME
} else if (type === OmsLoginType.Agent) {
routeName = AGENT_LOGIN_ROUTE_NAME
} else if (type === OmsLoginType.User) {
routeName = ADMIN_LOGIN_ROUTE_NAME
} else {
routeName = resolveLoginRouteNameFromPath(path)
}
// 手机/微信客户:回 H5 登录页,不进 PC 登录大页
if (routeName === CUSTOMER_LOGIN_ROUTE_NAME && preferH5Portal()) {
return {
name: CUSTOMER_LOGIN_ROUTE_NAME,
path: '/h5/login',
query: path.startsWith('/h5') && !isAdminH5Path(path)
? { redirect: path }
: { redirect: resolveCustomerEntryPath(path) },
}
}
if (routeName === ADMIN_LOGIN_ROUTE_NAME) {
const adminRedirect =
path &&
path !== '/' &&
path !== CUSTOMER_LOGIN_PATH &&
path !== ADMIN_LOGIN_PATH &&
path !== ADMIN_H5_LOGIN_PATH &&
!isCustomerExclusivePath(path)
? { redirect: isAdminH5Path(path) ? path : path }
: undefined
// 手机/微信员工:回管理端 H5 登录
if (preferH5Portal()) {
return {
name: ADMIN_LOGIN_ROUTE_NAME,
path: ADMIN_H5_LOGIN_PATH,
query: adminRedirect,
}
}
return {
name: ADMIN_LOGIN_ROUTE_NAME,
query: adminRedirect,
}
}
if (routeName === AGENT_LOGIN_ROUTE_NAME) {
return {
name: AGENT_LOGIN_ROUTE_NAME,
query:
path &&
path !== '/' &&
path !== AGENT_LOGIN_PATH &&
isAgentPortalPath(path)
? { redirect: path }
: undefined,
}
}
return {
name: routeName,
query:
path && path !== '/' && path !== CUSTOMER_LOGIN_PATH
? { redirect: path }
: undefined,
}
}