61 lines
2.4 KiB
TypeScript
61 lines
2.4 KiB
TypeScript
import type { PartnerMe, PartnerPermissionKey } from '@dukang/shared-types';
|
|
|
|
export type PartnerNavKind = 'primary' | 'store_staff' | 'warehouse_staff';
|
|
|
|
export function isPrimaryAccount(account: PartnerMe | null | undefined): boolean {
|
|
return account?.isPrimary !== false;
|
|
}
|
|
|
|
export function isSubAccount(account: PartnerMe | null | undefined): boolean {
|
|
return !!account && account.isPrimary === false;
|
|
}
|
|
|
|
export function hasPartnerPermission(
|
|
account: PartnerMe | null | undefined,
|
|
permission: PartnerPermissionKey,
|
|
): boolean {
|
|
if (!account) return false;
|
|
if (isPrimaryAccount(account)) return true;
|
|
return account.permissions?.includes(permission) ?? false;
|
|
}
|
|
|
|
/** 仓库管理员:warehouse:manage,或仅有 order:view(无门店权限) */
|
|
export function isWarehouseStaff(account: PartnerMe | null | undefined): boolean {
|
|
if (!account || isPrimaryAccount(account)) return false;
|
|
if (hasPartnerPermission(account, 'warehouse:manage')) return true;
|
|
const hasStore =
|
|
hasPartnerPermission(account, 'store:create') || hasPartnerPermission(account, 'store:manage');
|
|
if (hasPartnerPermission(account, 'order:view') && !hasStore) return true;
|
|
return false;
|
|
}
|
|
|
|
export function getPartnerNavKind(account: PartnerMe | null | undefined): PartnerNavKind {
|
|
if (!account || isPrimaryAccount(account)) return 'primary';
|
|
if (isWarehouseStaff(account)) return 'warehouse_staff';
|
|
return 'store_staff';
|
|
}
|
|
|
|
/** 是否已配置管仓:主账号看 hasWarehouseAccess;子账号还需有仓库相关权限 */
|
|
export function hasWarehouseAccess(account: PartnerMe | null | undefined): boolean {
|
|
if (!account) return false;
|
|
if (account.hasWarehouseAccess === false) return false;
|
|
if (account.hasWarehouseAccess === true) return true;
|
|
// 旧缓存无字段时保守:主账号未知视为无,避免误展示全城订单
|
|
return false;
|
|
}
|
|
|
|
export function partnerHomePath(account: PartnerMe | null | undefined): string {
|
|
return '/';
|
|
}
|
|
|
|
const STORE_STAFF_PREFIXES = ['/', '/stores', '/me', '/login'];
|
|
const WAREHOUSE_STAFF_PREFIXES = ['/', '/orders', '/me', '/login'];
|
|
|
|
export function isSubAccountPath(pathname: string, navKind: PartnerNavKind = 'store_staff'): boolean {
|
|
if (pathname === '/login') return true;
|
|
const prefixes = navKind === 'warehouse_staff' ? WAREHOUSE_STAFF_PREFIXES : STORE_STAFF_PREFIXES;
|
|
return prefixes.some(
|
|
(prefix) => prefix !== '/login' && (pathname === prefix || pathname.startsWith(`${prefix}/`)),
|
|
);
|
|
}
|