整合插件配置入口并保留权限与未保存保护
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-08 14:17:14 +08:00
1 parent 82f740d128
commit 6a17af13ed
13 files changed
+346 -250

No files matched your search

@@ -3,7 +3,6 @@ import { useGlobalStatusStore } from '@/stores/useGlobalStatusStore'
import BaseConfig from './BaseConfig/BaseConfig.vue'
import EmailConfig from './EmailConfig/EmailConfig.vue'
import SslConfig from './SslConfig/SslConfig.vue'
import PluginsConfig from './PluginsConfig/PluginsConfig.vue'
import ConfigMaintenance from './ConfigMaintenance/ConfigMaintenance.vue'
export function useNavigation() {
@@ -15,12 +14,7 @@ export function useNavigation() {
const pages = [
{ value: 'base', label: '基础配置', component: BaseConfig },
{ value: 'mail', label: '邮件服务', component: EmailConfig },
{ value: 'ssl', label: 'SSL 配置', component: SslConfig },
{ value: 'plugins', label: '插件配置', children: [
{ value: 'spam_block', label: '垃圾邮件拦截', component: PluginsConfig },
{ value: 'telegram_push', label: 'Telegram 推送', component: PluginsConfig },
{ value: 'wechat_push', label: '微信推送', component: PluginsConfig }
] }
{ value: 'ssl', label: 'SSL 配置', component: SslConfig }
]
const tools = [
{ value: 'migration', label: '数据库迁移', capability: 'database.migrate' },
@@ -83,7 +83,7 @@ export function usePage(pageKey = 'base') {
const scope = pending.payload && loadedText.value
? (changed.length ? `上次未完成的保存涉及:${changed.join('、')}。` : '上次未完成保存的插件运行配置未变化。')
: '上次未完成保存的内容尚未完整读取,请刷新后核对。'
return `${scope}三个插件共用一组运行配置,请在对应三级菜单核对;完成保存时会统一生效,其他插件的原有配置会保留。`
return `${scope}三个插件共用一组运行配置,请在“插件配置”的对应页面核对;完成保存时会统一生效,其他插件的原有配置会保留。`
}
if (pending?.section !== 'mail' || apiSection() !== 'mail') return ''
const active = readFormConfig(loadedText.value)
@@ -53,7 +53,8 @@ export function usePage() {
{ key: 'rule', label: lang.rule_setting, hint: '邮件处理规则', icon: Filter, component: RuleSettings, capability: 'user.read' },
{ key: 'users', label: lang.user_management, hint: '账号与权限管理', icon: User, component: UserManagement, capability: 'user.manage' },
{ key: 'plugins', label: lang.plugin_settings, hint: '扩展功能管理', icon: Grid, component: PluginSettings, capability: 'plugin.read' },
].filter(item => globalStatus.hasCapability(item.capability)))
].filter(item => globalStatus.hasCapability(item.capability) ||
(item.key === 'plugins' && globalStatus.hasCapability('config.read'))))
const activeSettings = computed(() => settingsMenu.value.find(item => item.key === globalStatus.settingsActiveTab))
const settingsPages = new Map()
@@ -1,34 +1,19 @@
.settings-card {
height: 100%;
display: flex;
flex-direction: column;
}
.settings-header {
margin-bottom: 18px;
}
.settings-header h3 {
font-size: 18px;
font-weight: 600;
color: var(--pm-text-primary);
margin: 0 0 6px;
}
.settings-desc {
font-size: 14px;
color: var(--pm-text-secondary);
margin: 0;
}
.plugin-container {
flex: 1;
.plugin-settings {
min-width: 0;
}
.plugin-list {
.plugin-list-error {
display: grid;
gap: 12px;
justify-items: start;
gap: 8px;
padding: 8px;
color: var(--el-color-danger);
font-size: 12px;
overflow-wrap: anywhere;
}
.plugin-settings :deep(.telegram-settings) {
padding: 0;
}
.plugin-section {
@@ -55,29 +40,7 @@
background: var(--pm-bg-primary);
}
.plugin-section-title {
display: flex;
align-items: center;
min-width: 0;
gap: 12px;
}
.plugin-section-index {
display: inline-flex;
align-items: center;
justify-content: center;
width: 30px;
height: 30px;
flex: 0 0 30px;
border-radius: 9px;
color: var(--pm-primary-color);
background: color-mix(in srgb, var(--pm-primary-color) 12%, var(--pm-bg-secondary));
font-size: 12px;
font-weight: 700;
letter-spacing: .04em;
}
.plugin-section-header h4 {
.plugin-section-header h3 {
overflow: hidden;
margin: 0 0 2px;
color: var(--pm-text-primary);
@@ -87,27 +50,6 @@
white-space: nowrap;
}
.plugin-section-header p {
margin: 0;
color: var(--pm-text-secondary);
font-size: 12px;
}
.plugin-section-status {
flex: 0 0 auto;
padding: 4px 9px;
border: 1px solid color-mix(in srgb, #16a34a 22%, var(--pm-border-color));
border-radius: 999px;
color: #15803d;
background: #f0fdf4;
font-size: 12px;
line-height: 1;
}
.plugin-section-status.is-active {
font-weight: 600;
}
.plugin-section :deep(button:focus-visible),
.plugin-section :deep(input:focus-visible),
.plugin-section :deep(textarea:focus-visible),
@@ -116,10 +58,6 @@
outline-offset: 2px;
}
.plugin-section-content {
min-width: 0;
}
.iframe-wrapper {
height: 320px;
min-height: 240px;
@@ -133,33 +71,11 @@ iframe {
border: 0;
}
.empty-state {
display: flex;
min-height: 260px;
justify-content: center;
align-items: center;
border: 1px dashed var(--pm-border-color);
border-radius: var(--pm-radius-md);
background: var(--pm-bg-secondary);
}
@media (max-width: 768px) {
.settings-header {
margin-bottom: 14px;
}
.plugin-list {
gap: 10px;
}
.plugin-section-header {
padding: 10px 12px;
}
.plugin-section-status {
display: none;
}
.iframe-wrapper {
height: 280px;
}
@@ -1,80 +1,80 @@
import { computed, reactive, ref } from 'vue'
import { computed, nextTick, onMounted, onBeforeUnmount, ref } from 'vue'
import { http } from '@/utils/axios'
import lang from '../../../i18n/i18n'
import { useGlobalStatusStore } from '@/stores/useGlobalStatusStore'
export function usePage() {
const pluginList = reactive({})
const pluginFrames = ref([])
const store = useGlobalStatusStore()
const selectedSection = ref(store.hasCapability('config.read') ? 'spam_block' : 'telegram-user')
const currentPage = ref(null)
const pageHost = ref(null)
const switching = ref(false)
const externalPlugins = ref([])
const loading = ref(false)
const errorMessage = ref('')
let disposed = false
const pluginLogicalID = name => {
const aliases = {
SpamBlock: 'spam_block',
WechatPush: 'wechat_push',
TelegramPush: 'telegram_push'
// 全局配置不依赖正在运行的插件列表,未启用的插件也必须可以配置。
const sectionOptions = computed(() => store.hasCapability('config.read') ? [
{ value: 'spam_block', label: '垃圾邮件拦截', kind: 'config' },
{ value: 'telegram_push', label: 'Telegram 推送', kind: 'config' },
{ value: 'wechat_push', label: '微信推送', kind: 'config' }
] : [])
const accountOptions = computed(() => store.hasCapability('plugin.read') && store.hasCapability('plugin.write_own')
? [{ value: 'telegram-user', label: 'Telegram 通知', kind: 'account' }]
: [])
const canReadExternal = () => store.hasCapability('config.read') && store.hasCapability('plugin.read')
const externalOptions = computed(() => canReadExternal() ? externalPlugins.value : [])
const visiblePages = () => [...sectionOptions.value, ...accountOptions.value, ...externalOptions.value]
const activePage = computed(() => visiblePages().find(item => item.value === selectedSection.value))
const beforeLeave = () => currentPage.value?.beforeLeave?.() ?? true
const selectSection = async value => {
const visible = () => visiblePages().some(item => item.value === value)
if (disposed || switching.value || value === selectedSection.value || !visible()) return
switching.value = true
try {
if (!await beforeLeave() || disposed || !visible()) return
selectedSection.value = value
await nextTick()
pageHost.value?.closest('.el-tabs__content')?.scrollTo({ top: 0 })
pageHost.value?.focus({ preventScroll: true })
} finally {
switching.value = false
}
return aliases[name] || name
}
const pluginEntries = computed(() => {
const order = {
spam_block: 1,
telegram_push: 2,
wechat_push: 3
}
const labels = {
spam_block: {
label: '垃圾邮件拦截',
description: '模型识别与过滤策略'
},
telegram_push: {
label: 'Telegram 推送',
description: '邮件通知与消息推送'
},
wechat_push: {
label: '微信推送',
description: '插件页面与推送参数'
const load = async () => {
if (disposed || loading.value || !canReadExternal()) return
loading.value = true
errorMessage.value = ''
try {
const response = await http.get('/api/v1/plugin/list')
if (disposed || !canReadExternal()) return
if (response?.errorNo !== 0 || !Array.isArray(response.data)) {
throw new Error(response?.errorMsg || '其他插件列表读取失败')
}
}
return Object.entries(pluginList)
.map(([name, src]) => {
const logicalID = pluginLogicalID(name)
const metadata = labels[logicalID] || {
const builtins = new Set([
'spam_block', 'telegram_push', 'wechat_push',
'SpamBlock', 'TelegramPush', 'WechatPush', 'WeChatPushHook'
])
externalPlugins.value = [...new Set(response.data)]
.filter(name => typeof name === 'string' && /^[a-zA-Z0-9_-]{1,64}$/.test(name) && !builtins.has(name))
.map(name => ({
value: `external:${name}`,
label: name,
description: '插件配置页面'
}
return {
name,
src,
logicalID,
label: metadata.label,
description: metadata.description
}
})
.sort((left, right) => (order[left.logicalID] || 99) - (order[right.logicalID] || 99))
})
http.get('/api/v1/plugin/list').then(response => {
if (Array.isArray(response.data) && response.data.length > 0) {
for (const name of response.data) {
if (typeof name !== 'string' || !/^[a-zA-Z0-9_-]{1,64}$/.test(name)) {
continue
}
pluginList[name] = `/api/v1/plugin/settings/${encodeURIComponent(name)}/index.html`
}
kind: 'external',
src: `/api/v1/plugin/settings/${encodeURIComponent(name)}/index.html`
}))
} catch (error) {
if (!disposed && canReadExternal()) errorMessage.value = error.message || '其他插件列表读取失败'
} finally {
loading.value = false
}
})
}
onMounted(load)
onBeforeUnmount(() => { disposed = true })
return {
ref,
reactive,
http,
lang,
pluginList,
pluginEntries,
pluginFrames
sectionOptions, accountOptions, externalOptions, selectedSection, activePage,
currentPage, pageHost, selectSection, beforeLeave, load, loading, errorMessage
}
}
@@ -1,46 +1,56 @@
<template>
<div class="settings-card">
<div class="settings-header">
<h3>{{ lang.extensions }}</h3>
<p class="settings-desc">{{ lang.extensions_desc }}</p>
</div>
<div class="plugin-container">
<div v-if="pluginEntries.length" class="plugin-list">
<section
v-for="(entry, index) in pluginEntries"
:key="entry.src"
class="plugin-section"
>
<header class="plugin-section-header">
<div class="plugin-section-title">
<span class="plugin-section-index">{{ String(index + 1).padStart(2, '0') }}</span>
<div>
<h4>{{ entry.label }}</h4>
<p>{{ entry.description }}</p>
</div>
</div>
<span class="plugin-section-status is-active">已启用</span>
</header>
<div class="plugin-section-content">
<SpamBlockSettings v-if="entry.logicalID === 'spam_block'" />
<TelegramSettings v-else-if="entry.logicalID === 'telegram_push'" />
<div v-else class="iframe-wrapper">
<iframe
:src="entry.src"
sandbox="allow-scripts"
referrerpolicy="no-referrer"
ref="pluginFrames"
:data-plugin="entry.name"
:title="entry.name"
/>
</div>
<div class="config-settings plugin-settings">
<div class="config-workspace">
<aside class="config-navigation">
<SettingsSubmenu
v-if="sectionOptions.length"
title="插件配置"
caption="运行配置"
:items="sectionOptions"
:model-value="selectedSection"
@select="selectSection"
/>
<SettingsSubmenu
v-if="accountOptions.length"
title="账号通知"
caption="当前账号"
:items="accountOptions"
:model-value="selectedSection"
@select="selectSection"
/>
<SettingsSubmenu
v-if="externalOptions.length"
title="其他插件"
:items="externalOptions"
:model-value="selectedSection"
@select="selectSection"
/>
<div v-if="errorMessage" class="plugin-list-error" role="alert">
<span>{{ errorMessage }}</span>
<el-button size="small" :loading="loading" @click="load">重试</el-button>
</div>
</aside>
<div ref="pageHost" class="config-main" :aria-label="activePage?.label" tabindex="-1">
<PluginsConfig
v-if="activePage?.kind === 'config'"
:key="selectedSection"
ref="currentPage"
:page-key="selectedSection"
/>
<TelegramSettings v-else-if="activePage?.kind === 'account'" ref="currentPage" />
<section v-else-if="activePage?.kind === 'external'" class="plugin-section">
<header class="plugin-section-header"><h3>{{ activePage.label }}</h3></header>
<div class="iframe-wrapper">
<iframe
:key="activePage.value"
:src="activePage.src"
sandbox="allow-scripts"
referrerpolicy="no-referrer"
:title="activePage.label"
/>
</div>
</section>
</div>
<div v-else class="empty-state">
<el-empty :description="lang.no_plugins_installed" :image-size="120" />
<el-empty v-else description="当前账号没有可访问的插件配置" :image-size="100" />
</div>
</div>
</div>
@@ -48,13 +58,17 @@
<script setup>
import { usePage } from './PluginSettings.js'
import SpamBlockSettings from './SpamBlockSettings/SpamBlockSettings.vue'
import SettingsSubmenu from '../SettingsSubmenu/SettingsSubmenu.vue'
import PluginsConfig from '../ConfigSettings/PluginsConfig/PluginsConfig.vue'
import TelegramSettings from './TelegramSettings/TelegramSettings.vue'
const {
lang,
pluginEntries
sectionOptions, accountOptions, externalOptions, selectedSection, activePage,
currentPage, pageHost, selectSection, beforeLeave, load, loading, errorMessage
} = usePage()
defineExpose({ beforeLeave })
</script>
<style scoped src="../ConfigSettings/ConfigSettings.css"></style>
<style scoped src="./PluginSettings.css"></style>
@@ -1,5 +1,5 @@
import { computed, onMounted, onBeforeUnmount, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
import { http } from '@/utils/axios'
const endpoint = '/api/v1/plugin/settings/telegram_push'
@@ -18,6 +18,8 @@ export function useTelegramSettings() {
const working = ref('')
const errorMessage = ref('')
const botUsername = ref('')
const savedSettings = ref('')
const hasChanges = computed(() => ready.value && JSON.stringify(settings) !== savedSettings.value)
let disposed = false
const botLink = computed(() => /^[a-zA-Z0-9_]{5,32}$/.test(botUsername.value)
? `https://t.me/${botUsername.value}` : '')
@@ -34,8 +36,21 @@ export function useTelegramSettings() {
settings[key] = key === 'chatId' ? String(data.settings[key] || '') : data.settings[key] === true
}
version.value = data.version
savedSettings.value = JSON.stringify(settings)
ready.value = true
}
const beforeLeave = async () => {
if (working.value) return false
if (!hasChanges.value) return true
try {
await ElMessageBox.confirm('当前通知设置尚未保存,确定离开吗?', '未保存的更改', {
type: 'warning', confirmButtonText: '离开', cancelButtonText: '继续编辑'
})
return true
} catch {
return false
}
}
const load = async () => {
if (working.value || disposed) return
working.value = 'load'
@@ -83,8 +98,22 @@ export function useTelegramSettings() {
working.value = ''
}
}
const refresh = async () => {
if (await beforeLeave()) await load()
}
const preventUnload = event => {
if (!hasChanges.value && !working.value) return
event.preventDefault()
event.returnValue = ''
}
onMounted(load)
onBeforeUnmount(() => { disposed = true })
return { settings, version, ready, working, errorMessage, botLink, load, submit }
onMounted(() => {
if (typeof window !== 'undefined') window.addEventListener('beforeunload', preventUnload)
load()
})
onBeforeUnmount(() => {
disposed = true
if (typeof window !== 'undefined') window.removeEventListener('beforeunload', preventUnload)
})
return { settings, version, ready, working, errorMessage, botLink, load, refresh, submit, beforeLeave, hasChanges }
}
@@ -27,7 +27,7 @@
</el-form>
<p class="telegram-hint">启用正文或附件后,相应内容会发送到 Telegram,请确认聊天对象和隐私需求。</p>
<footer class="telegram-actions">
<el-button :disabled="!!working" @click="load">重新加载</el-button>
<el-button :disabled="!!working" @click="refresh">重新加载</el-button>
<el-button :disabled="!ready || !!working || !settings.chatId.trim()" @click="submit('test')">发送测试消息</el-button>
<el-button type="primary" :disabled="!ready || !!working" @click="submit('save')">保存设置</el-button>
</footer>
@@ -37,7 +37,9 @@
<script setup>
import { useTelegramSettings } from './TelegramSettings.js'
const { settings, ready, working, errorMessage, botLink, load, submit } = useTelegramSettings()
const { settings, ready, working, errorMessage, botLink, refresh, submit, beforeLeave } = useTelegramSettings()
defineExpose({ beforeLeave })
</script>
<style scoped src="./TelegramSettings.css"></style>