Files
37duAutomation/desktop/electron-shell/frontend/src/components/PluginPage.vue
T
puddy 87f995ca47 feat(electron-shell): 启动链路修复与执行/配置收口插件侧
- 启动链路 P1-P7 修复:预算对齐、完成事件推送、端口占用提示、前端阶段文案接管
- 静态缓存按 IS_FROZEN 区分开发/打包态,bootPhaseOwned 竞态修复
- 一键执行与参数配置统一收口到插件界面,定时任务引用 config_id
- 环境包迁移到 %LOCALAPPDATA%\37duAutomation\python-env,重装零解压
- shell-updater:缓存包 sha256 校验、静默升级后自动拉起新版、自动清理旧安装包
- after-pack 钩子生成 shell.version 修复打包态版本识别
- 插件 xq0031/xq0045 迁移至 plugins/XQ0001-0100/ 号段分桶中文目录
2026-09-28 14:20:35 +08:00

478 lines
20 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 v-if="plugin" class="plugin-page">
<!-- Hero:场景身份 + 运行状态(执行入口收口在插件界面内,壳子不再提供一键执行) -->
<section class="hero card">
<div class="hero-main">
<span class="avatar" :style="{ background: avatarBg }">{{ plugin.name.slice(0, 1) }}</span>
<div class="hero-meta">
<h2 class="p-name">{{ plugin.name }}
<span class="ver-tag">v{{ plugin.version }}</span>
</h2>
<p class="p-desc">{{ plugin.description || '暂无描述' }}</p>
<div class="p-badges">
<span v-if="plugin.platform" class="badge" :class="platformOnline ? 'on' : 'off'">
<i class="bdot"></i>{{ platformLabel }} {{ platformOnline ? '已登录' : '未登录' }}
</span>
</div>
</div>
</div>
<div class="hero-actions">
<div v-if="store.running" class="run-chip">
<span class="ring">
<svg viewBox="0 0 36 36" width="22" height="22">
<circle class="ring-bg" cx="18" cy="18" r="15.5" fill="none" stroke-width="3.5" />
<circle class="ring-fg" cx="18" cy="18" r="15.5" fill="none" stroke-width="3.5"
stroke-linecap="round" :stroke-dasharray="ringDash" transform="rotate(-90 18 18)" />
</svg>
</span>
<span>执行中 {{ store.progress }}%</span>
</div>
<el-button v-if="rollbackVer" size="small" text type="warning" class="rollback-btn"
:disabled="store.running" @click="doRollback">回滚 v{{ rollbackVer }}</el-button>
<span class="hero-tip">在插件界面内配置并执行,运行日志与历史见下方</span>
</div>
</section>
<!-- 结果横幅 -->
<section v-if="store.lastResult" class="banner" :class="store.lastResult.ok ? 'ok' : 'err'">
<span class="b-ic">{{ store.lastResult.ok ? '✓' : '✕' }}</span>
<div class="b-body">
<b>{{ store.lastResult.ok ? (store.lastSummary || '执行完成') : '执行失败' }}</b>
<span v-if="!store.lastResult.ok">{{ store.lastResult.error }}</span>
</div>
<button class="b-close" @click="closeBanner" title="关闭">✕</button>
</section>
<!-- 运行台:插件界面 / 运行日志 / 历史运行
执行与参数配置收口在插件界面内(插件自包含前端+执行);日志归属"本次运行" -->
<div class="run-tabs card">
<div class="rt-head">
<div class="rt-tabs">
<button v-for="t in tabs" :key="t.k" class="rt-tab" :class="{ on: tab === t.k }" @click="tab = t.k">
{{ t.label }}
<span v-if="t.k === 'log' && store.logs.length" class="rt-n">{{ store.logs.length }}</span>
<i v-if="t.k === 'log' && store.running" class="rt-live"></i>
</button>
</div>
</div>
<div class="rt-body">
<!-- 插件专属界面(同源 iframe,自动携带会话 cookie)
sandbox 白名单加固:允许脚本/同源/表单,禁用 top 跳转与弹窗
仅一层容器承载尺寸,不再叠提示条/卡片 -->
<div v-if="plugin.ui" v-show="tab === 'ui'" class="ui-pane">
<iframe :key="plugin.id" :src="uiSrc" class="pui-frame" title="插件界面"
sandbox="allow-scripts allow-same-origin allow-forms"></iframe>
</div>
<!-- 运行日志(当前/本次运行) -->
<div v-show="tab === 'log'" class="rt-log"><LogPanel /></div>
<!-- 历史运行:本场景已完成的运行,点击回看该次日志 -->
<section v-show="tab === 'history'" class="history">
<div v-if="!replay" class="hist-view">
<div class="hist-head">
<span class="hist-title">本场景最近运行</span>
<el-button size="small" text :icon="Refresh" :loading="loadingRuns" @click="loadRuns">刷新</el-button>
</div>
<div v-if="!runs.length" class="hist-empty">
{{ loadingRuns ? '加载中…' : '该场景还没有运行记录,执行一次后这里可回看' }}
</div>
<ul v-else class="hist-list">
<li v-for="r in runs" :key="r.run_id" class="hist-item" @click="openReplay(r.run_id)">
<i class="hist-dot" :class="r.ok ? 'ok' : 'err'"></i>
<div class="hist-meta">
<b>{{ triggerLabel(r.trigger) }}</b>
<span class="hist-time">{{ fmtTime(r.started) }} · {{ durText(r.duration_sec) }}</span>
</div>
<span class="hist-state" :class="r.ok ? 'ok' : 'err'">{{ r.ok ? '成功' : '失败' }}</span>
<el-icon class="hist-arrow"><ArrowRight /></el-icon>
</li>
</ul>
</div>
<!-- 回看单次运行(结论 + 该次日志) -->
<div v-else class="hist-replay">
<div class="replay-head">
<el-button size="small" text :icon="ArrowLeft" :disabled="replayBusy" @click="replay = null">返回列表</el-button>
<div class="replay-tags">
<span class="badge" :class="replay.ok ? 'on' : 'off'"><i class="bdot"></i>{{ replay.ok ? '成功' : '失败' }}</span>
<span class="replay-tag">{{ triggerLabel(replay.trigger) }}</span>
<span class="replay-tag">{{ fmtTime(replay.started) }}</span>
<span class="replay-tag">耗时 {{ durText(replay.duration_sec) }}</span>
<span v-if="replay.summary" class="replay-sum">{{ replay.summary }}</span>
</div>
</div>
<div class="rt-log"><LogPanel :static-logs="replay.logs || []" /></div>
</div>
</section>
</div>
</div>
</div>
<div v-else-if="!store.plugins.length" class="empty-note">
<b>当前账号暂无可用场景</b><br>请联系管理员在控制面进行授权,授权后点击右上角刷新
</div>
<div v-else class="empty-note"><b>未选择场景</b></div>
</template>
<script setup>
import { computed, ref, watch } from 'vue'
import { Refresh, ArrowLeft, ArrowRight } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { store } from '../store'
import { getRun, fetchRuns, fetchMeta, listRollbacks, rollbackPlugin } from '../api'
import LogPanel from './LogPanel.vue'
const GRADS = [
'var(--grad-sky)', 'var(--grad-peach)', 'var(--grad-mint)',
'var(--grad-lilac)', 'var(--grad-honey)', 'var(--grad-blush)',
]
const idx = computed(() => Math.max(0, store.plugins.findIndex((p) => p.id === store.selectedId)))
const avatarBg = computed(() => GRADS[idx.value % GRADS.length])
const plugin = computed(() => store.plugins.find((p) => p.id === store.selectedId) || null)
// 专属界面插件:插件 UI 直接内嵌在页面主体(同源自动携带会话 cookie,不再弹窗/外跳)。
const uiSrc = computed(() => (plugin.value?.ui ? `/pui/${plugin.value.id}/` : ''))
// ===== 运行台标签页:插件界面(如有)/ 运行日志 / 历史运行 =====
// 执行与参数配置收口在插件界面内(插件自包含前端+执行按钮),壳子不再提供参数表单与一键执行
const tabs = computed(() => {
const t = plugin.value?.ui ? [{ k: 'ui', label: '插件界面' }] : []
return [...t, { k: 'log', label: '运行日志' }, { k: 'history', label: '历史运行' }]
})
const tab = ref('log')
watch(
() => plugin.value?.id,
() => {
tab.value = plugin.value?.ui ? 'ui' : 'log'
resetHistory()
consumePendingReplay()
},
{ immediate: true }
)
// ===== 历史运行 =====
const runs = ref([]) // 本场景最近运行(brief 列表)
const loadingRuns = ref(false)
const runsLoaded = ref(false) // 本场景是否已拉过(切换场景后重置)
const replay = ref(null) // 正在回看的单次运行 {brief..., logs:[...]}
const replayBusy = ref(false)
async function loadRuns() {
if (!plugin.value || loadingRuns.value) return
loadingRuns.value = true
try {
const data = await fetchRuns(60)
const mine = (data.runs || []).filter((r) => r.plugin_id === plugin.value.id)
runs.value = mine.slice(0, 30)
runsLoaded.value = true
} catch (_) { /* 历史是辅助能力,加载失败静默(列表保持上次/空) */ }
finally { loadingRuns.value = false }
}
async function openReplay(runId) {
if (!runId || replayBusy.value) return
replayBusy.value = true
try {
const data = await getRun(runId)
replay.value = { ...(data || {}), logs: (data && data.logs) || [] }
} catch (e) {
ElMessage.error('加载运行记录失败:' + (e.message || e))
} finally {
replayBusy.value = false
}
}
function resetHistory() {
runs.value = []
runsLoaded.value = false
replay.value = null
}
// 活动流「最近运行」点击 → 直达本场景回看:匹配当前场景则消费,否则等场景切换后消费
function consumePendingReplay() {
const p = store.pendingReplay
if (!p || !plugin.value || plugin.value.id !== p.pluginId) return false
store.pendingReplay = null
tab.value = 'history'
openReplay(p.runId)
return true
}
watch(() => store.pendingReplay, consumePendingReplay)
// 打开「历史运行」标签页且未拉过 → 拉取;运行结束(recents 变化)→ 若已拉过则刷新
watch(
() => tab.value,
(t) => { if (t === 'history' && !runsLoaded.value && !loadingRuns.value) loadRuns() }
)
watch(
() => store.recents.length,
() => { if (runsLoaded.value && !replay.value) loadRuns() }
)
function triggerLabel(t) {
return ({ manual: '手动执行', schedule: '定时任务' })[t] || t || '手动执行'
}
function durText(s) {
const n = parseInt(s, 10) || 0
if (n < 60) return n + 's'
const m = Math.floor(n / 60)
return m + '分' + (n % 60 ? (n % 60) + 's' : '')
}
function fmtTime(ts) {
if (!ts) return '—'
const d = new Date(ts * 1000)
if (isNaN(d.getTime())) return '—'
const p = (n) => String(n).padStart(2, '0')
return `${d.getMonth() + 1}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`
}
// 执行进度环(SVG stroke-dasharray)
const RING_C = 2 * Math.PI * 15.5
const ringDash = computed(() => `${(store.progress / 100) * RING_C} ${RING_C}`)
const platformLabel = computed(() => {
const p = store.platforms.find((x) => x.id === plugin.value?.platform)
return p?.label || plugin.value?.platform || ''
})
const platformOnline = computed(() =>
!!(store.platforms.find((x) => x.id === plugin.value?.platform) || {}).online
)
// ===== 插件回滚(升级前自动备份上一版,坏包可回退) =====
const rollbackVer = ref('')
async function loadRollbackInfo() {
if (!plugin.value) return
rollbackVer.value = ''
try {
const r = await listRollbacks()
const item = (r.items || []).find((i) => i.id === plugin.value.id)
rollbackVer.value = item ? item.version : ''
} catch (_) { /* 本地能力,失败静默 */ }
}
watch(() => plugin.value?.id, loadRollbackInfo, { immediate: true })
async function doRollback() {
if (!plugin.value || !rollbackVer.value) return
try {
await ElMessageBox.confirm(
`将「${plugin.value.name}」回滚到 v${rollbackVer.value}(当前 v${plugin.value.version})。` +
'回滚后如需最新版,再次点击右上角同步即可。确定回滚?',
'插件回滚',
{ type: 'warning', confirmButtonText: '回滚', cancelButtonText: '取消' }
)
} catch (_) { return }
try {
await rollbackPlugin(plugin.value.id)
ElMessage.success(`已回滚到 v${rollbackVer.value}`)
// 刷新插件列表:版本号等元数据由 /api/meta 重新下发
const meta = await fetchMeta()
store.plugins = meta.plugins || []
rollbackVer.value = ''
} catch (e) {
ElMessage.error('回滚失败:' + (e.message || e))
}
}
// 完成时拉取后端摘要填充横幅(触发源无关:插件界面/定时任务触发的运行同样回填)
watch(
() => store.lastResult,
async (res) => {
if (res && res.ok && store.lastRunId) {
try {
const run = await getRun(store.lastRunId)
store.lastSummary = run.result?.summary || ''
} catch (e) { }
}
}
)
function closeBanner() {
store.lastResult = null
}
</script>
<style scoped>
.plugin-page {
display: flex;
flex-direction: column;
gap: 14px;
flex: 1 1 auto; /* 填满中栏上下文区,运行台内部自己滚 */
min-height: 0;
}
.card {
background: var(--surface);
border: 1px solid var(--border);
border-radius: var(--radius-lg);
box-shadow: var(--shadow-sm);
}
/* ---------- Hero ---------- */
.hero {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 22px 24px;
background: var(--hero-grad);
}
.hero-main { display: flex; align-items: center; gap: 16px; min-width: 0; }
.avatar {
width: 54px; height: 54px; border-radius: 16px; flex-shrink: 0;
display: grid; place-items: center; color: var(--tile-fg);
font-size: 22px; font-weight: 700;
box-shadow: 0 10px 22px -10px rgba(var(--text-rgb), .35);
}
.hero-meta { min-width: 0; display: flex; flex-direction: column; gap: 5px; }
.p-name { margin: 0; font-size: 19px; font-weight: 700; color: var(--text); display: flex; align-items: center; gap: 9px; }
.ver-tag {
font-size: 10.5px; font-weight: 500; color: var(--text-faint);
background: var(--surface-2); border: 1px solid var(--border);
padding: 1px 8px; border-radius: 999px;
}
.p-desc { margin: 0; font-size: 12.5px; color: var(--text-sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 560px; }
.p-badges { display: flex; align-items: center; gap: 8px; margin-top: 2px; }
.badge {
display: inline-flex; align-items: center; gap: 6px;
font-size: 11px; color: var(--badge-warm-text);
background: var(--badge-warm-bg); border: 1px solid var(--badge-warm-border);
padding: 2px 10px; border-radius: 999px;
}
.badge.on { color: var(--ok-text); background: var(--ok-weak); border-color: rgba(var(--ok-rgb), .3); }
.badge.off { color: var(--badge-off-text); }
.bdot { width: 6px; height: 6px; border-radius: 50%; background: var(--badge-dot); }
.badge.on .bdot { background: var(--ok); }
.hero-actions { display: flex; flex-direction: column; align-items: flex-end; gap: 7px; flex-shrink: 0; }
/* 运行状态 chip:仅展示进度,不可点击(执行入口在插件界面内) */
.run-chip {
display: inline-flex; align-items: center; gap: 8px;
padding: 6px 15px; border-radius: 999px;
background: var(--primary-grad-3); color: var(--on-primary);
font-size: 12.5px; font-weight: 600; font-variant-numeric: tabular-nums;
box-shadow: 0 8px 18px -10px rgba(var(--primary-rgb), .8);
}
/* 进度环用 CSS 上色(SVG 表现属性不支持 var(),故不写在 stroke="" 上) */
.ring-bg { stroke: rgba(var(--on-primary-rgb), .35); }
.ring-fg { stroke: var(--on-primary); }
.ring { display: inline-flex; }
.ring-fg { transition: stroke-dasharray .25s ease; }
.hero-tip { font-size: 10.5px; color: var(--text-faint); letter-spacing: .02em; }
.rollback-btn { margin-top: -3px; }
/* ---------- 结果横幅 ---------- */
.banner {
display: flex; align-items: center; gap: 11px;
padding: 12px 16px; border-radius: 14px;
animation: riseIn .3s var(--ease) both;
}
.banner.ok { background: var(--ok-weak); border: 1px solid rgba(var(--ok-rgb), .35); }
.banner.err { background: var(--err-weak); border: 1px solid rgba(var(--err-rgb), .4); }
.b-ic {
width: 26px; height: 26px; border-radius: 50%; flex-shrink: 0;
display: grid; place-items: center; font-size: 13px; font-weight: 700; color: var(--on-primary);
}
.banner.ok .b-ic { background: var(--ok); }
.banner.err .b-ic { background: var(--err); }
.b-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.b-body b { font-size: 13px; color: var(--text); font-weight: 600; }
.b-body span { font-size: 12px; color: var(--text-sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.b-close {
border: none; background: transparent; color: rgba(var(--text-rgb), .4);
cursor: pointer; font-size: 12px; padding: 4px; border-radius: 50%;
}
.b-close:hover { color: var(--text); background: rgba(var(--on-primary-rgb), .6); }
/* ---------- 运行台(标签页容器) ---------- */
.run-tabs {
flex: 1 1 auto; min-height: 300px;
display: flex; flex-direction: column; overflow: hidden;
}
.rt-head {
display: flex; align-items: center; justify-content: space-between; gap: 10px;
padding: 0 14px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.rt-tabs { display: flex; align-items: center; gap: 2px; }
.rt-tab {
display: inline-flex; align-items: center; gap: 6px;
border: none; background: transparent; cursor: pointer; font-family: inherit;
padding: 11px 14px; font-size: 13px; color: var(--text-sub);
border-bottom: 2px solid transparent; margin-bottom: -1px;
transition: color .13s, border-color .13s;
}
.rt-tab:hover { color: var(--text); }
.rt-tab.on { color: var(--primary); font-weight: 600; border-bottom-color: var(--primary); }
.rt-n {
font-size: 10.5px; color: var(--text-faint); background: var(--surface-2);
border: 1px solid var(--border); border-radius: 999px; padding: 0 6px;
}
.rt-tab.on .rt-n { color: var(--primary); background: var(--primary-weak); border-color: var(--primary-line); }
.rt-live { width: 6px; height: 6px; border-radius: 50%; background: var(--ok); animation: rtBlink 1s infinite; }
@keyframes rtBlink { 50% { opacity: .2; } }
.rt-body { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.rt-log { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
/* ---------- 专属界面插件 ---------- */
.ui-pane { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; padding: 14px 16px 16px; }
.pui-frame {
display: block; width: 100%; flex: 1 1 auto; min-height: 340px;
border: 1px solid var(--border); border-radius: 12px; background: var(--surface);
}
/* ---------- 历史运行 ---------- */
.history { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.hist-view { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
.hist-head {
display: flex; align-items: center; justify-content: space-between; gap: 10px;
padding: 10px 16px 6px; flex-shrink: 0;
}
.hist-title { font-size: 12.5px; font-weight: 600; color: var(--text-sub); }
.hist-empty {
flex: 1 1 auto; display: grid; place-items: center;
color: var(--text-faint); font-size: 12.5px; padding: 30px 20px; text-align: center;
}
.hist-list { flex: 1 1 auto; min-height: 0; overflow: auto; list-style: none; margin: 4px 8px 10px; padding: 0; }
.hist-item {
display: flex; align-items: center; gap: 11px;
padding: 10px 12px; border-radius: 12px; cursor: pointer;
transition: background .13s ease;
}
.hist-item:hover { background: var(--surface-2); }
.hist-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.hist-dot.ok { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-weak); }
.hist-dot.err { background: var(--err); box-shadow: 0 0 0 3px var(--err-weak); }
.hist-meta { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.hist-meta b { font-size: 13px; color: var(--text); font-weight: 600; }
.hist-time { font-size: 11.5px; color: var(--text-faint); font-variant-numeric: tabular-nums; }
.hist-state { font-size: 12px; font-weight: 600; flex-shrink: 0; }
.hist-state.ok { color: var(--ok); }
.hist-state.err { color: var(--err); }
.hist-arrow { color: var(--text-faint); font-size: 13px; flex-shrink: 0; }
.hist-replay { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
.replay-head {
display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
padding: 8px 12px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.replay-tags { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; min-width: 0; }
.replay-tag {
font-size: 11px; color: var(--text-faint); font-variant-numeric: tabular-nums;
background: var(--surface-2); border: 1px solid var(--border);
padding: 1px 9px; border-radius: 999px;
}
.replay-sum {
font-size: 11.5px; color: var(--text-sub); white-space: nowrap;
overflow: hidden; text-overflow: ellipsis; max-width: 340px;
}
@keyframes riseIn {
from { opacity: 0; transform: translateY(6px); }
to { opacity: 1; transform: none; }
}
</style>