重构(半自动履约): 统一虎牙/微博状态写入、CK 直取与履约分类(迁移190)
迁移 190 - 新增 SKU 级履约分类:manual_fulfillment_configs.semi_auto_category 为唯一真源 (''/direct_exchange/presale + CHECK),semi_auto_tasks.semi_auto_category 为物化快照, 配置改动不回写历史任务;补 (platform, semi_auto_category, created_at, id) 列表索引, 并按配置真源回填一次存量空快照。 状态写入收敛 - 26 处 UPDATE semi_auto_tasks 统一到唯一写入口 convergeSemiAutoTaskState: 行锁 + 状态转换规则 + 单条 UPDATE + 事件,事件只写本次真正变更的状态组。 - 新增 task-state-rules 纯函数与单测;终态 completed/cancelled 不再被业务状态改写。 - 热路径默认不回读带 join 的完整行,避免 2 秒轮询重复重读。 CK 生命周期 - 删除一次性注入票据与票据 TTL:移除 /semi-auto/injections 路由、两个 Connector 与 Mock 的票据实现;browser-session 改为后台鉴权后直接下发账号级 CK, 并补平台一致性与账号 UID 一致性校验。 - 登录成功但 CK 导出失败不再谎报已授权:落 failed + 原因,保留会话可重试; 微博短信登录与扫码路径行为对齐。 - 凭证 invalid 不再是永久黑名单:恢复前实时验证、成功回写 valid、失效重试按 60 秒节流; 微博恢复补任务级 UID 比对。 扩展与前端 - live-hub 的 huya-exchange-helper 迁入 apps/extensions/semi-auto-exchange-helper: 支持虎牙与微博域、CustomEvent 注入协议、--env-file 读取后台域名构建、 平台原始过期时间优先、只做 Cookie 注入与打开目标页(兑换人工完成)。 - 半自动履约页改为虎牙/微博 Tab + 分类筛选;人工 SKU 配置新增「履约分类」; 注入链路改为真实回执,扩展缺失时明确提示并支持复制 CK 兜底。 文档 - 权威文档同步:票据与 TTL 删除、CK 直取下发、扩展单一归属、分类真源与快照、 唯一写入口、迁移 190 记录。 - 新增《虎牙微博半自动履约现状-2026-11-30》现状盘点(重构前基线); 删除 docs/README.md 中指向不存在文件的失效链接。
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
import type { SemiAutoPlatform } from '@/types/admin/semi-auto'
|
||||
import type {
|
||||
SemiAutoBrowserSession,
|
||||
SemiAutoBrowserSessionCookie,
|
||||
SemiAutoPlatform,
|
||||
} from '@/types/admin/semi-auto'
|
||||
|
||||
/**
|
||||
* 注入浏览器成功后自动打开的平台页面。
|
||||
* 只在客户页/后台侧使用;扩展只负责写入 Cookie,指定平台页由前端占位页打开。
|
||||
* 注入浏览器成功后打开的平台页面。
|
||||
*
|
||||
* 前端只把 URL 作为 `target_url` 交给扩展,**不再自己开关标签页**:
|
||||
* 真正的 Cookie 写入和打开目标页都在「半自动兑换助手」扩展里完成。
|
||||
*/
|
||||
export const POST_INJECTION_URLS: Record<SemiAutoPlatform, string> = {
|
||||
huya: 'https://m-shop.huya.com/points.html?pagecode=heping_baodian_credit_center_page&sourceId=yellowcarlist&pid=1199650619883&anchorUid=1199650619883&roomid=30596253',
|
||||
@@ -13,3 +19,130 @@ export function resolvePostInjectionUrl(platform: string | null | undefined): st
|
||||
if (platform === 'huya' || platform === 'weibo') return POST_INJECTION_URLS[platform]
|
||||
return ''
|
||||
}
|
||||
|
||||
/** 页面派发注入材料的固定事件名,必须与扩展 relay.js 保持一致。 */
|
||||
export const SEMI_AUTO_INJECT_EVENT = 'ORDER_SITE_SEMI_AUTO_INJECT'
|
||||
|
||||
/** 扩展回执事件名,必须与扩展 relay.js 保持一致。 */
|
||||
export const SEMI_AUTO_RESULT_EVENT = 'huya-exchange-result'
|
||||
|
||||
/** 扩展内容脚本写入的页面标记,用来判断扩展是否可用。 */
|
||||
export const SEMI_AUTO_HELPER_FLAGS = ['huyaExchangeHelper', 'semiAutoExchangeHelper'] as const
|
||||
|
||||
/** 派发给扩展的材料包:字段名是扩展协议的一部分,不要改名。 */
|
||||
export type SemiAutoInjectionKit = {
|
||||
request_id: string
|
||||
platform: SemiAutoPlatform
|
||||
target_url: string
|
||||
cookies: SemiAutoBrowserSessionCookie[]
|
||||
}
|
||||
|
||||
/** 扩展回执:`ok` 只代表 Cookie 写入成功,不代表兑换成功。 */
|
||||
export type SemiAutoInjectionResult = {
|
||||
request_id: string
|
||||
ok: boolean
|
||||
written: number
|
||||
failed: number
|
||||
error: string
|
||||
}
|
||||
|
||||
/**
|
||||
* 每次注入生成独立的 request_id。
|
||||
*
|
||||
* 扩展回执是页面级广播,多个标签页/多次点击会互相串,
|
||||
* 只有 request_id 与自己这一条匹配的结果才允许消费。
|
||||
*/
|
||||
export function createSemiAutoRequestId(): string {
|
||||
const cryptoApi = globalThis.crypto
|
||||
if (cryptoApi && typeof cryptoApi.randomUUID === 'function') {
|
||||
try {
|
||||
return cryptoApi.randomUUID()
|
||||
} catch {
|
||||
// 非安全上下文(http 内网地址)会抛错,走下面的兜底。
|
||||
}
|
||||
}
|
||||
return `sa-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 组装派发给扩展的材料包。
|
||||
*
|
||||
* `requestId` 显式传入时原样使用(同一次注入只会有一个 id),
|
||||
* cookies 直接透传后端返回的数组,前端不裁剪、不补字段。
|
||||
*/
|
||||
export function buildSemiAutoInjectionKit(
|
||||
session: SemiAutoBrowserSession,
|
||||
requestId: string = createSemiAutoRequestId(),
|
||||
): SemiAutoInjectionKit {
|
||||
return {
|
||||
request_id: requestId,
|
||||
platform: session.platform,
|
||||
target_url: String(session.targetUrl || ''),
|
||||
cookies: session.cookies ?? [],
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 派发注入材料。
|
||||
*
|
||||
* 同时把材料挂到 `window.__semiAutoExchangeKit` 作为兜底:
|
||||
* 扩展如果没收到事件(例如内容脚本尚未注入),操作者可以手动点扩展图标读取它。
|
||||
*/
|
||||
export function dispatchSemiAutoInjection(kit: SemiAutoInjectionKit): boolean {
|
||||
if (typeof window === 'undefined') return false
|
||||
;(window as Window & { __semiAutoExchangeKit?: SemiAutoInjectionKit }).__semiAutoExchangeKit = kit
|
||||
try {
|
||||
window.dispatchEvent(new CustomEvent(SEMI_AUTO_INJECT_EVENT, { detail: JSON.stringify(kit) }))
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析扩展回执。
|
||||
*
|
||||
* 回执 detail 是 JSON 字符串(扩展可能回传空串或非法 JSON),解析失败一律丢弃,
|
||||
* 绝不把「没读到结果」当成成功。
|
||||
*/
|
||||
export function parseSemiAutoInjectionResult(detail: unknown): SemiAutoInjectionResult | null {
|
||||
let payload: unknown = detail
|
||||
if (typeof detail === 'string') {
|
||||
try {
|
||||
payload = JSON.parse(detail) as unknown
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) return null
|
||||
const source = payload as Record<string, unknown>
|
||||
const requestId = typeof source.request_id === 'string' ? source.request_id.trim() : ''
|
||||
if (!requestId) return null
|
||||
return {
|
||||
request_id: requestId,
|
||||
ok: source.ok === true,
|
||||
written: Number.isFinite(Number(source.written)) ? Number(source.written) : 0,
|
||||
failed: Number.isFinite(Number(source.failed)) ? Number(source.failed) : 0,
|
||||
error: typeof source.error === 'string' ? source.error : '',
|
||||
}
|
||||
}
|
||||
|
||||
/** 只接受与自己这一次注入匹配的回执,其余(含其它标签页的结果)一律忽略。 */
|
||||
export function matchSemiAutoInjectionResult(
|
||||
detail: unknown,
|
||||
requestId: string,
|
||||
): SemiAutoInjectionResult | null {
|
||||
const expected = String(requestId || '').trim()
|
||||
if (!expected) return null
|
||||
const result = parseSemiAutoInjectionResult(detail)
|
||||
if (!result || result.request_id !== expected) return null
|
||||
return result
|
||||
}
|
||||
|
||||
/** 扩展是否已在本页打标记(标记由内容脚本写入,未安装/未启用时为 false)。 */
|
||||
export function isSemiAutoExchangeHelperReady(
|
||||
dataset: Record<string, string | undefined> | null | undefined,
|
||||
): boolean {
|
||||
if (!dataset) return false
|
||||
return SEMI_AUTO_HELPER_FLAGS.some((flag) => dataset[flag] === '1')
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import type { SemiAutoCategory, SemiAutoPlatform } from '@/types/admin/semi-auto'
|
||||
|
||||
/**
|
||||
* 半自动履约页的 tab = 平台。tab key 直接就是 `platform` 参数值,
|
||||
* 不再额外维护一张映射表(历史上多写一份名单的后果是「tab 点了没反应」)。
|
||||
*/
|
||||
export const SEMI_AUTO_PLATFORM_TABS: readonly SemiAutoPlatform[] = ['huya', 'weibo']
|
||||
|
||||
/** URL 不带 `?tab=` 或带了非法值时落到虎牙。 */
|
||||
export const DEFAULT_SEMI_AUTO_PLATFORM_TAB: SemiAutoPlatform = 'huya'
|
||||
|
||||
/**
|
||||
* 分类筛选器的取值。
|
||||
*
|
||||
* - `all`:不传 `category` 参数(全部)
|
||||
* - `''`:显式传空串,后端据此筛「未分类」
|
||||
* - `direct_exchange` / `presale`:按分类筛
|
||||
*/
|
||||
export type SemiAutoCategoryFilter = 'all' | SemiAutoCategory
|
||||
|
||||
export function isSemiAutoPlatformTab(value: string | null | undefined): value is SemiAutoPlatform {
|
||||
return SEMI_AUTO_PLATFORM_TABS.includes(value as SemiAutoPlatform)
|
||||
}
|
||||
|
||||
/** 把 URL 上的 tab 归一成合法平台,非法值回落默认 tab。 */
|
||||
export function resolveSemiAutoPlatformTab(value: string | null | undefined): SemiAutoPlatform {
|
||||
return isSemiAutoPlatformTab(value) ? value : DEFAULT_SEMI_AUTO_PLATFORM_TAB
|
||||
}
|
||||
|
||||
/**
|
||||
* 分类筛选器 → 请求参数。
|
||||
*
|
||||
* 「全部」必须返回 undefined(参数不出现),「未分类」必须返回空串
|
||||
* (axios 会序列化成 `category=`),两者不能混为一谈。
|
||||
*/
|
||||
export function resolveSemiAutoCategoryFilter(
|
||||
filter: SemiAutoCategoryFilter | null | undefined,
|
||||
): SemiAutoCategory | undefined {
|
||||
if (filter === 'direct_exchange' || filter === 'presale') return filter
|
||||
if (filter === '') return ''
|
||||
return undefined
|
||||
}
|
||||
|
||||
/** 列表查询参数:页面与单测共用同一份组装逻辑。 */
|
||||
export function buildSemiAutoTaskListParams(input: {
|
||||
tab: string | null | undefined
|
||||
categoryFilter: SemiAutoCategoryFilter
|
||||
keyword?: string
|
||||
status?: string
|
||||
page: number
|
||||
pageSize: number
|
||||
}): Record<string, unknown> {
|
||||
const params: Record<string, unknown> = {
|
||||
page: input.page,
|
||||
pageSize: input.pageSize,
|
||||
platform: resolveSemiAutoPlatformTab(input.tab),
|
||||
}
|
||||
const keyword = String(input.keyword || '').trim()
|
||||
if (keyword) params.keyword = keyword
|
||||
const status = String(input.status || '')
|
||||
if (status) params.status = status
|
||||
const category = resolveSemiAutoCategoryFilter(input.categoryFilter)
|
||||
if (category !== undefined) params.category = category
|
||||
return params
|
||||
}
|
||||
Reference in New Issue
Block a user