54 lines
2.1 KiB
TypeScript
54 lines
2.1 KiB
TypeScript
// 衣物处理状态语义的单一事实来源。
|
||
//
|
||
// 后端 image_process_status:1=抠图中 2=分类中 3=排队中 4=成功 5=失败,
|
||
// 列表与详情响应都会带出(老响应无该字段时做降级推断)。
|
||
// 这里把「状态 → 标识文案与样式」的判定收敛到一处,避免同一语义在衣橱、
|
||
// 衣物详情、上传页各写一遍 if-else 而逐渐分叉。
|
||
//
|
||
// 零彩色纪律下的映射:需要用户关注的失败 = 实心黑标识(ink);中性处理中 = 玻璃灰标识(plain)。
|
||
import { ProcessStatus } from './config'
|
||
|
||
export interface ClothesStatusLike {
|
||
image_process_status?: number
|
||
category_main?: string
|
||
processed_image?: string
|
||
}
|
||
|
||
export interface ClothesStatusFlag {
|
||
text: string
|
||
type: '' | 'plain' | 'ink'
|
||
}
|
||
|
||
// 处理中:排队中 / 抠图中 / 分类中。
|
||
// 老响应无状态字段时沿用旧推断:既无属性也无抠图结果,视为仍在处理。
|
||
export function isProcessing(c: ClothesStatusLike): boolean {
|
||
const status = Number(c.image_process_status)
|
||
if (
|
||
status === ProcessStatus.QUEUED ||
|
||
status === ProcessStatus.MATTING ||
|
||
status === ProcessStatus.CLASSIFYING
|
||
) {
|
||
return true
|
||
}
|
||
if (!status && !c.category_main && !c.processed_image) return true
|
||
return false
|
||
}
|
||
|
||
// 处理失败:后端已确认失败(该路径下额度已自动退回)
|
||
export function isProcessFailed(c: ClothesStatusLike): boolean {
|
||
return Number(c.image_process_status) === ProcessStatus.FAILED
|
||
}
|
||
|
||
// 终态:成功或失败,无需继续轮询
|
||
export function isProcessTerminal(c: ClothesStatusLike): boolean {
|
||
const status = Number(c.image_process_status)
|
||
return status === ProcessStatus.SUCCESS || status === ProcessStatus.FAILED
|
||
}
|
||
|
||
// 列表标识:处理失败(实心,需用户关注)> 处理中(玻璃,中性)> 无
|
||
export function clothesStatusFlag(c: ClothesStatusLike): ClothesStatusFlag {
|
||
if (isProcessFailed(c)) return { text: '处理失败', type: 'ink' }
|
||
if (isProcessing(c)) return { text: 'AI 处理中', type: 'plain' }
|
||
return { text: '', type: '' }
|
||
}
|