Files
37duAutomation/desktop/controlplane/frontend/src/views/AccountManageView.vue
T
puddy 8a196d6c87 feat: 重构控制面安全机制与分页规范
本次提交包含多项安全加固和体验优化:
1. 分离管理密钥与传输凭证,避免持 API Token 者伪造管理员票据
2. 钉钉回调地址增加白名单校验,防止开放重定向钓鱼
3. 初始管理员密码改为随机生成并日志记录,移除硬编码 123456
4. 新增登录防爆破计数表,支持 MySQL 多 worker 共享
5. 统一分页规范,新增 usePagedList composable 和 PagerBar 组件
6. 控制面 CORS 收敛为白名单,避免公网环境下任意站点跨域
7. 插件管理、凭证管理等列表接口支持分页和搜索
8. 强制初始密码修改,未改密前禁止使用管理功能
9. 定时任务触发被拒日志节流,避免授权失效时刷屏
10. 健康检查接口移除环境信息泄露,返回固定响应
2026-09-23 15:19:54 +08:00

338 lines
14 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 class="page">
<div class="page-head cp-rise">
<div>
<h3>账号管理 <span class="badge">Accounts</span></h3>
<p>建号 · 部门 / 管理员维护 · 密码重置(不能操作自己的账号)</p>
</div>
<div class="head-acts">
<el-button :loading="busy" :icon="Refresh" @click="refresh">刷新</el-button>
<el-button type="primary" :icon="Plus" @click="openCreate">新建账号</el-button>
</div>
</div>
<!-- 账号搜索(服务端模糊:账号/昵称/部门) -->
<div class="search-bar cp-card cp-rise">
<el-input v-model="kw" placeholder="搜索账号 / 昵称 / 部门" clearable :prefix-icon="Search" style="width: 300px" @keyup.enter="onSearch" @clear="onSearch" />
<span v-if="kw" class="dim-sm">搜索中:{{ kw }}</span>
</div>
<!-- 账号卡片 -->
<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, 2)" :key="p" size="small" type="primary" effect="light" round class="mono">{{ p }}</el-tag>
<span v-if="a.plugins.length > 2" class="more mono">+{{ a.plugins.length - 2 }}</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="!busy" description="暂无账号,点击右上角「新建账号」" :image-size="90" class="cp-card" />
<PagerBar v-if="total > 0" :total="total" :page="page" :page-size="pageSize" @change="onPageChange" />
<!-- 新建账号 -->
<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="至少 8 位" 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>
<div class="modal-foot">
<el-button @click="createOpen = false">取消</el-button>
<el-button type="primary" :loading="creating" @click="createAccount">建号</el-button>
</div>
</template>
</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>
<!-- 重置密码 -->
<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> 设置新密码(至少 8 位)
</el-alert>
<el-input v-model="resetPwdVal" type="password" placeholder="新密码(至少 8 位)" show-password @keyup.enter="confirmReset" />
<template #footer>
<div class="modal-foot">
<el-button @click="resetOpen = false">取消</el-button>
<el-button type="primary" :loading="resetting" @click="confirmReset">确认重置</el-button>
</div>
</template>
</el-dialog>
</div>
</template>
<script setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { ElMessage as message, ElMessageBox as dialog } from 'element-plus'
import { Refresh, Plus, Camera, EditPen, Edit, Delete, Search } from '@element-plus/icons-vue'
import { store } from '../store'
import { adminAccounts, adminAccountCreate, adminAccountUpdate, adminAccountReset, adminAccountDelete, adminAvatarUpload } from '../api'
import { fmtTime } from '../format'
import PagerBar from '../components/PagerBar.vue'
import { usePagedList } from '../composables/usePagedList'
const current = computed(() => store.current || '')
const kw = ref('')
const list = usePagedList((p, s) => adminAccounts(p, s, kw.value), { onError: e => message.error('读取失败:' + (e.message || e)) })
const { items: accounts, total, page, size: pageSize, busy, load: refresh, onPageChange, reset } = list
const busySwitch = ref('')
function onSearch() { reset() }
// ---------- 头像(显示 + 上传) ----------
const _fileInput = (() => {
const el = document.createElement('input')
el.type = 'file'
el.accept = 'image/*'
return el
})()
function pickAvatar(a) {
_fileInput.onchange = async () => {
const f = _fileInput.files && _fileInput.files[0]
_fileInput.value = ''
if (!f) return
try {
await adminAvatarUpload(a.account, f)
message.success(`已更新 ${a.account} 的头像`)
await refresh()
} catch (e) { message.error(e.message || e) }
}
_fileInput.click()
}
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 createOpen = ref(false)
const creating = ref(false)
const createFormRef = ref()
const form = reactive({ account: '', password: '', dept: '', nickname: '', is_admin: false })
const rules = {
account: { required: true, message: '请输入账号' },
password: [
{ required: true, message: '请输入初始密码' },
{ min: 8, message: '密码至少 8 位' },
],
}
function openCreate() { createOpen.value = true }
function resetCreate() {
form.account = form.password = form.dept = form.nickname = ''
form.is_admin = false
createFormRef.value?.resetFields()
}
async function createAccount() {
try { await createFormRef.value.validate() } catch (_) { return }
creating.value = true
try {
await adminAccountCreate({ ...form, account: form.account.trim() })
message.success(`已建号 ${form.account.trim()}`)
createOpen.value = false
await refresh()
} catch (e) { message.error('建号失败:' + (e.message || e)) }
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(a) {
resetTarget.value = a.account
resetPwdVal.value = ''
resetOpen.value = true
}
async function confirmReset() {
const p = resetPwdVal.value
if (p.length < 8) { message.warning('密码至少 8 位'); return }
resetting.value = true
try {
await adminAccountReset({ account: resetTarget.value, new_password: p })
message.success(`已重置 ${resetTarget.value} 密码`)
resetOpen.value = false
} catch (e) { message.error(e.message || e) }
finally { resetting.value = false }
}
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(() => {})
}
onMounted(refresh)
</script>
<style scoped>
.head-acts { display: flex; gap: 10px; }
.search-bar { display: flex; align-items: center; gap: 12px; padding: 14px 18px; margin-bottom: 16px; }
.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>