refactor: 第一批低风险收敛——任务缓存服务泛型化 + 前端死代码清理与状态映射统一
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* 任务状态 → 文案 / 样式类 的统一映射(前端各任务页 Tab 共用)。
|
||||
*
|
||||
* 默认表取自各 Tab 本地映射的多数派(并集):
|
||||
* - 文案:SUCCESS/COMPLETED→已完成、FAILED→失败、RUNNING→执行中、
|
||||
* PENDING→等待中、CANCELLED→已取消、SCHEDULED→待执行、QUEUED→排队中;
|
||||
* 未命中回退「等待中」。
|
||||
* - 样式:SUCCESS/COMPLETED→success、FAILED/CANCELLED→failed、RUNNING→running;
|
||||
* 未命中回退 'pending'。
|
||||
*
|
||||
* 与默认表不一致的 Tab 通过 overrides 覆盖,保证显示效果与原实现一致。
|
||||
* 状态键统一大小写不敏感(内部先 trim + toUpperCase)。
|
||||
*/
|
||||
|
||||
/** 任务样式类(与 shared/components/tasks/types.ts 的 TaskStatusClass 一致) */
|
||||
export type TaskStatusClassName = 'success' | 'failed' | 'running' | 'pending';
|
||||
|
||||
export interface TaskStatusOptions {
|
||||
/** 文案覆盖表(replace=true 时完全替换默认表,否则按键合并) */
|
||||
text?: Record<string, string>;
|
||||
/** 样式覆盖表(replace=true 时完全替换默认表,否则按键合并) */
|
||||
classMap?: Record<string, TaskStatusClassName>;
|
||||
/** 未命中时的文案,默认「等待中」 */
|
||||
textFallback?: string;
|
||||
/** 未命中时的样式类,默认 'pending' */
|
||||
classFallback?: TaskStatusClassName;
|
||||
/** true 时忽略默认表,只用 overrides */
|
||||
replace?: boolean;
|
||||
}
|
||||
|
||||
export const TASK_STATUS_TEXT: Record<string, string> = {
|
||||
SUCCESS: '已完成',
|
||||
COMPLETED: '已完成',
|
||||
FAILED: '失败',
|
||||
RUNNING: '执行中',
|
||||
PENDING: '等待中',
|
||||
CANCELLED: '已取消',
|
||||
SCHEDULED: '待执行',
|
||||
QUEUED: '排队中',
|
||||
};
|
||||
|
||||
export const TASK_STATUS_CLASS: Record<string, TaskStatusClassName> = {
|
||||
SUCCESS: 'success',
|
||||
COMPLETED: 'success',
|
||||
FAILED: 'failed',
|
||||
CANCELLED: 'failed',
|
||||
RUNNING: 'running',
|
||||
};
|
||||
|
||||
export const TASK_STATUS_TEXT_FALLBACK = '等待中';
|
||||
export const TASK_STATUS_CLASS_FALLBACK: TaskStatusClassName = 'pending';
|
||||
|
||||
function normalizeStatusKey(status?: string | null): string {
|
||||
return String(status ?? '').trim().toUpperCase();
|
||||
}
|
||||
|
||||
/** 任务状态 → 展示文案 */
|
||||
export function taskStatusText(status?: string | null, overrides?: TaskStatusOptions): string {
|
||||
const key = normalizeStatusKey(status);
|
||||
const table = overrides?.replace
|
||||
? overrides.text
|
||||
: { ...TASK_STATUS_TEXT, ...overrides?.text };
|
||||
if (table && key in table) return table[key];
|
||||
return overrides?.textFallback ?? TASK_STATUS_TEXT_FALLBACK;
|
||||
}
|
||||
|
||||
/** 任务状态 → 样式类 */
|
||||
export function taskStatusClass(status?: string | null, overrides?: TaskStatusOptions): TaskStatusClassName {
|
||||
const key = normalizeStatusKey(status);
|
||||
const table = overrides?.replace
|
||||
? overrides.classMap
|
||||
: { ...TASK_STATUS_CLASS, ...overrides?.classMap };
|
||||
if (table && key in table) return table[key];
|
||||
return overrides?.classFallback ?? TASK_STATUS_CLASS_FALLBACK;
|
||||
}
|
||||
|
||||
/**
|
||||
* 非终态一律显示「执行中」/ running 的页面(BrandWithdrawTab、BrandPatrolDeleteTab、
|
||||
* BrandQueryAsinTab 原行为):只区分 SUCCESS/COMPLETED/FAILED 三种终态。
|
||||
*/
|
||||
export const RUNNING_UNTIL_DONE_STATUS_OPTIONS: TaskStatusOptions = {
|
||||
replace: true,
|
||||
text: { SUCCESS: '已完成', COMPLETED: '已完成', FAILED: '失败' },
|
||||
classMap: { SUCCESS: 'success', COMPLETED: 'success', FAILED: 'failed' },
|
||||
textFallback: '执行中',
|
||||
classFallback: 'running',
|
||||
};
|
||||
Reference in New Issue
Block a user