719 lines
37 KiB
TypeScript
719 lines
37 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||
// fe4-handlers.ts Day 53(D24):记录中心 msw 契约 mock——下行批次记录先行。
|
||
// 对齐后端 Day60 计划接口 api/v1/record.go(5 天热数据 Redis 缓存+状态聚合);
|
||
// 数据口径=message_task 聚合:progressOf 五段(sending+四态)=mobile_count。
|
||
// 顺带承载 /fe-errors 上报兜底(runbook §6.4:mock 模式下上报通道可闭环)。
|
||
import { http, HttpResponse } from 'msw'
|
||
import type { DownRecord, DownQuery, InterceptedRow, DownDetail, ChannelLogEntry, HistoryRecord, ExportTask, UplinkRecord, RepushReport, PendingDetail } from '../src/api/record'
|
||
import type { DownlinkDetailRow } from '../src/api/sms'
|
||
import { progressOf } from '../src/api/record-progress'
|
||
|
||
function ok<T>(data: T): HttpResponse<any> {
|
||
return HttpResponse.json({ code: 'ISC:000', msg: '成功', data })
|
||
}
|
||
function fail(code: string, msg: string, status?: number): HttpResponse<any> {
|
||
return HttpResponse.json({ code, msg, data: null }, { status })
|
||
}
|
||
|
||
// ---- 内存任务库(D24;覆盖五类发送类型×四类任务态×进度分布) ----
|
||
function mk(p: Partial<DownRecord> & Pick<DownRecord, 'id' | 'task_no'>): DownRecord {
|
||
const base = {
|
||
send_type: 1,
|
||
sign: '盈讯网络',
|
||
content: '【盈讯网络】您的验证码是 {code},5 分钟内有效。',
|
||
fee_total: 0,
|
||
audit_status: 1,
|
||
status: 2,
|
||
created_at: '2026-09-13 09:00:00',
|
||
finished_at: '2026-09-13 09:00:35',
|
||
success_count: 0,
|
||
fail_count: 0,
|
||
unknown_count: 0,
|
||
intercepted_count: 0,
|
||
sending_count: 0,
|
||
mobile_count: 0,
|
||
}
|
||
return { ...base, ...p } as DownRecord
|
||
}
|
||
|
||
// 分段构造:sum(五段)=mobile_count(契约锁定,测试断言)
|
||
function seg(mobile: number, s: number, send: number, u: number, f: number, i: number) {
|
||
return {
|
||
mobile_count: mobile,
|
||
success_count: s,
|
||
sending_count: send,
|
||
unknown_count: u,
|
||
fail_count: f,
|
||
intercepted_count: i,
|
||
}
|
||
}
|
||
|
||
export const downDB: DownRecord[] = [
|
||
mk({ id: 1, task_no: 'T20260913090001', ...seg(10000, 9260, 0, 40, 320, 380), fee_total: 10000 }),
|
||
mk({ id: 2, task_no: 'T20260913090102', ...seg(5000, 5000, 0, 0, 0, 0), send_type: 2, fee_total: 5000, finished_at: '2026-09-13 09:02:10' }),
|
||
// 进行中:发送中占比>0(进度条动态段)
|
||
mk({ id: 3, task_no: 'T20260913100003', ...seg(8000, 4100, 3600, 0, 120, 180), send_type: 4, status: 1, fee_total: 4400, finished_at: null }),
|
||
// 全拦截:下载按钮主用例
|
||
mk({ id: 4, task_no: 'T20260913103004', ...seg(300, 0, 0, 0, 0, 300), fee_total: 0, created_at: '2026-09-13 10:30:00', finished_at: '2026-09-13 10:30:02' }),
|
||
// 定时待发:发送中=全部
|
||
mk({ id: 5, task_no: 'T20260914090005', ...seg(2000, 0, 2000, 0, 0, 0), send_type: 3, status: 0, created_at: '2026-09-13 11:00:00', finished_at: null }),
|
||
mk({ id: 6, task_no: 'T20260912160006', ...seg(660, 610, 0, 0, 20, 30), created_at: '2026-09-12 16:00:00', finished_at: '2026-09-12 16:00:28' }),
|
||
mk({ id: 7, task_no: 'T20260912150007', ...seg(120, 0, 0, 120, 0, 0), send_type: 2, fee_total: 0, created_at: '2026-09-12 15:00:00', finished_at: '2026-09-12 15:01:00' }),
|
||
mk({ id: 8, task_no: 'T20260911140008', ...seg(50, 44, 0, 0, 6, 0), send_type: 4, fee_total: 50, created_at: '2026-09-11 14:00:00', finished_at: '2026-09-11 14:00:12' }),
|
||
mk({ id: 9, task_no: 'T20260911130009', ...seg(1000, 980, 0, 0, 10, 10), fee_total: 1000, created_at: '2026-09-11 13:00:00', finished_at: '2026-09-11 13:00:40' }),
|
||
mk({ id: 10, task_no: 'T20260910120010', ...seg(240, 200, 0, 20, 10, 10), fee_total: 240, created_at: '2026-09-10 12:00:00', finished_at: '2026-09-10 12:00:18' }),
|
||
mk({ id: 11, task_no: 'T20260910110011', ...seg(80, 0, 0, 0, 80, 0), send_type: 2, status: 3, fee_total: 80, created_at: '2026-09-10 11:00:00', finished_at: '2026-09-10 11:00:05' }),
|
||
mk({ id: 12, task_no: 'T20260909100012', ...seg(500, 470, 0, 10, 10, 10), send_type: 3, fee_total: 500, created_at: '2026-09-09 10:00:00', finished_at: '2026-09-09 10:01:00' }),
|
||
]
|
||
|
||
// 拦截号码明细(id=4 全拦 300 行为代表;id=1 黑名单 380 行)
|
||
export const interceptedDB: Record<number, InterceptedRow[]> = {
|
||
4: Array.from({ length: 300 }, (_, i) => ({
|
||
mobile: `138${String(3000 + i).padStart(4, '0')}${String(1000 + i).padStart(4, '0')}`,
|
||
error_code: i % 2 === 0 ? 'ISC:032' : 'UT:0023',
|
||
reason: i % 2 === 0 ? '频次策略拦截' : '一级黑关键字',
|
||
})),
|
||
1: Array.from({ length: 380 }, (_, i) => ({
|
||
mobile: `139${String(2000 + i).padStart(4, '0')}${String(2000 + i).padStart(4, '0')}`,
|
||
error_code: 'UT:0006',
|
||
reason: '黑名单',
|
||
})),
|
||
}
|
||
|
||
function interceptedRows(task: DownRecord): InterceptedRow[] {
|
||
if (interceptedDB[task.id]) return interceptedDB[task.id]
|
||
return Array.from({ length: task.intercepted_count }, (_, i) => ({
|
||
mobile: `137${String(8000 + i).padStart(4, '0')}${String(5000 + i).padStart(4, '0')}`,
|
||
error_code: 'UT:0009',
|
||
reason: '屏蔽地区',
|
||
}))
|
||
}
|
||
|
||
export const fe4Handlers = [
|
||
// GET /record/down 分页+筛选(task_no 精确/send_type/status/时间范围)
|
||
http.get('*/api/v1/record/down', ({ request }) => {
|
||
const url = new URL(request.url)
|
||
const q: DownQuery = {
|
||
page: Number(url.searchParams.get('page') ?? 1),
|
||
page_size: Number(url.searchParams.get('page_size') ?? 100),
|
||
task_no: url.searchParams.get('task_no') ?? undefined,
|
||
send_type: url.searchParams.get('send_type') ? Number(url.searchParams.get('send_type')) : undefined,
|
||
status: url.searchParams.get('status') != null && url.searchParams.get('status') !== '' ? Number(url.searchParams.get('status')) : undefined,
|
||
start_time: url.searchParams.get('start_time') ?? undefined,
|
||
end_time: url.searchParams.get('end_time') ?? undefined,
|
||
}
|
||
let rows = [...downDB]
|
||
if (q.task_no) rows = rows.filter((r) => r.task_no === q.task_no)
|
||
if (q.send_type) rows = rows.filter((r) => r.send_type === q.send_type)
|
||
if (q.status != null) rows = rows.filter((r) => r.status === q.status)
|
||
if (q.start_time) rows = rows.filter((r) => r.created_at >= q.start_time!)
|
||
if (q.end_time) rows = rows.filter((r) => r.created_at <= q.end_time!)
|
||
// 契约校验:五段聚合=mobile_count(mock 数据自证,测试断言同式)
|
||
for (const r of rows) {
|
||
const sum = r.success_count + r.sending_count + r.unknown_count + r.fail_count + r.intercepted_count
|
||
if (sum !== r.mobile_count) return fail('ISC:500', `mock 数据聚合不一致:${r.task_no}`, 500)
|
||
}
|
||
rows.sort((a, b) => (a.created_at < b.created_at ? 1 : -1))
|
||
const total = rows.length
|
||
const start = (q.page - 1) * q.page_size
|
||
return ok({ list: rows.slice(start, start + q.page_size), total })
|
||
}),
|
||
|
||
// GET /record/down/:id/intercepted?format=csv → CSV blob(无拦截 404 业务码)
|
||
http.get('*/api/v1/record/down/:id/intercepted', ({ params, request }) => {
|
||
const id = Number(params.id)
|
||
const task = downDB.find((r) => r.id === id)
|
||
if (!task) return fail('ISC:404', '任务不存在', 404)
|
||
if (task.intercepted_count === 0) return fail('ISC:404', '该批次无拦截号码', 404)
|
||
const rows = interceptedRows(task)
|
||
const format = new URL(request.url).searchParams.get('format') ?? 'csv'
|
||
if (format !== 'csv') return fail('ISC:400', '不支持的导出格式', 400)
|
||
const csv = ['mobile,error_code,reason', ...rows.map((r) => `${r.mobile},${r.error_code},${r.reason}`)].join('\n')
|
||
return new HttpResponse(csv, {
|
||
headers: { 'Content-Type': 'text/csv; charset=utf-8', 'Content-Disposition': `attachment; filename="intercepted-${id}.csv"` },
|
||
})
|
||
}),
|
||
|
||
// POST /fe-errors 上报兜底(§6.4:mock 模式闭环,静默成功)
|
||
http.post('*/api/v1/fe-errors', () => ok(null)),
|
||
]
|
||
|
||
// ---- D25 下行明细(号码级)+CMPP 通道日志钻入 ----
|
||
// mock 口径:每批次生成代表性行(≤24 行/任务,五态覆盖——生产为全量 message_detail,
|
||
// 契约与分页语义一致);detail.id=task.id*10000+i 稳定(通道日志检索键)。
|
||
const CARRIERS = ['移动', '联通', '电信']
|
||
const PROVINCES = ['广东', '北京', '上海', '浙江', '江苏', '四川']
|
||
|
||
/** 明细行五态分布(按任务段非零缩样;[status, errorCode 候选])。 */
|
||
const DETAIL_STATES: Array<[number, string[]]> = [
|
||
[2, ['']], // 成功
|
||
[3, ['ISC:027', 'ISC:012', 'UT:0008']], // 失败
|
||
[4, ['ISC:010']], // 未知(回执超时)
|
||
[5, ['ISC:032', 'UT:0006', 'ISC:031']], // 拦截
|
||
[0, ['']], // 待发
|
||
]
|
||
|
||
function detailRows(task: DownRecord): DownDetail[] {
|
||
const rows: DownDetail[] = []
|
||
const segs = progressOf(task).filter((s) => s.n > 0)
|
||
let i = 0
|
||
for (const seg of segs) {
|
||
const match = DETAIL_STATES.find(([st]) =>
|
||
st === 2 ? seg.key === 'success' : st === 3 ? seg.key === 'fail' : st === 4 ? seg.key === 'unknown' : st === 5 ? seg.key === 'intercepted' : seg.key === 'sending' && st === 0,
|
||
)
|
||
if (!match) continue
|
||
const n = Math.min(seg.n, seg.key === 'sending' ? 2 : 6) // 代表子集
|
||
for (let k = 0; k < n; k++, i++) {
|
||
const code = match[1][k % match[1].length]
|
||
rows.push({
|
||
id: task.id * 10000 + i,
|
||
task_id: task.id,
|
||
task_no: task.task_no,
|
||
mobile: `${13 + (task.id % 3)}8${String(1000 + i * 7).padStart(4, '0')}${String(task.id * 100 + i).padStart(4, '0')}`,
|
||
province: PROVINCES[(task.id + i) % PROVINCES.length],
|
||
carrier: CARRIERS[(task.id + i) % CARRIERS.length],
|
||
status: match[0],
|
||
error_code: code,
|
||
ext_code: '',
|
||
call_data: '',
|
||
fee: match[0] === 5 ? 0 : 1,
|
||
created_at: task.created_at,
|
||
})
|
||
}
|
||
}
|
||
return rows
|
||
}
|
||
|
||
export const downDetailDB: DownDetail[] = downDB.flatMap(detailRows)
|
||
|
||
// 通道日志(成功→connect/submit/deliver 三段;失败→deliver 带错误码;未知→回执超时;拦截→空)
|
||
function channelLogs(d: DownDetail): ChannelLogEntry[] {
|
||
const base = (kind: string, detail: string, latency = 30): ChannelLogEntry => ({
|
||
at: d.created_at,
|
||
kind,
|
||
level: 0,
|
||
msg_id: `M${d.id}`,
|
||
detail,
|
||
latency_ms: latency,
|
||
})
|
||
switch (d.status) {
|
||
case 2:
|
||
return [base('connect', '通道连接建立(size=50)'), base('submit', `CMPP2 submit 分包下发 ${d.mobile}`), base('deliver', 'DELIV 回执:成功', 45)]
|
||
case 3:
|
||
return [base('connect', '通道连接建立(size=50)'), base('submit', `CMPP2 submit 分包下发 ${d.mobile}`), { ...base('deliver', `回执错误:${d.error_code}`, 120), level: 2 }]
|
||
case 4:
|
||
return [base('connect', '通道连接建立(size=50)'), base('submit', `CMPP2 submit 分包下发 ${d.mobile}`), { ...base('reclaim', '回执超时,状态机挂起待补偿扫描', 0), level: 1 }]
|
||
case 1:
|
||
return [base('connect', '通道连接建立(size=50)'), base('submit', 'CMPP2 submit 已入在途窗口', 15)]
|
||
default:
|
||
return [] // 拦截/待发:未投通道
|
||
}
|
||
}
|
||
|
||
export const fe4DetailHandlers = [
|
||
// GET /record/down/details 号码级明细分页+筛选(mobile 尾号模糊/task_no 精确/status/error_code 精确)
|
||
http.get('*/api/v1/record/down/details', ({ request }) => {
|
||
const url = new URL(request.url)
|
||
const page = Number(url.searchParams.get('page') ?? 1)
|
||
const pageSize = Number(url.searchParams.get('page_size') ?? 100)
|
||
const mobile = url.searchParams.get('mobile') ?? undefined
|
||
const taskNo = url.searchParams.get('task_no') ?? undefined
|
||
const status = url.searchParams.get('status')
|
||
const errorCode = url.searchParams.get('error_code') ?? undefined
|
||
const start = url.searchParams.get('start_time') ?? undefined
|
||
const end = url.searchParams.get('end_time') ?? undefined
|
||
let rows = [...downDetailDB]
|
||
if (taskNo) rows = rows.filter((r) => r.task_no === taskNo)
|
||
if (mobile) rows = rows.filter((r) => r.mobile.endsWith(mobile))
|
||
if (status != null && status !== '') rows = rows.filter((r) => r.status === Number(status))
|
||
if (errorCode) rows = rows.filter((r) => r.error_code === errorCode)
|
||
if (start) rows = rows.filter((r) => r.created_at >= start!)
|
||
if (end) rows = rows.filter((r) => r.created_at <= end!)
|
||
rows.sort((a, b) => (a.id < b.id ? 1 : -1))
|
||
const s = (page - 1) * pageSize
|
||
return ok({ list: rows.slice(s, s + pageSize), total: rows.length })
|
||
}),
|
||
|
||
// GET /record/down/details/:id/channel-logs CMPP/通道日志钻入
|
||
http.get('*/api/v1/record/down/details/:id/channel-logs', ({ params }) => {
|
||
const d = downDetailDB.find((r) => r.id === Number(params.id))
|
||
if (!d) return fail('ISC:404', '明细不存在', 404)
|
||
const list = channelLogs(d)
|
||
return ok({ total: list.length, list })
|
||
}),
|
||
]
|
||
|
||
// ---- D26 历史记录(>5 天冷数据;种子 6~20 天前,与热数据窗口天然隔离) ----
|
||
|
||
function daysAgo(n: number, hh = 10, mm = 0): string {
|
||
const d = new Date(Date.now() - n * 86400_000)
|
||
d.setHours(hh, mm, 0, 0)
|
||
const p = (v: number) => String(v).padStart(2, '0')
|
||
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`
|
||
}
|
||
|
||
// 种子:4 个历史批次×终态四态分布,时间 6/10/14/20 天前(跨 >5 天窗口)
|
||
function seedHistory(): HistoryRecord[] {
|
||
const rows: HistoryRecord[] = []
|
||
const tasks = [
|
||
{ tid: 901, no: 'T20260830001', day: 20 },
|
||
{ tid: 902, no: 'T20260903001', day: 14 },
|
||
{ tid: 903, no: 'T20260907001', day: 10 },
|
||
{ tid: 904, no: 'T20260910001', day: 6 },
|
||
]
|
||
// 终态分布轮转:成功/失败/未知/拦截(冷数据无进行中态)
|
||
const dist: Array<[number, string]> = [
|
||
[2, ''],
|
||
[3, 'ISC:EXPIRED'],
|
||
[2, ''],
|
||
[4, ''],
|
||
[2, ''],
|
||
[5, 'ISC:035'],
|
||
[2, ''],
|
||
[3, 'DELIV:0608'],
|
||
]
|
||
tasks.forEach((t, ti) => {
|
||
for (let i = 0; i < 24; i++) {
|
||
const [status, err] = dist[(ti * 3 + i) % dist.length]
|
||
rows.push({
|
||
id: t.tid * 10000 + i,
|
||
task_id: t.tid,
|
||
task_no: t.no,
|
||
mobile: `159${String(10000 + t.tid)}${String(i).padStart(4, '0')}`.slice(0, 11),
|
||
province: ['广东', '浙江', '江苏', '北京'][i % 4],
|
||
carrier: ['移动', '联通', '电信'][i % 3],
|
||
status,
|
||
error_code: err,
|
||
fee: status === 2 ? 2 : 0,
|
||
created_at: daysAgo(t.day, 9 + (i % 10), i % 60),
|
||
})
|
||
}
|
||
})
|
||
return rows
|
||
}
|
||
|
||
export const historyDB: HistoryRecord[] = seedHistory()
|
||
|
||
// 导出任务存储(模块级,下载中心轮询可见);预置 1 条 ready 任务首屏可验。
|
||
export const exportDB: ExportTask[] = [
|
||
{
|
||
id: 8801,
|
||
scope: 'task_no=T20260903001',
|
||
status: 'ready',
|
||
total: 24,
|
||
created_at: daysAgo(1, 15, 30),
|
||
expires_at: daysAgo(-1, 15, 30),
|
||
},
|
||
]
|
||
|
||
// 筛选条件→范围描述(导出任务快照,下载中心可辨识)
|
||
function scopeOf(q: Record<string, unknown>): string {
|
||
const parts: string[] = []
|
||
if (q.mobile) parts.push(`mobile=*${q.mobile}`)
|
||
if (q.task_no) parts.push(`task_no=${q.task_no}`)
|
||
if (q.status != null && q.status !== '') parts.push(`status=${q.status}`)
|
||
if (q.start_time || q.end_time) parts.push(`时间=${q.start_time ?? '…'}~${q.end_time ?? '…'}`)
|
||
return parts.length ? parts.join(' ') : '全部历史'
|
||
}
|
||
|
||
export const fe4HistoryHandlers = [
|
||
// GET /record/history 全量历史明细分页(时间范围为主筛选;mock 同 ClickHouse 分区裁剪语义)
|
||
http.get('*/api/v1/record/history', ({ request }) => {
|
||
const url = new URL(request.url)
|
||
const page = Number(url.searchParams.get('page') ?? 1)
|
||
const pageSize = Number(url.searchParams.get('page_size') ?? 100)
|
||
const mobile = url.searchParams.get('mobile') ?? undefined
|
||
const taskNo = url.searchParams.get('task_no') ?? undefined
|
||
const status = url.searchParams.get('status')
|
||
const start = url.searchParams.get('start_time') ?? undefined
|
||
const end = url.searchParams.get('end_time') ?? undefined
|
||
let rows = [...historyDB]
|
||
if (taskNo) rows = rows.filter((r) => r.task_no === taskNo)
|
||
if (mobile) rows = rows.filter((r) => r.mobile.endsWith(mobile))
|
||
if (status != null && status !== '') rows = rows.filter((r) => r.status === Number(status))
|
||
if (start) rows = rows.filter((r) => r.created_at >= start!)
|
||
if (end) rows = rows.filter((r) => r.created_at <= end!)
|
||
rows.sort((a, b) => (a.created_at < b.created_at ? 1 : -1))
|
||
const s = (page - 1) * pageSize
|
||
return ok({ list: rows.slice(s, s + pageSize), total: rows.length })
|
||
}),
|
||
|
||
// POST /record/history/export 提交异步导出(状态机 pending→ready;mock 创建 2s 后 ready)
|
||
http.post('*/api/v1/record/history/export', async ({ request }) => {
|
||
const body = (await request.json()) as Record<string, unknown>
|
||
const id = 8900 + exportDB.length
|
||
exportDB.unshift({
|
||
id,
|
||
scope: scopeOf(body),
|
||
status: 'pending',
|
||
total: 0,
|
||
created_at: new Date().toISOString().slice(0, 19).replace('T', ' '),
|
||
expires_at: daysAgo(-1),
|
||
})
|
||
return ok({ id })
|
||
}),
|
||
|
||
// GET /record/history/exports 下载中心:导出任务列表(pending 2s 后转 ready;过期隐藏)
|
||
http.get('*/api/v1/record/history/exports', () => {
|
||
const now = Date.now()
|
||
const list = exportDB
|
||
.map((t) => {
|
||
if (t.status === 'pending' && now - new Date(t.created_at.replace(' ', 'T') + 'Z').getTime() > 2000) {
|
||
return { ...t, status: 'ready' as const, total: 24 }
|
||
}
|
||
return t
|
||
})
|
||
.filter((t) => new Date(t.expires_at.replace(' ', 'T') + 'Z').getTime() > now)
|
||
return ok({ list })
|
||
}),
|
||
]
|
||
|
||
// ---- D27 上行记录(message_uplink 口径;15 天匹配窗口) ----
|
||
// 种子:3 码号×已匹配/未匹配/窗口外三态;recv_at 相对当前时间生成
|
||
// (15 天窗口提示口径:匹配窗口内下行才有 match_task_id,窗口外保留 0)。
|
||
export const uplinkDB: UplinkRecord[] = [
|
||
{ id: 1, dest_code: '10690331', src_mobile: '13750082528', content: 'TD退订', account_id: 2, account_name: '盈讯网络', channel_id: 1, recv_at: daysAgo(0, 9, 5), match_task_id: 910000000001 },
|
||
{ id: 2, dest_code: '10690331', src_mobile: '13811220001', content: '1', account_id: 2, account_name: '盈讯网络', channel_id: 1, recv_at: daysAgo(1, 14, 22), match_task_id: 910000000012 },
|
||
{ id: 3, dest_code: '1069055', src_mobile: '13933445566', content: '【回执】已收到', account_id: 3, account_name: '星辰科技', channel_id: 1, recv_at: daysAgo(2, 8, 41), match_task_id: 0 },
|
||
{ id: 4, dest_code: '1069055', src_mobile: '13566778899', content: 'R(拒收)', account_id: 3, account_name: '星辰科技', channel_id: 1, recv_at: daysAgo(3, 19, 3), match_task_id: 910000000007 },
|
||
// 15 天窗口外:无匹配(窗口提示主用例)
|
||
{ id: 5, dest_code: '10690331001', src_mobile: '13699887766', content: '你好', account_id: 2, account_name: '盈讯网络', channel_id: 1, recv_at: daysAgo(18, 10, 0), match_task_id: 0 },
|
||
{ id: 6, dest_code: '1069088', src_mobile: '13155667788', content: '查询余额', account_id: 4, account_name: '迅达物流', channel_id: 1, recv_at: daysAgo(4, 11, 30), match_task_id: 910000000003 },
|
||
]
|
||
|
||
// 上行筛选→范围描述(导出任务快照)
|
||
function uplinkScopeOf(q: Record<string, unknown>): string {
|
||
const parts: string[] = ['上行']
|
||
if (q.dest_code) parts.push(`码号=${q.dest_code}`)
|
||
if (q.src_mobile) parts.push(`手机号=*${q.src_mobile}`)
|
||
if (q.matched != null && q.matched !== '') parts.push(`匹配=${q.matched === 1 ? '已匹配' : '未匹配'}`)
|
||
if (q.start_time || q.end_time) parts.push(`时间=${q.start_time ?? '…'}~${q.end_time ?? '…'}`)
|
||
return parts.join(' ')
|
||
}
|
||
|
||
export const fe4UplinkHandlers = [
|
||
// GET /record/uplink 分页(码号前缀/手机号尾号模糊/匹配态/时间范围)
|
||
http.get('*/api/v1/record/uplink', ({ request }) => {
|
||
const url = new URL(request.url)
|
||
const page = Number(url.searchParams.get('page') ?? 1)
|
||
const pageSize = Number(url.searchParams.get('page_size') ?? 100)
|
||
const destCode = url.searchParams.get('dest_code') ?? undefined
|
||
const srcMobile = url.searchParams.get('src_mobile') ?? undefined
|
||
const matched = url.searchParams.get('matched')
|
||
const start = url.searchParams.get('start_time') ?? undefined
|
||
const end = url.searchParams.get('end_time') ?? undefined
|
||
let rows = [...uplinkDB]
|
||
if (destCode) rows = rows.filter((r) => r.dest_code.startsWith(destCode))
|
||
if (srcMobile) rows = rows.filter((r) => r.src_mobile.endsWith(srcMobile))
|
||
if (matched != null && matched !== '') rows = rows.filter((r) => (Number(matched) === 1 ? r.match_task_id > 0 : r.match_task_id === 0))
|
||
if (start) rows = rows.filter((r) => r.recv_at >= start!)
|
||
if (end) rows = rows.filter((r) => r.recv_at <= end!)
|
||
rows.sort((a, b) => (a.recv_at < b.recv_at ? 1 : -1))
|
||
const s = (page - 1) * pageSize
|
||
return ok({ list: rows.slice(s, s + pageSize), total: rows.length })
|
||
}),
|
||
|
||
// POST /record/uplink/export 异步导出(复用 D26 下载中心任务模型)
|
||
http.post('*/api/v1/record/uplink/export', async ({ request }) => {
|
||
const body = (await request.json()) as Record<string, unknown>
|
||
const id = 8900 + exportDB.length
|
||
exportDB.unshift({
|
||
id,
|
||
scope: uplinkScopeOf(body),
|
||
status: 'pending',
|
||
total: 0,
|
||
created_at: new Date().toISOString().slice(0, 19).replace('T', ' '),
|
||
expires_at: daysAgo(-1),
|
||
})
|
||
return ok({ id })
|
||
}),
|
||
]
|
||
|
||
// ---- D28 回执重推+手动配对(message_report 30 天窗口/挂起明细 3h 口径) ----
|
||
// 种子:回执 8 行 0.5~31 天前(三态×推送三态分布,含 29 天临界与 31 天窗口外);
|
||
// 挂起明细 6 行 4h~40h 前(>3h 可配对);pair 后 updated 行离场=列表回写语义。
|
||
export const repushDB: RepushReport[] = [
|
||
{ id: 1, detail_id: 920000000001, task_no: 'T20260913090001', mobile: '13800001001', account_id: 2, status: 'SUCCESS', raw_code: 'DELIVRD', error_code: '', report_at: daysAgo(0, 9, 12), push_status: 1, pushed_at: daysAgo(0, 9, 13) },
|
||
{ id: 2, detail_id: 920000000002, task_no: 'T20260913090001', mobile: '13800001002', account_id: 2, status: 'FAILURE', raw_code: 'GB:0001', error_code: 'GB:0001', report_at: daysAgo(1, 14, 40), push_status: 2, pushed_at: daysAgo(1, 14, 41) },
|
||
{ id: 3, detail_id: 920000000003, task_no: 'T20260912150007', mobile: '13800001003', account_id: 3, status: 'UNKNOWN', raw_code: 'EXPIRED', error_code: 'EXPIRED', report_at: daysAgo(2, 8, 5), push_status: 0, pushed_at: null },
|
||
{ id: 4, detail_id: 920000000004, task_no: 'T20260911130009', mobile: '13800001004', account_id: 2, status: 'SUCCESS', raw_code: 'DELIVRD', error_code: '', report_at: daysAgo(5, 16, 22), push_status: 1, pushed_at: daysAgo(5, 16, 23) },
|
||
{ id: 5, detail_id: 920000000005, task_no: 'T20260910120010', mobile: '13800001005', account_id: 3, status: 'FAILURE', raw_code: 'UNDELIV', error_code: 'UNDELIV', report_at: daysAgo(10, 11, 8), push_status: 2, pushed_at: daysAgo(10, 11, 9) },
|
||
{ id: 6, detail_id: 920000000006, task_no: 'T20260909100012', mobile: '13800001006', account_id: 4, status: 'SUCCESS', raw_code: 'DELIVRD', error_code: '', report_at: daysAgo(20, 13, 45), push_status: 0, pushed_at: null },
|
||
// 29 天临界:窗口内最后一格
|
||
{ id: 7, detail_id: 920000000007, task_no: 'T20260817090007', mobile: '13800001007', account_id: 2, status: 'UNKNOWN', raw_code: 'EXPIRED', error_code: 'EXPIRED', report_at: daysAgo(29, 10, 0), push_status: 0, pushed_at: null },
|
||
// 31 天:窗口外(重推超窗主用例种子)
|
||
{ id: 8, detail_id: 920000000008, task_no: 'T20260815090008', mobile: '13800001008', account_id: 2, status: 'SUCCESS', raw_code: 'DELIVRD', error_code: '', report_at: daysAgo(31, 10, 0), push_status: 1, pushed_at: daysAgo(31, 10, 1) },
|
||
]
|
||
|
||
// 重推任务受理记录(页面反馈+测试断言;推送执行属 Day58 后端 repush 域)
|
||
export const repushTaskDB: { id: number; count: number; created_at: string; start_time: string; end_time: string }[] = []
|
||
|
||
// 挂起明细种子(3h 外 status=1;age_hours 与 sent_at 一致,>3 为可配对)
|
||
export const pendingDB: PendingDetail[] = [
|
||
{ detail_id: 930000000001, task_no: 'T20260913090001', msg_id: '8a01f2', mobile: '13800002001', content: '【盈讯网络】您的验证码是 883221', sent_at: daysAgo(0, 5, 0), age_hours: 4 },
|
||
{ detail_id: 930000000002, task_no: 'T20260913090001', msg_id: '8a01f3', mobile: '13800002002', content: '【盈讯网络】您的验证码是 883222', sent_at: daysAgo(0, 3, 30), age_hours: 6 },
|
||
{ detail_id: 930000000003, task_no: 'T20260912150007', msg_id: '7b02c1', mobile: '13800002003', content: '【星辰科技】账单提醒:本月消费 299 元', sent_at: daysAgo(1, 9, 15), age_hours: 25 },
|
||
{ detail_id: 930000000004, task_no: 'T20260912150007', msg_id: '7b02c2', mobile: '13800002004', content: '【星辰科技】账单提醒:本月消费 399 元', sent_at: daysAgo(1, 9, 16), age_hours: 25 },
|
||
{ detail_id: 930000000005, task_no: 'T20260911130009', msg_id: '6c03d9', mobile: '13800002005', content: '【迅达物流】包裹已到驿站,凭码取件', sent_at: daysAgo(1, 20, 0), age_hours: 14 },
|
||
{ detail_id: 930000000006, task_no: 'T20260909100012', msg_id: '5d04e7', mobile: '13800002006', content: '【优选商城】您关注的商品已降价', sent_at: daysAgo(1, 22, 30), age_hours: 40 },
|
||
]
|
||
|
||
// 30 天窗口校验(mock 第二道闸;页面为第一道——disabled-picker+提交前校验)
|
||
function withinWindow(start?: string, end?: string): { ok: boolean; msg?: string } {
|
||
const DAY = 86400_000
|
||
const now = Date.now()
|
||
if (!start || !end) return { ok: false, msg: '时间窗必填' }
|
||
const s = new Date(start.replace(' ', 'T') + 'Z').getTime()
|
||
const e = new Date(end.replace(' ', 'T') + 'Z').getTime()
|
||
if (Number.isNaN(s) || Number.isNaN(e)) return { ok: false, msg: '时间格式非法' }
|
||
if (s > e) return { ok: false, msg: '开始时间晚于结束时间' }
|
||
if (now - s > 30 * DAY) return { ok: false, msg: '重推窗口最长 30 天:开始时间超出窗口' }
|
||
if (e - s > 30 * DAY) return { ok: false, msg: '重推窗口最长 30 天:时间跨度超限' }
|
||
return { ok: true }
|
||
}
|
||
|
||
export const fe4RepushHandlers = [
|
||
// GET /record/repush 回执分页(账号/尾号/任务号/状态/时间窗)
|
||
http.get('*/api/v1/record/repush', ({ request }) => {
|
||
const url = new URL(request.url)
|
||
const page = Number(url.searchParams.get('page') ?? 1)
|
||
const pageSize = Number(url.searchParams.get('page_size') ?? 100)
|
||
const account = url.searchParams.get('account_id')
|
||
const mobile = url.searchParams.get('mobile') ?? undefined
|
||
const taskNo = url.searchParams.get('task_no') ?? undefined
|
||
const status = url.searchParams.get('status') ?? undefined
|
||
const start = url.searchParams.get('start_time') ?? undefined
|
||
const end = url.searchParams.get('end_time') ?? undefined
|
||
let rows = [...repushDB]
|
||
if (account) rows = rows.filter((r) => r.account_id === Number(account))
|
||
if (mobile) rows = rows.filter((r) => r.mobile.endsWith(mobile!))
|
||
if (taskNo) rows = rows.filter((r) => r.task_no === taskNo)
|
||
if (status) rows = rows.filter((r) => r.status === status)
|
||
if (start) rows = rows.filter((r) => r.report_at >= start!)
|
||
if (end) rows = rows.filter((r) => r.report_at <= end!)
|
||
rows.sort((a, b) => (a.report_at < b.report_at ? 1 : -1))
|
||
const s = (page - 1) * pageSize
|
||
return ok({ list: rows.slice(s, s + pageSize), total: rows.length })
|
||
}),
|
||
|
||
// POST /record/repush 重推受理(30 天窗口强校验=第二道闸;count=窗内匹配数)
|
||
http.post('*/api/v1/record/repush', async ({ request }) => {
|
||
const body = (await request.json()) as Record<string, unknown>
|
||
const w = withinWindow(body.start_time as string, body.end_time as string)
|
||
if (!w.ok) return fail('ISC:400', w.msg!, 400)
|
||
const account = body.account_id as number | undefined
|
||
const mobiles = (body.mobiles as string[] | undefined) ?? []
|
||
const start = body.start_time as string
|
||
const end = body.end_time as string
|
||
const hit = repushDB.filter(
|
||
(r) =>
|
||
r.report_at >= start &&
|
||
r.report_at <= end &&
|
||
(!account || r.account_id === account) &&
|
||
(mobiles.length === 0 || mobiles.includes(r.mobile)),
|
||
)
|
||
const id = 8800 + repushTaskDB.length + 1
|
||
repushTaskDB.unshift({
|
||
id,
|
||
count: hit.length,
|
||
created_at: new Date().toISOString().slice(0, 19).replace('T', ' '),
|
||
start_time: start,
|
||
end_time: end,
|
||
})
|
||
return ok({ task_id: id, count: hit.length })
|
||
}),
|
||
|
||
// GET /record/repush/pending 挂起明细分页(3h 外;pair 后 updated 行已离场)
|
||
http.get('*/api/v1/record/repush/pending', ({ request }) => {
|
||
const url = new URL(request.url)
|
||
const page = Number(url.searchParams.get('page') ?? 1)
|
||
const pageSize = Number(url.searchParams.get('page_size') ?? 100)
|
||
const mobile = url.searchParams.get('mobile') ?? undefined
|
||
const taskNo = url.searchParams.get('task_no') ?? undefined
|
||
let rows = [...pendingDB]
|
||
if (mobile) rows = rows.filter((r) => r.mobile.endsWith(mobile!))
|
||
if (taskNo) rows = rows.filter((r) => r.task_no === taskNo)
|
||
rows.sort((a, b) => (a.age_hours < b.age_hours ? 1 : -1))
|
||
const s = (page - 1) * pageSize
|
||
return ok({ list: rows.slice(s, s + pageSize), total: rows.length })
|
||
}),
|
||
|
||
// POST /record/repush/pair 手动配对(四方式圈中→指定终态回写;updated 行离场)
|
||
http.post('*/api/v1/record/repush/pair', async ({ request }) => {
|
||
const body = (await request.json()) as Record<string, unknown>
|
||
const mode = body.mode as string
|
||
const target = Number(body.target_status)
|
||
if (!['msg_mobile', 'detail', 'task_time', 'time_mobile'].includes(mode)) {
|
||
return fail('ISC:400', '配对方式非法', 400)
|
||
}
|
||
if (![2, 3, 4].includes(target)) return fail('ISC:400', '目标状态须为 2/3/4', 400)
|
||
const start = body.start_time as string | undefined
|
||
const end = body.end_time as string | undefined
|
||
let matched: PendingDetail[]
|
||
switch (mode) {
|
||
case 'msg_mobile':
|
||
matched = pendingDB.filter((r) => r.msg_id === body.msg_id && r.mobile === body.mobile)
|
||
break
|
||
case 'detail':
|
||
matched = pendingDB.filter((r) => r.detail_id === Number(body.detail_id))
|
||
break
|
||
case 'task_time':
|
||
matched = pendingDB.filter(
|
||
(r) => r.task_no === body.task_no && (!start || r.sent_at >= start!) && (!end || r.sent_at <= end!),
|
||
)
|
||
break
|
||
default: // time_mobile
|
||
matched = pendingDB.filter(
|
||
(r) => r.mobile === body.mobile && (!start || r.sent_at >= start!) && (!end || r.sent_at <= end!),
|
||
)
|
||
}
|
||
// 回写:终态行离场(mock 语义;真实后端 detail 终态锁定+计费确认同 Day57 口径)
|
||
for (const row of matched) {
|
||
const i = pendingDB.indexOf(row)
|
||
if (i >= 0) pendingDB.splice(i, 1)
|
||
}
|
||
return ok({ matched: matched.length, updated: matched.length, skipped: 0 })
|
||
}),
|
||
]
|
||
|
||
// ---- D24-25 重构(2026-09-14):短信下行明细 v2(/sms/downlink-details) ----
|
||
// 种子口径:3 账号(264305/264306 挂 100001 树,264307 挂 100002 旁支——上级账号
|
||
// 筛选可证伪)×2 通道码号(10690331+ext/10659+ext)×六态分布(成功×2 含长短信
|
||
// fee=2/失败×2 原始码 FD:047+GB:0001/拦截/在途/待发)×双来源(API/CMPP);
|
||
// 时间=今天 4 组(默认当天窗口非空)+1/3/4 天前(5 天窗口语义);耗时=回执-发送。
|
||
|
||
const DL_ACCOUNTS = [
|
||
{ id: 264305, name: '264305', sub: '广东移动-行业-壹通道', parent: 100001 },
|
||
{ id: 264306, name: '264306', sub: '江苏联通-营销-贰通道', parent: 100001 },
|
||
{ id: 264307, name: '264307', sub: '北京电信-验证码通道', parent: 100002 },
|
||
]
|
||
const DL_CHANNELS = [
|
||
{ id: 1, code: '10690331', name: '移动壹通道', ext: '6108' },
|
||
{ id: 2, code: '10659', name: '联通贰通道', ext: '0117' },
|
||
]
|
||
|
||
/** [status, rawCode(通道原始码), errorCode(转义码), fee(计费条数)]。 */
|
||
const DL_STATES: Array<[number, string, string, number]> = [
|
||
[2, 'DELIVRD', '', 1],
|
||
[2, 'DELIVRD', '', 2], // 长短信:计费 2 条(≠段数,Day52 定论)
|
||
[3, 'FD:047', 'ISC:012', 1],
|
||
[3, 'GB:0001', 'ISC:027', 1],
|
||
[5, '', 'ISC:031', 0], // 拦截未投通道
|
||
[1, '', '', 1], // 已提交在途(有 send 无 report)
|
||
[0, '', '', 1], // 待发(未投通道)
|
||
]
|
||
|
||
function dlFmt(d: Date): string {
|
||
const p = (v: number) => String(v).padStart(2, '0')
|
||
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`
|
||
}
|
||
|
||
function dlSeed(): DownlinkDetailRow[] {
|
||
const rows: DownlinkDetailRow[] = []
|
||
const groups = [0, 1, 2, 3, 1, 3, 4].map((day, idx) => ({ day, batch: 829289401 + idx, g: idx }))
|
||
for (const { day, batch, g } of groups) {
|
||
for (let i = 0; i < 6; i++) {
|
||
const [status, raw, err, fee] = DL_STATES[(g * 3 + i) % DL_STATES.length]
|
||
const acc = DL_ACCOUNTS[(g + i) % DL_ACCOUNTS.length]
|
||
const ch = DL_CHANNELS[(g + i) % DL_CHANNELS.length]
|
||
const created = day === 0 ? dlFmt(new Date(Date.now() - (5 + g * 37 + i * 3) * 60_000)) : daysAgo(day, 9 + i, g * 10)
|
||
const invested = status !== 0 && status !== 5 // 待发/拦截未投通道
|
||
const sendTs = new Date(created.replace(/-/g, '/')).getTime() + 1_000
|
||
const send = invested ? dlFmt(new Date(sendTs)) : null
|
||
const duration = status === 2 ? 4 : status === 3 ? 2 : null
|
||
const report = duration != null && send ? dlFmt(new Date(sendTs + duration * 1_000)) : null
|
||
rows.push({
|
||
batch_id: batch,
|
||
task_no: `T2026${batch}`,
|
||
account_id: acc.id,
|
||
account_name: acc.name,
|
||
account_sub: acc.sub,
|
||
parent_account_id: acc.parent,
|
||
channel_id: ch.id,
|
||
channel_code: ch.code,
|
||
channel_name: ch.name,
|
||
channel_number: ch.code + ch.ext,
|
||
ext_code: ch.ext,
|
||
mobile: `13${(acc.id + g) % 3}${String(7000 + i * 3 + g).padStart(4, '0')}${String((batch % 10000) + i).padStart(4, '0')}`.slice(0, 11),
|
||
carrier: CARRIERS[(g + i) % CARRIERS.length],
|
||
province: PROVINCES[(g + i) % PROVINCES.length],
|
||
content: fee === 2
|
||
? '【盈讯网络】尊敬的客户,您本月账单已生成,长短信内容跨多条计费示例,请登录营业厅查看消费明细并核对套餐余量,如有疑问请联系客服热线 951050 转人工服务,感谢您的理解与支持。'
|
||
: '【盈讯网络】您的验证码是 8837,5 分钟内有效。',
|
||
created_at: created,
|
||
send_at: send,
|
||
report_at: report,
|
||
fee,
|
||
duration_s: duration,
|
||
status,
|
||
raw_code: raw,
|
||
error_code: err,
|
||
source: (g + i) % 2 + 1,
|
||
call_data: i % 2 === 0 ? `cd-${batch}-${i}` : '',
|
||
})
|
||
}
|
||
}
|
||
return rows
|
||
}
|
||
|
||
export const downlinkDB: DownlinkDetailRow[] = dlSeed()
|
||
|
||
export const smsDownlinkHandlers = [
|
||
// GET /sms/downlink-details 三行筛选全语义+分页(默认 10/页)
|
||
http.get('*/api/v1/sms/downlink-details', ({ request }) => {
|
||
const p = new URL(request.url)
|
||
const num = (k: string) => {
|
||
const v = p.searchParams.get(k)
|
||
return v != null && v !== '' ? Number(v) : undefined
|
||
}
|
||
const str = (k: string) => p.searchParams.get(k) ?? undefined
|
||
let rows = [...downlinkDB]
|
||
const batch = str('batch_id')
|
||
if (batch) rows = rows.filter((r) => String(r.batch_id) === batch || r.task_no === batch)
|
||
const acc = num('account_id')
|
||
if (acc) rows = rows.filter((r) => r.account_id === acc)
|
||
const parent = num('parent_account_id')
|
||
if (parent) rows = rows.filter((r) => r.parent_account_id === parent)
|
||
const mobile = str('mobile')
|
||
if (mobile) rows = rows.filter((r) => r.mobile.includes(mobile))
|
||
const content = str('content')
|
||
if (content) rows = rows.filter((r) => r.content.includes(content))
|
||
const feeOp = str('fee_op') ?? '>='
|
||
const fee = num('fee')
|
||
if (fee != null) rows = rows.filter((r) => (feeOp === '=' ? r.fee === fee : feeOp === '<=' ? r.fee <= fee : r.fee >= fee))
|
||
const source = num('source')
|
||
if (source) rows = rows.filter((r) => r.source === source)
|
||
const carrier = str('carrier')
|
||
if (carrier) rows = rows.filter((r) => r.carrier === carrier)
|
||
const province = str('province')
|
||
if (province) rows = rows.filter((r) => r.province === province)
|
||
const ch = num('channel_id')
|
||
if (ch) rows = rows.filter((r) => r.channel_id === ch)
|
||
const status = num('status')
|
||
if (status != null) rows = rows.filter((r) => r.status === status)
|
||
const cn = str('channel_number')
|
||
if (cn) {
|
||
const pref = cn.replace(/\*+$/, '') // 10659* / 10659 均前缀匹配
|
||
rows = rows.filter((r) => r.channel_number.startsWith(pref))
|
||
}
|
||
const cd = str('call_data')
|
||
if (cd) rows = rows.filter((r) => r.call_data.includes(cd))
|
||
const raw = str('raw_code')
|
||
if (raw) rows = rows.filter((r) => r.raw_code === raw)
|
||
const raws = [...p.searchParams.getAll('raw_codes'), ...p.searchParams.getAll('raw_codes[]')]
|
||
if (raws.length) rows = rows.filter((r) => raws.includes(r.raw_code))
|
||
const dur = num('duration_s')
|
||
if (dur != null) rows = rows.filter((r) => r.duration_s != null && r.duration_s >= dur)
|
||
const st = str('start_time')
|
||
if (st) rows = rows.filter((r) => r.created_at >= st!)
|
||
const en = str('end_time')
|
||
if (en) rows = rows.filter((r) => r.created_at <= en!)
|
||
rows.sort((a, b) => (a.created_at < b.created_at ? 1 : -1))
|
||
const page = num('page') ?? 1
|
||
const ps = num('page_size') ?? 10
|
||
const s0 = (page - 1) * ps
|
||
return ok({ list: rows.slice(s0, s0 + ps), total: rows.length })
|
||
}),
|
||
]
|
||
|