Files
crawler-plugin/admin-frontend-vue/src/api/user-secrets.ts
T
huangzd1997 988cc633a0 fix(admin-vue): 密钥管理/用量统计两菜单扩京东侧+两页按平台 tab 隔离取数(2026-11-22 用户口径「后台京东tab下也要加密钥管理和密钥统计量 数据不要弄过来」)
- platform-scope.ts BOTH_PLATFORM_MENU_KEYS 加 admin_user_secrets/admin_user_secret_usage(与后端 AdminMenuPlatformScope.BOTH_MENU_KEYS 逐字同源;侧栏分组/授权树/AdminLayout 切 tab 自动跟随)
- user-secrets.ts/user-secret-usage.ts 查询加 platform 入参(同 companyId 形状:白名单构造不收,只认原始入参)
- 两页 fetch 带 platform: platformTab.active+watch(platformTab.active) 重置关键字/页码并重取(UsersPage 同形)
- company-tab-wiring PLATFORM_WIRING_PINS 钉 api/fetch/watch 三形状(含正负判据自证);platform-menu-filter/menu-manage-platform/admin-layout-platform-lock 三测试期望同步(node 21 绿+mount 6 绿+vue-tsc rc=0)

随下一 admin-vue 窗上线;与后端 4819b243 必须同窗发布(单发一侧=侧栏分组与权限门错位)
2026-11-22 21:51:33 +08:00

205 lines
8.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { http } from './http'
import { unwrap } from './envelope'
/** 单模块状态(脱敏值 + 连通性)。 */
export interface AdminUserSecretModule {
moduleKey: string
moduleLabel: string
/**
* 货源:按模块分域,后端 `UserSecretModule.channelOrDefault` 已归一——
* LLM 行为渠道(longtaitou/t8star),代理行为供应商(qingguo/jikip)。
* ⚠ 界面不得用一张合表渲染这两域(代理行贴出「龙抬头」就是这么来的),
* 走 `user-secret-model.ts` 的 `moduleSourceLabel`。
*/
channel?: string | null
/** 脱敏值:代理列为 `scheme://host`,不含提取链接里的 key/pwd。 */
masked: string
/**
* 完整明文值:后台侧**恒为空**(2026-11-02 `SecretMasking.maskProxy` 收口,代理链接里带着账密)。
* 字段保留只为接口兼容;界面**不得**读它、更不得拿它做"masked 空就回落原文"的兜底,
* 也不许有任何复制/导出整条链接的旁路。
*/
full: string
exists: boolean
/** 是否由后台代填并锁定:锁定时客户端不可修改或清空。 */
locked: boolean
checkStatus: string
checkCode: string
checkMessage: string
checkLatencyMs: number | null
checkedAt: string | null
updatedAt: string | null
}
/** 一行一用户:三个字段列 + 行级状态(三类都检测通过才算 passed)。 */
export interface AdminUserSecretRow {
userId: number
username: string
/** 所属数据权限分组名(可能多个)。 */
groups: string[]
/** 所属主管(创建人)用户名;无分组时的兜底展示。 */
leaderUsername: string
similarAsin: AdminUserSecretModule
appearancePatent: AdminUserSecretModule
proxy: AdminUserSecretModule
status: string
statusMessage: string
/** 首次配置时间(三模块中最早);从未配置为 null。 */
createdAt: string | null
updatedAt: string | null
}
export interface AdminUserSecretPage {
items: AdminUserSecretRow[]
total: number
page: number
pageSize: number
/** 分组筛选项(超管=全部;主管=自己带的分组)。 */
groupOptions?: GroupOption[]
}
export interface GroupOption {
id: number
groupName: string
}
/**
* LLM 一格的余额(**人民币口径**,后端取龙抬头 `credits` 块)。
* 与 `AdminProxyBalanceCell` 是两套语义:这里的 `balance` 是钱、单位 CREDIT(=⚡=人民币),
* 代理那边的 `surplus` 是「次」。界面不得用一张函数渲染两域(同 `channel` 那张表的教训)。
*/
export interface AdminLlmBalanceCell {
moduleKey: string
moduleLabel: string
channel: string
channelLabel: string
available: boolean
/** 上游原样小数串(未取整);available=false 时为 null。 */
balance: string | null
availableBalance: string | null
frozen: string | null
/** 恒为 CREDIT;不是这个值就按「认不出口径」处理,不猜。 */
unit: string | null
message: string | null
checkedAt: string | null
}
/** 代理余量一格:jikip 套餐口径(`surplus`=IP 次数、`scope`=谁的池子)。青果为 scope=none。 */
export interface AdminProxyBalanceCell {
available: boolean
surplus: string | null
balance: string | null
/** user=该用户自己的套餐;platform=平台池子(不是这个用户的钱);none=查不了。 */
scope: string | null
message: string | null
low?: boolean
lowReason?: string | null
}
export interface AdminUserSecretBalanceItem {
userId: number
llm: AdminLlmBalanceCell[]
proxy: AdminProxyBalanceCell
}
export interface AdminUserSecretBalances {
items: AdminUserSecretBalanceItem[]
/** 被数据权限剔出、本次没查的用户(不静默少给数)。 */
deniedUserIds?: number[]
/** 超出整请求时间预算、这轮没取到数的用户。 */
timedOutUserIds?: number[]
}
export interface UserSecretQuery {
keyword?: string
checkStatus?: string
/** 按数据权限分组筛选(仅超管生效);后端参数为 snake_case。 */
groupId?: number
/** 公司 tab 的当前口径;不传 = 后端按操作者自己的公司(批次 1 里非超管只有这一家)。 */
companyId?: number | null
/** 平台 tab(amazon/jd):按 users.platform 过滤(2026-11-22 京东侧新增);不传 = 旧行为不筛。 */
platform?: string
page: number
pageSize: number
}
/** 分页查询用户密钥(一行一用户):GET /api/admin/user-secrets(筛选参数必须 snake_case,camel 会被后端静默忽略)。 */
export async function fetchUserSecretList(params: UserSecretQuery): Promise<AdminUserSecretPage> {
const query: Record<string, string | number> = { page: params.page, pageSize: params.pageSize }
if (params.keyword) query.keyword = params.keyword
if (params.checkStatus) query.checkStatus = params.checkStatus
if (params.groupId) query.group_id = params.groupId
// 用 `!= null` 而不是上面几行的 truthy 写法:这一项类型是 `number | null`(null = 没选公司)。
// 后端把 company_id<=0 归一成「没选公司」(DataReadScope.readScope 第一行),0 与缺失等价,按类型判即可。
if (params.companyId != null) query.company_id = params.companyId
// 平台值(2026-11-22)同 companyId 形状:白名单构造不收,只认原始入参。
if (params.platform) query.platform = params.platform
const { data } = await http.get('/api/admin/user-secrets', { params: query })
return unwrap<AdminUserSecretPage>(data)
}
/**
* 刷新本页用户的余额侧写:POST /api/admin/user-secrets/balances。
*
* 实时打上游、不落库,所以服务端带三条护栏(单次 ≤20 个用户、固定并发、整请求 10s 预算),
* 客户端的超时必须**长于**服务端预算,否则服务端还在取数、这边先报错,
* 界面就会把「查得慢」显示成「查不到」(与铁律 10「前端超时不得短于服务端预算」同形)。
*/
export async function fetchAdminSecretBalances(
userIds: number[],
companyId?: number | null,
): Promise<AdminUserSecretBalances> {
const body: Record<string, unknown> = { userIds }
if (companyId != null) body.company_id = companyId
const { data } = await http.post('/api/admin/user-secrets/balances', body, { timeout: 30_000 })
return unwrap<AdminUserSecretBalances>(data)
}
/** 立即检测该用户全部已配置项:POST /api/admin/user-secrets/{userId}/check
* 逐模块检测(每项最多「首查 + 传输层失败重试一次」,最坏约 31s/项),故放宽超时到 180s。 */
export async function checkUserSecret(userId: number) {
const { data } = await http.post(`/api/admin/user-secrets/${userId}/check`, undefined, { timeout: 180_000 })
return unwrap<
Array<{
moduleKey: string
checkStatus: string
checkCode: string
checkMessage: string
checkLatencyMs: number | null
checkedAt: string | null
viaProxy: boolean
}>
>(data)
}
/** 清空该用户全部密钥与代理配置:DELETE /api/admin/user-secrets/{userId} */
export async function deleteUserSecret(userId: number): Promise<void> {
const { data } = await http.delete(`/api/admin/user-secrets/${userId}`)
unwrap<unknown>(data)
}
/**
* 代填并锁定该用户某模块配置(当前仅代理):PUT /api/admin/user-secrets/{userId}/modules/{moduleKey}
* `value` 是**一次性写载荷**(代理为现拼的提取链接,带着 key/pwd):调用方不得把它渲染回界面、
* 不得打进日志;`channel` 是同一单的货源(代理=供应商 key),后端据此选解析分支——
* 不给则由后端按链接域名识别,识别不出回落默认供应商,会静默换执行方,所以本面一律显式带上。
*/
export async function saveUserSecretModule(
userId: number,
moduleKey: string,
value: string,
channel?: string,
): Promise<AdminUserSecretModule> {
const { data } = await http.put(`/api/admin/user-secrets/${userId}/modules/${moduleKey}`, {
value,
...(channel ? { channel } : {}),
})
return unwrap<AdminUserSecretModule>(data)
}
/** 清空该用户某模块配置并解除锁定:DELETE /api/admin/user-secrets/{userId}/modules/{moduleKey} */
export async function clearUserSecretModule(userId: number, moduleKey: string): Promise<void> {
const { data } = await http.delete(`/api/admin/user-secrets/${userId}/modules/${moduleKey}`)
unwrap<unknown>(data)
}