Files
37duAutomation/desktop/electron-shell/frontend/src/components/PluginPage.vue
T
puddy 0a32518060 refactor: 重构多插件 UI 与配置结构,新增抖音/新美大平台核对插件
- 新增 XQ0042 抖音平台核对、XQ0041 新美大平台核对插件的 UI 源码与打包脚本
- 重构续签电子签、服务岗转正不通过电子签等插件的版本与配置
- 删除旧版考勤异常处理、微盟数据导出等冗余插件文件
- 修复出勤积分更新插件的 OPM 推送判断逻辑
- 优化壳子插件页面的 iframe 滚动条样式
2026-10-28 10:51:27 +08:00

518 lines
22 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">
<!-- 页头:场景身份 + 平台状态 + 运行中/回滚入口(与其它一级视图共用 PageHead) -->
<PageHead eyebrow="CONSOLE · 运行台">
<template #title>
<span class="p-name">{{ plugin.name }}<span class="ver-tag">v{{ plugin.version }}</span></span>
</template>
<template #sub>{{ plugin.description || '暂无描述' }}</template>
<template #actions>
<span v-if="plugin.platform" class="badge" :class="platformOnline ? 'on' : 'off'">
<i class="bdot"></i>{{ platformLabel }} {{ platformOnline ? '已登录' : '未登录' }}
</span>
<span v-if="queuedCount" class="qchip" :title="`该场景有 ${queuedCount} 个任务正在排队,可在右侧「执行队列」查看或取消`">
排队 {{ queuedCount }}
</span>
<RunIndicator v-if="store.running" variant="chip" :progress="store.progress" />
<button v-if="rollbackVer" class="btn rollback-btn" :disabled="store.running" @click="doRollback">
回滚 v{{ rollbackVer }}
</button>
</template>
</PageHead>
<!-- 结果横幅 -->
<section v-if="store.lastResult" class="banner"
:class="store.lastResult.ok ? 'ok' : (store.lastResult.cancelled ? 'mid' : 'err')">
<span class="b-ic">{{ store.lastResult.ok ? '✓' : (store.lastResult.cancelled ? '⊘' : '✕') }}</span>
<div class="b-body">
<b>{{ store.lastResult.ok ? (store.lastSummary || '执行完成') : (store.lastResult.cancelled ? '任务已取消' : '执行失败') }}</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>
<RunIndicator v-if="t.k === 'log' && store.running" variant="dot" />
</button>
</div>
</div>
<div class="rt-body">
<!-- 插件专属界面(同源 iframe,自动携带会话 cookie)
sandbox 白名单加固:允许脚本/同源/表单,禁用 top 跳转与弹窗
仅一层容器承载尺寸,不再叠提示条/卡片 -->
<div v-if="plugin.ui" v-show="tab === 'ui'" class="ui-pane">
<iframe ref="uiFrame" :key="plugin.id" :src="uiSrc" class="pui-frame" title="插件界面"
sandbox="allow-scripts allow-same-origin allow-forms" @load="onUiLoad"></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">
<div class="hist-titles">
<span class="sec-eyebrow">HISTORY</span>
<span class="hist-title">本场景最近运行</span>
</div>
<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="runState(r).cls"></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="runState(r).cls">{{ runState(r).text }}</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, fetchQueue } from '../api'
import LogPanel from './LogPanel.vue'
import RunIndicator from './RunIndicator.vue'
import PageHead from './PageHead.vue'
import { durText, fmtTime } from '../utils/format'
const plugin = computed(() => store.plugins.find((p) => p.id === store.selectedId) || null)
// 运行结论三态:成功 / 已取消(用户取消、排队超时、退出取消)/ 失败 / 已中断(客户端退出遗留)
// 「取消」不能显示成「失败」——两者对用户的含义完全不同,也不该混进失败统计。
function runState(r) {
if (!r) return { text: '—', cls: 'mid' }
if (r.ok) return { text: '成功', cls: 'ok' }
if (r.status === 'cancelled') return { text: '已取消', cls: 'mid' }
if (r.status === 'interrupted') return { text: '已中断', cls: 'mid' }
return { text: '失败', cls: 'err' }
}
// 本场景在队列里等待的任务数:连续点了几次「一键执行」时,页头直接给出排队提示
const queuedCount = computed(() => {
const pid = plugin.value?.id
if (!pid) return 0
return (store.queue.items || []).filter((it) => it.plugin_id === pid).length
})
// 专属界面插件:插件 UI 直接内嵌在页面主体(同源自动携带会话 cookie,不再弹窗/外跳)。
const uiSrc = computed(() => (plugin.value?.ui ? `/pui/${plugin.value.id}/` : ''))
// ===== 入队确认 =====
// 插件界面里的「一键执行」直接 POST /api/runs(插件自己不知道队列的存在)。壳子在同源 iframe 的
// window 上包一层 fetch:提交前先问一次队列状态,需要排队就弹确认框,用户点「加入队列」才真正提交;
// 取消则返回 409,插件自身横幅会显示"启动执行失败:已取消加入队列…",不会误以为已经在跑。
const uiFrame = ref(null)
async function queueSnapshotNow() {
try {
const snap = await fetchQueue()
if (snap && Array.isArray(snap.items)) {
return { busy: !!snap.busy, running: snap.running || null, waiting: snap.items.length }
}
} catch (_) { /* 后端不可达:退回 WS 快照,至少不会漏提示 */ }
return { busy: store.queue.busy, running: store.queue.running, waiting: store.queue.waiting }
}
// true = 继续提交;false = 用户取消,不入队
async function confirmEnqueue() {
const q = await queueSnapshotNow()
if (!q.busy && !q.waiting) return true // 设备空闲:不打扰,直接开跑
const bits = []
if (q.running && q.running.name) bits.push(`「${q.running.name}」正在执行`)
if (q.waiting) bits.push(`队列里还有 ${q.waiting} 个任务`)
const pos = q.waiting + (q.busy ? 2 : 1)
const name = plugin.value?.name || '当前场景'
try {
await ElMessageBox.confirm(
`${bits.join(',')}。执行「${name}」需要排队,确认后约排第 ${pos} 位,可在右侧「执行队列」随时取消。`,
'当前有其他任务在执行,是否加入队列?',
{ confirmButtonText: '加入队列', cancelButtonText: '取消', type: 'warning' }
)
return true
} catch (_) {
return false
}
}
// 插件界面是独立文档(同源 iframe),壳子的 ::-webkit-scrollbar 只作用于壳子自身文档、
// 不会穿透到 iframe 里,插件内容区滚动条会退回 Chromium 原生粗灰条 → 载入时注入同款细滚动条。
const FRAME_SCROLLBAR_CSS = `
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: #C6CBD6; border-radius: 4px; border: 2px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-thumb:hover { background: #A3AAB8; }
::-webkit-scrollbar-track, ::-webkit-scrollbar-corner { background: transparent; }
`
function injectFrameScrollbarStyle(doc) {
if (!doc || doc.getElementById('__shell-scrollbar')) return
const st = doc.createElement('style')
st.id = '__shell-scrollbar'
st.textContent = FRAME_SCROLLBAR_CSS
;(doc.head || doc.documentElement).appendChild(st)
}
function onUiLoad() {
const w = uiFrame.value && uiFrame.value.contentWindow
if (!w) return
injectFrameScrollbarStyle(w.document)
if (w.__queueConfirmPatched) return
w.__queueConfirmPatched = true
const origFetch = w.fetch ? w.fetch.bind(w) : null
if (!origFetch) return
w.fetch = async (input, init) => {
let url = ''
let method = 'GET'
if (typeof input === 'string') {
url = input
method = (init && init.method) || 'GET'
} else if (input && typeof input === 'object') {
url = input.url || ''
method = (init && init.method) || input.method || 'GET'
}
// 只拦"提交一次运行",配置/停止/轮询等其它接口原样放行
if (String(method).toUpperCase() !== 'POST' || !/\/api\/runs\/?$/.test(url)) {
return origFetch(input, init)
}
if (await confirmEnqueue()) return origFetch(input, init)
const R = w.Response || Response
return new R(JSON.stringify({ detail: '已取消加入队列(当前有其他任务在执行)' }), {
status: 409,
headers: { 'Content-Type': 'application/json; charset=utf-8' },
})
}
}
// ===== 运行台标签页:插件界面(如有)/ 运行日志 / 历史运行 =====
// 执行与参数配置收口在插件界面内(插件自包含前端+执行按钮),壳子不再提供参数表单与一键执行
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: '定时任务', flow: '流程编排' })[t] || t || '手动执行'
}
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: var(--space-3);
padding: var(--page-pad);
flex: 1 1 auto; /* 填满中栏上下文区,运行台内部自己滚 */
min-height: 0;
}
/* ---------- 页头内的场景身份 ---------- */
.p-name { display: inline-flex; align-items: center; gap: var(--space-2); }
.ver-tag {
font: 500 10.5px/16px var(--f-num); color: var(--t3);
background: var(--field); border: 1px solid var(--line);
padding: 0 var(--space-2); border-radius: var(--r-pill);
}
.badge {
display: inline-flex; align-items: center; gap: var(--space-1-5);
font: 500 10.5px/16px var(--f-cn);
padding: 1px var(--space-2-5); border-radius: var(--r-pill);
}
.badge.on { color: var(--good-ink); background: var(--good-soft); }
.badge.off { color: var(--t3); background: var(--field); }
.bdot { width: 6px; height: 6px; border-radius: 50%; background: var(--t5); }
.badge.on .bdot { background: var(--good); }
.rollback-btn { height: 28px; padding: 0 var(--space-3); font-size: 11.5px; }
/* 本场景排队中:与运行中的进度胶囊并列,点击去看右侧队列明细 */
.qchip {
display: inline-flex; align-items: center; height: 20px; padding: 0 var(--space-2);
border-radius: var(--r-pill); background: var(--brand-soft); color: var(--brand-ink);
font: 500 10px/1 var(--f-cn); white-space: nowrap;
}
/* ---------- 结果横幅 ---------- */
.banner {
display: flex; align-items: center; gap: var(--space-3);
padding: var(--space-3) var(--space-4); border-radius: var(--r-card);
flex-shrink: 0;
animation: riseIn .3s var(--ease) both;
}
.banner.ok { background: var(--good-soft); border: 1px solid var(--good-line); }
.banner.err { background: var(--danger-soft); border: 1px solid var(--danger-line); }
.banner.mid { background: var(--field); border: 1px solid var(--line); }
.b-ic {
width: 24px; height: 24px; border-radius: 50%; flex-shrink: 0;
display: grid; place-items: center; font-size: 12px; font-weight: 700; color: var(--on-primary);
}
.banner.ok .b-ic { background: var(--good); }
.banner.err .b-ic { background: var(--danger); }
.banner.mid .b-ic { background: var(--t4); }
.b-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.b-body b { font-size: 12.5px; color: var(--t1); font-weight: 600; }
.b-body span { font-size: 11.5px; color: var(--t2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.b-close {
border: none; background: transparent; color: var(--t4);
cursor: pointer; font-size: 12px; padding: 4px; border-radius: 50%;
}
.b-close:hover { color: var(--t1); background: var(--field); }
/* ---------- 运行台(标签页容器) ---------- */
.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: var(--space-2-5);
padding: 0 var(--space-3); border-bottom: 1px solid var(--line-soft); flex-shrink: 0;
}
.rt-tabs { display: flex; align-items: center; gap: 2px; }
.rt-tab {
display: inline-flex; align-items: center; gap: var(--space-1-5);
border: none; background: transparent; cursor: pointer; font-family: inherit;
padding: var(--space-3) var(--space-3); font: 500 12.5px/18px var(--f-cn); color: var(--t3);
border-bottom: 2px solid transparent; margin-bottom: -1px;
transition: color var(--dur), border-color var(--dur);
}
.rt-tab:hover { color: var(--t1); }
.rt-tab.on { color: var(--brand-ink); font-weight: 600; border-bottom-color: var(--brand); }
.rt-n {
font: 500 10px/16px var(--f-num); color: var(--t3);
background: var(--field); border-radius: var(--r-pill); padding: 0 var(--space-2);
}
.rt-tab.on .rt-n { color: var(--brand-ink); background: var(--brand-soft); }
.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: var(--space-3) var(--space-4) var(--space-4); }
.pui-frame {
display: block; width: 100%; flex: 1 1 auto; min-height: 340px;
border: 1px solid var(--line); border-radius: var(--r-sub); 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: flex-start; justify-content: space-between; gap: var(--space-2-5);
padding: var(--space-2-5) var(--space-4) var(--space-1-5); flex-shrink: 0;
}
.hist-titles { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.hist-title { font: 600 12px/17px var(--f-cn); color: var(--t2); }
.hist-empty {
flex: 1 1 auto; display: grid; place-items: center;
color: var(--t4); font-size: 12px; padding: var(--space-6) var(--space-5); text-align: center;
}
.hist-list { flex: 1 1 auto; min-height: 0; overflow: auto; list-style: none; margin: var(--space-1) var(--space-2) var(--space-2-5); padding: 0; }
.hist-item {
display: flex; align-items: center; gap: var(--space-3);
padding: var(--space-2) var(--space-3); border-radius: var(--r-row); cursor: pointer;
transition: background var(--dur);
}
.hist-item:hover { background: var(--field); }
.hist-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.hist-dot.ok { background: var(--good); box-shadow: 0 0 0 3px var(--good-soft); }
.hist-dot.err { background: var(--danger); box-shadow: 0 0 0 3px var(--danger-soft); }
.hist-dot.mid { background: var(--t4); box-shadow: 0 0 0 3px var(--field); }
.hist-meta { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.hist-meta b { font: 600 12.5px/18px var(--f-cn); color: var(--t1); }
.hist-time { font: 400 10.5px/15px var(--f-num); color: var(--t4); }
.hist-state { font: 600 11.5px/16px var(--f-cn); flex-shrink: 0; }
.hist-state.ok { color: var(--good-ink); }
.hist-state.err { color: var(--danger-ink); }
.hist-state.mid { color: var(--t4); }
.hist-arrow { color: var(--t5); 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: var(--space-2); flex-wrap: wrap;
padding: var(--space-2) var(--space-3); border-bottom: 1px solid var(--line-soft); flex-shrink: 0;
}
.replay-tags { display: flex; align-items: center; gap: var(--space-2); flex-wrap: wrap; min-width: 0; }
.replay-tag {
font: 400 10.5px/16px var(--f-num); color: var(--t3);
background: var(--field); border: 1px solid var(--line);
padding: 0 var(--space-2); border-radius: var(--r-pill);
}
.replay-sum {
font-size: 11px; color: var(--t2); 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>