Files
37duAutomation/desktop/webhost/frontend/src/App.vue
T
puddy 6d2dd93628 refactor: 整体迁移控制面至MySQL + 替换UI框架为Element Plus
1. 移除naive-ui依赖,迁移前端至Element Plus
2. 重构后端数据库层,切换为MySQL替代SQLite
3. 调整项目部署架构,停止exe打包改为源码部署
4. 新增设备上报接口与相关数据库模型
5. 优化配置路径与SQL语法,替换为MySQL占位符格式
6. 调整站点名称与文案,统一为「37du 插件管理平台」
7. 新增SQLite到MySQL迁移脚本
2026-09-16 15:58:45 +08:00

422 lines
17 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="booting" class="boot">
<span class="boot-logo">37</span>
<span class="boot-txt">{{ syncing ? '正在同步插件,请稍候…' : (store.appName || '自动化工作台') }}</span>
<span class="boot-dots"><i></i><i></i><i></i></span>
</div>
<LoginPage v-else-if="!store.current" :app-name="store.appName" @ok="onLogin" />
<div class="__app" v-else>
<!-- 壳子更新提示:控制面有新版本且已自动下载,点击重启升级 -->
<div v-if="shellUpdate.downloaded" class="update-bar">
<span class="upd-txt">发现新版本 <b>v{{ shellUpdate.new }}</b><em v-if="shellUpdate.notes">{{ shellUpdate.notes }}</em></span>
<el-button size="small" type="primary" round :loading="updating" @click="doShellUpdate">重启更新</el-button>
</div>
<!-- 顶部:品牌 + 平台登录态 + 当前账号/登出 + 进度 -->
<header class="topbar panel">
<div class="brand">
<span class="logo">37</span>
<div class="brand-meta">
<strong>{{ store.appName }}</strong>
<span>{{ wsText }}</span>
</div>
</div>
<el-divider direction="vertical" class="vdiv" />
<div class="band">
<span class="band-label">平台登录态</span>
<el-tooltip
v-for="p in store.platforms"
:key="p.id"
:content="p.allowed === false ? '无平台访问权限,需管理员在控制面授权' : (p.online ? '已登录,点击刷新' : '未登录,点击登录')"
placement="bottom"
:disabled="p.online && p.allowed !== false"
>
<el-button
class="chip"
:class="{ online: p.online, locked: p.allowed === false }"
:disabled="busyLogin === p.id || p.allowed === false"
:loading="busyLogin === p.id"
round
@click="login(p.id)"
>
<i class="dot"></i>
<span>{{ busyLogin === p.id ? '登录中…' : p.label }}</span>
</el-button>
</el-tooltip>
</div>
<div class="spacer"></div>
<div class="run-state">
<template v-if="store.running">
<span class="run-pct">{{ store.progress }}%</span>
<el-progress
:percentage="store.progress"
:show-text="false"
:stroke-width="8"
color="#3b6cff"
class="run-bar"
/>
</template>
</div>
<div class="acct">
<button class="acct-btn" title="个人中心" @click="profileOpen = true">
<el-avatar :size="26" class="acct-avatar" :src="accountAvatar()" :key="accountAvatar()" @error="onAvatarError">
<span class="acct-avatar-fb">{{ (store.nickname || store.current).slice(0, 1).toUpperCase() }}</span>
</el-avatar>
<span class="acct-name">{{ store.nickname || store.current }}</span>
<el-tag v-if="store.isAdmin" size="small" type="primary" effect="light" round>管理员</el-tag>
</button>
<el-button
v-if="store.isAdmin"
v-show="store.controlplane"
class="chip admin-chip"
title="打开中心机控制面进行插件/账号/授权管理"
round
@click="openConsole"
>
<el-icon class="geo"><Setting /></el-icon>管理控制台
</el-button>
<el-button class="chip" round @click="logout">登出</el-button>
</div>
</header>
<!-- 主体:左场景 | 右插件页 + 日志 -->
<div class="body">
<aside class="panel side">
<div class="side-head">
<span class="sec-title">场景</span>
<span class="count">{{ store.plugins.length }}</span>
</div>
<ul class="scene-list">
<li
v-for="(p, i) in store.plugins"
:key="p.id"
:class="{ active: p.id === store.selectedId && store.view === 'plugin' }"
@click="openPlugin(p.id)"
>
<span class="scene-icon" :style="{ background: PALETTE[i % PALETTE.length] }">
{{ p.name.slice(0, 1) }}
</span>
<span class="scene-name">{{ p.name }}</span>
<em>v{{ p.version }}</em>
</li>
</ul>
<div v-if="!store.plugins.length" class="scene-empty">当前账号暂无可用场景<br><span>请联系管理员进行授权</span></div>
<div class="side-foot">场景以插件方式接入</div>
</aside>
<main class="main">
<div class="page-scroll panel">
<PluginPage v-if="store.view === 'plugin'" />
</div>
<LogPanel />
</main>
</div>
</div>
<ProfilePanel v-if="profileOpen" @close="profileOpen = false" />
</template>
<script setup>
import { computed, onMounted, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import PluginPage from './components/PluginPage.vue'
import LogPanel from './components/LogPanel.vue'
import LoginPage from './components/LoginPage.vue'
import ProfilePanel from './components/ProfilePanel.vue'
import { store, handleEvent, clearLogs, log, loadAvatar, clearAvatarCache, accountAvatar } from './store'
import { connectWS, fetchMeta, loginPlatform, adminLogout, adminMe, adminPassword, syncPlugins, getShellStatus, applyShellUpdate } from './api'
function openConsole() {
if (!store.controlplane) return
window.open(store.controlplane, '_blank', 'noopener')
}
const PALETTE = ['#3b6cff', '#7c5cff', '#12a4d8', '#22b07a', '#e88a3a', '#e5484d']
// 顶栏头像加载失败:清除缓存返回首字符兜底
function onAvatarError() {
clearAvatarCache()
}
const wsText = computed(() =>
store.wsOn ? '服务已连接' : '连接中断,正在重连…')
function openPlugin(id) {
store.selectedId = id
store.view = 'plugin'
}
function openWS() {
if (window.__ws) window.__ws.close()
const ws = connectWS(handleEvent)
window.__ws = ws
ws.onopen = () => (store.wsOn = true)
ws.onclose = () => {
store.wsOn = false
// 仅登录态下自动重连刷新;未登录被拒(1008)时静默,避免 reload 死循环
if (store.current) setTimeout(() => (location.reload()), 2500)
}
}
onMounted(async () => {
const meta = await fetchMeta()
store.appName = meta.appName
store.controlplane = meta.controlplane || ''
store.platforms = meta.platforms
store.plugins = meta.plugins || []
store.selectedId = store.plugins[0]?.id || ''
clearLogs()
openWS()
await checkSession()
booting.value = false // 会话判定完成(登录/未登录/后端不可达均已定),再渲染首屏
loadAvatar()
loadShellStatus()
})
const profileOpen = ref(false)
const busyLogin = ref('')
const booting = ref(true) // 鉴权判定中:未确定会话前先显示加载骨架,避免「闪一下登录页再进首页」
const syncing = ref(false) // 首次启动/启动时:按账号权限自下载/更新插件(等待下载界面)
// ===== 壳子自动更新:控制面有新版本且已自动下载 → 提示用户点击重启升级 =====
const shellUpdate = ref({})
const updating = ref(false)
async function loadShellStatus() {
try { shellUpdate.value = await getShellStatus() } catch (_) {}
}
async function doShellUpdate() {
updating.value = true
try {
await applyShellUpdate() // 后端运行静默安装并退出进程,升级后自动重启
} catch (e) {
updating.value = false
ElMessage.error('更新失败:' + (e.message || e))
}
}
// 平台登录等轻量操作反馈:统一走 element-plus 全局消息
function showToast(msg, type = 'info') {
ElMessage({ message: msg, type, duration: 2200 })
}
async function checkSession() {
// 登录态一律以**后端会话 cookie** 为准(钉钉扫码/账号登录/后端重启后均从 admin/me 判定),
// 不再以 localStorage 的 cp_account 早退——否则全新扫码流程(未写过 cp_account)回跳后无法进入插件页。
try {
const me = await adminMe()
if (!me.account) {
store.current = ''; store.isAdmin = false; store.dept = ''; store.nickname = ''; store.avatar = ''
try { localStorage.removeItem('cp_account') } catch (_) {}
return
}
store.current = me.account
store.isAdmin = !!me.is_admin
store.dept = me.dept || ''
store.nickname = me.nickname || ''
store.avatar = me.avatar || ''
try { localStorage.setItem('cp_account', me.account) } catch (_) {}
// 会话确认后:按账号权限自下载/更新插件(首次启动等待下载完成;离线/失败直接跳过)
syncing.value = true
try { await syncPlugins() } catch (_) {}
syncing.value = false
// 会话有效:补拉插件清单(未登录时 meta 不返回插件)
await refreshMeta()
} catch (_) { /* 后端 down:无法校验,保持现状(可能为登录页或本地态) */ }
}
async function refreshMeta() {
try {
const meta = await fetchMeta()
store.controlplane = meta.controlplane || ''
store.platforms = meta.platforms
store.plugins = meta.plugins || []
if (!store.selectedId) store.selectedId = store.plugins[0]?.id || ''
} catch (_) {}
}
async function onLogin(ok) {
store.current = ok.account
store.isAdmin = !!ok.is_admin
store.dept = ok.dept || ''
store.nickname = ok.nickname || ''
store.avatar = ok.avatar || ''
try { localStorage.setItem('cp_account', ok.account) } catch (_) {}
// 登录后:刷新平台权限与插件清单,并用带会话的连接重建 WS
await refreshMeta()
openWS()
loadAvatar()
// 首次登录/钉钉自动建号的默认密码:提示立即改密
if (ok.must_change_pwd) { promptChangePwd(ok.account) }
}
async function promptChangePwd(account) {
try {
const { value: np } = await ElMessageBox.prompt(
'当前账号正使用默认密码,请立即设置新密码(至少 6 位),避免他人误登录',
'首次登录 · 修改密码',
{ inputType: 'password', inputPattern: /^.{6,}$/, inputErrorMessage: '密码至少 6 位',
confirmButtonText: '设置新密码', cancelButtonText: '稍后再说' })
try {
await adminPassword({ account, old_password: '', new_password: np })
ElMessage.success('密码已更新')
} catch (e) {
ElMessage.warning('改密失败:' + (e.message || e))
}
} catch (_) { /* 用户取消 */ }
}
async function logout() {
try { await adminLogout() } catch (_) {}
store.current = ''
store.isAdmin = false
store.nickname = ''
store.avatar = ''
store.avatarUrl = ''
try { localStorage.removeItem('cp_account') } catch (_) {}
}
async function login(id) {
if (busyLogin.value) return
const p = store.platforms.find((x) => x.id === id)
const label = p?.label || id
if (p?.online) {
showToast(`${label} 已登录`, 'success')
return
}
busyLogin.value = id
try {
const r = await loginPlatform(id)
const ok = !!(r && r.online)
if (p) p.online = ok
log(ok ? 'INFO' : 'ERROR',
ok ? `平台 ${label} 登录成功` : `平台 ${label} 登录未生效:${(r && r.message) || ''}`)
showToast(ok ? `${label} 登录成功` : `${label} 登录失败:${(r && r.message) || '登录态未生效'}`, ok ? 'success' : 'error')
} catch (e) {
log('ERROR', `平台 ${label} 登录失败:${e.message || e}`)
showToast(`登录失败:${e.message || e}`, 'error')
} finally {
busyLogin.value = ''
}
}
</script>
<style scoped>
.boot {
min-height: 100vh;
display: flex; flex-direction: column; align-items: center; justify-content: center;
gap: 14px; background: var(--bg);
}
.boot-logo {
width: 54px; height: 54px; border-radius: 16px;
display: grid; place-items: center;
background: var(--primary-grad); color: #fff;
font-size: 20px; font-weight: 800;
box-shadow: 0 10px 26px -8px rgba(59,108,255,.65);
animation: bootpop .4s ease;
}
.boot-txt { font-size: 13px; color: var(--text-faint); letter-spacing: .06em; }
.boot-dots { display: flex; gap: 5px; }
.boot-dots i {
width: 6px; height: 6px; border-radius: 50%; background: var(--primary);
animation: bootblink 1s infinite ease-in-out;
}
.boot-dots i:nth-child(2) { animation-delay: .15s; }
.boot-dots i:nth-child(3) { animation-delay: .3s; }
@keyframes bootpop { from { transform: scale(.7); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes bootblink { 0%,80%,100% { opacity: .25; } 40% { opacity: 1; transform: translateY(-2px); } }
.topbar {
display: flex; align-items: center; gap: 16px;
padding: 12px 18px;
flex-shrink: 0;
}
.update-bar {
display: flex; align-items: center; justify-content: space-between; gap: 12px;
padding: 8px 14px; margin-bottom: 12px; flex-shrink: 0;
background: var(--primary-weak);
border: 1px solid rgba(59, 108, 255, .35);
border-radius: var(--radius-sm);
font-size: 13px; color: var(--text-sub);
}
.update-bar b { color: var(--primary); }
.update-bar em { font-style: normal; color: var(--text-faint); margin-left: 6px; }
.brand { display: flex; align-items: center; gap: 11px; }
.logo {
width: 36px; height: 36px; border-radius: 10px;
display: grid; place-items: center;
background: var(--primary-grad); color: #fff;
font-size: 14px; font-weight: 800;
box-shadow: 0 4px 12px -3px rgba(59,108,255,.6);
}
.brand-meta { display: flex; flex-direction: column; line-height: 1.2; }
.brand-meta strong { font-size: 14px; }
.brand-meta span { font-size: 11px; color: var(--text-faint); }
.vdiv { height: 26px; margin: 0 2px; }
.band { display: flex; align-items: center; gap: 8px; }
.band-label { font-size: 12px; color: var(--text-faint); margin-right: 4px; }
.chip { padding: 5px 14px; border-radius: 999px; }
.chip .dot { width: 9px; height: 9px; border-radius: 50%; background: #c6ccdc; margin-right: 6px; display: inline-block; }
.chip.online { border-color: transparent; background: var(--ok-weak); color: #1a8f63; }
.chip.online .dot { background: var(--ok); box-shadow: 0 0 0 3px rgba(34,176,122,.18); }
.chip.locked { border-style: dashed; opacity: .5; }
.admin-chip { border-color: rgba(59,108,255,.45); color: var(--primary); background: var(--primary-weak-2); margin-left: 4px; }
.admin-chip:hover { border-color: var(--primary); }
.admin-chip .geo { margin-right: 4px; }
.spacer { flex: 1; }
.run-state { display: flex; align-items: center; gap: 10px; }
.run-bar { width: 180px; flex-shrink: 0; }
.run-pct { font-size: 12px; font-weight: 700; color: var(--primary); font-variant-numeric: tabular-nums; }
.acct { display: flex; align-items: center; gap: 8px; margin-left: 12px; }
.acct-btn {
display: flex; align-items: center; gap: 8px;
border: none; background: transparent; cursor: pointer;
padding: 4px 8px; margin: -4px -8px; border-radius: 8px;
font: inherit; transition: background .13s;
}
.acct-btn:hover { background: var(--surface-2); }
.acct-avatar { flex-shrink: 0; }
.acct-avatar-fb { font-size: 12px; font-weight: 700; }
.acct-name { font-size: 13px; font-weight: 600; color: var(--text-sub); }
.body { flex: 1; display: flex; gap: 14px; min-height: 0; margin-top: 14px; }
.side { width: 232px; flex-shrink: 0; display: flex; flex-direction: column; padding: 14px 12px; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 0 6px 10px; }
.count { font-size: 11px; color: var(--text-faint); background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; padding: 1px 8px; }
.scene-list { list-style: none; margin: 0; padding: 0; flex: 1; overflow: auto; display: flex; flex-direction: column; gap: 2px; }
.scene-list li {
display: flex; align-items: center; gap: 10px;
padding: 9px 10px; border-radius: var(--radius-sm); cursor: pointer;
position: relative;
transition: background .13s, transform .13s ease;
}
.scene-list li:hover { background: var(--surface-2); transform: translateX(2px); }
.scene-list li.active { background: var(--primary-weak); }
.scene-list li.active::before {
content: ''; position: absolute; left: 0; top: 9px; bottom: 9px;
width: 3px; border-radius: 3px; background: var(--primary);
}
.scene-list li.active .scene-name { color: var(--primary); font-weight: 600; }
.scene-icon {
width: 26px; height: 26px; border-radius: 8px; flex-shrink: 0;
display: grid; place-items: center; color: #fff; font-size: 12px; font-weight: 700;
}
.scene-name { flex: 1; color: var(--text-sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.scene-list em { font-style: normal; font-size: 10px; color: var(--text-faint); }
.scene-empty {
flex: 1; display: grid; place-items: center; text-align: center;
font-size: 12.5px; color: var(--text-sub); line-height: 1.9; padding: 20px 12px;
}
.scene-empty span { font-size: 11px; color: var(--text-faint); }
.side-foot { font-size: 10px; color: var(--text-faint); text-align: center; padding-top: 10px; border-top: 1px dashed var(--border); margin-top: 8px; }
.main { flex: 1; display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.page-scroll { flex: 1; min-height: 0; padding: 20px 24px; overflow: auto; }
</style>