Files
live-hub-py/web/frontend/src/pages/HuyaCollabPageConfig.ts
T

323 lines
12 KiB
TypeScript

import type {
HuyaCollabGift,
HuyaCollabImageSlot,
HuyaCollabStatus,
} from '../api/modules';
export function hasTextInputFocus(): boolean {
const active = document.activeElement;
return (
active instanceof HTMLElement &&
(active.tagName === 'INPUT' || active.tagName === 'TEXTAREA' || active.isContentEditable)
);
}
/** 实时同步暂停开关按浏览器记住:刷新页面后仍保持暂停,直到用户手动恢复。 */
export const REALTIME_PAUSED_STORAGE_KEY = 'huya.collab.realtime-paused';
export function readRealtimePaused(): boolean {
try {
return window.localStorage.getItem(REALTIME_PAUSED_STORAGE_KEY) === '1';
} catch {
return false;
}
}
export const STATUS_META: Record<HuyaCollabStatus, { label: string; color: string }> = {
// 只登记了订单号 + 店铺,客户资料(CK / 礼包 / 主页截图)到了再补
incomplete: { label: '待补资料', color: 'gold' },
// 资料齐全且 CK 有效;等充值
to_recharge: { label: '待充值', color: 'orange' },
recharged: { label: '已充值', color: 'geekblue' },
scripted: { label: '已上脚本', color: 'purple' },
// 抢购成功(枚举值仍是 done):从「已上脚本」查到兑换记录 → 自动进这一步
done: { label: '抢购成功', color: 'success' },
// 充值完成但积分不足/超出阈值
points_error: { label: '积分异常', color: 'volcano' },
// 旁路终态:抢购后查兑换记录没查到这一单的礼包(枚举值仍是 failed)
failed: { label: '抢购失败', color: 'red' },
void: { label: '已作废', color: 'error' },
};
/** 判定结论的配色(生命周期时间线用):与「业务状态名」分开,这里说的是"兑换记录里查到没有" */
export const VERDICT_META: Record<string, { label: string; color: string }> = {
success: { label: '兑换成功', color: 'success' },
failed: { label: '兑换失败', color: 'error' },
unknown: { label: '无法判定', color: 'default' },
};
/** 「改状态」能选的目标(顺序 = 业务流程顺序,回退要显式勾选)
* 「待补资料」只作为新建起点,不放进「改状态」:它只能通过「回退状态」进入。 */
export const STATUS_ORDER_OPTIONS: { value: HuyaCollabStatus; label: string }[] = [
{ value: 'to_recharge', label: '待充值' },
{ value: 'recharged', label: '已充值(核账)' },
{ value: 'scripted', label: '已上脚本' },
{ value: 'done', label: '抢购成功' },
{ value: 'failed', label: '抢购失败' },
{ value: 'void', label: '已作废' },
];
/** 「回退」能选的目标:流程链(含「待补资料」;要进「已作废 / 抢购失败」走「改状态」或「作废」入口) */
export const FLOW_STATUS_OPTIONS: { value: HuyaCollabStatus; label: string }[] = [
{ value: 'incomplete', label: '待补资料' },
{ value: 'to_recharge', label: '待充值' },
{ value: 'recharged', label: '已充值(核账)' },
{ value: 'scripted', label: '已上脚本' },
{ value: 'done', label: '抢购成功' },
];
export const CK_META: Record<string, { label: string; color: string }> = {
unverified: { label: '未验证', color: 'default' },
valid: { label: '有效', color: 'success' },
invalid: { label: '已失效', color: 'error' },
disabled: { label: '已停用', color: 'warning' },
};
/** 表头排序字段(后端白名单):seq = 固定创建序号;点击一次正序,再点一次反序。 */
export type CollabSortKey =
| 'seq'
| 'order_no'
| 'shop'
| 'owner'
| 'bind'
| 'gift_name'
| 'status'
| 'uid'
| 'ck_status'
| 'updated_at';
export type CollabSortOrder = 'ascend' | 'descend';
export const COLLAB_SORT_KEYS: ReadonlySet<string> = new Set<CollabSortKey>([
'seq',
'order_no',
'shop',
'owner',
'bind',
'gift_name',
'status',
'uid',
'ck_status',
'updated_at',
]);
export const ACTION_LABELS: Record<string, string> = {
'huya:collab:create': '新建',
'huya:collab:update': '编辑',
'huya:collab:status': '状态变更',
'huya:collab:ck': 'CK 变更',
'huya:collab:ck_status': 'CK 验证',
'huya:collab:probe_snapshot': '绑定快照刷新',
'huya:collab:recharge_invalidate': '核账快照失效',
'huya:collab:image': '图片变更',
'huya:collab:delete': '删除',
};
export const SLOT_META: Record<HuyaCollabImageSlot, { label: string; keysKey: 'bind_images' | 'home_images' }> = {
// 绑定图不再人工上传:绑定关系由探针自动读(2026-10-16 用户拍板),只留老数据只读展示
bind_images: { label: '历史绑定图', keysKey: 'bind_images' },
home_images: { label: '主页截图', keysKey: 'home_images' },
};
export const IMPORT_PREVIEW_ROWS = 20;
// 「礼包列表」的本地缓存键:一个活动期间就那一批礼包,没必要每贴一份 CK 都去虎牙拉一次;
// v3 = 2026-11-11 起自动合并积分中心全部页签,换键避免继续用只含「精英专享」的旧缓存。
export const GIFTS_CACHE_KEY = 'huya.collab.gifts.v3';
export const RECHARGE_THRESHOLD_KEY = 'huya.collab.rechargeThreshold';
export const RECHARGE_THRESHOLD_DEFAULT = 100;
export function readRechargeThreshold(): number {
try {
const raw = window.localStorage.getItem(RECHARGE_THRESHOLD_KEY);
if (raw === null || raw === '') return RECHARGE_THRESHOLD_DEFAULT;
const value = Number(raw);
return Number.isFinite(value) && value >= 0 ? value : RECHARGE_THRESHOLD_DEFAULT;
} catch {
return RECHARGE_THRESHOLD_DEFAULT;
}
}
export const MAX_IMAGES_PER_SLOT = 20;
/** 读礼包缓存(读不到 / 解析坏 / 空列表都当没缓存) */
export function readGiftCache(): { gifts: HuyaCollabGift[]; at: number } | null {
try {
const raw = window.localStorage.getItem(GIFTS_CACHE_KEY);
if (!raw) return null;
const parsed = JSON.parse(raw) as { at?: number; gifts?: HuyaCollabGift[] };
const gifts = Array.isArray(parsed.gifts) ? parsed.gifts : [];
if (gifts.length === 0) return null;
return { gifts, at: Number(parsed.at) || 0 };
} catch {
return null;
}
}
/** 写礼包缓存:写不进去(隐私模式 / 配额满)也不影响功能,静静跳过 */
export function writeGiftCache(gifts: HuyaCollabGift[]): void {
try {
window.localStorage.setItem(GIFTS_CACHE_KEY, JSON.stringify({ at: Date.now(), gifts }));
} catch {
// 忽略:缓存只是体验优化
}
}
// 「新建协作单」草稿:填一半关掉 / 刷新页面后再打开不能清空(2026-10-17 用户需求)。
// 按登录用户分键,避免同一台电脑换账号后看到上一个人的 CK。
export const CREATE_DRAFT_KEY_PREFIX = 'huya.collab.createDraft';
// 「只填订单号 + 店铺直接建单」的首次提示只弹一次(按浏览器记,不再打扰)
export const CREATE_DRAFT_HINT_KEY = 'huya.collab.draftHintShown';
export interface CreateDraft {
order_no: string;
shop: string;
ck_text: string;
gift_name: string;
required_points: number | null;
remark: string;
home_images: string[];
saved_at: number;
}
export function createDraftKey(userId?: number | null): string {
return userId ? `${CREATE_DRAFT_KEY_PREFIX}.${userId}` : '';
}
/** 读草稿(读不到 / 解析坏都当没有) */
export function readCreateDraft(userId?: number | null): CreateDraft | null {
const key = createDraftKey(userId);
if (!key) return null;
try {
const raw = window.localStorage.getItem(key);
if (!raw) return null;
const parsed = JSON.parse(raw) as Partial<CreateDraft>;
const savedAt = Number(parsed.saved_at) || 0;
// 草稿只留 7 天:里面可能有一份 CK,不能无限期躺在浏览器里
if (!savedAt || Date.now() - savedAt > 7 * 24 * 60 * 60 * 1000) {
window.localStorage.removeItem(key);
return null;
}
return {
order_no: typeof parsed.order_no === 'string' ? parsed.order_no : '',
shop: typeof parsed.shop === 'string' ? parsed.shop : '',
ck_text: typeof parsed.ck_text === 'string' ? parsed.ck_text : '',
gift_name: typeof parsed.gift_name === 'string' ? parsed.gift_name : '',
required_points: typeof parsed.required_points === 'number' ? parsed.required_points : null,
remark: typeof parsed.remark === 'string' ? parsed.remark : '',
home_images: Array.isArray(parsed.home_images)
? parsed.home_images.filter((item): item is string => typeof item === 'string')
: [],
saved_at: Number(parsed.saved_at) || 0,
};
} catch {
return null;
}
}
export function clearCreateDraft(userId?: number | null): void {
const key = createDraftKey(userId);
if (!key) return;
try {
window.localStorage.removeItem(key);
} catch {
// 忽略:清不掉也不影响当前表单
}
}
/** 「资料没填全时直接建单 → 待补资料」的首次提示是否已经展示过 */
export function draftHintShown(): boolean {
try {
return window.localStorage.getItem(CREATE_DRAFT_HINT_KEY) === '1';
} catch {
return true; // 存不了就不要再反复弹
}
}
export function markDraftHintShown(): void {
try {
window.localStorage.setItem(CREATE_DRAFT_HINT_KEY, '1');
} catch {
// 忽略:提示只是体验
}
}
export function hasDraftContent(draft: Omit<CreateDraft, 'saved_at'>): boolean {
// 礼包名不算「填过东西」:它现在默认空白、也不从草稿恢复(见下面 createForm 那段的说明),
// 否则一份「只被上次自动填了礼包」的老草稿会一直弹「已恢复上次未提交的草稿」。
return Boolean(
draft.order_no.trim()
|| draft.shop.trim()
|| draft.ck_text.trim()
|| draft.remark.trim()
|| draft.home_images.length,
);
}
// 「补资料」草稿:按登录用户 + 订单 id 分开存。填一半关掉 / 刷新后,
// 再点这一行的「补资料」会原样恢复(含刚贴的 CK 和刚上传的截图 key)。
export const SUPPLEMENT_DRAFT_KEY_PREFIX = 'huya.collab.supplementDraft';
export interface SupplementDraft {
shop: string;
ck_text: string;
gift_name: string;
required_points: number | null;
remark: string;
home_images: string[];
saved_at: number;
}
export interface SupplementFormValues {
shop: string;
ck_text?: string;
gift_name?: string;
required_points?: number | null;
remark?: string;
home_images?: string[];
}
export function supplementDraftKey(userId?: number | null, orderId?: number): string {
return userId && orderId ? `${SUPPLEMENT_DRAFT_KEY_PREFIX}.${userId}.${orderId}` : '';
}
export function readSupplementDraft(userId: number | null | undefined, orderId: number): SupplementDraft | null {
const key = supplementDraftKey(userId, orderId);
if (!key) return null;
try {
const raw = window.localStorage.getItem(key);
if (!raw) return null;
const parsed = JSON.parse(raw) as Partial<SupplementDraft>;
const savedAt = Number(parsed.saved_at) || 0;
if (!savedAt || Date.now() - savedAt > 7 * 24 * 60 * 60 * 1000) {
window.localStorage.removeItem(key);
return null;
}
return {
shop: typeof parsed.shop === 'string' ? parsed.shop : '',
ck_text: typeof parsed.ck_text === 'string' ? parsed.ck_text : '',
gift_name: typeof parsed.gift_name === 'string' ? parsed.gift_name : '',
required_points: typeof parsed.required_points === 'number' ? parsed.required_points : null,
remark: typeof parsed.remark === 'string' ? parsed.remark : '',
home_images: Array.isArray(parsed.home_images)
? parsed.home_images.filter((item): item is string => typeof item === 'string')
: [],
saved_at: savedAt,
};
} catch {
return null;
}
}
export function clearSupplementDraft(userId: number | null | undefined, orderId: number): void {
const key = supplementDraftKey(userId, orderId);
if (!key) return;
try {
window.localStorage.removeItem(key);
} catch {
// 忽略:清不掉也不影响当前表单
}
}
export function formatTime(value: string | null | undefined): string {
if (!value) return '-';
const time = new Date(value);
return Number.isNaN(time.getTime()) ? String(value) : time.toLocaleString();
}