/** * 密钥管理页的纯逻辑模型(货源标签 + 代理重填 + 检测结果档位)。 * * 四条收口都在这个文件里,页模板只调这里导出的函数,不各写各的字典: * * 1. **货源标签按模块分字典**(2026-11-02 修):原先 `UserSecretsPage.vue` 里是一整张 * `sourceLabel()`,把 LLM 渠道(longtaitou/t8star)与代理供应商(qingguo/jikip)混在同一个 * switch 上,于是「按值匹配」的字典会把它认识的**任何**值贴到**任何**列上——代理行因此能长出 * 「龙抬头」这种标签。后端 VO 的 `channel` 本来就是按模块归一的(`UserSecretModule.channelOrDefault` * 代理走 `ProxyProvider`、密钥走 `LlmChannel`),所以错标只会来自前端这张合表。 * 现在两张表互不相通:代理列只认供应商、密钥列只认渠道,跨域的值一律落到「—」而不是被硬认下来。 * * 2. **代理列不再显示、也不再回传原文**(2026-11-02 后端 `SecretMasking.maskProxy` 收口): * `AdminUserSecretModuleVo.full` 后台侧恒为空,界面只读 `masked`(`scheme://host`,不含 * key/pwd)。因此原先「代填/改代理」用 `window.prompt` 预填整条链接的交互已失效 * (预填必然是空的,保存还会把空串顶上去)——改成重新填账密:青果填 Authkey/Authpwd、 * 由 `composeQingguoLink` 现拼;极客按其产品口径填自家提取链接。 * ⚠ 拼出来的链接是一次性写请求的载荷,**不得**回显、回填、进 tooltip、进剪贴板。 * * 3. **检测结果按 `check_code` 分档渲染**(2026-11-02 加,见下方档位表):状态列原先只看 * `check_status`,把后端新分出来的一族「等一会再点就行」的码(`channel_busy` 等)一律显示成 * 「无法判定」,管理员分不清「被自己占着」「密钥坏了」「欠费」三种完全不同的处置。 * 码表只在这一份文件里(页面不再自带第二张表),句子一律用后端的 `check_message` 原文。 * * 零 `@/` 别名依赖:本文件被 `node --test` 直测(那颗 runner 不认 Vite 别名)。 */ import { composeQingguoLink } from '../../shared/utils/qingguo-link.ts' export type LlmChannelKey = 'longtaitou' | 't8star' export type ProxyProviderKey = 'qingguo' | 'jikip' /** 代理模块 key(与后端 `UserSecretModule.PROXY.key()` 同值):写请求的路径段与「这一格是代理」的判据。 */ export const PROXY_MODULE_KEY = 'proxy' /* ------------------------------------------------------------------ * * 检测结果的渲染档位(2026-11-02 加,起因是现网 uid=1 的 proxy 行) * * 后端 `UserApiSecretCheckService` 把青果检测从「无条件假通过」改成真提取后**按供应商返回码分档**, * 于是 `check_code` 成了这一格到底怎么了的最细信号;而界面当时只认 `check_status` 三档 * (passed/failed/error),`error` 一律写成「无法判定」——`channel_busy`(青果通道被自己的采集 * 占着,等约 3 分钟就能重试)因此被显示成「查不出」,管理员既看不出「等一会就行」, * 也分不清它和「密钥坏了」「欠费」是三种完全不同的处置。 * * 所以现在**按码建表、按状态兜底**: * - 认得的码 ⇒ 各有自己的档位(可重试这一族另给独立配色,既不红也不黄); * - 「无法判定」只留给真正没有结论的情况(空码/这表里没有的码 + `check_status=error`); * - `check_status=unknown`(后端 upsert 后的初值,等于「从没检测过」)仍是「未检测」, * 不许冒充「无法判定」——那是两件事,前者点一次检测就有答案,后者点了也还是没答案。 * * ⚠ 措辞的权威在后端:给人看的那句中文在 `check_message` 里,界面只出**档名**(一个词), * 绝不按码自己拼句子;句子通过 `moduleTooltip`/行级 tooltip 原样透出。 * ------------------------------------------------------------------ */ /** 药丸配色档位,对应 `UserSecretsPage.vue` 里 `.wh-pill.is-*` 那套样式类。 */ export type StatusTone = 'is-allowed' | 'is-blocked' | 'is-warn' | 'is-retry' | 'is-unknown' /** 一个渲染档位:档名 + 配色。档名是给管理员扫一眼用的短词,不是解释句。 */ export interface StatusBand { label: string tone: StatusTone } /** 单格「没配」与「没检测」是两档,但都用灰色:都不代表密钥本身有问题。 */ export const NOT_CONFIGURED_BAND: StatusBand = { label: '未配置', tone: 'is-unknown' } export const NOT_CHECKED_BAND: StatusBand = { label: '未检测', tone: 'is-unknown' } /** 真的问不出结论(空码/未知码 + `check_status=error`)才落到这一档。 */ export const INDETERMINATE_BAND: StatusBand = { label: '无法判定', tone: 'is-warn' } /** 后端 `STATUS_PASSED`(或码 `ok`):唯一可以当成「这项配好了」的一档。 */ export const PASSED_BAND: StatusBand = { label: '通过', tone: 'is-allowed' } /** 后端新增了一档、前端还没登记码名时的兜底:按状态给个不撒谎的词,不当场判死。 */ const GENERIC_FAILED_BAND: StatusBand = { label: '失败', tone: 'is-blocked' } /** 后端 `UserApiSecretCheckService` 的 `CODE_*` 全集(2026-11-02 逐条对着源码登记)。 */ const CHECK_CODE_BANDS: Record = { ok: PASSED_BAND, /** 通道被自己的采集占着:等释放后重点一次,密钥没坏——这一档就是本次要修的错渲染。 */ channel_busy: { label: '无可用通道', tone: 'is-retry' }, rate_limited: { label: '限流重试', tone: 'is-retry' }, timeout: { label: '超时重试', tone: 'is-retry' }, /** 传输层失败:后端两个分支都给这个码(提取接口不可达=failed、探测异常=error),都可重试。 */ network_error: { label: '网络重试', tone: 'is-retry' }, server_error: { label: '上游重试', tone: 'is-retry' }, /** 欠费(现网 21 行):修之前就只有 `check_status=failed` 这一条路径认得它,改后按码认、不再吊在状态上。 */ insufficient_balance: { label: '欠费', tone: 'is-warn' }, /** 提取成功但链接里组不出可验连的地址(没带 key/pwd):要用户重填,不是「查不出」。 */ tunnel_unverified: { label: '链接缺账密', tone: 'is-warn' }, invalid_key: { label: '密钥失效', tone: 'is-blocked' }, forbidden: { label: '鉴权被拒', tone: 'is-blocked' }, bad_request: { label: '参数被拒', tone: 'is-blocked' }, provider_error: { label: '供应商报错', tone: 'is-blocked' }, } /** 行级档位:后端 `UserApiSecretService` 按 failed > incomplete > passed > error > unknown 聚合,界面不重排这个优先级。 */ const ROW_STATUS_BANDS: Record = { passed: { label: '检测通过', tone: 'is-allowed' }, failed: { label: '检测失败', tone: 'is-blocked' }, incomplete: { label: '未配齐', tone: 'is-warn' }, error: INDETERMINATE_BAND, } /** 行级「可重试」:仅当整行的问题**只**出在可重试那一族码上时替换掉「无法判定」(后端给不了这一档,前端按格子的码补)。 */ const ROW_RETRY_BAND: StatusBand = { label: '可重试', tone: 'is-retry' } /** 单模块的可比较形状(不引 `@/api/*`:本文件要能被零别名的 `node --test` 直测)。 */ interface CheckModuleLike { exists?: boolean | null moduleLabel?: string | null checkStatus?: string | null checkCode?: string | null checkMessage?: string | null } /** 一行的可比较形状:只用到行状态 + 三个格子。 */ interface SecretRowLike { status?: string | null similarAsin?: CheckModuleLike | null appearancePatent?: CheckModuleLike | null proxy?: CheckModuleLike | null } /** 按归一后的键查档位表:空键与表里没有的键都返回 null(调用方兜底,绝不硬认一档)。 */ function bandOf(table: Record, raw: unknown): StatusBand | null { const key = normalizeKey(raw) if (!key || !Object.prototype.hasOwnProperty.call(table, key)) return null return table[key] ?? null } /** 码 → 档位;空码或这表里没有的码返回 null(由调用方按状态兜底,不硬认一档)。 */ export function checkCodeBand(code: string | null | undefined): StatusBand | null { return bandOf(CHECK_CODE_BANDS, code) } /** 这一档是不是「等一等再点」那一族(可重试):配色与行级补档都读这一个判据,不散落码名。 */ export function isRetryBand(band: StatusBand | null | undefined): boolean { return band?.tone === 'is-retry' } /** * 单格状态药丸:先看是否已配置,再看 `check_status=passed`,然后**按 `check_code` 分档**, * 都不认才按状态兜底。 */ export function moduleStatusBand(module: CheckModuleLike | null | undefined): StatusBand { if (!module || !module.exists) return NOT_CONFIGURED_BAND const status = normalizeKey(module.checkStatus) if (status === 'passed') return PASSED_BAND const band = checkCodeBand(module.checkCode) if (band) return band if (status === 'error') return INDETERMINATE_BAND if (status === 'failed') return GENERIC_FAILED_BAND // ''/unknown/后端下一版新增的状态值:宁可说「未检测」,也不替后端编造一个结论 return NOT_CHECKED_BAND } /** 行级状态药丸:后端聚合成 `error` 时,若这一行**没有结论的格子全是可重试那一族**,补成「可重试」。 */ export function rowStatusBand(row: SecretRowLike | null | undefined): StatusBand { const status = normalizeKey(row?.status) if (status === 'error' && awaitsRetryOnly(row)) return ROW_RETRY_BAND // 行级 `unknown`=后端还没问过(不是「问了问不出」),留「未检测」这一档,与筛选项里那一行同词 return bandOf(ROW_STATUS_BANDS, status) ?? NOT_CHECKED_BAND } /** * 这一行是不是「点一次就有答案」:后端把 error 之上的 failed/incomplete 都判掉了才轮到 error, * 所以这里只需再确认剩下那些没通过的格子都落在可重试档——混进一格「链接缺账密」就不算可重试, * 那一格不会自己好,把整行说成「可重试」等于让管理员一直等。 */ function awaitsRetryOnly(row: SecretRowLike | null | undefined): boolean { const pending = modulesOf(row).filter( (module) => Boolean(module?.exists) && normalizeKey(module?.checkStatus) !== 'passed', ) return pending.length > 0 && pending.every((module) => isRetryBand(moduleStatusBand(module))) } /** * 行级 tooltip 的明细:逐格把**后端原文** `check_message` 抄出来(运维判「是欠费还是被自己占着」就靠这句)。 * 前端不拼句子、不改一个字;没检测过的格子没有这句话,自然不出现。 */ export function rowDetailLines(row: SecretRowLike | null | undefined): string[] { return modulesOf(row) .filter((module) => Boolean(module?.exists)) .map((module) => { const message = String(module?.checkMessage ?? '').trim() if (!message) return '' const label = String(module?.moduleLabel ?? '').trim() return label ? `${label}:${message}` : message }) .filter(Boolean) } function modulesOf(row: SecretRowLike | null | undefined): CheckModuleLike[] { return [row?.similarAsin, row?.appearancePatent, row?.proxy].filter(Boolean) as CheckModuleLike[] } /** 后端 `ProxyProvider.DEFAULT`=极客:新建默认极客,青果手动切(2026-10-30 拍板)。 */ export const DEFAULT_PROXY_PROVIDER: ProxyProviderKey = 'jikip' /** 认不出的货源一律这个占位,不许回退成"某个具体渠道名"(那正是错标的来源)。 */ export const UNKNOWN_SOURCE_LABEL = '—' const LLM_CHANNEL_LABELS: Record = { longtaitou: '龙抬头', t8star: '贞贞', } const PROXY_PROVIDER_LABELS: Record = { qingguo: '青果', jikip: '极客', } function normalizeKey(raw: unknown): string { return String(raw ?? '').trim().toLowerCase() } function known(table: Record, raw: unknown): string | null { const key = normalizeKey(raw) return Object.prototype.hasOwnProperty.call(table, key) ? table[key as T] : null } /** LLM 密钥列的渠道名:只查 LLM 字典,代理供应商的值在这里认不出。 */ export function llmSourceLabel(channel: string | null | undefined): string { return known(LLM_CHANNEL_LABELS, channel) ?? UNKNOWN_SOURCE_LABEL } /** * 代理列的供应商名:只查供应商字典。 * 传进 LLM 渠道值(`longtaitou`/`t8star`)必须返回占位——这条是"代理行贴龙抬头"的直接判据。 */ export function proxySourceLabel(channel: string | null | undefined): string { return known(PROXY_PROVIDER_LABELS, channel) ?? UNKNOWN_SOURCE_LABEL } /** * 归一为可选供应商;未知值不猜,返回 null 让调用方保持原选择。 */ export function asProxyProvider(raw: unknown): ProxyProviderKey | null { const key = normalizeKey(raw) return key === 'qingguo' || key === 'jikip' ? key : null } /** * 代理列的唯一显示文本:后端下发的 `masked`(青果形状 `https://overseas.proxy.qg.net`)。 * 刻意**不读** `full`,也不做任何"masked 空就回落到原文"的兜底——回落等于把脱敏前的行为留在界面里。 */ export function proxyDisplayText(module: { masked?: string | null } | null | undefined): string { const masked = String(module?.masked ?? '').trim() return masked || UNKNOWN_SOURCE_LABEL } /** 代填/改代理的表单草稿:青果用前三个字段,极客用 `link`。 */ export interface ProxyEntryDraft { provider: ProxyProviderKey authkey: string authpwd: string link: string } /** * 空草稿:`provider` 用后端已登记的那一列预置(只是选中的单选框,不是凭据), * 三个输入位**一律空**——脱敏后没有任何原文可回填。 */ export function emptyProxyEntryDraft(currentProvider?: string | null): ProxyEntryDraft { return { provider: asProxyProvider(currentProvider) ?? DEFAULT_PROXY_PROVIDER, authkey: '', authpwd: '', link: '', } } /** 与后端 `PROXY_FORMAT_HINT` 同一句口径:本函数只做前置提示,真判仍在服务端。 */ const PROXY_LINK_FORMAT_HINT = '代理地址格式不正确,请填写 http://host:port 形式的代理地址,或代理服务商的提取链接(https://...)' /** http(s) + 有主机名:镜像后端 `validateProxyValue`,不在前端扩大判死范围。 */ function isHttpUrl(raw: string): boolean { try { const parsed = new URL(raw) return (parsed.protocol === 'http:' || parsed.protocol === 'https:') && Boolean(parsed.hostname) } catch { return false } } /** * 校验草稿,返回给界面显示的文案(空串=可以提交)。 * 点名"缺哪把":缺 pwd 时青果回 `API_AUTH_DENY`,所以两把都得填,不能只填 key 就当配好了。 */ export function validateProxyEntry(draft: ProxyEntryDraft): string { if (!asProxyProvider(draft.provider)) return '请选择代理供应商' if (draft.provider === 'qingguo') { if (!String(draft.authkey || '').trim()) return '请填写青果 Authkey' if (!String(draft.authpwd || '').trim()) return '请填写青果 Authpwd(只填 Authkey 青果会判鉴权拒绝)' return '' } const link = String(draft.link || '').trim() if (!link) return '请填写极客提取链接' if (!isHttpUrl(link)) return PROXY_LINK_FORMAT_HINT return '' } /** * 组装 PUT `/api/admin/user-secrets/{userId}/modules/proxy` 的请求体。 * 不合法返回 null(调用方不得把 null 顶上去,更不得拿旧值凑数)。 */ export function buildProxyEntryPayload( draft: ProxyEntryDraft, ): { value: string; channel: ProxyProviderKey } | null { if (validateProxyEntry(draft)) return null if (draft.provider === 'qingguo') { return { value: composeQingguoLink(draft.authkey, draft.authpwd), channel: 'qingguo' } } 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 | 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() 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 = { 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 } }