主要成果:
- 新增独立插件 SDK:[PluginContracts.cs (line 11)](E:/jiaowu/src/Jiaowu.Plugin.Abstractions/PluginContracts.cs:11)
- 支持 RSA-PSS/SHA-256 签名插件包、Host API 兼容性和 ZIP 安全检查:[PluginPackageValidator.cs (line 14)](E:/jiaowu/src/Jiaowu.Api/Infrastructure/Plugins/PluginPackageValidator.cs:14)
- 支持暂存、待激活、重启装载、失败记录、版本回滚:[PluginPackageService.cs (line 32)](E:/jiaowu/src/Jiaowu.Api/Infrastructure/Plugins/PluginPackageService.cs:32)
- 插件可注册控制器、服务、数据库迁移、后台任务和事件处理器
- 外部插件 API 统一使用 /api/plugin-extensions/{pluginId},并受启用状态中间件控制
- 插件中心新增 ZIP/SIG 上传、状态展示、激活、回滚和删除功能:[PluginsView.vue (line 212)](E:/jiaowu/web/src/views/PluginsView.vue:212)
- 提供可运行示例插件:[SamplePlugin.cs (line 10)](E:/jiaowu/samples/Jiaowu.SamplePlugin/SamplePlugin.cs:10)
- 提供打包签名脚本:[package-plugin.ps1](E:/jiaowu/scripts/package-plugin.ps1)
- 完整开发文档:[plugin-sdk.md (line 1)](E:/jiaowu/docs/plugin-sdk.md:1)
- 系统版本已调整为 3.0.0-beta1
426 lines
15 KiB
Vue
426 lines
15 KiB
Vue
<script setup lang="ts">
|
||
import { computed, onMounted, ref } from 'vue'
|
||
import { Connection, Lock, SwitchButton, Upload } from '@element-plus/icons-vue'
|
||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||
import http, { apiErrorMessage } from '../api/http'
|
||
import { type PluginRuntimeItem, usePluginStore } from '../stores/plugins'
|
||
|
||
const pluginStore = usePluginStore()
|
||
const loading = ref(true)
|
||
const changingId = ref('')
|
||
const uploading = ref(false)
|
||
const packageInput = ref<HTMLInputElement>()
|
||
const signatureInput = ref<HTMLInputElement>()
|
||
const packageFile = ref<File>()
|
||
const signatureFile = ref<File>()
|
||
|
||
interface PackageRelease {
|
||
id: string
|
||
pluginId: string
|
||
name: string
|
||
version: string
|
||
publisherId: string
|
||
hostApiVersion: string
|
||
sha256: string
|
||
packageSize: number
|
||
status: 'Staged' | 'PendingActivation' | 'Active' | 'Superseded' | 'Failed'
|
||
createdAt: string
|
||
activationRequestedAt?: string
|
||
failureMessage?: string
|
||
}
|
||
|
||
const packageReleases = ref<PackageRelease[]>([])
|
||
|
||
const groupedPlugins = computed(() => {
|
||
const groups = new Map<string, PluginRuntimeItem[]>()
|
||
for (const plugin of pluginStore.items) {
|
||
const items = groups.get(plugin.category) ?? []
|
||
items.push(plugin)
|
||
groups.set(plugin.category, items)
|
||
}
|
||
return Array.from(groups, ([category, items]) => ({ category, items }))
|
||
})
|
||
|
||
const enabledCount = computed(() =>
|
||
pluginStore.items.filter((plugin) => plugin.isEnabled).length,
|
||
)
|
||
|
||
async function loadPlugins() {
|
||
loading.value = true
|
||
try {
|
||
const [, packageResponse] = await Promise.all([
|
||
pluginStore.load(true),
|
||
http.get<PackageRelease[]>('/plugins/packages'),
|
||
])
|
||
packageReleases.value = packageResponse.data
|
||
} catch (error) {
|
||
ElMessage.error(apiErrorMessage(error))
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
function selectFile(event: Event, target: 'package' | 'signature') {
|
||
const file = (event.target as HTMLInputElement).files?.[0]
|
||
if (target === 'package') packageFile.value = file
|
||
else signatureFile.value = file
|
||
}
|
||
|
||
async function uploadPackage() {
|
||
if (!packageFile.value || !signatureFile.value) {
|
||
ElMessage.warning('请选择插件 ZIP 包和对应的签名文件。')
|
||
return
|
||
}
|
||
uploading.value = true
|
||
try {
|
||
const body = new FormData()
|
||
body.append('package', packageFile.value)
|
||
body.append('signature', signatureFile.value)
|
||
await http.post('/plugins/packages', body)
|
||
packageFile.value = undefined
|
||
signatureFile.value = undefined
|
||
if (packageInput.value) packageInput.value.value = ''
|
||
if (signatureInput.value) signatureInput.value.value = ''
|
||
ElMessage.success('签名校验通过,插件包已进入暂存区。')
|
||
await loadPlugins()
|
||
} catch (error) {
|
||
ElMessage.error(apiErrorMessage(error))
|
||
} finally {
|
||
uploading.value = false
|
||
}
|
||
}
|
||
|
||
async function activatePackage(row: unknown) {
|
||
const release = row as PackageRelease
|
||
try {
|
||
await ElMessageBox.confirm(
|
||
`版本 ${release.version} 将在下次服务重启时激活。当前活动版本会保留,可用同样方式回滚。`,
|
||
`激活 ${release.name}`,
|
||
{ confirmButtonText: '标记并等待重启', cancelButtonText: '取消', type: 'warning' },
|
||
)
|
||
await http.post(`/plugins/packages/${release.id}/activate`)
|
||
ElMessage.success('已标记为待激活,请按维护流程重启服务。')
|
||
await loadPlugins()
|
||
} catch (error) {
|
||
if (error === 'cancel' || error === 'close') return
|
||
ElMessage.error(apiErrorMessage(error))
|
||
}
|
||
}
|
||
|
||
async function deletePackage(row: unknown) {
|
||
const release = row as PackageRelease
|
||
try {
|
||
await ElMessageBox.confirm(
|
||
`确认删除 ${release.name} ${release.version} 的包记录?`,
|
||
'删除插件包',
|
||
{ confirmButtonText: '删除', cancelButtonText: '取消', type: 'warning' },
|
||
)
|
||
await http.delete(`/plugins/packages/${release.id}`)
|
||
ElMessage.success('插件包记录已删除。')
|
||
await loadPlugins()
|
||
} catch (error) {
|
||
if (error === 'cancel' || error === 'close') return
|
||
ElMessage.error(apiErrorMessage(error))
|
||
}
|
||
}
|
||
|
||
function packageStatus(status: PackageRelease['status']) {
|
||
return {
|
||
Staged: '已暂存',
|
||
PendingActivation: '待重启激活',
|
||
Active: '活动版本',
|
||
Superseded: '历史版本',
|
||
Failed: '装载失败',
|
||
}[status]
|
||
}
|
||
|
||
function formatSize(bytes: number) {
|
||
return bytes < 1024 * 1024
|
||
? `${(bytes / 1024).toFixed(1)} KB`
|
||
: `${(bytes / 1024 / 1024).toFixed(1)} MB`
|
||
}
|
||
|
||
async function changeState(plugin: PluginRuntimeItem, value: string | number | boolean) {
|
||
if (typeof value !== 'boolean' || value === plugin.isEnabled) return
|
||
const action = value ? '启用' : '停用'
|
||
try {
|
||
await ElMessageBox.confirm(
|
||
value
|
||
? `启用后,具备原有角色权限的用户将重新看到并可使用“${plugin.name}”。`
|
||
: `停用后,“${plugin.name}”的菜单、页面和后端接口都会关闭,已有数据不会删除。`,
|
||
`${action}${plugin.name}`,
|
||
{
|
||
confirmButtonText: `确认${action}`,
|
||
cancelButtonText: '取消',
|
||
type: value ? 'success' : 'warning',
|
||
},
|
||
)
|
||
} catch {
|
||
return
|
||
}
|
||
|
||
changingId.value = plugin.id
|
||
try {
|
||
const { data } = await http.put<PluginRuntimeItem>(
|
||
`/plugins/${encodeURIComponent(plugin.id)}/state`,
|
||
{ isEnabled: value },
|
||
)
|
||
pluginStore.update(data)
|
||
ElMessage.success(`${plugin.name}已${action}。`)
|
||
} catch (error) {
|
||
ElMessage.error(apiErrorMessage(error))
|
||
} finally {
|
||
changingId.value = ''
|
||
}
|
||
}
|
||
|
||
function formatTime(value?: string) {
|
||
return value
|
||
? new Date(value).toLocaleString('zh-CN', { hour12: false })
|
||
: '随当前系统版本安装'
|
||
}
|
||
|
||
onMounted(loadPlugins)
|
||
</script>
|
||
|
||
<template>
|
||
<main v-loading="loading" class="plugin-center">
|
||
<header class="plugin-heading">
|
||
<div>
|
||
<span>EXTENSION CONTROL PLANE</span>
|
||
<h2>统一插件平台</h2>
|
||
<p>集中管理内置业务能力的入口、运行状态和服务端访问边界。</p>
|
||
</div>
|
||
<div class="plugin-summary">
|
||
<strong>{{ enabledCount }}</strong>
|
||
<span>/ {{ pluginStore.items.length }} 个插件运行中</span>
|
||
</div>
|
||
</header>
|
||
|
||
<section class="safety-strip">
|
||
<el-icon><Lock /></el-icon>
|
||
<div>
|
||
<strong>停用不会删除数据</strong>
|
||
<p>状态变更立即作用于菜单、路由和 API;角色权限与数据范围校验仍由原业务接口负责。</p>
|
||
</div>
|
||
</section>
|
||
|
||
<section class="package-panel">
|
||
<div class="package-heading">
|
||
<div>
|
||
<span>SIGNED PACKAGE PIPELINE</span>
|
||
<h3>签名插件包</h3>
|
||
<p>上传只进入暂存区;校验通过后仍需标记激活并重启服务。</p>
|
||
</div>
|
||
<div class="package-upload">
|
||
<label>
|
||
<input ref="packageInput" type="file" accept=".zip" @change="selectFile($event, 'package')">
|
||
{{ packageFile?.name || '选择 ZIP 包' }}
|
||
</label>
|
||
<label>
|
||
<input ref="signatureInput" type="file" accept=".sig,.bin" @change="selectFile($event, 'signature')">
|
||
{{ signatureFile?.name || '选择签名文件' }}
|
||
</label>
|
||
<el-button type="primary" :icon="Upload" :loading="uploading" @click="uploadPackage">
|
||
校验并暂存
|
||
</el-button>
|
||
</div>
|
||
</div>
|
||
<el-table :data="packageReleases" empty-text="尚未上传签名插件包">
|
||
<el-table-column label="插件 / 版本" min-width="210">
|
||
<template #default="{ row }">
|
||
<strong>{{ row.name }}</strong>
|
||
<small>{{ row.pluginId }} · v{{ row.version }}</small>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column label="发布者" min-width="130">
|
||
<template #default="{ row }">{{ row.publisherId }}</template>
|
||
</el-table-column>
|
||
<el-table-column label="校验" min-width="190">
|
||
<template #default="{ row }">
|
||
<code>API {{ row.hostApiVersion }} · {{ formatSize(row.packageSize) }}</code>
|
||
<small>SHA-256 {{ row.sha256.slice(0, 12) }}…</small>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column label="状态" width="130">
|
||
<template #default="{ row }">
|
||
<el-tag :type="row.status === 'Active' ? 'success' : row.status === 'Failed' ? 'danger' : row.status === 'PendingActivation' ? 'warning' : 'info'">
|
||
{{ packageStatus(row.status) }}
|
||
</el-tag>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column label="操作" width="190" fixed="right">
|
||
<template #default="{ row }">
|
||
<el-button
|
||
v-if="row.status !== 'Active' && row.status !== 'PendingActivation'"
|
||
link
|
||
type="primary"
|
||
@click="activatePackage(row)"
|
||
>
|
||
{{ row.status === 'Superseded' ? '回滚到此版本' : '激活' }}
|
||
</el-button>
|
||
<el-button
|
||
v-if="row.status === 'Staged' || row.status === 'Superseded'"
|
||
link
|
||
type="danger"
|
||
@click="deletePackage(row)"
|
||
>删除</el-button>
|
||
</template>
|
||
</el-table-column>
|
||
</el-table>
|
||
</section>
|
||
|
||
<section v-for="group in groupedPlugins" :key="group.category" class="plugin-group">
|
||
<div class="group-title">
|
||
<span>{{ group.category }}</span>
|
||
<i>{{ group.items.length }}</i>
|
||
</div>
|
||
<div class="plugin-grid">
|
||
<article
|
||
v-for="plugin in group.items"
|
||
:key="plugin.id"
|
||
class="plugin-card"
|
||
:class="{ disabled: !plugin.isEnabled }"
|
||
>
|
||
<div class="plugin-card-top">
|
||
<div class="plugin-symbol">
|
||
<el-icon><Connection v-if="plugin.isEnabled" /><SwitchButton v-else /></el-icon>
|
||
</div>
|
||
<div class="plugin-state">
|
||
<span :class="plugin.isEnabled ? 'online' : 'offline'">
|
||
{{ plugin.requiresRestart ? '等待重启' : plugin.isEnabled ? '运行中' : '已停用' }}
|
||
</span>
|
||
<el-switch
|
||
:model-value="plugin.isEnabled"
|
||
:loading="changingId === plugin.id"
|
||
:disabled="Boolean(changingId) || (plugin.source === 'package' && plugin.status !== 'Active')"
|
||
@change="(value) => changeState(plugin, value)"
|
||
/>
|
||
</div>
|
||
</div>
|
||
<h3>{{ plugin.name }}</h3>
|
||
<code>{{ plugin.id }} · v{{ plugin.version }}</code>
|
||
<p>{{ plugin.description }}</p>
|
||
<div class="capabilities">
|
||
<span v-for="capability in plugin.capabilities" :key="capability">
|
||
{{ capability }}
|
||
</span>
|
||
</div>
|
||
<footer>
|
||
<span>{{ plugin.navigationPaths.length }} 个页面入口</span>
|
||
<time>{{ formatTime(plugin.stateChangedAt) }}</time>
|
||
</footer>
|
||
</article>
|
||
</div>
|
||
</section>
|
||
</main>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.plugin-center {
|
||
--ink: #1f2930;
|
||
--muted: #6b7780;
|
||
--line: #d9dfe1;
|
||
--signal: #1f7468;
|
||
color: var(--ink);
|
||
display: grid;
|
||
gap: 18px;
|
||
padding-bottom: 30px;
|
||
}
|
||
|
||
.plugin-heading {
|
||
align-items: flex-end;
|
||
border-bottom: 1px solid var(--line);
|
||
display: flex;
|
||
justify-content: space-between;
|
||
padding: 5px 2px 17px;
|
||
}
|
||
|
||
.plugin-heading > div > span,
|
||
.group-title span {
|
||
color: #75828a;
|
||
font-family: "Cascadia Mono", Consolas, monospace;
|
||
font-size: 10px;
|
||
letter-spacing: .1em;
|
||
}
|
||
|
||
.plugin-heading h2 {
|
||
font-size: clamp(26px, 3vw, 38px);
|
||
letter-spacing: -.045em;
|
||
line-height: 1;
|
||
margin: 9px 0;
|
||
}
|
||
|
||
.plugin-heading p,
|
||
.safety-strip p { color: var(--muted); margin: 0; }
|
||
.plugin-summary { color: var(--muted); text-align: right; }
|
||
.plugin-summary strong { color: var(--signal); font-size: 34px; }
|
||
.plugin-summary span { font-size: 12px; }
|
||
|
||
.safety-strip {
|
||
align-items: center;
|
||
background: #f2f7f6;
|
||
border: 1px solid #cbdad7;
|
||
border-left: 4px solid var(--signal);
|
||
display: flex;
|
||
gap: 14px;
|
||
padding: 15px 18px;
|
||
}
|
||
|
||
.safety-strip .el-icon { color: var(--signal); font-size: 22px; }
|
||
.safety-strip strong { font-size: 13px; }
|
||
.safety-strip p { font-size: 12px; margin-top: 3px; }
|
||
.plugin-group { display: grid; gap: 10px; }
|
||
.package-panel { background: #fff; border: 1px solid var(--line); }
|
||
.package-heading { align-items: center; display: flex; gap: 24px; justify-content: space-between; padding: 18px; }
|
||
.package-heading span { color: #75828a; font-family: "Cascadia Mono", Consolas, monospace; font-size: 10px; letter-spacing: .1em; }
|
||
.package-heading h3 { margin: 5px 0; }
|
||
.package-heading p { color: var(--muted); font-size: 12px; margin: 0; }
|
||
.package-upload { align-items: center; display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
|
||
.package-upload label { border: 1px dashed #b9c4c7; color: #637078; cursor: pointer; font-size: 11px; max-width: 160px; overflow: hidden; padding: 8px 10px; text-overflow: ellipsis; white-space: nowrap; }
|
||
.package-upload input { display: none; }
|
||
.package-panel strong,.package-panel small { display: block; }
|
||
.package-panel small { color: #7d898f; font-size: 10px; margin-top: 4px; }
|
||
.package-panel code { color: #53636b; font-size: 10px; }
|
||
.group-title { align-items: center; display: flex; gap: 8px; }
|
||
.group-title i { background: #e8eded; border-radius: 9px; font-size: 10px; font-style: normal; padding: 2px 7px; }
|
||
.plugin-grid { display: grid; gap: 14px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
|
||
|
||
.plugin-card {
|
||
background: #fff;
|
||
border: 1px solid var(--line);
|
||
border-top: 3px solid var(--signal);
|
||
display: flex;
|
||
flex-direction: column;
|
||
min-height: 245px;
|
||
padding: 18px;
|
||
transition: border-color .18s ease, opacity .18s ease;
|
||
}
|
||
|
||
.plugin-card.disabled { border-top-color: #9aa4aa; opacity: .72; }
|
||
.plugin-card-top { align-items: flex-start; display: flex; justify-content: space-between; }
|
||
.plugin-symbol { align-items: center; background: #e9f1ef; color: var(--signal); display: flex; height: 34px; justify-content: center; width: 34px; }
|
||
.plugin-card.disabled .plugin-symbol { background: #eef0f1; color: #7d878d; }
|
||
.plugin-state { align-items: center; display: flex; gap: 10px; }
|
||
.plugin-state span { font-size: 10px; font-weight: 750; }
|
||
.plugin-state .online { color: var(--signal); }
|
||
.plugin-state .offline { color: #7d878d; }
|
||
.plugin-card h3 { font-size: 18px; margin: 17px 0 4px; }
|
||
.plugin-card code { color: #7b868d; font-size: 10px; }
|
||
.plugin-card > p { color: var(--muted); font-size: 12px; line-height: 1.6; margin: 14px 0; }
|
||
.capabilities { display: flex; flex-wrap: wrap; gap: 6px; }
|
||
.capabilities span { background: #f1f4f4; color: #56646c; font-family: "Cascadia Mono", Consolas, monospace; font-size: 9px; padding: 4px 7px; }
|
||
.plugin-card footer { border-top: 1px solid #e5e9ea; color: #849097; display: flex; font-size: 9px; justify-content: space-between; margin-top: auto; padding-top: 13px; }
|
||
|
||
@media (prefers-reduced-motion: reduce) { .plugin-card { transition: none; } }
|
||
@media (max-width: 850px) { .plugin-grid { grid-template-columns: 1fr; } }
|
||
@media (max-width: 560px) {
|
||
.plugin-heading { align-items: flex-start; flex-direction: column; gap: 15px; }
|
||
.plugin-summary { text-align: left; }
|
||
.safety-strip { align-items: flex-start; }
|
||
.plugin-card footer { align-items: flex-start; flex-direction: column; gap: 5px; }
|
||
.package-heading { align-items: flex-start; flex-direction: column; }
|
||
.package-upload { justify-content: flex-start; }
|
||
}
|
||
</style>
|