增加配置页就地身份确认
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-07 17:13:06 +08:00
1 parent 77abc51a9d
commit 659d98b589
10 files changed
+426 -13

No files matched your search

@@ -0,0 +1,49 @@
import { ElMessageBox } from 'element-plus'
import { http } from '@/utils/axios'
const recentAuthenticationRequired = 'RECENT_AUTH_REQUIRED'
const cancelledCode = 'RECENT_AUTH_CANCELLED'
export function isRecentAuthenticationCancelled(error) {
return error?.code === cancelledCode
}
export async function requestWithRecentAuthentication(request) {
try {
return await request()
} catch (error) {
if (error?.apiErrorCode !== recentAuthenticationRequired) throw error
}
let password = ''
try {
const result = await ElMessageBox.prompt(
'为保护敏感配置,请确认当前登录密码。确认后将自动继续应用。',
'确认当前密码',
{
inputType: 'password',
inputPlaceholder: '请输入当前密码',
confirmButtonText: '确认并继续',
cancelButtonText: '取消',
closeOnClickModal: false,
inputValidator: value => String(value || '').length > 0 || '请输入当前密码'
}
)
password = String(result.value || '')
} catch (error) {
const cancelled = new Error('已取消身份确认')
cancelled.code = cancelledCode
throw cancelled
}
try {
const response = await http.post('/api/v1/security/reauth', { password })
if (!response || response.errorNo !== 0) {
throw new Error(response?.errorMsg || '身份确认失败')
}
} finally {
password = ''
}
return request()
}
@@ -2,6 +2,7 @@ import { computed, onMounted, onBeforeUnmount, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { http } from '@/utils/axios'
import { useGlobalStatusStore } from '@/stores/useGlobalStatusStore'
import { isRecentAuthenticationCancelled, requestWithRecentAuthentication } from '@/utils/recentAuthentication'
import { readFormConfig, updateFormConfig, validateFormConfig } from './ConfigForm/ConfigForm.js'
const sections = [
@@ -566,7 +567,9 @@ export function usePage(pageKey = 'base') {
working.value = 'save'
try {
const request = buildRequest()
const response = await http.put(`/api/v1/config/${apiSection()}`, request)
const response = await requestWithRecentAuthentication(
() => http.put(`/api/v1/config/${apiSection()}`, request)
)
notifyResponseError(response, '配置保存失败')
savedGeneration.value = response.data?.generation || 0
pendingCandidate.value = { ...response.data, section: apiSection(), sameSession: true }
@@ -577,6 +580,7 @@ export function usePage(pageKey = 'base') {
if (!quiet) ElMessage.success('配置已保存,等待应用')
return true
} catch (error) {
if (isRecentAuthenticationCancelled(error)) return false
ElMessage.error(error.message || '配置保存失败')
return false
} finally {
@@ -593,10 +597,13 @@ export function usePage(pageKey = 'base') {
}
working.value = 'apply'
try {
const response = await http.post(`/api/v1/config/${apiSection()}/apply`, {
generation: savedGeneration.value,
candidateId: pendingCandidate.value?.candidateId
})
const response = await requestWithRecentAuthentication(() => http.post(
`/api/v1/config/${apiSection()}/apply`,
{
generation: savedGeneration.value,
candidateId: pendingCandidate.value?.candidateId
}
))
notifyResponseError(response, '配置应用失败')
savedGeneration.value = 0
await Promise.all([loadStatus(), loadSection()])
@@ -604,6 +611,7 @@ export function usePage(pageKey = 'base') {
ElMessage.success(successMessage)
return true
} catch (error) {
if (isRecentAuthenticationCancelled(error)) return false
ElMessage.error(error.message || '配置应用失败')
return false
} finally {
@@ -2,6 +2,7 @@ import { computed, onMounted, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { http } from '@/utils/axios'
import { useGlobalStatusStore } from '@/stores/useGlobalStatusStore'
import { isRecentAuthenticationCancelled, requestWithRecentAuthentication } from '@/utils/recentAuthentication'
const defaultSettings = {
url: '',
@@ -121,7 +122,9 @@ export function useSpamBlockSettings() {
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())
const response = await requestWithRecentAuthentication(
() => http.put('/api/v1/config/plugins', buildRequest())
)
notifyResponseError(response, '插件配置保存失败')
savedGeneration.value = Number(response.data?.generation || 0)
pendingCandidate.value = { ...response.data, section: 'plugins', sameSession: true }
@@ -131,6 +134,7 @@ export function useSpamBlockSettings() {
if (!quiet) ElMessage.success('垃圾邮件插件配置已保存,等待应用')
return true
} catch (error) {
if (isRecentAuthenticationCancelled(error)) return false
ElMessage.error(error.message || '插件配置保存失败')
return false
} finally {
@@ -146,16 +150,20 @@ export function useSpamBlockSettings() {
}
working.value = 'apply'
try {
const response = await http.post('/api/v1/config/plugins/apply', {
generation: savedGeneration.value,
candidateId: pendingCandidate.value?.candidateId
})
const response = await requestWithRecentAuthentication(() => http.post(
'/api/v1/config/plugins/apply',
{
generation: savedGeneration.value,
candidateId: pendingCandidate.value?.candidateId
}
))
notifyResponseError(response, '插件配置应用失败')
savedGeneration.value = 0
await load()
ElMessage.success(successMessage)
return true
} catch (error) {
if (isRecentAuthenticationCancelled(error)) return false
ElMessage.error(error.message || '垃圾邮件插件配置应用失败')
return false
} finally {