来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
60 lines
2.4 KiB
TypeScript
60 lines
2.4 KiB
TypeScript
import { ROLE_LABELS } from '../config/roleCatalog';
|
||
|
||
/** 角色定义最小字段(/api/roles 与 role_definitions 的公共形状) */
|
||
export interface RoleDefinitionLite {
|
||
role_code: string;
|
||
role_name: string;
|
||
}
|
||
|
||
type TranslateLike = (key: string) => string;
|
||
|
||
/**
|
||
* 角色标签的唯一口径。四处展示(账号菜单、Layout 顶栏、操作日志、角色权限页)
|
||
* 必须共用它,避免同一个角色在不同界面显示出不同结果。
|
||
*
|
||
* 取值顺序:
|
||
* 1. `layout.role.<code>` 命中时优先 —— 这是唯一的本地化来源(zh-CN/en/id),
|
||
* 内置 9 个角色的中文名已与 role_definitions、src/config/roleCatalog.ts 对齐;
|
||
* 2. 未命中时取库里的 role_name —— 自建角色没有翻译条目,走这里才能显示中文名
|
||
* (与用户管理、角色权限页一致);
|
||
* 3. 再退回内置角色表(接口异常时的兜底);
|
||
* 4. 最后回落角色代码 —— **绝不把 i18n key 原样显示给用户**。
|
||
* 此前的缺陷是账号菜单直接用 t(),自建角色把 `layout.role.custom_xxx` 打到了界面上。
|
||
*/
|
||
export function buildRoleLabelResolver(options: {
|
||
t?: TranslateLike | null;
|
||
definitions?: readonly RoleDefinitionLite[] | null;
|
||
}): (code: string | null | undefined) => string {
|
||
const translate = options.t || null;
|
||
const byCode = new Map<string, string>();
|
||
(options.definitions || []).forEach((row) => {
|
||
const code = String(row?.role_code || '').trim();
|
||
const name = String(row?.role_name || '').trim();
|
||
if (code && name) byCode.set(code, name);
|
||
});
|
||
|
||
return (code) => {
|
||
const key = String(code || '').trim();
|
||
if (!key) return '';
|
||
if (translate) {
|
||
const i18nKey = `layout.role.${key}`;
|
||
const translated = translate(i18nKey);
|
||
// translate() 在完全缺失时原样返回 key(见 src/i18n/i18n.test.ts)
|
||
if (translated && translated !== i18nKey) return translated;
|
||
}
|
||
const fromDefinitions = byCode.get(key);
|
||
if (fromDefinitions) return fromDefinitions;
|
||
const builtin = (ROLE_LABELS as Record<string, string>)[key];
|
||
if (builtin) return builtin;
|
||
return key;
|
||
};
|
||
}
|
||
|
||
/** 操作日志 operator_role 的存储形态:逗号/空白分隔的角色码串 */
|
||
export function splitRoleCodes(raw: string | null | undefined): string[] {
|
||
return String(raw || '')
|
||
.split(/[,,\s]+/)
|
||
.map((r) => r.trim())
|
||
.filter(Boolean);
|
||
}
|