Files
SupplierSystem/src/components/UserAccountMenu.tsx
T
雨天 a10eb2ff2a
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
feat(账号信息): 超级管理员不再展示接入编码
账号信息浮层「接入编码」改为只对分公司管理员(admin 且非 super_admin)显示本分公司编码;超管改在「分公司管理」页看(有接入编码列 + 复制 + 绑定关系)。同步设计文档 20261013 至 v2。
2026-10-13 11:11:23 +08:00

229 lines
9.7 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 React, { useEffect, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import { useNavigate } from 'react-router-dom';
import { ChevronDown, LogOut, Phone, Building2, Shield, User, Lock, X, KeyRound, Copy } from 'lucide-react';
import { useAuth } from '../hooks/useAuth';
import { supabase } from '../supabase/client';
import { notify } from '../utils/notify';
import { ChangePasswordForm } from './ChangePasswordForm';
import { useClickOutside } from '../hooks/useClickOutside';
import { useI18n } from '../i18n/I18nProvider';
import { useRoleLabels } from '../hooks/useRoleLabels';
function DetailRow({
icon: Icon,
label,
value,
}: {
icon: React.ElementType;
label: string;
value: string;
}) {
return (
<div className="flex items-start gap-3 py-2">
<Icon className="app-foreground-muted w-4 h-4 mt-0.5 shrink-0" />
<div className="min-w-0 flex-1">
<p className="app-foreground-muted text-xs">{label}</p>
<p className="text-sm text-slate-900 dark:text-slate-100 break-all">{value}</p>
</div>
</div>
);
}
export const UserAccountMenu: React.FC = () => {
const { t } = useI18n();
const navigate = useNavigate();
const { profile, logout } = useAuth();
const [open, setOpen] = useState(false);
const [showChangePassword, setShowChangePassword] = useState(false);
const [accessCodes, setAccessCodes] = useState<
Array<{ branch_code: string; branch_name: string; access_code: string }>
>([]);
const containerRef = useRef<HTMLDivElement>(null);
const displayName = profile?.full_name || profile?.username || t('layout.userFallback');
// 统一口径:本地化文案 → role_definitions.role_name → 内置角色表 → 角色代码。
// 自建角色没有对应翻译条目,此前直接把 i18n key 显示到了界面上。
const { roleLabel: resolveRoleLabel } = useRoleLabels();
const primaryRole = profile?.roles?.[0];
const roleLabel = primaryRole ? resolveRoleLabel(primaryRole) : t('layout.unknownRole');
const roleText = profile?.roles?.length
? profile.roles.map((role) => resolveRoleLabel(role)).join('、')
: roleLabel;
const isSuperAdmin = Boolean(profile?.roles?.includes('super_admin'));
const companyName =
profile?.branch_name || (isSuperAdmin ? t('layout.allPlatform') : null);
useClickOutside(containerRef, open, () => setOpen(false));
/**
* 「接入编码」:**分公司管理员**看本分公司的码;**超级管理员不展示**(2026-10-13 开发者要求)
* —— 超管要看全部编码请去「分公司管理」(那里有「接入编码」列 + 复制 + 绑定关系),
* 账号菜单里再列一遍既长又没必要。其它角色整行不展示。
*/
const canSeeAccessCode = !isSuperAdmin && Boolean(profile?.roles?.some((role) => role === 'admin'));
useEffect(() => {
if (!open || !canSeeAccessCode) return;
let alive = true;
void (async () => {
const { data } = await supabase.auth.admin.getAccessCodes();
if (alive && data?.access_codes) setAccessCodes(data.access_codes);
})();
return () => {
alive = false;
};
}, [open, canSeeAccessCode]);
const handleLogout = async () => {
setOpen(false);
await logout();
navigate('/login');
};
/** 复制接入编码(与分公司管理页同款实现与文案) */
const copyAccessCode = async (code: string) => {
try {
await navigator.clipboard.writeText(code);
notify.success(t('system.branches.toast.copied'));
} catch {
notify.warning(code);
}
};
return (
<div ref={containerRef} className="app-border relative pl-4 border-l">
<button
type="button"
onClick={() => setOpen((prev) => !prev)}
aria-expanded={open}
aria-label={t('layout.accountInfo')}
className="app-row-hover flex items-center gap-3 rounded-md transition-colors duration-150 py-1.5 pr-1 pl-1"
>
<div className="app-surface-subtle app-border w-9 h-9 rounded-full flex items-center justify-center border shrink-0">
<User className="w-4 h-4 text-slate-600 dark:text-slate-300" />
</div>
<div className="hidden md:block text-left">
<p className="text-sm font-medium text-slate-900 dark:text-slate-100 leading-tight">{displayName}</p>
<p className="app-foreground-muted text-xs leading-tight">
{companyName || roleLabel}
</p>
</div>
<ChevronDown
className={`w-4 h-4 text-slate-400 hidden md:block transition-transform duration-150 ${
open ? 'rotate-180' : ''
}`}
/>
</button>
{open && (
<div className="app-surface app-foreground absolute right-0 top-full mt-2 w-80 rounded-xl border shadow-lg z-50 overflow-hidden">
<div className="app-surface-subtle app-border px-4 py-4 border-b">
<div className="flex items-center gap-3">
<div className="w-12 h-12 bg-indigo-100 rounded-full flex items-center justify-center border border-indigo-200">
<User className="w-5 h-5 text-indigo-600" />
</div>
<div className="min-w-0">
<p className="text-base font-semibold text-slate-900 dark:text-slate-100 truncate">{displayName}</p>
<p className="app-foreground-muted text-sm truncate">@{profile?.username || '-'}</p>
</div>
</div>
</div>
<div className="app-border px-4 py-2 divide-y">
<DetailRow icon={Shield} label={t('layout.role')} value={roleText} />
<DetailRow
icon={Building2}
label={t('layout.company')}
value={companyName || t('layout.unassigned')}
/>
{profile?.phone && <DetailRow icon={Phone} label={t('layout.phone')} value={profile.phone} />}
<DetailRow
icon={User}
label={t('layout.accountStatus')}
value={profile?.status === 1 ? t('layout.statusActive') : t('layout.statusDisabled')}
/>
{canSeeAccessCode && accessCodes.length > 0 && (
<div className="flex items-start gap-3 py-2">
<KeyRound className="app-foreground-muted w-4 h-4 mt-0.5 shrink-0" />
<div className="min-w-0 flex-1">
<p className="app-foreground-muted text-xs">{t('layout.accessCode')}</p>
<div className="max-h-40 overflow-y-auto space-y-1">
{accessCodes.map((row) => (
<div key={row.branch_code} className="flex items-center gap-2">
<span className="min-w-0 flex-1 text-sm text-slate-900 dark:text-slate-100 break-all">
{accessCodes.length > 1 ? `${row.branch_name || row.branch_code}:` : ''}
<code className="text-xs px-1.5 py-0.5 rounded app-border bg-gray-50 dark:bg-gray-800">
{row.access_code}
</code>
</span>
<button
type="button"
onClick={() => void copyAccessCode(row.access_code)}
className="app-border app-row-hover inline-flex shrink-0 items-center gap-1 px-1.5 py-0.5 text-xs rounded border text-gray-700 dark:text-gray-300"
>
<Copy className="w-3 h-3" />
{t('system.branches.act.copy')}
</button>
</div>
))}
</div>
</div>
</div>
)}
</div>
<button
type="button"
onClick={() => {
setOpen(false);
setShowChangePassword(true);
}}
className="app-border app-row-hover w-full flex items-center gap-2 px-4 py-3 text-sm text-slate-700 dark:text-slate-300 border-t transition-colors"
>
<Lock className="w-4 h-4" />
{t('layout.changePassword')}
</button>
<button
type="button"
onClick={handleLogout}
className="app-border w-full flex items-center justify-center gap-2 px-4 py-3 text-sm text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-950/30 border-t transition-colors"
>
<LogOut className="w-4 h-4" />
{t('layout.logout')}
</button>
</div>
)}
{showChangePassword &&
createPortal(
<div className="fixed inset-0 z-[100] flex items-center justify-center bg-black/40 p-4">
<div className="app-surface app-foreground flex max-h-[min(90vh,720px)] w-full max-w-md flex-col overflow-hidden rounded-xl shadow-xl">
<div className="app-border flex shrink-0 items-center justify-between border-b px-5 py-4">
<h2 className="flex items-center gap-2 text-lg font-semibold text-slate-900 dark:text-slate-100">
<Lock className="h-5 w-5 text-emerald-600" />
{t('layout.changePassword')}
</h2>
<button
type="button"
onClick={() => setShowChangePassword(false)}
className="app-row-hover rounded-md p-1"
aria-label={t('common.close')}
>
<X className="h-5 w-5 text-slate-400" />
</button>
</div>
<div className="min-h-0 overflow-y-auto p-5">
<ChangePasswordForm
showCancel
onCancel={() => setShowChangePassword(false)}
/>
</div>
</div>
</div>,
document.body
)}
</div>
);
};