1. 移除naive-ui依赖,迁移前端至Element Plus 2. 重构后端数据库层,切换为MySQL替代SQLite 3. 调整项目部署架构,停止exe打包改为源码部署 4. 新增设备上报接口与相关数据库模型 5. 优化配置路径与SQL语法,替换为MySQL占位符格式 6. 调整站点名称与文案,统一为「37du 插件管理平台」 7. 新增SQLite到MySQL迁移脚本
295 lines
12 KiB
Vue
295 lines
12 KiB
Vue
<template>
|
||
<div class="page">
|
||
<div class="page-head cp-rise">
|
||
<div>
|
||
<h3>权限授权 <span class="badge">Grants</span></h3>
|
||
<p>按账号勾选可用插件与可登录平台,保存时仅提交差异项</p>
|
||
</div>
|
||
<el-button :loading="overviewBusy" :icon="Refresh" @click="refresh">刷新</el-button>
|
||
</div>
|
||
|
||
<div class="cp-card panel cp-rise">
|
||
<!-- 账号选择 -->
|
||
<div class="pick-row">
|
||
<div class="pick-label">选择账号</div>
|
||
<el-select v-model="account" filterable clearable placeholder="选择账号…" style="width: 260px" :prefix-icon="User">
|
||
<el-option v-for="a in accountOptions" :key="a.value" :label="a.label" :value="a.value" />
|
||
</el-select>
|
||
<div class="pick-hint" v-if="account">
|
||
已授权 <b class="mono">{{ grantedIds.length }}</b> 个插件 / <b class="mono">{{ platChecked.length }}</b> 个平台
|
||
</div>
|
||
</div>
|
||
|
||
<template v-if="account">
|
||
<!-- 插件授权 -->
|
||
<div class="section">
|
||
<div class="section-head">
|
||
<div class="card-title">插件授权</div>
|
||
<el-checkbox :model-value="allChecked" :indeterminate="someChecked" @change="toggleAll">全选</el-checkbox>
|
||
</div>
|
||
<el-checkbox-group v-model="grantedIds" class="plist">
|
||
<div class="prow" v-for="p in plugins" :key="p.id" :class="{ on: grantedIds.includes(p.id) }">
|
||
<el-checkbox :value="p.id" class="pck">
|
||
<span class="pname mono">{{ p.id }}</span>
|
||
<span class="psub dim">{{ p.name }}</span>
|
||
</el-checkbox>
|
||
<div class="pswitches">
|
||
<div class="psw">
|
||
<span class="psw-label" :class="{ on: canRun(p.id) }">可运行</span>
|
||
<el-switch size="small" :model-value="canRun(p.id)" :disabled="!grantedIds.includes(p.id)" @change="v => setGrant(p.id, { canRun: v })" />
|
||
</div>
|
||
<div class="psw">
|
||
<span class="psw-label" :class="{ on: canDownload(p.id) }">可下载</span>
|
||
<el-switch size="small" :model-value="canDownload(p.id)" :disabled="!grantedIds.includes(p.id)" @change="v => setGrant(p.id, { canDownload: v })" />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</el-checkbox-group>
|
||
<el-empty v-if="!plugins.length" description="暂无插件,请先在「插件管理」上传" :image-size="60" />
|
||
</div>
|
||
|
||
<!-- 平台授权 -->
|
||
<div class="section">
|
||
<div class="section-head">
|
||
<div class="card-title">平台授权</div>
|
||
</div>
|
||
<el-checkbox-group v-model="platChecked" class="pf-list">
|
||
<el-checkbox v-for="pf in PLATFORM_OPTS" :key="pf.id" :value="pf.id" class="pf-chip">
|
||
<span>{{ pf.label }}</span>
|
||
<span class="dim mono">({{ pf.id }})</span>
|
||
</el-checkbox>
|
||
</el-checkbox-group>
|
||
</div>
|
||
</template>
|
||
|
||
<el-empty v-else description="先在上方选择一个账号" :image-size="80" class="no-acc" />
|
||
</div>
|
||
|
||
<!-- 底部保存栏 -->
|
||
<transition name="savebar">
|
||
<div class="savebar cp-card cp-rise" v-if="account">
|
||
<div class="save-hint">
|
||
<span class="dot" :class="{ dirty: dirtyCount > 0 }"></span>
|
||
{{ dirtyCount > 0 ? `有 ${dirtyCount} 项授权变更待保存` : '授权已是最新' }}
|
||
</div>
|
||
<el-button type="primary" :loading="saving" :disabled="dirtyCount === 0" :icon="Check" @click="saveAll">
|
||
保存授权
|
||
</el-button>
|
||
</div>
|
||
</transition>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup>
|
||
import { computed, onMounted, ref, watch } from 'vue'
|
||
import { Refresh, User, Check } from '@element-plus/icons-vue'
|
||
import { ElMessage as message } from 'element-plus'
|
||
import { overview, overviewBusy, loadOverview } from '../store'
|
||
import { adminGrant, adminPlatformGrant, adminPlatforms } from '../api'
|
||
|
||
const PLATFORM_OPTS = ref([])
|
||
|
||
const plugins = computed(() => overview.value?.plugins || [])
|
||
const accounts = computed(() => overview.value?.accounts || [])
|
||
const accountOptions = computed(() => accounts.value.map(a => ({
|
||
label: a.dept ? `${a.account} · ${a.dept}` : a.account,
|
||
value: a.account,
|
||
})))
|
||
|
||
const account = ref('')
|
||
const grantedIds = ref([])
|
||
const grantState = ref({}) // plugin_id -> { granted, canRun, canDownload }
|
||
const platChecked = ref([])
|
||
const saving = ref(false)
|
||
let heldGrantState = {} // 已保存快照,用于差异比对
|
||
let heldPlatforms = new Set()
|
||
|
||
function setGrant(pid, patch) {
|
||
grantState.value[pid] = { ...grantState.value[pid], ...patch }
|
||
grantState.value = { ...grantState.value }
|
||
}
|
||
function canRun(pid) { return grantState.value[pid]?.canRun !== false }
|
||
function canDownload(pid) { return grantState.value[pid]?.canDownload !== false }
|
||
|
||
// 全选 / 半选
|
||
const allChecked = computed(() => plugins.value.length > 0 && grantedIds.value.length === plugins.value.length)
|
||
const someChecked = computed(() => grantedIds.value.length > 0 && !allChecked.value)
|
||
function toggleAll(v) {
|
||
grantedIds.value = v ? plugins.value.map(p => p.id) : []
|
||
}
|
||
|
||
// 选中账号即自动载入其现有授权
|
||
watch(account, (acc) => {
|
||
const a = accounts.value.find(x => x.account === acc)
|
||
const state = {}
|
||
;(a?.plugins || []).forEach(pid => {
|
||
const g = a?.grants?.[pid] || {}
|
||
state[pid] = { granted: true, canRun: g.canRun !== false, canDownload: g.canDownload !== false }
|
||
})
|
||
heldGrantState = JSON.parse(JSON.stringify(state))
|
||
grantState.value = JSON.parse(JSON.stringify(state))
|
||
grantedIds.value = [...(a?.plugins || [])]
|
||
heldPlatforms = new Set(a?.platforms || [])
|
||
platChecked.value = [...heldPlatforms]
|
||
})
|
||
|
||
// 勾选/取消勾选插件时同步 granted 状态
|
||
watch(grantedIds, (ids) => {
|
||
const state = { ...grantState.value }
|
||
ids.forEach(pid => {
|
||
if (!state[pid]) state[pid] = { granted: true, canRun: true, canDownload: true }
|
||
else state[pid].granted = true
|
||
})
|
||
Object.keys(state).forEach(pid => {
|
||
if (!ids.includes(pid)) state[pid].granted = false
|
||
})
|
||
grantState.value = state
|
||
})
|
||
|
||
// 差异计算
|
||
const pluginDirty = computed(() => {
|
||
const want = {}
|
||
Object.entries(grantState.value).forEach(([pid, s]) => { if (s.granted) want[pid] = s })
|
||
const allPids = [...new Set([...Object.keys(want), ...Object.keys(heldGrantState)])]
|
||
return allPids.filter(pid => {
|
||
const w = want[pid]; const h = heldGrantState[pid]
|
||
if (!!w !== !!h) return true
|
||
if (w && h) return w.canRun !== h.canRun || w.canDownload !== h.canDownload
|
||
return false
|
||
})
|
||
})
|
||
const platDirty = computed(() => {
|
||
const want = new Set(platChecked.value)
|
||
return [...new Set([...want, ...heldPlatforms])].filter(pf => want.has(pf) !== heldPlatforms.has(pf))
|
||
})
|
||
const dirtyCount = computed(() => pluginDirty.value.length + platDirty.value.length)
|
||
|
||
async function refresh() {
|
||
try { await loadOverview(true) } catch (e) { message.error('读取失败:' + (e.message || e)) }
|
||
}
|
||
async function loadPlatforms() {
|
||
try {
|
||
const r = await adminPlatforms()
|
||
PLATFORM_OPTS.value = (r.platforms || []).map(p => ({ id: p.id, label: p.label }))
|
||
} catch (e) { message.error('平台列表读取失败:' + (e.message || e)) }
|
||
}
|
||
|
||
async function savePluginDiff() {
|
||
const acc = account.value
|
||
const want = {}
|
||
Object.entries(grantState.value).forEach(([pid, s]) => { if (s.granted) want[pid] = s })
|
||
const diff = pluginDirty.value
|
||
const results = await Promise.allSettled(diff.map(pid => adminGrant({
|
||
account: acc, plugin_id: pid, granted: !!want[pid],
|
||
can_run: want[pid]?.canRun !== false, can_download: want[pid]?.canDownload !== false,
|
||
})))
|
||
const failed = results.filter(r => r.status === 'rejected')
|
||
failed.forEach(r => message.error(r.reason?.message || String(r.reason)))
|
||
if (failed.length) { message.warning(`插件授权:${diff.length - failed.length} 项成功,${failed.length} 项失败`); return 0 }
|
||
heldGrantState = JSON.parse(JSON.stringify(grantState.value))
|
||
return diff.length
|
||
}
|
||
async function savePlatDiff() {
|
||
const acc = account.value
|
||
const want = new Set(platChecked.value)
|
||
const diff = platDirty.value
|
||
const results = await Promise.allSettled(diff.map(pf => adminPlatformGrant({
|
||
account: acc, platform_id: pf, granted: want.has(pf),
|
||
})))
|
||
const failed = results.filter(r => r.status === 'rejected')
|
||
failed.forEach(r => message.error(r.reason?.message || String(r.reason)))
|
||
if (failed.length) { message.warning(`平台授权:${diff.length - failed.length} 项成功,${failed.length} 项失败`); return 0 }
|
||
heldPlatforms = new Set(platChecked.value)
|
||
return diff.length
|
||
}
|
||
|
||
async function saveAll() {
|
||
const acc = account.value; if (!acc) return
|
||
saving.value = true
|
||
try {
|
||
let okP = 0, okPl = 0
|
||
if (pluginDirty.value.length) okP = await savePluginDiff()
|
||
if (platDirty.value.length) okPl = await savePlatDiff()
|
||
if (!pluginDirty.value.length && !platDirty.value.length) { message.info('授权无需变更'); return }
|
||
message.success(`已保存:插件 ${okP} 项,平台 ${okPl} 项`)
|
||
await refresh()
|
||
} catch (e) { message.error('保存失败:' + (e.message || e)) }
|
||
finally { saving.value = false }
|
||
}
|
||
onMounted(() => { refresh(); loadPlatforms() })
|
||
</script>
|
||
|
||
<style scoped>
|
||
.panel { padding: 22px 24px 26px; }
|
||
|
||
.pick-row { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; padding-bottom: 20px; border-bottom: 1px dashed var(--cp-line-strong); }
|
||
.pick-label { font-size: 13px; font-weight: 700; color: var(--cp-ink-1); }
|
||
.pick-hint { font-size: 12.5px; color: var(--cp-ink-3); }
|
||
.pick-hint b { color: var(--cp-blue); font-size: 13px; }
|
||
|
||
.section { margin-top: 24px; }
|
||
.section-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
|
||
.section-head .card-title { margin-bottom: 0; }
|
||
|
||
/* 插件授权行 */
|
||
.plist { display: flex; flex-direction: column; gap: 8px; }
|
||
.prow {
|
||
display: flex; align-items: center; justify-content: space-between; gap: 16px;
|
||
padding: 12px 16px;
|
||
border: 1px solid var(--cp-line);
|
||
border-radius: 12px;
|
||
background: var(--cp-panel);
|
||
transition: border-color .18s, background .18s, box-shadow .18s;
|
||
}
|
||
.prow:hover { border-color: rgba(47, 92, 240, .35); }
|
||
.prow.on {
|
||
border-color: rgba(47, 92, 240, .4);
|
||
background: linear-gradient(90deg, rgba(47, 92, 240, .05), rgba(15, 159, 155, .03));
|
||
box-shadow: 0 8px 18px -12px rgba(47, 92, 240, .4);
|
||
}
|
||
.prow .pck { flex: 1; min-width: 0; }
|
||
.pname { font-weight: 700; color: var(--cp-ink-1); font-size: 13px; }
|
||
.psub { font-size: 12px; margin-left: 8px; }
|
||
|
||
.pswitches { display: flex; gap: 22px; flex-shrink: 0; }
|
||
.psw { display: flex; align-items: center; gap: 8px; }
|
||
.psw-label { font-size: 12px; color: var(--cp-ink-3); transition: color .18s; }
|
||
.psw-label.on { color: var(--cp-blue); font-weight: 600; }
|
||
|
||
/* 平台授权 chips */
|
||
.pf-list { display: flex; flex-wrap: wrap; gap: 10px; }
|
||
.pf-chip { margin: 0 !important; }
|
||
.pf-chip :deep(.el-checkbox__input) { display: none; }
|
||
.pf-chip :deep(.el-checkbox__label) {
|
||
display: inline-flex; align-items: center; gap: 6px;
|
||
padding: 8px 16px;
|
||
border: 1px solid var(--cp-line);
|
||
border-radius: 12px;
|
||
background: var(--cp-panel);
|
||
font-weight: 600;
|
||
transition: border-color .18s, background .18s, box-shadow .18s, color .18s;
|
||
}
|
||
.pf-chip:hover :deep(.el-checkbox__label) { border-color: rgba(47, 92, 240, .4); }
|
||
.pf-chip.is-checked :deep(.el-checkbox__label) {
|
||
border-color: rgba(47, 92, 240, .55);
|
||
background: linear-gradient(135deg, rgba(47, 92, 240, .10), rgba(15, 159, 155, .06));
|
||
color: var(--cp-blue);
|
||
box-shadow: 0 6px 14px -10px rgba(47, 92, 240, .5);
|
||
}
|
||
.no-acc { padding: 30px 0; }
|
||
|
||
/* 底部保存栏 */
|
||
.savebar {
|
||
position: sticky; bottom: 18px; z-index: 10;
|
||
display: flex; align-items: center; justify-content: space-between;
|
||
margin-top: 20px; padding: 14px 20px;
|
||
background: rgba(255, 253, 248, .92);
|
||
backdrop-filter: blur(12px);
|
||
}
|
||
.save-hint { display: flex; align-items: center; gap: 9px; font-size: 13px; color: var(--cp-ink-2); font-weight: 600; }
|
||
.save-hint .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--cp-success); }
|
||
.save-hint .dot.dirty { background: var(--cp-amber); box-shadow: 0 0 0 4px rgba(217, 119, 6, .18); }
|
||
.savebar-enter-active, .savebar-leave-active { transition: opacity .25s ease, transform .25s ease; }
|
||
.savebar-enter-from, .savebar-leave-to { opacity: 0; transform: translateY(10px); }
|
||
</style>
|