refactor: 整体迁移控制面至MySQL + 替换UI框架为Element Plus

1. 移除naive-ui依赖,迁移前端至Element Plus
2. 重构后端数据库层,切换为MySQL替代SQLite
3. 调整项目部署架构,停止exe打包改为源码部署
4. 新增设备上报接口与相关数据库模型
5. 优化配置路径与SQL语法,替换为MySQL占位符格式
6. 调整站点名称与文案,统一为「37du 插件管理平台」
7. 新增SQLite到MySQL迁移脚本
This commit is contained in:
2026-09-16 15:58:45 +08:00
parent 4363116e31
commit 84d3e103ec
54 changed files with 5542 additions and 1890 deletions
@@ -5,123 +5,149 @@
<h3>账号管理 <span class="badge">Accounts</span></h3>
<p>建号 · 部门 / 管理员维护 · 密码重置(不能操作自己的账号)</p>
</div>
<n-space :size="10">
<n-button :loading="overviewBusy" @click="refresh">
<template #icon><n-icon><Refresh /></n-icon></template>刷新
</n-button>
<n-button type="primary" @click="openCreate">
<template #icon><n-icon><Plus /></n-icon></template>新建账号
</n-button>
</n-space>
<div class="head-acts">
<el-button :loading="overviewBusy" :icon="Refresh" @click="refresh">刷新</el-button>
<el-button type="primary" :icon="Plus" @click="openCreate">新建账号</el-button>
</div>
</div>
<n-card class="cp-rise" :bordered="false">
<n-data-table
:columns="columns"
:data="accounts"
:loading="overviewBusy"
:row-key="r => r.account"
:single-line="false"
/>
</n-card>
<!-- 账号卡片 -->
<div class="acct-grid" v-if="accounts.length">
<div class="acct-card cp-card cp-rise" v-for="(a, i) in accounts" :key="a.account" :style="{ animationDelay: i * 40 + 'ms' }">
<div class="acct-top">
<div class="avatar-wrap">
<img v-if="a.avatar" :src="`/api/avatar/${a.account}?v=${encodeURIComponent(a.avatar)}`" class="avatar"
@error="e => e.target.style.display = 'none'" />
<span v-else class="avatar avatar-fallback">{{ (a.nickname || a.account || '?')[0].toUpperCase() }}</span>
<el-tooltip content="上传头像" placement="top">
<span class="avatar-edit" @click="pickAvatar(a)"><el-icon><Camera /></el-icon></span>
</el-tooltip>
</div>
<div class="acct-id">
<div class="id-row">
<span class="acct-account mono">{{ a.account }}</span>
<el-tag v-if="a.is_admin" size="small" type="primary" round effect="light">管理员</el-tag>
</div>
<div class="acct-nick">{{ a.nickname || '未设置昵称' }}</div>
</div>
<div class="acct-actions">
<el-tooltip content="重置密码"><el-button link type="primary" :icon="EditPen" @click="resetPwd(a)" /></el-tooltip>
<el-tooltip content="编辑资料"><el-button link type="info" :icon="Edit" @click="openEdit(a)" /></el-tooltip>
<el-tooltip content="删除账号"><el-button link type="danger" :icon="Delete" :disabled="a.account === current" @click="delAccount(a)" /></el-tooltip>
</div>
</div>
<div class="acct-tags">
<span class="tag-label">状态</span>
<el-switch size="small" :model-value="!!a.status" :disabled="a.account === current"
:loading="busySwitch === a.account" @change="v => toggleStatus(a, v)" />
<el-tag v-if="a.dept" size="small" type="info" effect="plain" round>{{ a.dept }}</el-tag>
</div>
<div class="acct-tags" v-if="a.plugins?.length">
<span class="tag-label">插件</span>
<el-tag v-for="p in a.plugins.slice(0, 3)" :key="p" size="small" type="primary" effect="light" round class="mono">{{ p }}</el-tag>
<span v-if="a.plugins.length > 3" class="more mono">+{{ a.plugins.length - 3 }}</span>
</div>
<div class="acct-tags" v-else>
<span class="tag-label">插件</span>
<span class="dim">未授权</span>
</div>
<div class="acct-tags" v-if="a.platforms?.length">
<span class="tag-label">平台</span>
<el-tag v-for="pf in a.platforms" :key="pf" size="small" type="success" effect="light" round>{{ pf }}</el-tag>
</div>
<div class="acct-foot">
<span class="mono created dim-sm">创建于 {{ fmtTime(a.created_at) }}</span>
<span class="mono uid dim-sm">ID {{ a.id ?? '—' }}</span>
</div>
</div>
</div>
<el-empty v-else-if="!overviewBusy" description="暂无账号,点击右上角「新建账号」" :image-size="90" class="cp-card" />
<!-- 新建账号 -->
<n-modal v-model:show="createOpen" preset="card" title="新建账号" :style="{ width: '420px' }" :bordered="false" @after-leave="resetCreate">
<n-form ref="formRef" :model="form" :rules="rules" label-placement="top">
<n-form-item label="账号" path="account">
<n-input v-model:value="form.account" placeholder="登录账号" />
</n-form-item>
<n-form-item label="昵称">
<n-input v-model:value="form.nickname" placeholder="真实姓名,方便识别(可填钉钉名)" />
</n-form-item>
<n-form-item label="初始密码" path="password">
<n-input v-model:value="form.password" type="password" show-password-on="click" placeholder="至少 6 位" />
</n-form-item>
<n-form-item label="部门">
<n-input v-model:value="form.dept" placeholder="如 财务部 / IT" />
</n-form-item>
<n-form-item label="管理员">
<n-switch v-model:value="form.is_admin" />
</n-form-item>
</n-form>
<el-dialog v-model="createOpen" title="新建账号" width="440px" align-center destroy-on-close @closed="resetCreate">
<el-form ref="createFormRef" :model="form" :rules="rules" label-position="top">
<el-form-item label="账号" prop="account">
<el-input v-model="form.account" placeholder="登录账号" />
</el-form-item>
<el-form-item label="昵称">
<el-input v-model="form.nickname" placeholder="真实姓名,方便识别(可填钉钉名)" />
</el-form-item>
<el-form-item label="初始密码" prop="password">
<el-input v-model="form.password" type="password" placeholder="至少 6 位" show-password />
</el-form-item>
<el-form-item label="部门">
<el-input v-model="form.dept" placeholder="如 财务部 / IT" />
</el-form-item>
<el-form-item label="管理员">
<el-switch v-model="form.is_admin" />
</el-form-item>
</el-form>
<template #footer>
<n-space justify="end">
<n-button @click="createOpen = false">取消</n-button>
<n-button type="primary" :loading="creating" @click="createAccount">建号</n-button>
</n-space>
<div class="modal-foot">
<el-button @click="createOpen = false">取消</el-button>
<el-button type="primary" :loading="creating" @click="createAccount">建号</el-button>
</div>
</template>
</n-modal>
</el-dialog>
<!-- 编辑资料 -->
<el-dialog v-model="editOpen" title="编辑账号资料" width="420px" align-center destroy-on-close>
<el-form :model="editForm" label-position="top">
<el-form-item label="账号">
<el-input :model-value="editTarget" disabled class="mono" />
</el-form-item>
<el-form-item label="昵称">
<el-input v-model="editForm.nickname" placeholder="真实姓名" />
</el-form-item>
<el-form-item label="部门">
<el-input v-model="editForm.dept" placeholder="如 财务部 / IT" />
</el-form-item>
<el-form-item label="管理员">
<el-switch v-model="editForm.is_admin" :disabled="editTarget === current" />
</el-form-item>
</el-form>
<template #footer>
<div class="modal-foot">
<el-button @click="editOpen = false">取消</el-button>
<el-button type="primary" :loading="savingEdit" @click="saveEdit">保存</el-button>
</div>
</template>
</el-dialog>
<!-- 重置密码 -->
<n-modal v-model:show="resetOpen" preset="card" title="重置密码" :style="{ width: '400px' }" :bordered="false">
<n-alert v-if="resetTarget" type="info" :bordered="false" style="margin-bottom: 14px">
为账号 <b>{{ resetTarget }}</b> 设置新密码(至少 6 位)
</n-alert>
<n-input v-model:value="resetPwdVal" type="password" show-password-on="click" placeholder="新密码(至少 6 位)" @keydown.enter="confirmReset" />
<el-dialog v-model="resetOpen" title="重置密码" width="420px" align-center destroy-on-close>
<el-alert type="info" :closable="false" class="reset-tip" show-icon>
为账号 <b class="mono">{{ resetTarget }}</b> 设置新密码(至少 6 位)
</el-alert>
<el-input v-model="resetPwdVal" type="password" placeholder="新密码(至少 6 位)" show-password @keyup.enter="confirmReset" />
<template #footer>
<n-space justify="end">
<n-button @click="resetOpen = false">取消</n-button>
<n-button type="primary" :loading="resetting" @click="confirmReset">确认重置</n-button>
</n-space>
<div class="modal-foot">
<el-button @click="resetOpen = false">取消</el-button>
<el-button type="primary" :loading="resetting" @click="confirmReset">确认重置</el-button>
</div>
</template>
</n-modal>
</el-dialog>
</div>
</template>
<script setup>
import { computed, h, onMounted, reactive, ref } from 'vue'
import { NButton, NInput, NSwitch, NTag } from 'naive-ui'
import { Refresh, Plus } from '@element-plus/icons-vue'
import { computed, onMounted, reactive, ref } from 'vue'
import { ElMessage as message, ElMessageBox as dialog } from 'element-plus'
import { Refresh, Plus, Camera, EditPen, Edit, Delete } from '@element-plus/icons-vue'
import { store, overview, overviewBusy, loadOverview } from '../store'
import { adminAccountCreate, adminAccountUpdate, adminAccountReset, adminAccountDelete, adminAvatarUpload } from '../api'
import { message, dialog } from '../naive'
import { fmtTime } from '../format'
const current = computed(() => store.current || '')
const accounts = computed(() => overview.value?.accounts || [])
const busySwitch = ref('')
// ---------- 部门 / 昵称 行内编辑 ----------
const editing = ref('') // 正在编辑的账号
const editField = ref('') // 'dept' | 'nickname'
const editDraft = ref('')
function startEdit(row, field) {
if (row.account === current.value) return
editing.value = row.account
editField.value = field
editDraft.value = row[field] || ''
}
async function saveEdit(row) {
if (editing.value !== row.account) return
const field = editField.value
editing.value = ''
const val = (editDraft.value || '').trim()
if (val === (row[field] || '')) return
try {
await adminAccountUpdate({ account: row.account, [field]: val })
message.success(`已更新 ${row.account} 的${field === 'nickname' ? '昵称' : '部门'}`)
} catch (e) { message.error(e.message || e) }
}
function inlineInput(row, field) {
if (row.account === editing.value && editField.value === field) {
return h(NInput, {
value: editDraft.value,
size: 'small',
style: 'width: 140px',
onUpdateValue: v => (editDraft.value = v),
onKeyup: (e) => { if (e.key === 'Enter') saveEdit(row) },
onBlur: () => saveEdit(row),
})
}
return h('span', { class: 'dept', onClick: () => startEdit(row, field) },
row[field] || '—')
}
async function toggleAdmin(row) {
try { await adminAccountUpdate({ account: row.account, is_admin: !row.is_admin }); await refresh() }
catch (e) { message.error(e.message || e) }
}
async function toggleStatus(row, v) {
if (row.account === current.value && !v) { message.warning('不能停用当前登录的账号'); return }
try { await adminAccountUpdate({ account: row.account, status: v }); await refresh() }
catch (e) { message.error(e.message || e) }
async function refresh() {
try { await loadOverview(true) } catch (e) { message.error('读取失败:' + (e.message || e)) }
}
// ---------- 头像(显示 + 上传) ----------
@@ -131,83 +157,32 @@ const _fileInput = (() => {
el.accept = 'image/*'
return el
})()
function pickAvatar(row) {
function pickAvatar(a) {
_fileInput.onchange = async () => {
const f = _fileInput.files && _fileInput.files[0]
_fileInput.value = ''
if (!f) return
try {
await adminAvatarUpload(row.account, f)
message.success(`已更新 ${row.account} 的头像`)
await adminAvatarUpload(a.account, f)
message.success(`已更新 ${a.account} 的头像`)
await refresh()
} catch (e) { message.error(e.message || e) }
}
_fileInput.click()
}
function avatarCell(row) {
const img = row.avatar
? h('img', {
src: `/api/avatar/${row.account}?v=${encodeURIComponent(row.avatar)}`,
class: 'cell-avatar',
onError: (e) => { e.target.style.display = 'none' },
})
: h('span', { class: 'dim' }, '—')
return h('div', { class: 'cell-avatar-wrap' }, [
img,
h(NButton, { text: true, size: 'tiny', type: 'primary', onClick: () => pickAvatar(row) }, { default: () => '上传' }),
])
}
function renderTags(list, kind) {
if (!list || !list.length) return h('span', { class: 'dim' }, '—')
return h('div', { class: 'tags' }, list.map(t => h(NTag, {
size: 'small', round: true, bordered: false,
type: kind === 'platform' ? 'success' : 'primary',
}, { default: () => t })))
async function toggleStatus(a, v) {
if (a.account === current.value && !v) { message.warning('不能停用当前登录的账号'); return }
busySwitch.value = a.account
try { await adminAccountUpdate({ account: a.account, status: v }); await refresh() }
catch (e) { message.error(e.message || e) }
finally { busySwitch.value = '' }
}
const columns = computed(() => [
{ title: 'ID', key: 'id', width: 64, render: r => h('span', { class: 'dim' }, r.id ?? '—') },
{ title: '用户名(工号)', key: 'account', width: 130, render: r => h('b', { class: 'cell-strong' }, r.account) },
{ title: '昵称(姓名)', key: 'nickname', width: 130, render: r => inlineInput(r, 'nickname') },
{ title: '部门', key: 'dept', width: 130, render: (row) => inlineInput(row, 'dept') },
{ title: '头像', key: 'avatar', width: 120, render: r => avatarCell(r) },
{
title: '状态', key: 'status', width: 90,
render: (row) => h(NSwitch, {
value: !!row.status,
disabled: row.account === current.value,
onUpdateValue: (v) => toggleStatus(row, v),
}),
},
{ title: '创建时间', key: 'created_at', width: 150, render: r => h('span', { class: 'dim' }, r.created_at || '—') },
{
title: '管理员', key: 'is_admin', width: 90,
render: (row) => h(NSwitch, {
value: !!row.is_admin,
disabled: row.account === current.value,
onUpdateValue: () => toggleAdmin(row),
}),
},
{ title: '已授权插件', key: 'plugins', minWidth: 160, render: r => renderTags(r.plugins, 'plugin') },
{ title: '已授权平台', key: 'platforms', minWidth: 130, render: r => renderTags(r.platforms, 'platform') },
{
title: '操作', key: 'actions', width: 170,
render: (row) => h('div', { class: 'ops' }, [
h(NButton, { text: true, type: 'primary', onClick: () => resetPwd(row) }, { default: () => '重置密码' }),
h(NButton, {
text: true, type: 'error',
disabled: row.account === current.value,
onClick: () => delAccount(row),
}, { default: () => '删除' }),
]),
},
])
// ---------- 新建账号 ----------
const createOpen = ref(false)
const creating = ref(false)
const formRef = ref()
const createFormRef = ref()
const form = reactive({ account: '', password: '', dept: '', nickname: '', is_admin: false })
const rules = {
account: { required: true, message: '请输入账号' },
@@ -220,10 +195,10 @@ function openCreate() { createOpen.value = true }
function resetCreate() {
form.account = form.password = form.dept = form.nickname = ''
form.is_admin = false
formRef.value?.restoreValidation()
createFormRef.value?.resetFields()
}
async function createAccount() {
try { await formRef.value.validate() } catch (_) { return }
try { await createFormRef.value.validate() } catch (_) { return }
creating.value = true
try {
await adminAccountCreate({ ...form, account: form.account.trim() })
@@ -234,13 +209,36 @@ async function createAccount() {
finally { creating.value = false }
}
// ---------- 编辑资料 ----------
const editOpen = ref(false)
const savingEdit = ref(false)
const editTarget = ref('')
const editForm = reactive({ nickname: '', dept: '', is_admin: false })
function openEdit(a) {
editTarget.value = a.account
editForm.nickname = a.nickname || ''
editForm.dept = a.dept || ''
editForm.is_admin = !!a.is_admin
editOpen.value = true
}
async function saveEdit() {
savingEdit.value = true
try {
await adminAccountUpdate({ account: editTarget.value, nickname: editForm.nickname, dept: editForm.dept, is_admin: editForm.is_admin })
message.success(`已更新 ${editTarget.value}`)
editOpen.value = false
await refresh()
} catch (e) { message.error(e.message || e) }
finally { savingEdit.value = false }
}
// ---------- 重置密码 ----------
const resetOpen = ref(false)
const resetTarget = ref('')
const resetPwdVal = ref('')
const resetting = ref(false)
function resetPwd(row) {
resetTarget.value = row.account
function resetPwd(a) {
resetTarget.value = a.account
resetPwdVal.value = ''
resetOpen.value = true
}
@@ -256,34 +254,75 @@ async function confirmReset() {
finally { resetting.value = false }
}
function delAccount(row) {
dialog.warning({
title: '删除账号',
content: `确认删除账号 ${row.account}?其插件授权将一并清除`,
positiveText: '删除',
negativeText: '取消',
onPositiveClick: async () => {
try { await adminAccountDelete({ account: row.account }); message.success(`已删除 ${row.account}`); await refresh() }
catch (e) { message.error(e.message || e) }
},
})
function delAccount(a) {
dialog.confirm(`确认删除账号 ${a.account}?其插件授权将一并清除`, '删除账号', {
type: 'warning',
confirmButtonText: '删除',
cancelButtonText: '取消',
}).then(async () => {
try { await adminAccountDelete({ account: a.account }); message.success(`已删除 ${a.account}`); await refresh() }
catch (e) { message.error(e.message || e) }
}).catch(() => {})
}
async function refresh() {
try { await loadOverview(true) } catch (e) { message.error('读取失败:' + (e.message || e)) }
}
onMounted(refresh)
</script>
<style scoped>
.dept { cursor: pointer; border-bottom: 1px dashed transparent; transition: border-color .15s; }
.dept:hover { color: var(--cp-blue); border-bottom-color: rgba(59, 108, 255, .4); }
</style>
.head-acts { display: flex; gap: 10px; }
<!-- 列 render 中由 naive-ui 动态渲染的节点不继承 scoped scopeId,须用全局样式 -->
<style>
.ops { display: flex; align-items: center; gap: 16px; }
.ops .n-button { padding: 0 4px; }
.cell-avatar-wrap { display: flex; align-items: center; gap: 8px; }
.cell-avatar { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; background: #f2f3f7; flex: none; }
.acct-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
gap: 18px;
}
.acct-card {
padding: 20px;
display: flex; flex-direction: column; gap: 12px;
}
.acct-top { display: flex; align-items: center; gap: 14px; }
.avatar-wrap { position: relative; flex-shrink: 0; }
.avatar {
width: 52px; height: 52px; border-radius: 16px; object-fit: cover;
display: block; box-shadow: 0 6px 14px -8px rgba(35, 42, 58, .4);
}
.avatar-fallback {
display: grid; place-items: center;
background: linear-gradient(135deg, var(--cp-blue), var(--cp-teal));
color: #fff; font-size: 20px; font-weight: 800;
}
.avatar-edit {
position: absolute; right: -6px; bottom: -6px;
width: 22px; height: 22px; border-radius: 8px;
display: grid; place-items: center;
background: var(--cp-panel); border: 1px solid var(--cp-line-strong);
color: var(--cp-ink-2); cursor: pointer;
transition: all .18s;
}
.avatar-edit:hover { color: var(--cp-blue); border-color: var(--cp-blue); background: rgba(47, 92, 240, .06); }
.acct-id { flex: 1; min-width: 0; }
.id-row { display: flex; align-items: center; gap: 8px; }
.acct-account { font-size: 16px; font-weight: 800; color: var(--cp-ink-1); }
.acct-nick { font-size: 12.5px; color: var(--cp-ink-3); margin-top: 4px; }
.acct-actions { display: flex; align-items: center; gap: 2px; flex-shrink: 0; }
.acct-actions .el-button { font-size: 16px; }
.acct-tags { display: flex; align-items: center; flex-wrap: wrap; gap: 7px; }
.tag-label {
font-size: 11px; font-weight: 700; color: var(--cp-ink-3);
letter-spacing: .5px; margin-right: 2px; flex-shrink: 0;
}
.more { font-size: 12px; color: var(--cp-ink-3); font-weight: 700; }
.acct-foot {
display: flex; justify-content: space-between; align-items: center;
margin-top: auto; padding-top: 12px;
border-top: 1px dashed var(--cp-line-strong);
}
.dim-sm { color: var(--cp-ink-3); font-size: 11px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; }
.reset-tip { margin-bottom: 16px; }
</style>