refactor(frontend): 重构前端第二版
This commit is contained in:
1 parent
2631f78b00
commit
4b562cfa82
205 files changed
+15399
-12015
No files matched your search
@@ -0,0 +1,335 @@
|
||||
/**
|
||||
* shared/utils/wecom.ts
|
||||
* 企业微信登录/绑定相关工具与文案格式化。
|
||||
*/
|
||||
export type WecomMatchField = 'employee_id' | 'mobile' | 'email';
|
||||
|
||||
export interface WecomNormalizedMatchRules {
|
||||
operator: 'AND' | 'OR';
|
||||
fields: WecomMatchField[];
|
||||
}
|
||||
|
||||
export interface WecomLoginErrorPayload {
|
||||
error?: string | null;
|
||||
error_code?: string | null;
|
||||
reason?: string | null;
|
||||
missing_field?: string | null;
|
||||
candidate_count?: number | null;
|
||||
match_rules?: unknown;
|
||||
}
|
||||
|
||||
export interface WecomLoginDialogContent {
|
||||
title: string;
|
||||
summary: string;
|
||||
details: string[];
|
||||
technical?: string;
|
||||
}
|
||||
|
||||
export interface WecomAuditRowLike {
|
||||
status?: string | null;
|
||||
reason?: string | null;
|
||||
error_code?: string | null;
|
||||
match_rules?: string | null;
|
||||
match_operator?: string | null;
|
||||
match_result?: string | null;
|
||||
}
|
||||
|
||||
type TranslateFn = (key: string, params?: Record<string, unknown>) => string;
|
||||
|
||||
const KNOWN_MATCH_FIELDS: WecomMatchField[] = ['employee_id', 'mobile', 'email'];
|
||||
|
||||
function cleanText(value: unknown): string {
|
||||
return typeof value === 'string' ? value.trim() : String(value ?? '').trim();
|
||||
}
|
||||
|
||||
function normalizeMatchField(value: unknown): WecomMatchField | null {
|
||||
const normalized = cleanText(value).toLowerCase();
|
||||
return KNOWN_MATCH_FIELDS.includes(normalized as WecomMatchField)
|
||||
? (normalized as WecomMatchField)
|
||||
: null;
|
||||
}
|
||||
|
||||
function getFieldGuide(field: WecomMatchField | null, t: TranslateFn): string {
|
||||
if (field === 'mobile') return t('login.wecomFieldGuides.mobile');
|
||||
if (field === 'email') return t('login.wecomFieldGuides.email');
|
||||
if (field === 'employee_id') return t('login.wecomFieldGuides.employee_id');
|
||||
return t('login.wecomFieldGuides.default');
|
||||
}
|
||||
|
||||
function extractMissingField(reason: string): WecomMatchField | null {
|
||||
if (!reason.startsWith('missing_attr:')) return null;
|
||||
return normalizeMatchField(reason.split(':', 2)[1]);
|
||||
}
|
||||
|
||||
function formatRuleList(fields: WecomMatchField[], operator: 'AND' | 'OR', t: TranslateFn): string {
|
||||
if (fields.length === 0) return t('login.wecomFieldLabels.match_rules');
|
||||
const labels = fields.map((field) => formatWecomFieldLabel(field, t));
|
||||
if (labels.length === 1) return labels[0] || t('login.wecomFieldLabels.match_rules');
|
||||
const joiner = operator === 'OR' ? ` ${t('login.wecomRuleJoin.or')} ` : ` ${t('login.wecomRuleJoin.and')} `;
|
||||
return labels.join(joiner);
|
||||
}
|
||||
|
||||
function buildTechnicalInfo(errorCode: string, rawError: string): string | undefined {
|
||||
const parts: string[] = [];
|
||||
if (errorCode) parts.push(`code=${errorCode}`);
|
||||
if (rawError) parts.push(rawError);
|
||||
return parts.length ? parts.join(' | ') : undefined;
|
||||
}
|
||||
|
||||
export function normalizeWecomMatchRules(
|
||||
input: unknown,
|
||||
fallbackOperator: unknown = 'AND',
|
||||
): WecomNormalizedMatchRules {
|
||||
let fieldsSource: unknown[] = [];
|
||||
let operator: 'AND' | 'OR' = cleanText(fallbackOperator).toUpperCase() === 'OR' ? 'OR' : 'AND';
|
||||
|
||||
if (Array.isArray(input)) {
|
||||
fieldsSource = input;
|
||||
} else if (typeof input === 'string') {
|
||||
fieldsSource = input.split(',');
|
||||
} else if (input && typeof input === 'object') {
|
||||
const maybeRules = input as Record<string, unknown>;
|
||||
fieldsSource = Array.isArray(maybeRules.fields) ? maybeRules.fields : [];
|
||||
operator = cleanText(maybeRules.operator).toUpperCase() === 'OR' ? 'OR' : operator;
|
||||
}
|
||||
|
||||
const fields = fieldsSource
|
||||
.map((value) => normalizeMatchField(value))
|
||||
.filter((value): value is WecomMatchField => Boolean(value));
|
||||
|
||||
return { operator, fields };
|
||||
}
|
||||
|
||||
export function formatWecomFieldLabel(field: unknown, t: TranslateFn): string {
|
||||
const normalized = normalizeMatchField(field);
|
||||
if (normalized === 'employee_id') return t('login.wecomFieldLabels.employee_id');
|
||||
if (normalized === 'mobile') return t('login.wecomFieldLabels.mobile');
|
||||
if (normalized === 'email') return t('login.wecomFieldLabels.email');
|
||||
return t('login.wecomFieldLabels.match_rules');
|
||||
}
|
||||
|
||||
export function formatWecomRuleText(
|
||||
input: unknown,
|
||||
t: TranslateFn,
|
||||
fallbackOperator: unknown = 'AND',
|
||||
): string {
|
||||
const rules = normalizeWecomMatchRules(input, fallbackOperator);
|
||||
return formatRuleList(rules.fields, rules.operator, t);
|
||||
}
|
||||
|
||||
export function buildWecomLoginDialogContent(
|
||||
payload: WecomLoginErrorPayload,
|
||||
t: TranslateFn,
|
||||
): WecomLoginDialogContent {
|
||||
const rawError = cleanText(payload.error);
|
||||
const errorCode = cleanText(payload.error_code);
|
||||
const reason = cleanText(payload.reason);
|
||||
const rulesText = formatWecomRuleText(payload.match_rules, t);
|
||||
const technical = buildTechnicalInfo(errorCode, rawError);
|
||||
|
||||
if (errorCode === 'WECOM_MATCH_ATTR_MISSING') {
|
||||
const missingField = normalizeMatchField(payload.missing_field) || extractMissingField(reason);
|
||||
return {
|
||||
title: t('login.wecomDialogTitles.matchAttrMissing'),
|
||||
summary: t('login.wecomDialogSummaries.matchAttrMissing', {
|
||||
field: formatWecomFieldLabel(missingField, t),
|
||||
}),
|
||||
details: [
|
||||
t('login.wecomDialogDetails.currentRules', { rules: rulesText }),
|
||||
getFieldGuide(missingField, t),
|
||||
t('login.wecomDialogDetails.bindFallback'),
|
||||
],
|
||||
technical,
|
||||
};
|
||||
}
|
||||
|
||||
if (errorCode === 'WECOM_MATCH_AMBIGUOUS') {
|
||||
const candidateCount = Number(payload.candidate_count || 0);
|
||||
return {
|
||||
title: t('login.wecomDialogTitles.matchAmbiguous'),
|
||||
summary: candidateCount > 1
|
||||
? t('login.wecomDialogSummaries.matchAmbiguousWithCount', { count: candidateCount })
|
||||
: t('login.wecomDialogSummaries.matchAmbiguous'),
|
||||
details: [
|
||||
t('login.wecomDialogDetails.currentRules', { rules: rulesText }),
|
||||
t('login.wecomDialogDetails.ambiguousAction'),
|
||||
],
|
||||
technical,
|
||||
};
|
||||
}
|
||||
|
||||
if (errorCode === 'WECOM_BIND_REQUIRED') {
|
||||
return {
|
||||
title: t('login.wecomDialogTitles.bindRequired'),
|
||||
summary: t('login.wecomDialogSummaries.bindRequired'),
|
||||
details: [
|
||||
t('login.wecomDialogDetails.currentRules', { rules: rulesText }),
|
||||
t('login.wecomDialogDetails.bindRequiredAction'),
|
||||
],
|
||||
technical,
|
||||
};
|
||||
}
|
||||
|
||||
if (errorCode === 'WECOM_BIND_INPUT_REQUIRED') {
|
||||
return {
|
||||
title: t('login.wecomDialogTitles.bindInputRequired'),
|
||||
summary: t('login.wecomDialogSummaries.bindInputRequired'),
|
||||
details: [
|
||||
t('login.wecomDialogDetails.bindInputRequiredAction'),
|
||||
],
|
||||
technical,
|
||||
};
|
||||
}
|
||||
|
||||
if (errorCode === 'WECOM_BIND_CONFLICT') {
|
||||
return {
|
||||
title: t('login.wecomDialogTitles.bindConflict'),
|
||||
summary: t('login.wecomDialogSummaries.bindConflict'),
|
||||
details: [
|
||||
rawError || t('login.wecomDialogDetails.bindConflictAction'),
|
||||
t('login.wecomDialogDetails.bindConflictAction'),
|
||||
],
|
||||
technical,
|
||||
};
|
||||
}
|
||||
|
||||
if (errorCode === 'WECOM_NOT_IN_AGENT_SCOPE') {
|
||||
return {
|
||||
title: t('login.wecomDialogTitles.notInScope'),
|
||||
summary: t('login.wecomDialogSummaries.notInScope'),
|
||||
details: [
|
||||
rawError || t('login.wecomDialogDetails.notInScopeAction'),
|
||||
],
|
||||
technical,
|
||||
};
|
||||
}
|
||||
|
||||
if (rawError.includes('Invalid or expired WeCom state')) {
|
||||
return {
|
||||
title: t('login.wecomDialogTitles.stateExpired'),
|
||||
summary: t('login.wecomStateExpired'),
|
||||
details: [t('login.wecomDialogDetails.retryLogin')],
|
||||
technical,
|
||||
};
|
||||
}
|
||||
|
||||
if (rawError.includes('Account is disabled')) {
|
||||
return {
|
||||
title: t('login.wecomDialogTitles.accountDisabled'),
|
||||
summary: t('login.wecomAccountDisabled'),
|
||||
details: [t('login.wecomDialogDetails.contactAdmin')],
|
||||
technical,
|
||||
};
|
||||
}
|
||||
|
||||
if (rawError.includes('bind WeCom account') || rawError.includes('No linked account found')) {
|
||||
return {
|
||||
title: t('login.wecomDialogTitles.bindRequired'),
|
||||
summary: t('login.wecomDialogSummaries.bindRequired'),
|
||||
details: [
|
||||
t('login.wecomDialogDetails.currentRules', { rules: rulesText }),
|
||||
t('login.wecomDialogDetails.bindRequiredAction'),
|
||||
],
|
||||
technical,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
title: t('login.wecomDialogTitles.loginFailed'),
|
||||
summary: rawError || t('login.wecomLoginFailed'),
|
||||
details: [t('login.wecomDialogDetails.retryLogin')],
|
||||
technical,
|
||||
};
|
||||
}
|
||||
|
||||
function formatWecomMatchResult(result: string, t: TranslateFn): string {
|
||||
if (result === 'already_bound') return t('admin.wecom.auditResults.already_bound');
|
||||
if (result === 'matched_and_bound') return t('admin.wecom.auditResults.matched_and_bound');
|
||||
if (result === 'auto_created') return t('admin.wecom.auditResults.auto_created');
|
||||
if (result === 'input_verified_and_bound') return t('admin.wecom.auditResults.input_verified_and_bound');
|
||||
return result || '-';
|
||||
}
|
||||
|
||||
export function formatWecomAuditReason(row: WecomAuditRowLike, t: TranslateFn): string {
|
||||
const reason = cleanText(row.reason);
|
||||
const errorCode = cleanText(row.error_code);
|
||||
|
||||
if (cleanText(row.status) === 'success') {
|
||||
const result = cleanText(row.match_result);
|
||||
return result ? formatWecomMatchResult(result, t) : t('admin.wecom.auditMessages.loginSuccess');
|
||||
}
|
||||
|
||||
if (errorCode === 'WECOM_MATCH_ATTR_MISSING') {
|
||||
const field = extractMissingField(reason);
|
||||
return t('admin.wecom.auditMessages.matchAttrMissing', {
|
||||
field: formatWecomFieldLabel(field, t),
|
||||
});
|
||||
}
|
||||
|
||||
if (errorCode === 'WECOM_MATCH_AMBIGUOUS') {
|
||||
return t('admin.wecom.auditMessages.matchAmbiguous');
|
||||
}
|
||||
|
||||
if (errorCode === 'WECOM_BIND_REQUIRED') {
|
||||
return t('admin.wecom.auditMessages.bindRequired');
|
||||
}
|
||||
|
||||
if (errorCode === 'WECOM_BIND_CONFLICT') {
|
||||
return t('admin.wecom.auditMessages.bindConflict');
|
||||
}
|
||||
|
||||
if (errorCode === 'WECOM_BIND_INPUT_REQUIRED') {
|
||||
return t('admin.wecom.auditMessages.bindInputRequired');
|
||||
}
|
||||
|
||||
if (errorCode === 'WECOM_BIND_INPUT_NOT_FOUND') {
|
||||
return t('admin.wecom.auditMessages.bindInputNotFound');
|
||||
}
|
||||
|
||||
if (errorCode === 'WECOM_BIND_INPUT_AMBIGUOUS') {
|
||||
return t('admin.wecom.auditMessages.bindInputAmbiguous');
|
||||
}
|
||||
|
||||
if (errorCode === 'WECOM_NOT_IN_AGENT_SCOPE') {
|
||||
return t('admin.wecom.auditMessages.notInScope');
|
||||
}
|
||||
|
||||
if (reason === 'invalid_state') return t('admin.wecom.auditMessages.invalidState');
|
||||
if (reason === 'missing_code') return t('admin.wecom.auditMessages.missingCode');
|
||||
if (reason === 'account_disabled') return t('admin.wecom.auditMessages.accountDisabled');
|
||||
if (reason.startsWith('auth_failed:')) {
|
||||
return t('admin.wecom.auditMessages.authFailed', {
|
||||
detail: reason.split(':', 2)[1] || '-',
|
||||
});
|
||||
}
|
||||
if (reason.startsWith('user_fetch_failed:')) {
|
||||
return t('admin.wecom.auditMessages.userFetchFailed', {
|
||||
detail: reason.split(':', 2)[1] || '-',
|
||||
});
|
||||
}
|
||||
if (reason.startsWith('not_in_agent_scope:')) return t('admin.wecom.auditMessages.notInScope');
|
||||
if (reason.startsWith('exception:')) {
|
||||
return t('admin.wecom.auditMessages.exception', {
|
||||
detail: reason.split(':', 2)[1] || '-',
|
||||
});
|
||||
}
|
||||
|
||||
return reason || '-';
|
||||
}
|
||||
|
||||
export function buildWecomAuditMeta(row: WecomAuditRowLike, t: TranslateFn): string[] {
|
||||
const items: string[] = [];
|
||||
const rulesText = formatWecomRuleText(row.match_rules, t, row.match_operator);
|
||||
if (cleanText(row.match_rules)) {
|
||||
items.push(t('admin.wecom.auditMeta.rules', { rules: rulesText }));
|
||||
}
|
||||
if (cleanText(row.match_result)) {
|
||||
items.push(t('admin.wecom.auditMeta.result', {
|
||||
result: formatWecomMatchResult(cleanText(row.match_result), t),
|
||||
}));
|
||||
}
|
||||
if (cleanText(row.error_code)) {
|
||||
items.push(t('admin.wecom.auditMeta.errorCode', { code: cleanText(row.error_code) }));
|
||||
}
|
||||
return items;
|
||||
}
|
||||
Reference in new issue
Block a user