refactor(user-secret): 密钥设置面板 1114→775 行,压回可读性棘轮线内
可读性棘轮不允许登记新增超长文件(新增只能拆)。按职责抽出四个模块: - useProxyWhitelist:白名单 IP/代加动作与可用态; - useProxyPlan:极客套餐读取/保存/清除与余量查询(含余额查询的既有降级); - api-secret-module-view:模块卡片的描述/占位/状态文案派生; - api-secret-proxy-config:proxy_users[uid] 的读取与写入补丁。 行为与文案一字未改;静默 catch 随 loadBalance 搬家,逐文件台账同步迁移 (面板 1→0、useProxyPlan=1,总数与 blocks 不变)。
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
import type {
|
||||
ApiSecretCheckStatus,
|
||||
ApiSecretSnapshot,
|
||||
UserApiSecretCheckResult,
|
||||
} from '@/shared/utils/api-secret-store'
|
||||
import type { LlmChannelKey } from '@/shared/utils/api-secret-channel'
|
||||
|
||||
/** 模块卡片的状态形状(与面板内的 reactive 记录同构)。 */
|
||||
export interface ModuleState {
|
||||
input: string
|
||||
checking: boolean
|
||||
result: UserApiSecretCheckResult | null
|
||||
error: string
|
||||
/** 最近一次检测的来源:saved=密钥配置里已保存的密钥,input=输入框里的未保存值。 */
|
||||
lastCheckSource: 'saved' | 'input' | ''
|
||||
}
|
||||
|
||||
const MODULE_DESCRIPTIONS: Record<string, string> = {
|
||||
'appearance-patent': '仅用于外观专利检测,保存在服务端并绑定当前账号。',
|
||||
'similar-asin': '仅用于货源查询,保存在服务端并绑定当前账号。',
|
||||
}
|
||||
|
||||
const CHECK_STATUS_TEXT: Record<ApiSecretCheckStatus, string> = {
|
||||
passed: '检测通过',
|
||||
failed: '检测失败',
|
||||
error: '暂时无法判定',
|
||||
unknown: '已保存,未检测',
|
||||
}
|
||||
|
||||
export interface ModuleViewDeps {
|
||||
moduleChannels: Record<string, LlmChannelKey>
|
||||
ensureModuleState: (moduleKey: string) => ModuleState
|
||||
snapshotOf: (moduleKey: string) => ApiSecretSnapshot
|
||||
}
|
||||
|
||||
/** 模块卡片的展示派生值:描述/占位/状态文案与样式类。 */
|
||||
export function createModuleView({ moduleChannels, ensureModuleState, snapshotOf }: ModuleViewDeps) {
|
||||
function descriptionOf(moduleKey: string) {
|
||||
return MODULE_DESCRIPTIONS[moduleKey] || '保存在服务端并绑定当前账号。'
|
||||
}
|
||||
|
||||
function placeholderOf(moduleKey: string) {
|
||||
const snapshot = snapshotOf(moduleKey)
|
||||
if (snapshot.exists) {
|
||||
return `已保存(${snapshot.masked || '****'}),输入新值可覆盖`
|
||||
}
|
||||
return moduleChannels[moduleKey] === 'longtaitou'
|
||||
? '请输入龙抬头接口密钥(sk-...)'
|
||||
: '请输入贞贞接口密钥(sk-...)'
|
||||
}
|
||||
|
||||
function statusPillTextOf(moduleKey: string) {
|
||||
const state = ensureModuleState(moduleKey)
|
||||
if (state.checking) return '检测中'
|
||||
const status = state.result?.checkStatus || snapshotOf(moduleKey).checkStatus
|
||||
if (status === 'passed') return '可用'
|
||||
if (status === 'failed') return '不可用'
|
||||
if (status === 'error') return '待确认'
|
||||
return snapshotOf(moduleKey).exists ? '未检测' : '未配置'
|
||||
}
|
||||
|
||||
function statusPillClassOf(moduleKey: string) {
|
||||
const state = ensureModuleState(moduleKey)
|
||||
const status = state.result?.checkStatus || snapshotOf(moduleKey).checkStatus
|
||||
return {
|
||||
'status-pill--ok': status === 'passed',
|
||||
'status-pill--fail': status === 'failed',
|
||||
'status-pill--warn': status === 'error',
|
||||
}
|
||||
}
|
||||
|
||||
function formatTime(millis: number | null) {
|
||||
if (!millis) return ''
|
||||
const date = new Date(millis)
|
||||
const hour = String(date.getHours()).padStart(2, '0')
|
||||
const minute = String(date.getMinutes()).padStart(2, '0')
|
||||
return `${hour}:${minute}`
|
||||
}
|
||||
|
||||
function statusTextOf(moduleKey: string) {
|
||||
const state = ensureModuleState(moduleKey)
|
||||
if (state.checking) return state.input.trim() ? '正在检测输入值...' : '正在检测配置密钥...'
|
||||
if (state.result) {
|
||||
const latency = state.result.checkLatencyMs != null ? `(${state.result.checkLatencyMs}ms)` : ''
|
||||
const suffix = state.result.viaProxy === true ? ' · 经代理' : ''
|
||||
const snapshot = snapshotOf(moduleKey)
|
||||
// 明确展示本次检测的对象:输入值(未保存)或该用户密钥配置里保存的密钥(脱敏)
|
||||
const prefix = state.lastCheckSource === 'input'
|
||||
? '输入值(未保存):'
|
||||
: `配置密钥${snapshot.masked ? ` ${snapshot.masked}` : ''}:`
|
||||
if (state.result.checkStatus === 'passed') return `${prefix}检测通过${latency}${suffix}`
|
||||
if (state.result.checkCode === 'insufficient_balance') return `${prefix}欠费:${state.result.checkMessage || '代理服务商余额不足'}${suffix}`
|
||||
return `${prefix}${state.result.checkMessage || '检测失败'}${suffix}`
|
||||
}
|
||||
if (state.error) return state.error
|
||||
const snapshot = snapshotOf(moduleKey)
|
||||
if (!snapshot.exists) return '当前未保存,请填写后保存'
|
||||
const status = CHECK_STATUS_TEXT[snapshot.checkStatus] || '已保存'
|
||||
const time = formatTime(snapshot.checkedAt)
|
||||
const message = snapshot.checkStatus === 'unknown' ? '' : `:${snapshot.checkMessage || ''}`
|
||||
const masked = snapshot.masked ? `配置密钥 ${snapshot.masked} · ` : ''
|
||||
return `${masked}${status}${message}${time ? ` · ${time}` : ''}`
|
||||
}
|
||||
|
||||
function resultClassOf(moduleKey: string) {
|
||||
const state = ensureModuleState(moduleKey)
|
||||
const status = state.result?.checkStatus || snapshotOf(moduleKey).checkStatus
|
||||
return {
|
||||
'check-result--ok': status === 'passed',
|
||||
'check-result--fail': status === 'failed',
|
||||
'check-result--warn': status === 'error',
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
descriptionOf,
|
||||
placeholderOf,
|
||||
statusPillTextOf,
|
||||
statusPillClassOf,
|
||||
formatTime,
|
||||
statusTextOf,
|
||||
resultClassOf,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user