精简配置保存与应用流程
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-07 16:48:46 +08:00
1 parent 7aab26221b
commit f4c50226f8
8 files changed
+237 -95

No files matched your search

@@ -43,6 +43,19 @@
padding-top: 2px;
}
.action-row {
display: flex;
align-items: center;
justify-content: flex-end;
flex-wrap: wrap;
gap: 10px;
}
.action-row :deep(.el-button) {
min-width: 112px;
margin-left: 0;
}
.overview-meta {
display: flex;
flex-wrap: wrap;
@@ -128,6 +141,14 @@
padding-top: 0;
}
.action-row {
justify-content: stretch;
}
.action-row :deep(.el-button) {
flex: 1;
}
.overview-meta {
gap: 8px 16px;
}
@@ -78,7 +78,7 @@
<div v-if="canWrite" class="editor-footer">
<el-alert
v-if="pendingCandidate"
:title="`服务器存在待应用候选,代次 ${pendingCandidate.generation}。已锁定编辑;如需修改,请先取消候选。刷新页面不会丢失候选。`"
title="有一项配置尚未生效,可以继续应用或放弃更改。"
type="warning"
:closable="false"
show-icon
@@ -90,30 +90,38 @@
:closable="false"
show-icon
/>
<el-checkbox v-model="confirmed" class="confirmation-check" :disabled="sectionLoading || !!working || !!sectionError">
我已确认这是受控变更,并允许使用最近认证完成高风险配置操作
</el-checkbox>
<div class="action-row">
<el-button :loading="working === 'validate'" :disabled="sectionLoading || !!working || !!sectionError || !!pendingCandidate" @click="validateConfig">
只验证
<el-button
v-if="!pendingCandidate"
type="primary"
:loading="working === 'save' || working === 'apply'"
:disabled="sectionLoading || !!working || !!sectionError || !hasChanges"
@click="saveAndApply"
>
保存并应用
</el-button>
<el-button type="primary" :loading="working === 'save'" :disabled="sectionLoading || !!working || !!sectionError || !!pendingCandidate" @click="saveConfig">
保存候选配置
<el-button
v-else
type="primary"
:loading="working === 'resume' || working === 'apply'"
:disabled="sectionLoading || !!working || !!sectionError"
@click="continuePending"
>
继续应用
</el-button>
<el-button v-if="pendingCandidate && !pendingCandidate.sameSession" :disabled="!!working || !confirmed" @click="managePending('resume')">
恢复候选到当前会话
</el-button>
<el-button v-if="pendingCandidate" type="warning" plain :disabled="!!working || pendingCandidate.committed" @click="managePending('cancel')">
取消候选配置
</el-button>
<el-button type="success" plain :loading="working === 'apply'" :disabled="sectionLoading || !!working || !savedGeneration" @click="applyConfig">
{{ savedGeneration ? `应用代次 ${savedGeneration}` : '应用配置' }}
<el-button
v-if="pendingCandidate"
type="danger"
plain
:disabled="!!working || pendingCandidate.committed"
@click="managePending('cancel')"
>
放弃更改
</el-button>
</div>
</div>
<div v-if="validationResult" class="result-box" :class="validationResult.valid ? 'result-success' : 'result-error'" role="status">
<strong>{{ validationResult.valid ? '验证通过' : '验证未通过' }}</strong>
<span v-if="validationResult.generation">候选代次:{{ validationResult.generation }}</span>
<div v-if="validationResult && !validationResult.valid" class="result-box result-error" role="status">
<strong>配置检查未通过</strong>
<ul v-if="validationResult.issues?.length">
<li v-for="issue in validationResult.issues" :key="`${issue.code}-${issue.message}`">
{{ issue.code }}:{{ issue.message }}
@@ -145,19 +153,15 @@ const {
sectionError,
secretField,
secretValue,
confirmed,
savedGeneration,
pendingCandidate,
pendingScopeHint,
managePending,
validationResult,
hasChanges,
canWrite,
refreshPage,
validateConfig,
saveConfig,
applyConfig,
formatTime,
serviceLabel
saveAndApply,
continuePending
} = inject('smail-config-page')
</script>
@@ -243,27 +243,31 @@ export function usePage(pageKey = 'base') {
}
const managePending = async action => {
if (!canOperate() || !pendingCandidate.value) return
if (!canOperate() || !pendingCandidate.value) return false
working.value = action
try {
await ElMessageBox.confirm(
action === 'cancel' ? '取消后保留当前生效配置,候选修改不会应用。确认取消吗?' : '恢复后将候选配置绑定到当前会话,请确认已核对候选内容。',
action === 'cancel' ? '取消候选配置' : '恢复候选配置',
{ type: 'warning', confirmButtonText: '确认', cancelButtonText: '返回' }
)
if (action === 'cancel') {
await ElMessageBox.confirm(
'放弃后会保留当前生效配置,本次修改不会应用。',
'放弃本次更改',
{ type: 'warning', confirmButtonText: '放弃更改', cancelButtonText: '返回' }
)
}
const response = await http.post('/api/v1/config/pending', {
generation: pendingCandidate.value.generation,
candidateId: pendingCandidate.value.candidateId,
action,
confirmation: { confirmed: confirmed.value, method: 'recent_auth' }
confirmation: { confirmed: true, method: 'recent_auth' }
})
notifyResponseError(response, '候选配置操作失败')
await loadStatus()
await loadSection()
await loadPending()
ElMessage.success(action === 'cancel' ? '候选已取消,可以继续编辑' : '候选已恢复,请核对后应用')
if (action === 'cancel') ElMessage.success('本次更改已放弃')
return true
} catch (error) {
if (error !== 'cancel' && error !== 'close') ElMessage.error(error.message || '候选配置操作失败')
return false
} finally {
working.value = ''
}
@@ -528,7 +532,7 @@ export function usePage(pageKey = 'base') {
expectedGeneration: Number(status.activeGeneration),
payload: parsePayload(),
secretChanges,
confirmation: { confirmed: confirmed.value, method: 'recent_auth' }
confirmation: { confirmed: true, method: 'recent_auth' }
}
}
@@ -557,8 +561,8 @@ export function usePage(pageKey = 'base') {
}
}
const saveConfig = async () => {
if (!canEdit() || !checkForm()) return
const saveConfig = async ({ quiet = false } = {}) => {
if (!canEdit() || !checkForm()) return false
working.value = 'save'
try {
const request = buildRequest()
@@ -570,20 +574,22 @@ export function usePage(pageKey = 'base') {
validationResult.value = { valid: true, generation: savedGeneration.value, issues: [] }
await loadStatus()
await loadPending()
ElMessage.success(`候选配置已保存,代次 ${savedGeneration.value} 等待应用`)
if (!quiet) ElMessage.success('配置已保存,等待应用')
return true
} catch (error) {
ElMessage.error(error.message || '配置保存失败')
return false
} finally {
working.value = ''
}
}
const applyConfig = async () => {
if (!canOperate() || !savedGeneration.value) return
const applyConfig = async ({ successMessage = '配置已应用' } = {}) => {
if (!canOperate() || !savedGeneration.value) return false
if (!savedInput || savedInput.text !== editorText.value || savedInput.secret !== secretValue.value) {
invalidateCandidate()
ElMessage.warning('填写内容已变更,请重新保存候选配置后再应用')
return
return false
}
working.value = 'apply'
try {
@@ -595,14 +601,34 @@ export function usePage(pageKey = 'base') {
savedGeneration.value = 0
await Promise.all([loadStatus(), loadSection()])
await loadPending()
ElMessage.success('配置已应用')
ElMessage.success(successMessage)
return true
} catch (error) {
ElMessage.error(error.message || '配置应用失败')
return false
} finally {
working.value = ''
}
}
const saveAndApply = async () => {
if (pendingCandidate.value) {
await continuePending()
return
}
const saved = await saveConfig({ quiet: true })
if (saved) await applyConfig({ successMessage: '配置已保存并应用' })
}
const continuePending = async () => {
if (!pendingCandidate.value) return
if (!pendingCandidate.value.sameSession) {
const resumed = await managePending('resume')
if (!resumed) return
}
await applyConfig()
}
const previewRollback = async () => {
if (!canRollback.value || working.value || !rollbackGeneration.value) return
working.value = 'preview'
@@ -715,7 +741,7 @@ export function usePage(pageKey = 'base') {
sectionError, sensitiveHint, secretField, secretValue,
confirmed, savedGeneration, pendingCandidate, managePending, validationResult, canWrite, canRollback, canAudit, canExportAudit,
backupsLoading, backupsError, backups, rollbackGeneration, rollbackConfirmed, rollbackPreview,
auditLoading, auditError, auditItems, loadAll, loadSection, validateConfig, saveConfig, applyConfig,
auditLoading, auditError, auditItems, loadAll, loadSection, validateConfig, saveConfig, applyConfig, saveAndApply, continuePending,
loadBackups, previewRollback, executeRollback, loadAudit, exportAudit, formatTime,
serviceLabel, sectionLabel, operationLabel, databaseConfig, databaseTypeLabel, canMigrate,
migrationTargetName, migrationPreview, migrationToken, migrationRecord, migrationStatusLabel, migrationStatusType,
@@ -56,17 +56,19 @@
margin-top: -4px;
}
.spam-confirmation {
margin-top: 20px;
}
.spam-actions {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 10px;
margin-top: 20px;
}
.spam-actions :deep(.el-button) {
min-width: 112px;
margin-left: 0;
}
.spam-result {
display: grid;
gap: 7px;
@@ -101,4 +103,4 @@
grid-template-columns: 1fr;
gap: 0;
}
}
}
@@ -1,4 +1,4 @@
import { onMounted, reactive, ref } from 'vue'
import { computed, onMounted, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { http } from '@/utils/axios'
import { useGlobalStatusStore } from '@/stores/useGlobalStatusStore'
@@ -16,7 +16,6 @@ export function useSpamBlockSettings() {
const loading = ref(false)
const working = ref('')
const errorMessage = ref('')
const confirmed = ref(false)
const savedGeneration = ref(0)
const pendingCandidate = ref(null)
let savedInput = ''
@@ -24,6 +23,7 @@ export function useSpamBlockSettings() {
const validationResult = ref(null)
const pluginsPayload = ref(null)
const settings = reactive({ ...defaultSettings })
const hasChanges = computed(() => JSON.stringify(settings) !== savedInput)
const canWrite = () => globalStatus.hasCapability('config.write_global')
@@ -95,7 +95,7 @@ export function useSpamBlockSettings() {
expectedGeneration: activeGeneration.value,
payload: buildPayload(),
confirmation: {
confirmed: confirmed.value,
confirmed: true,
method: 'recent_auth'
}
})
@@ -117,8 +117,8 @@ export function useSpamBlockSettings() {
}
}
const save = async () => {
if (!canWrite() || loading.value || working.value || errorMessage.value || pendingCandidate.value) return
const save = async ({ quiet = false } = {}) => {
if (!canWrite() || loading.value || working.value || errorMessage.value || pendingCandidate.value) return false
working.value = 'save'
try {
const response = await http.put('/api/v1/config/plugins', buildRequest())
@@ -128,19 +128,21 @@ export function useSpamBlockSettings() {
savedInput = JSON.stringify(settings)
validationResult.value = { valid: true, generation: savedGeneration.value, issues: [] }
await load()
ElMessage.success(`垃圾邮件插件配置已保存,代次 ${savedGeneration.value} 等待应用`)
if (!quiet) ElMessage.success('垃圾邮件插件配置已保存,等待应用')
return true
} catch (error) {
ElMessage.error(error.message || '插件配置保存失败')
return false
} finally {
working.value = ''
}
}
const apply = async () => {
if (!canWrite() || loading.value || working.value || !savedGeneration.value) return
const apply = async ({ successMessage = '垃圾邮件插件配置已应用' } = {}) => {
if (!canWrite() || loading.value || working.value || !savedGeneration.value) return false
if (savedInput !== JSON.stringify(settings)) {
ElMessage.warning('填写内容与候选不一致,请刷新查看候选,或先取消候选再修改')
return
return false
}
working.value = 'apply'
try {
@@ -151,49 +153,75 @@ export function useSpamBlockSettings() {
notifyResponseError(response, '插件配置应用失败')
savedGeneration.value = 0
await load()
ElMessage.success('垃圾邮件插件配置已应用')
ElMessage.success(successMessage)
return true
} catch (error) {
ElMessage.error(error.message || '垃圾邮件插件配置应用失败')
return false
} finally {
working.value = ''
}
}
const managePending = async action => {
if (!canWrite() || loading.value || working.value || !pendingCandidate.value) return
if (!canWrite() || loading.value || working.value || !pendingCandidate.value) return false
working.value = action
try {
await ElMessageBox.confirm(action === 'cancel' ? '取消候选后不会影响当前生效配置,确认继续吗?' : '确认将已保存候选恢复到当前会话吗?', '管理候选配置', { type: 'warning', confirmButtonText: '确认', cancelButtonText: '返回' })
if (action === 'cancel') {
await ElMessageBox.confirm('放弃后会保留当前生效配置,本次修改不会应用。', '放弃本次更改', { type: 'warning', confirmButtonText: '放弃更改', cancelButtonText: '返回' })
}
const response = await http.post('/api/v1/config/pending', {
generation: pendingCandidate.value.generation, action,
candidateId: pendingCandidate.value.candidateId,
confirmation: { confirmed: confirmed.value, method: 'recent_auth' }
confirmation: { confirmed: true, method: 'recent_auth' }
})
notifyResponseError(response, '候选配置操作失败')
await load()
if (action === 'cancel') ElMessage.success('本次更改已放弃')
return true
} catch (error) {
if (error !== 'cancel' && error !== 'close') ElMessage.error(error.message || '候选配置操作失败')
return false
} finally {
working.value = ''
}
}
const saveAndApply = async () => {
if (pendingCandidate.value) {
await continuePending()
return
}
const saved = await save({ quiet: true })
if (saved) await apply({ successMessage: '垃圾邮件插件配置已保存并应用' })
}
const continuePending = async () => {
if (!pendingCandidate.value) return
if (!pendingCandidate.value.sameSession) {
const resumed = await managePending('resume')
if (!resumed) return
}
await apply()
}
onMounted(load)
return {
loading,
working,
errorMessage,
confirmed,
savedGeneration,
pendingCandidate,
managePending,
validationResult,
settings,
hasChanges,
canWrite,
load,
validate,
save,
apply
apply,
saveAndApply,
continuePending
}
}
@@ -25,7 +25,7 @@
show-icon
/>
<el-alert v-if="pendingCandidate" title="存在待应用候选,编辑已锁定。刷新可恢复候选,需要修改时请先取消。" type="warning" :closable="false" show-icon />
<el-alert v-if="pendingCandidate" title="有一项配置尚未生效,可以继续应用或放弃更改。" type="warning" :closable="false" show-icon />
<el-form label-position="top" class="spam-form">
<el-form-item label="模型 API 接口地址">
<el-input
@@ -60,21 +60,29 @@
</div>
<p class="spam-help">阈值越低,过滤越严格,也更容易产生误判;请根据模型返回结果逐步调整。</p>
<el-checkbox v-if="canWrite()" v-model="confirmed" class="spam-confirmation">
我已确认这是受控变更,并允许使用最近认证完成高风险配置操作
</el-checkbox>
<div class="spam-actions">
<el-button :loading="working === 'validate'" :disabled="!canWrite() || !!working || !!pendingCandidate || loading" @click="validate">只验证</el-button>
<el-button type="primary" :loading="working === 'save'" :disabled="!canWrite() || !!working || !!pendingCandidate || loading" @click="save">保存候选配置</el-button>
<el-button type="success" plain :loading="working === 'apply'" :disabled="!canWrite() || !savedGeneration || !!working || loading" @click="apply">
应用{{ savedGeneration ? `代次 ${savedGeneration}` : '' }}
<el-button
v-if="!pendingCandidate"
type="primary"
:loading="working === 'save' || working === 'apply'"
:disabled="!canWrite() || !!working || loading || !hasChanges"
@click="saveAndApply"
>
保存并应用
</el-button>
<el-button v-if="canWrite() && pendingCandidate && !pendingCandidate.sameSession" :disabled="!!working || !confirmed" @click="managePending('resume')">恢复候选到当前会话</el-button>
<el-button v-if="canWrite() && pendingCandidate" type="warning" plain :disabled="!!working || pendingCandidate.committed" @click="managePending('cancel')">取消候选配置</el-button>
<el-button
v-else
type="primary"
:loading="working === 'resume' || working === 'apply'"
:disabled="!canWrite() || !!working || loading"
@click="continuePending"
>
继续应用
</el-button>
<el-button v-if="canWrite() && pendingCandidate" type="danger" plain :disabled="!!working || pendingCandidate.committed" @click="managePending('cancel')">放弃更改</el-button>
</div>
<div v-if="validationResult" class="spam-result" :class="validationResult.valid ? 'is-success' : 'is-error'">
<strong>{{ validationResult.valid ? '验证通过' : '验证未通过' }}</strong>
<span v-if="validationResult.generation">候选代次:{{ validationResult.generation }}</span>
<div v-if="validationResult && !validationResult.valid" class="spam-result is-error">
<strong>配置检查未通过</strong>
<ul v-if="validationResult.issues?.length">
<li v-for="issue in validationResult.issues" :key="`${issue.code}-${issue.message}`">{{ issue.code }}:{{ issue.message }}</li>
</ul>
@@ -90,17 +98,15 @@ const {
loading,
working,
errorMessage,
confirmed,
savedGeneration,
pendingCandidate,
managePending,
validationResult,
settings,
hasChanges,
canWrite,
load,
validate,
save,
apply
saveAndApply,
continuePending
} = useSpamBlockSettings()
</script>