Files
crawler-plugin/admin-frontend-vue/src/pages/shop/shop-key-model.ts
T

87 lines
3.7 KiB
TypeScript

/** 店铺密钥列表加载模型(任务 82):解析 ShopKeyPageVo(camel) 密钥行,纯逻辑。 */
import { unwrap } from '../../api/envelope.ts'
import {
emptyShopKeyPageResult,
toSensitiveString,
type IpWhitelistStatus,
type ShopKeyItem,
type ShopKeyPageResult,
} from './shop-dto.ts'
const IP_WHITELIST_VALUES: readonly string[] = ['UNKNOWN', 'ALLOWED', 'BLOCKED']
function text(value: unknown): string {
return typeof value === 'string' ? value.trim() : ''
}
function numberOrNull(value: unknown): number | null {
return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null
}
/** 归一白名单状态;未知/缺省一律回 UNKNOWN。 */
export function normalizeIpWhitelistStatus(value: unknown): IpWhitelistStatus {
const textValue = typeof value === 'string' ? value.trim().toUpperCase() : ''
return (IP_WHITELIST_VALUES.includes(textValue) ? textValue : 'UNKNOWN') as IpWhitelistStatus
}
export interface WhitelistStatusMeta {
/** 展示文案(对齐 admin:正常/未加白名单/未检测)。 */
label: string
/** Element tag 语义色。 */
tag: 'success' | 'danger' | 'info'
}
/** admin 三态文案:ALLOWED→正常、BLOCKED→未加白名单、UNKNOWN→未检测。 */
const SHOP_KEY_STATUS_META: Record<IpWhitelistStatus, WhitelistStatusMeta> = {
ALLOWED: { label: '正常', tag: 'success' },
BLOCKED: { label: '未加白名单', tag: 'danger' },
UNKNOWN: { label: '未检测', tag: 'info' },
}
/** 白名单状态 → 展示元数据;未知回「未检测」。 */
export function whitelistStatusMeta(status: IpWhitelistStatus | null | undefined): WhitelistStatusMeta {
const key = normalizeIpWhitelistStatus(status)
return SHOP_KEY_STATUS_META[key] || SHOP_KEY_STATUS_META.UNKNOWN
}
/** 解析单条店铺密钥行;缺 id 视为无效。 */
export function toShopKeyItem(raw: unknown): ShopKeyItem | null {
if (!raw || typeof raw !== 'object') return null
const record = raw as Record<string, unknown>
const id = numberOrNull(record.id)
if (id === null) return null
const item: ShopKeyItem = {
id,
remarkName: text(record.remarkName ?? record.remark_name),
ziniaoAccountName: text(record.ziniaoAccountName ?? record.ziniao_account_name),
ziniaoToken: toSensitiveString(text(record.ziniaoToken ?? record.ziniao_token)),
ipWhitelistStatus: normalizeIpWhitelistStatus(record.ipWhitelistStatus ?? record.ip_whitelist_status),
ipWhitelistMessage: text(record.ipWhitelistMessage ?? record.ip_whitelist_message),
}
const checkedAt = text(record.ipWhitelistCheckedAt ?? record.ip_whitelist_checked_at)
if (checkedAt) item.ipWhitelistCheckedAt = checkedAt
const createdAt = text(record.createdAt ?? record.created_at)
if (createdAt) item.createdAt = createdAt
const updatedAt = text(record.updatedAt ?? record.updated_at)
if (updatedAt) item.updatedAt = updatedAt
return item
}
/** 归一化密钥分页负载(信封或已解包 VO)为前端结果;缺省字段回默认。 */
export function parseShopKeyPage(payload: unknown): ShopKeyPageResult {
const out = emptyShopKeyPageResult()
const core = unwrap<unknown>(payload)
if (!core || typeof core !== 'object') return out
const record = core as Record<string, unknown>
if (Array.isArray(record.items)) {
out.items = record.items
.map((raw) => toShopKeyItem(raw))
.filter((item): item is ShopKeyItem => item !== null)
}
if (typeof record.total === 'number') out.total = Math.floor(record.total)
if (typeof record.page === 'number' && record.page >= 1) out.page = Math.floor(record.page)
const rawSize = record.pageSize ?? record.page_size
if (typeof rawSize === 'number' && rawSize >= 1) out.pageSize = Math.floor(rawSize)
return out
}