feat(后管): 密钥管理拆两列显示余额(LLM 人民币余额/代理余量),实时查不落库
按拍板:数据源=实时查上游不落库(⇒ 本窗零 Flyway、零 Schema);列形态=拆两列,不是一列两行—— 两张货源字典本就互不相通(LLM 行为渠道 longtaitou/t8star,代理行为供应商 qingguo/jikip),合成一格 就是 11-02 那次"代理行贴出龙抬头"的串档形状重来一遍(铁律 25)。不加 v-if="isSuperAdmin":主管必须 能解释组员任务为什么挂欠费,鉴权维持 requireAdmin + DataReadScope。 - src/api/user-secrets.ts:fetchAdminSecretBalances(userIds, companyId?),POST /api/admin/user-secrets/balances, 超时放宽到 30s(服务端整请求预算 10s,客户端必须比它长,否则拿到的是本地掐断而不是"上游未应答")。 - user-secret-model.ts:llmBalanceLines/proxyBalanceLine 纯函数——**两张字典各查各的**、跨域一律 —、 未知渠道只出 — 不出钱;available=false 时把 message 挂进 title 而不是画成 0;同一账号两个模块归到 同一个渠道名下只列一次钱。保持零 @/ 别名以便 node --test 直测。 - UserSecretsPage.vue:两个 <th>(LLM 余额 170px/代理余量 160px,放「更新时间」前),两个 <td> 首屏画 —/查询中, 列表返回后异步填(不阻塞首屏);colspan 由 isSuperAdmin ? 10 : 9 改成 12 : 11(两处同改,892d88ed 的提交信息 专门写过这句);表格 min-width 相应 +240px;「刷新余额」按钮按 BALANCE_BATCH=20 分批请求。 - 已核对 ops-cell-must-wrap.test.ts:它的 opsColDecl() 正则锚在表头文字「操作」上,新增 <th> 排在它之前不影响 UserSecretsPage: 314 下限 ⇒ 那张表不改(实测绿)。 - mock-admin-server.mjs 补两列夹具(一条有数、一条全查不到)供 dev/e2e。 测试:tests/密钥余额列读数.test.ts 12 例(跨域不认、坏值不画 0)+ tests/mount/user-secret-balance-columns.mount.test.ts 9 例——按表头文字定位不靠列序数,并钉 <th> 数=<td> 数= 空行 colspan(超管与非超管两种形状各一遍),把"加列忘改 colspan"直接钉死。既有 status-band 挂载格的夹具 mock 补一个 fetchAdminSecretBalances 函数名(页面挂载即取余额,缺它整个模块 mock 会炸),不是判据改动。 刻意不新增 readSource 钉 ⇒ 不动 tests/baselines/source-pin-ledger.json。vue-tsc rc=0、node --test 1890 例、vitest 77 例。
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* 密钥管理页的纯逻辑模型(货源标签 + 代理重填 + 检测结果档位)。
|
||||
*
|
||||
* 三条收口都在这个文件里,页模板只调这里导出的函数,不各写各的字典:
|
||||
* 四条收口都在这个文件里,页模板只调这里导出的函数,不各写各的字典:
|
||||
*
|
||||
* 1. **货源标签按模块分字典**(2026-11-02 修):原先 `UserSecretsPage.vue` 里是一整张
|
||||
* `sourceLabel()`,把 LLM 渠道(longtaitou/t8star)与代理供应商(qingguo/jikip)混在同一个
|
||||
@@ -312,3 +312,152 @@ export function buildProxyEntryPayload(
|
||||
}
|
||||
return { value: String(draft.link || '').trim(), channel: 'jikip' }
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ *
|
||||
* 余额侧写(2026-11-05 B83):第四处收口,仍守「两张字典互不相通」那一条。
|
||||
*
|
||||
* LLM 那格的单位是**钱**(人民币=龙抬头站内 ⚡,后端只取 `credits` 块),
|
||||
* 代理那格的 `surplus` 是**次数**、`scope` 说这份量属于谁的池子。
|
||||
* 两者都叫「余额」,混用一张函数就会读出「极客 ¥34」这种既不像钱也不像次的数——
|
||||
* 所以这里各写各的,跨域字段一个都不读,认不出来一律 `—`(同货源标签那条判据)。
|
||||
* ------------------------------------------------------------------ */
|
||||
|
||||
/** 后端 credits 块登记的单位;不是这个值就说明口径换了,宁可不显示也不猜。 */
|
||||
export const LLM_BALANCE_UNIT = 'CREDIT'
|
||||
|
||||
interface LlmBalanceLike {
|
||||
moduleKey?: string | null
|
||||
moduleLabel?: string | null
|
||||
channel?: string | null
|
||||
channelLabel?: string | null
|
||||
available?: boolean | null
|
||||
balance?: string | null
|
||||
availableBalance?: string | null
|
||||
frozen?: string | null
|
||||
unit?: string | null
|
||||
message?: string | null
|
||||
checkedAt?: string | null
|
||||
}
|
||||
|
||||
interface ProxyBalanceLike {
|
||||
available?: boolean | null
|
||||
surplus?: string | null
|
||||
balance?: string | null
|
||||
scope?: string | null
|
||||
message?: string | null
|
||||
}
|
||||
|
||||
/** 一行的显示内容:`text` 进格子,`title` 进悬停(整句原因与取数时刻都在那儿)。 */
|
||||
export interface BalanceLine {
|
||||
text: string
|
||||
title: string
|
||||
}
|
||||
|
||||
const BALANCE_PLACEHOLDER: BalanceLine = { text: UNKNOWN_SOURCE_LABEL, title: '本轮没取到数(不是余额为 0)' }
|
||||
const BALANCE_PENDING: BalanceLine = { text: '查询中…', title: '余额按实时查询,不落库' }
|
||||
|
||||
/** 人民币读数:解析不出数字时原样回显,不返回 `¥0`(一格 0 会被读成「没钱了」)。 */
|
||||
export function formatYuan(raw: string | null | undefined): string {
|
||||
const text = String(raw ?? '').trim()
|
||||
if (!text) return ''
|
||||
const value = Number(text.replace(/,/g, ''))
|
||||
if (!Number.isFinite(value)) return text
|
||||
return `¥${value.toFixed(2)}`
|
||||
}
|
||||
|
||||
function llmDetail(cell: LlmBalanceLike): string {
|
||||
const parts: string[] = []
|
||||
if (cell.moduleLabel) parts.push(String(cell.moduleLabel))
|
||||
const total = formatYuan(cell.balance)
|
||||
const usable = formatYuan(cell.availableBalance)
|
||||
const frozen = formatYuan(cell.frozen)
|
||||
if (total) parts.push(`总 ${total}`)
|
||||
if (usable) parts.push(`可用 ${usable}`)
|
||||
if (frozen) parts.push(`预冻结 ${frozen}`)
|
||||
if (cell.checkedAt) parts.push(`取数于 ${String(cell.checkedAt).replace('T', ' ').slice(0, 16)}`)
|
||||
const message = String(cell.message ?? '').trim()
|
||||
if (message) parts.push(message)
|
||||
return parts.join(';') || '暂无余额信息'
|
||||
}
|
||||
|
||||
/**
|
||||
* LLM 余额列:一把密钥一行,前缀是**渠道名**(钱挂在渠道账户上,不在模块上)。
|
||||
*
|
||||
* `pending=true` ⇒ 这一轮还在取数;取到但不合格(单位不是 CREDIT/available=false)
|
||||
* 一律走占位与后端原文,不显示金额。
|
||||
*/
|
||||
export function llmBalanceLine(cell: LlmBalanceLike | null | undefined, pending = false): BalanceLine {
|
||||
if (pending && !cell) return BALANCE_PENDING
|
||||
if (!cell) return BALANCE_PLACEHOLDER
|
||||
const label = known(LLM_CHANNEL_LABELS, cell.channel) ?? UNKNOWN_SOURCE_LABEL
|
||||
// 渠道认不出(LLM 列被喂了供应商名这类跨域值)时整格给占位:宁可什么都不显示,
|
||||
// 也不让一个不知道属于谁的数字或句子进界面(同货源标签那张表「跨域一律 —」的判据)
|
||||
if (label === UNKNOWN_SOURCE_LABEL) {
|
||||
return { text: UNKNOWN_SOURCE_LABEL, title: `${llmDetail(cell)}|渠道值认不出,未显示金额` }
|
||||
}
|
||||
if (!cell.available || String(cell.unit ?? '').trim().toUpperCase() !== LLM_BALANCE_UNIT) {
|
||||
const message = String(cell.message ?? '').trim()
|
||||
return { text: message ? `${label} 查不到` : `${label} ${UNKNOWN_SOURCE_LABEL}`, title: llmDetail(cell) }
|
||||
}
|
||||
const total = formatYuan(cell.balance)
|
||||
if (!total) return { text: `${label} ${UNKNOWN_SOURCE_LABEL}`, title: llmDetail(cell) }
|
||||
return { text: `${label} ${total}`, title: llmDetail(cell) }
|
||||
}
|
||||
|
||||
/**
|
||||
* LLM 余额列的整格内容:**按渠道折叠**,一个渠道一行。
|
||||
*
|
||||
* 钱挂在渠道账户上、不挂在模块上:两把密钥(外观专利/货源查询)都在龙抬头时,
|
||||
* 上游回的是同一个余额——排两遍会读成「这个账户有两笔钱」。折叠后 title 里仍留每一把的原文,
|
||||
* 运维要判「是哪把密钥的渠道」看得回来。
|
||||
*/
|
||||
export function llmBalanceLines(
|
||||
cells: Array<LlmBalanceLike | null | undefined> | null | undefined,
|
||||
pending = false,
|
||||
): BalanceLine[] {
|
||||
const list = (cells ?? []).filter((cell): cell is LlmBalanceLike => Boolean(cell))
|
||||
if (!list.length) return pending ? [BALANCE_PENDING] : []
|
||||
const grouped = new Map<string, BalanceLine>()
|
||||
for (const cell of list) {
|
||||
const key = normalizeKey(cell.channel) || 'unknown'
|
||||
const line = llmBalanceLine(cell, pending)
|
||||
const hit = grouped.get(key)
|
||||
grouped.set(key, hit ? { text: hit.text, title: `${hit.title}|${line.title}` } : line)
|
||||
}
|
||||
return [...grouped.values()]
|
||||
}
|
||||
|
||||
/** 代理池子的归属说法:platform 那份是**平台的钱**,绝不允许被读成这个用户的余量。 */
|
||||
const PROXY_SCOPE_LABELS: Record<string, string> = {
|
||||
user: '你的套餐',
|
||||
platform: '平台池',
|
||||
none: '查不了',
|
||||
}
|
||||
|
||||
/**
|
||||
* 代理余量列:极客给「剩余 IP 次数」(单位是次、不是钱);青果按 scope=none 说查不了。
|
||||
* `scope` 认不出来时不硬认(回落「来源未标注」并保留原值进 title 供运维核)。
|
||||
*/
|
||||
export function proxyBalanceLine(cell: ProxyBalanceLike | null | undefined, pending = false): BalanceLine {
|
||||
if (pending && !cell) return BALANCE_PENDING
|
||||
if (!cell) return BALANCE_PLACEHOLDER
|
||||
const scopeKey = normalizeKey(cell.scope)
|
||||
const scopeLabel = Object.prototype.hasOwnProperty.call(PROXY_SCOPE_LABELS, scopeKey)
|
||||
? PROXY_SCOPE_LABELS[scopeKey]
|
||||
: '来源未标注'
|
||||
const message = String(cell.message ?? '').trim()
|
||||
const detailParts = [`${scopeLabel}`]
|
||||
const surplus = String(cell.surplus ?? '').trim()
|
||||
const amount = String(cell.balance ?? '').trim()
|
||||
if (surplus) detailParts.push(`剩余 IP ${surplus}`)
|
||||
if (amount) detailParts.push(`账户余额 ${amount}`)
|
||||
if (message) detailParts.push(message)
|
||||
const title = detailParts.join(';') || '暂无余量信息'
|
||||
if (!cell.available) {
|
||||
// 后端那句原文(「青果不提供余量查询…」「上游未在预算内应答…」)全在 title 里;
|
||||
// 格子只说「查不了」——留 `—` 会被读成「这人没配代理」,那是另一件事。
|
||||
return { text: message ? '查不了' : `${scopeLabel} ${UNKNOWN_SOURCE_LABEL}`, title }
|
||||
}
|
||||
// 次数就是次数:这里不套 ¥ 符号、不换算,读数带「次」把单位写在格子里
|
||||
return { text: `${scopeLabel} ${surplus || UNKNOWN_SOURCE_LABEL} 次`, title }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user