Files
crawler-plugin/frontend-vue/src/pages/setup/DesktopSecretSetupPage.vue
T
huangzd1997 9b79d6b5db fix(密钥配置): 保存沿用刚检测过的输入值结果,消除「三项检测通过却提示未检测」死循环
- 服务端:对「输入值(未保存)」的检测结果按 uid+模块+值指纹暂存 Redis(TTL 30min,
  Redis 异常降级为需重新检测,不阻断保存);保存同一个值时落库该结果
  (passed/failed/error 一并沿用),改过值或从未检测则维持未检测
- 前端:保存后清理本地「输入值(未保存)」绿字,展示统一走服务端快照,避免展示与门禁矛盾;
  门禁提示改列「模块名(掩码):未检测 / 检测失败:原因 / 未配置」,同名掩码也能分辨模块
- 测试:UserApiSecretServiceTest 补沿用 / 值不一致 / Redis 降级用例;
  新增前后端一致性守卫测试(保存后必须清检测结果、提示必须带模块名)
2026-09-14 13:49:25 +08:00

223 lines
5.9 KiB
Vue
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.
<template>
<div class="setup-root">
<header class="setup-header">
<span class="setup-title">数富AI</span>
<div class="setup-header-right">
<NotificationBell />
<span class="username-text">{{ username || '未登录' }}</span>
<router-link to="/login?logout=1" class="logout-link">退出</router-link>
</div>
</header>
<main class="setup-main">
<div class="setup-card">
<div class="setup-card-title">完成密钥配置后即可使用</div>
<div class="setup-card-desc">
密钥保存在服务端并绑定当前账号,换设备登录后自动同步。
请填写以下密钥,点「检测」确认可用后保存;全部检测通过即可进入工具台。
</div>
<ApiSecretSettingsPanel ref="panelRef" />
<div class="setup-actions">
<button type="button" class="setup-submit" :disabled="submitting" @click="submit">
{{ submitting ? '校验中...' : '保存并进入' }}
</button>
</div>
<div class="setup-hint">{{ hint }}</div>
</div>
</main>
</div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import ApiSecretSettingsPanel from '@/shared/components/ApiSecretSettingsPanel.vue'
import NotificationBell from '@/shared/components/NotificationBell.vue'
import {
getStoredApiSecretSnapshot,
listApiSecretModules,
loadApiSecrets,
type ApiSecretModuleKey,
} from '@/shared/utils/api-secret-store'
const router = useRouter()
const route = useRoute()
const panelRef = ref<InstanceType<typeof ApiSecretSettingsPanel> | null>(null)
const submitting = ref(false)
const username = ref('')
const hint = ref('填写密钥后请先点「检测」确认可用再保存(检测通过的输入值保存时会沿用同一结果),全部检测通过后即可进入工具台。')
onMounted(() => {
try {
username.value = window.localStorage.getItem('username') || ''
} catch {
/* 忽略存储异常 */
}
})
/**
* 提交:保存后只校验检测状态;检测一律由用户手动点「检测」触发(服务端会沿用刚检测过的同一个输入值,
* 因此「检测 → 保存并进入」一次即可通过;改过值或从未检测的模块仍需重新检测)。
*/
async function submit() {
if (submitting.value) return
const panel = panelRef.value
if (!panel) return
submitting.value = true
try {
const saved = await panel.saveAll({ requireAll: true })
if (!saved) return
const state = await loadApiSecrets({ force: true })
if (state === 'incomplete') {
const failed = listApiSecretModules()
.map((module) => ({
module,
snapshot: getStoredApiSecretSnapshot(module.moduleKey as ApiSecretModuleKey),
}))
.filter(({ snapshot }) => !snapshot.exists || (snapshot.checkStatus !== 'passed' && snapshot.checkStatus !== 'error'))
// 逐项列出「模块名(掩码)+ 未通过原因」:同一密钥填进多个模块时也能分清是哪个模块
const detail = failed.map(({ module, snapshot }) => {
const masked = snapshot.masked || '未配置'
if (!snapshot.exists) {
return `${module.moduleLabel}(${masked})尚未配置`
}
if (snapshot.checkStatus === 'failed') {
const reason = snapshot.checkMessage ? `:${snapshot.checkMessage.slice(0, 40)}` : ''
return `${module.moduleLabel}(${masked})检测失败${reason}`
}
return `${module.moduleLabel}(${masked})尚未检测`
})
hint.value = `以下密钥尚未通过检测:${detail.join(';')}。请点击上方对应模块的「检测」按钮确认后重试。`
ElMessage.warning('密钥尚未全部检测通过,请点击「检测」按钮逐项确认')
return
}
const redirect = typeof route.query.redirect === 'string' && route.query.redirect.startsWith('/')
? route.query.redirect
: '/home'
console.log('[api-secret] 密钥配置完成,进入', redirect)
await router.replace(redirect)
} catch (error) {
ElMessage.error(error instanceof Error ? error.message : '保存失败')
} finally {
submitting.value = false
}
}
</script>
<style scoped>
.setup-root {
min-height: 100vh;
background: #12161a;
display: flex;
flex-direction: column;
}
.setup-header {
display: flex;
align-items: center;
justify-content: space-between;
height: 56px;
padding: 0 24px;
background: #171b20;
border-bottom: 1px solid #262d35;
}
.setup-title {
color: #f2f6fa;
font-size: 16px;
font-weight: 700;
}
.setup-header-right {
display: flex;
align-items: center;
gap: 14px;
}
.username-text {
color: #9aa6b3;
font-size: 13px;
}
.logout-link {
color: #8dc4ff;
font-size: 13px;
text-decoration: none;
}
.logout-link:hover {
text-decoration: underline;
}
.setup-main {
flex: 1;
display: flex;
justify-content: center;
padding: 40px 16px 64px;
}
.setup-card {
width: 640px;
max-width: 100%;
padding: 26px;
border: 1px solid #2c3540;
border-radius: 16px;
background: #171b20;
box-shadow: 0 24px 70px rgba(0, 0, 0, .45);
height: fit-content;
}
.setup-card-title {
color: #f5f7fa;
font-size: 18px;
font-weight: 700;
}
.setup-card-desc {
margin: 8px 0 18px;
color: #8f9aa7;
font-size: 13px;
line-height: 1.6;
}
.setup-actions {
display: flex;
justify-content: flex-end;
margin-top: 16px;
}
.setup-submit {
min-width: 132px;
height: 40px;
padding: 0 20px;
border: none;
border-radius: 10px;
background: #4f8fda;
color: #fff;
font-size: 14px;
font-weight: 700;
cursor: pointer;
}
.setup-submit:hover:not(:disabled) {
background: #67a6ed;
}
.setup-submit:disabled {
cursor: not-allowed;
opacity: .58;
}
.setup-hint {
margin-top: 12px;
color: #7f8a96;
font-size: 12px;
line-height: 1.6;
text-align: right;
}
</style>