225 lines
14 KiB
TypeScript
225 lines
14 KiB
TypeScript
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/ban-ts-comment */
|
|
/** FE-2 Mock handlers:通道/上家/通道组/策略。数据源 fe2-data.ts。 */
|
|
import { http, HttpResponse } from 'msw'
|
|
import {
|
|
upstreamDB, channelDB, groupDB, groupItemDB,
|
|
blacklistDB, whitelistDB, keywordDB, freqDB, blockedCodeDB, routeRuleDB,
|
|
ids, now,
|
|
} from './fe2-data'
|
|
|
|
|
|
const ok = (data: unknown) => HttpResponse.json({ code: 'ISC:000', msg: '成功', data })
|
|
const fail = (code: string, msg: string, status?: number) => HttpResponse.json({ code, msg, data: null as never }, { status })
|
|
|
|
export const fe2Handlers = [
|
|
http.get('*/api/v1/channels/upstreams', ({ request }) => {
|
|
const url = new URL(request.url)
|
|
const name = url.searchParams.get('name') ?? ''
|
|
const rows = upstreamDB.filter((u) => !name || u.name.includes(name))
|
|
return ok({ list: rows, total: rows.length })
|
|
}),
|
|
http.post('*/api/v1/channels/upstreams', async ({ request }) => {
|
|
const body = (await request.json()) as Record<string, unknown>
|
|
if (!body.name) return fail('ISC:400', '名称不能为空')
|
|
const u = { id: ids.upstream++, name: String(body.name), contact: String(body.contact ?? ''), phone: String(body.phone ?? ''), category: String(body.category ?? ''), status: 1, remark: String(body.remark ?? ''), created_at: now() }
|
|
upstreamDB.push(u)
|
|
return ok(u)
|
|
}),
|
|
http.put('*/api/v1/channels/upstreams/:id', async ({ params, request }) => {
|
|
const u = upstreamDB.find((x) => x.id === Number(params.id))
|
|
if (!u) return fail('ISC:404', '不存在', 404)
|
|
Object.assign(u, await request.json())
|
|
return ok(u)
|
|
}),
|
|
http.delete('*/api/v1/channels/upstreams/:id', ({ params }) => {
|
|
const idx = upstreamDB.findIndex((x) => x.id === Number(params.id))
|
|
if (idx < 0) return fail('ISC:404', '不存在', 404)
|
|
if (channelDB.some((c) => c.upstream_id === upstreamDB[idx].id)) return fail('ISC:030', '该上家下有通道,不可删除')
|
|
upstreamDB.splice(idx, 1)
|
|
return ok({ id: Number(params.id) })
|
|
}),
|
|
http.get('*/api/v1/channels', ({ request }) => {
|
|
const url = new URL(request.url)
|
|
const rows = channelDB.filter((c) => {
|
|
const p = url.searchParams.get('protocol') ?? ''
|
|
const s = url.searchParams.get('status') ?? ''
|
|
const n = url.searchParams.get('name') ?? ''
|
|
return (!p || c.protocol === p) && (!s || String(c.status) === s) && (!n || c.name.includes(n))
|
|
})
|
|
return ok({ list: rows, total: rows.length })
|
|
}),
|
|
http.post('*/api/v1/channels', async ({ request }) => {
|
|
const body = (await request.json()) as Record<string, unknown>
|
|
if (!body.name || !body.code || !body.protocol) return fail('ISC:400', 'name/code/protocol 必填')
|
|
if (channelDB.some((c) => c.code === body.code)) return fail('ISC:400', `通道号 ${body.code} 已存在`)
|
|
const c = { id: ids.channel++, name: String(body.name), code: String(body.code), protocol: String(body.protocol), upstream_id: Number(body.upstream_id ?? 0), host: String(body.host ?? ''), port: Number(body.port ?? 0), username: String(body.username ?? ''), speed: Number(body.speed ?? 100), conn_count: Number(body.conn_count ?? 1), status: 1, is_default: Number(body.is_default ?? 0), priority: Number(body.priority ?? 0), weight: Number(body.weight ?? 1), day_limit: Number(body.day_limit ?? 0), night_limit: Number(body.night_limit ?? 0), sign_mode: 0, ext_length: 0, black_keywords: String(body.black_keywords ?? ''), block_regions: String(body.block_regions ?? ''), template_mode: 0, remark: '', created_at: now() }
|
|
channelDB.push(c)
|
|
return ok(c)
|
|
}),
|
|
http.put('*/api/v1/channels/:id', async ({ params, request }) => {
|
|
const c = channelDB.find((x) => x.id === Number(params.id))
|
|
if (!c) return fail('ISC:404', '不存在', 404)
|
|
Object.assign(c, await request.json())
|
|
return ok(c)
|
|
}),
|
|
http.delete('*/api/v1/channels/:id', ({ params }) => {
|
|
const idx = channelDB.findIndex((x) => x.id === Number(params.id))
|
|
if (idx < 0) return fail('ISC:404', '不存在', 404)
|
|
channelDB.splice(idx, 1)
|
|
return ok({ id: Number(params.id) })
|
|
}),
|
|
http.post('*/api/v1/channels/:id/test', async ({ params }) => {
|
|
const c = channelDB.find((x) => x.id === Number(params.id))
|
|
if (!c) return fail('ISC:404', '不存在', 404)
|
|
await new Promise((r) => setTimeout(r, 300 + Math.random() * 500))
|
|
return ok({ ok: true, latency_ms: Math.floor(20 + Math.random() * 80), message: `${c.protocol.toUpperCase()} 连接成功` })
|
|
}),
|
|
http.get('*/api/v1/channels/:id/link-status', ({ params }) => {
|
|
const c = channelDB.find((x) => x.id === Number(params.id))
|
|
if (!c) return fail('ISC:404', '不存在', 404)
|
|
if (c.protocol !== 'cmpp' || c.status !== 1) return ok({ channel_id: c.id, running: false, total: 0, healthy: 0 })
|
|
return ok({ channel_id: c.id, running: true, total: 2, healthy: 2, started_at: new Date(Date.now() - 7200_000).toISOString().slice(0, 19).replace('T', ' ') })
|
|
}),
|
|
http.get('*/api/v1/channels/:id/receipts', ({ params }) => {
|
|
const c = channelDB.find((x) => x.id === Number(params.id))
|
|
if (!c) return fail('ISC:404', '不存在', 404)
|
|
const t = (off: number) => new Date(Date.now() - off * 60_000).toISOString().slice(0, 19).replace('T', ' ')
|
|
const list = Array.from({ length: 5 }, (_, i) =>
|
|
i % 3 === 2
|
|
? { channel_id: c.id, msg_id: `9${i}${Date.now() % 100}`, is_report: false, src: `1370000${String(i).padStart(4, '0')}`, content: 'TD', at: t(i + 1) }
|
|
: { channel_id: c.id, msg_id: `9${i}${Date.now() % 100}`, is_report: true, stat: i % 2 ? 'UNDELIV' : 'DELIVRD', submit_time: '0910113021', done_time: '0910113029', dest: `1380000${String(i).padStart(4, '0')}`, at: t(i + 1) },
|
|
)
|
|
return ok({ channel_id: c.id, total: list.length, list })
|
|
}),
|
|
http.get('*/api/v1/channel-groups', () => {
|
|
const groups = groupDB.map((g) => ({ ...g, items: groupItemDB.filter((i) => i.group_id === g.id).map((i) => ({ ...i, channel_name: channelDB.find((c) => c.id === i.channel_id)?.name ?? `通道#${i.channel_id}` })) }))
|
|
return ok({ list: groups, total: groups.length })
|
|
}),
|
|
http.post('*/api/v1/channel-groups', async ({ request }) => {
|
|
const body = (await request.json()) as { name: string; remark?: string }
|
|
if (!body.name) return fail('ISC:400', '名称不能为空')
|
|
groupDB.push({ id: ids.group++, name: body.name, remark: body.remark ?? '', status: 1, created_at: now() })
|
|
return ok(groupDB[groupDB.length - 1])
|
|
}),
|
|
http.delete('*/api/v1/channel-groups/:id', ({ params }) => {
|
|
const gid = Number(params.id)
|
|
const idx = groupDB.findIndex((g) => g.id === gid)
|
|
if (idx < 0) return fail('ISC:404', '不存在', 404)
|
|
groupDB.splice(idx, 1)
|
|
for (let i = groupItemDB.length - 1; i >= 0; i--) { if (groupItemDB[i].group_id === gid) groupItemDB.splice(i, 1) }
|
|
return ok({ id: gid })
|
|
}),
|
|
http.post('*/api/v1/channel-groups/:gid/items', async ({ params, request }) => {
|
|
const body = (await request.json()) as Record<string, unknown>
|
|
if (!body.channel_id) return fail('ISC:400', 'channel_id 必填')
|
|
groupItemDB.push({ id: ids.item++, group_id: Number(params.gid), channel_id: Number(body.channel_id), step: Number(body.step ?? 3) as 1 | 2 | 3 | 4, priority: Number(body.priority ?? 0), weight: Number(body.weight ?? 1), region: String(body.region ?? ''), carrier: String(body.carrier ?? ''), keyword: String(body.keyword ?? '') })
|
|
return ok(groupItemDB[groupItemDB.length - 1])
|
|
}),
|
|
http.put('*/api/v1/channel-groups/:gid/items/:iid', async ({ params, request }) => {
|
|
const item = groupItemDB.find((i) => i.group_id === Number(params.gid) && i.id === Number(params.iid))
|
|
if (!item) return fail('ISC:404', '不存在', 404)
|
|
Object.assign(item, await request.json())
|
|
return ok(item)
|
|
}),
|
|
http.delete('*/api/v1/channel-groups/:gid/items/:iid', ({ params }) => {
|
|
const idx = groupItemDB.findIndex((i) => i.group_id === Number(params.gid) && i.id === Number(params.iid))
|
|
if (idx < 0) return fail('ISC:404', '不存在', 404)
|
|
groupItemDB.splice(idx, 1)
|
|
return ok({ id: Number(params.iid) })
|
|
}),
|
|
http.get('*/api/v1/policies/blacklist', ({ request }) => {
|
|
const url = new URL(request.url)
|
|
const m = url.searchParams.get('mobile') ?? ''
|
|
const rows = blacklistDB.filter((b) => !m || b.mobile.includes(m))
|
|
return ok({ list: rows, total: rows.length })
|
|
}),
|
|
http.post('*/api/v1/policies/blacklist', async ({ request }) => {
|
|
const body = (await request.json()) as { mobile: string }
|
|
if (!body.mobile) return fail('ISC:400', '手机号不能为空')
|
|
if (blacklistDB.some((b) => b.mobile === body.mobile)) return fail('ISC:400', '已在黑名单')
|
|
blacklistDB.push({ id: ids.black++, mobile: body.mobile, account_id: 0, source: 1, operator: 'admin', created_at: now() })
|
|
return ok(blacklistDB[blacklistDB.length - 1])
|
|
}),
|
|
http.delete('*/api/v1/policies/blacklist/:id', ({ params }) => {
|
|
const idx = blacklistDB.findIndex((b) => b.id === Number(params.id))
|
|
if (idx < 0) return fail('ISC:404', '不存在', 404)
|
|
blacklistDB.splice(idx, 1)
|
|
return ok({ id: Number(params.id) })
|
|
}),
|
|
http.post('*/api/v1/policies/blacklist/import', async ({ request }) => {
|
|
const body = (await request.json()) as { mobiles: string[] }
|
|
let imported = 0
|
|
for (const m of body.mobiles) {
|
|
if (!blacklistDB.some((b) => b.mobile === m)) { blacklistDB.push({ id: ids.black++, mobile: m, account_id: 0, source: 1, operator: 'admin', created_at: now() }); imported++ }
|
|
}
|
|
return ok({ imported })
|
|
}),
|
|
http.get('*/api/v1/policies/whitelist', () => ok({ list: whitelistDB, total: whitelistDB.length })),
|
|
http.post('*/api/v1/policies/whitelist', async ({ request }) => {
|
|
const body = (await request.json()) as { mobile: string }
|
|
whitelistDB.push({ id: ids.white++, mobile: body.mobile, account_id: 0, source: 1, operator: 'admin', created_at: now() })
|
|
return ok(whitelistDB[whitelistDB.length - 1])
|
|
}),
|
|
http.delete('*/api/v1/policies/whitelist/:id', ({ params }) => {
|
|
whitelistDB.splice(whitelistDB.findIndex((b) => b.id === Number(params.id)), 1)
|
|
return ok({ id: Number(params.id) })
|
|
}),
|
|
http.get('*/api/v1/policies/keywords', ({ request }) => {
|
|
const url = new URL(request.url)
|
|
const l = url.searchParams.get('level') ?? ''
|
|
const rows = keywordDB.filter((k) => !l || String(k.level) === l)
|
|
return ok({ list: rows, total: rows.length })
|
|
}),
|
|
http.post('*/api/v1/policies/keywords', async ({ request }) => {
|
|
const body = (await request.json()) as { word: string; level: number }
|
|
keywordDB.push({ id: ids.kw++, word: body.word, level: body.level as 1 | 2 | 3, status: 1, operator: 'admin', created_at: now() })
|
|
return ok(keywordDB[keywordDB.length - 1])
|
|
}),
|
|
http.delete('*/api/v1/policies/keywords/:id', ({ params }) => {
|
|
keywordDB.splice(keywordDB.findIndex((k) => k.id === Number(params.id)), 1)
|
|
return ok({ id: Number(params.id) })
|
|
}),
|
|
http.get('*/api/v1/policies/frequencies', () => ok({ list: freqDB, total: freqDB.length })),
|
|
http.post('*/api/v1/policies/frequencies', async ({ request }) => {
|
|
const body = (await request.json()) as Record<string, unknown>
|
|
freqDB.push({ id: ids.freq++, account_id: Number(body.account_id ?? 0), dim: (body.dim ?? 1) as 1 | 2, window_unit: (body.window_unit ?? 1) as 1 | 2, max_count: Number(body.max_count ?? 1), status: 1, created_at: now() } as (typeof freqDB)[number])
|
|
return ok(freqDB[freqDB.length - 1])
|
|
}),
|
|
http.put('*/api/v1/policies/frequencies/:id', async ({ params, request }) => {
|
|
const f = freqDB.find((x) => x.id === Number(params.id))
|
|
if (!f) return fail('ISC:404', '不存在', 404)
|
|
Object.assign(f, await request.json())
|
|
return ok(f)
|
|
}),
|
|
http.get('*/api/v1/policies/blocked-codes', () => ok({ list: blockedCodeDB, total: blockedCodeDB.length })),
|
|
http.post('*/api/v1/policies/blocked-codes', async ({ request }) => {
|
|
const body = (await request.json()) as { channel_id: number; code: string }
|
|
blockedCodeDB.push({ id: ids.code++, channel_id: body.channel_id, code: body.code, remark: '', created_at: now() })
|
|
return ok(blockedCodeDB[blockedCodeDB.length - 1])
|
|
}),
|
|
http.delete('*/api/v1/policies/blocked-codes/:id', ({ params }) => {
|
|
blockedCodeDB.splice(blockedCodeDB.findIndex((b) => b.id === Number(params.id)), 1)
|
|
return ok({ id: Number(params.id) })
|
|
}),
|
|
http.get('*/api/v1/policies/route-rules', () => {
|
|
const rules = routeRuleDB.map((r) => ({ ...r, channel_name: channelDB.find((c) => c.id === r.channel_id)?.name ?? `通道#${r.channel_id}` }))
|
|
return ok({ list: rules, total: rules.length })
|
|
}),
|
|
http.post('*/api/v1/policies/route-rules', async ({ request }) => {
|
|
const body = (await request.json()) as Record<string, unknown>
|
|
if (!body.rule_type || !body.rule_config || !body.channel_id) return fail('ISC:400', '类型/配置/通道必填')
|
|
routeRuleDB.push({ id: ids.rule++, account_id: Number(body.account_id ?? 0), channel_id: Number(body.channel_id), rule_type: String(body.rule_type) as (typeof routeRuleDB)[number]['rule_type'], rule_config: String(body.rule_config), priority: Number(body.priority ?? 0), status: 1, created_at: now() })
|
|
return ok(routeRuleDB[routeRuleDB.length - 1])
|
|
}),
|
|
http.put('*/api/v1/policies/route-rules/:id', async ({ params, request }) => {
|
|
const r = routeRuleDB.find((x) => x.id === Number(params.id))
|
|
if (!r) return fail('ISC:404', '不存在', 404)
|
|
Object.assign(r, await request.json())
|
|
return ok(r)
|
|
}),
|
|
http.delete('*/api/v1/policies/route-rules/:id', ({ params }) => {
|
|
routeRuleDB.splice(routeRuleDB.findIndex((r) => r.id === Number(params.id)), 1)
|
|
return ok({ id: Number(params.id) })
|
|
}),
|
|
]
|