<template>
|
<div class="page">
|
<div class="page-title">
|
<div><h2>用户管理</h2><p>左侧查询用户,右侧设置该用户拥有的角色,一个用户可设置多个角色</p></div>
|
</div>
|
<div class="user-layout">
|
<section class="user-left">
|
<div class="toolbar">
|
<el-button type="primary" plain icon="el-icon-s-check" :disabled="!selectedRows.length" v-permission="'system:users:assign'" @click="openBatchDialog">批量设置角色</el-button>
|
<span v-if="selectedRows.length" class="selected-count">已选 {{ selectedRows.length }} 人</span>
|
<el-input v-model="filters.keyword" clearable placeholder="用户名 / 账户名" style="width:190px" @keyup.enter.native="search"></el-input>
|
<el-select v-model="filters.deptId" clearable filterable placeholder="所属部门" style="width:200px">
|
<el-option v-for="item in depts" :key="item.id" :label="item.name" :value="item.id"></el-option>
|
</el-select>
|
<el-select v-model="filters.roleId" clearable placeholder="角色" style="width:170px">
|
<el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id"></el-option>
|
</el-select>
|
<el-button type="primary" icon="el-icon-search" v-permission="'system:users:query'" @click="search">查询</el-button>
|
<el-button icon="el-icon-refresh-left" @click="reset">重置</el-button>
|
</div>
|
<el-table ref="userTable" row-key="userId"
|
v-loading="loading"
|
:data="rows"
|
border
|
stripe
|
highlight-current-row
|
height="calc(100vh - 250px)"
|
@selection-change="selectedRows = $event"
|
@current-change="selectUser"
|
>
|
<el-table-column type="selection" width="46" align="center"></el-table-column>
|
<el-table-column prop="nickName" label="用户名" min-width="110"></el-table-column>
|
<el-table-column prop="userName" label="账户名" min-width="140"></el-table-column>
|
<el-table-column prop="deptName" label="所属部门" min-width="180" show-overflow-tooltip></el-table-column>
|
<el-table-column label="角色权限" min-width="220">
|
<template slot-scope="{ row }">
|
<el-tag v-for="role in row.roles" :key="role.roleId" size="mini" class="role-tag">{{ role.roleName }}</el-tag>
|
<span v-if="!row.roles || !row.roles.length" class="muted">未分配</span>
|
</template>
|
</el-table-column>
|
<el-table-column prop="phoneNumber" label="手机号" width="130"></el-table-column>
|
<el-table-column label="状态" width="80" align="center">
|
<template slot-scope="{ row }">
|
<el-tag size="mini" :type="isDisabled(row) ? 'info' : 'success'">{{ isDisabled(row) ? '停用' : '正常' }}</el-tag>
|
</template>
|
</el-table-column>
|
</el-table>
|
<el-pagination
|
class="pager"
|
background
|
layout="total, sizes, prev, pager, next"
|
:total="total"
|
:current-page="current"
|
:page-size="size"
|
:page-sizes="[20, 50, 100]"
|
@current-change="handleCurrentChange"
|
@size-change="handleSizeChange"
|
></el-pagination>
|
</section>
|
<section class="user-right">
|
<div class="panel-head">
|
<h3>角色授权</h3>
|
<span v-if="selected" class="selected-user">{{ selected.nickName || selected.userName }}({{ selected.userName }})</span>
|
</div>
|
<div v-if="!selected" class="empty-tip">请选择左侧用户</div>
|
<template v-else>
|
<el-checkbox-group v-model="checkedRoleIds" class="role-list">
|
<el-checkbox v-for="item in roles" :key="item.id" :label="item.id" class="role-item">
|
<span class="role-name">{{ item.name }}</span>
|
<span class="role-code">{{ item.code }}</span>
|
</el-checkbox>
|
</el-checkbox-group>
|
<el-button
|
type="primary"
|
icon="el-icon-check"
|
class="save-button"
|
v-permission="'system:users:assign'"
|
:loading="saving"
|
@click="saveRoles"
|
>保存授权</el-button>
|
</template>
|
</section>
|
</div>
|
<el-dialog title="批量设置角色" :visible.sync="batchVisible" width="520px" append-to-body :close-on-click-modal="false">
|
<div class="batch-summary">已选择 {{ selectedRows.length }} 个用户</div>
|
<el-radio-group v-model="batchMode" class="batch-mode">
|
<el-radio label="append">追加角色</el-radio>
|
<el-radio label="replace">覆盖角色</el-radio>
|
</el-radio-group>
|
<el-alert :title="batchMode === 'append' ? '保留所选用户原有角色,并追加勾选角色' : '使用勾选角色覆盖所选用户的原有角色'" type="warning" :closable="false" show-icon></el-alert>
|
<el-checkbox-group v-model="batchRoleIds" class="batch-role-list">
|
<el-checkbox v-for="item in roles" :key="item.id" :label="item.id" class="role-item">
|
<span class="role-name">{{ item.name }}</span>
|
<span class="role-code">{{ item.code }}</span>
|
</el-checkbox>
|
</el-checkbox-group>
|
<span slot="footer">
|
<el-button @click="batchVisible = false">取消</el-button>
|
<el-button type="primary" :loading="batchSaving" v-permission="'system:users:assign'" @click="saveBatchRoles">保存</el-button>
|
</span>
|
</el-dialog>
|
</div>
|
</template>
|
<script>
|
import api from '@/api'
|
export default {
|
name: 'UserManage',
|
data() {
|
return {
|
rows: [],
|
total: 0,
|
current: 1,
|
size: 20,
|
loading: false,
|
saving: false,
|
batchSaving: false,
|
batchVisible: false,
|
batchRoleIds: [],
|
batchMode: 'append',
|
selectedRows: [],
|
depts: [],
|
roles: [],
|
selected: null,
|
checkedRoleIds: [],
|
filters: { keyword: '', deptId: null, roleId: null }
|
}
|
},
|
created() {
|
this.loadRoles()
|
this.loadDepts()
|
this.load()
|
},
|
methods: {
|
load() {
|
this.clearSelection()
|
this.loading = true
|
api.get('/system/users', {
|
params: {
|
keyword: this.filters.keyword || undefined,
|
deptId: this.filters.deptId || undefined,
|
roleId: this.filters.roleId || undefined,
|
current: this.current,
|
size: this.size
|
}
|
}).then(res => {
|
const data = res.data || {}
|
this.rows = data.records || []
|
this.total = data.total || 0
|
if (this.selected) {
|
const matched = this.rows.find(item => item.userId === this.selected.userId)
|
this.selected = matched || null
|
if (!matched) this.checkedRoleIds = []
|
}
|
}).finally(() => { this.loading = false })
|
},
|
loadRoles() {
|
api.get('/system/roles').then(res => { this.roles = res.data || [] })
|
},
|
loadDepts() {
|
api.get('/system/depts').then(res => { this.depts = res.data || [] })
|
},
|
search() {
|
this.current = 1
|
this.load()
|
},
|
reset() {
|
this.filters = { keyword: '', deptId: null, roleId: null }
|
this.search()
|
},
|
handleCurrentChange(page) {
|
this.current = page
|
this.load()
|
},
|
handleSizeChange(size) {
|
this.size = size
|
this.current = 1
|
this.load()
|
},
|
clearSelection() {
|
this.selectedRows = []
|
this.$nextTick(() => { if (this.$refs.userTable) this.$refs.userTable.clearSelection() })
|
},
|
openBatchDialog() {
|
if (!this.selectedRows.length) return
|
this.batchRoleIds = []
|
this.batchMode = 'append'
|
this.batchVisible = true
|
},
|
saveBatchRoles() {
|
if (!this.selectedRows.length) return
|
const userIds = this.selectedRows.map(item => item.userId)
|
const roleIds = this.batchRoleIds.slice()
|
if (this.batchMode === 'append' && !roleIds.length) {
|
this.$message.warning('请选择要追加的角色')
|
return
|
}
|
const message = this.batchMode === 'append'
|
? '将向 ' + userIds.length + ' 个用户追加所选角色,是否继续?'
|
: (roleIds.length ? '将使用所选角色覆盖 ' + userIds.length + ' 个用户的现有角色,是否继续?' : '未选择角色,将清空 ' + userIds.length + ' 个用户的全部角色,是否继续?')
|
this.$confirm(message, '批量设置角色', { type: 'warning' }).then(() => {
|
this.batchSaving = true
|
api.put('/system/users/roles/batch', { userIds, roleIds, mode: this.batchMode }).then(() => {
|
this.$message.success('批量角色设置成功')
|
this.batchVisible = false
|
this.load()
|
if (this.selected) this.selectUser(this.selected)
|
}).finally(() => { this.batchSaving = false })
|
}).catch(() => {})
|
},
|
selectUser(row) {
|
this.selected = row || null
|
this.checkedRoleIds = []
|
if (!row) return
|
api.get('/system/users/' + row.userId + '/roles').then(res => {
|
this.checkedRoleIds = res.data || []
|
})
|
},
|
saveRoles() {
|
if (!this.selected) return
|
this.saving = true
|
api.put('/system/users/' + this.selected.userId + '/roles', { roleIds: this.checkedRoleIds }).then(() => {
|
this.$message.success('角色授权已保存')
|
this.load()
|
}).finally(() => { this.saving = false })
|
},
|
isDisabled(row) {
|
return row.status === '1'
|
}
|
}
|
}
|
</script>
|
<style scoped>
|
.user-layout { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 12px; }
|
.user-left, .user-right { min-width: 0; padding: 14px; border: 1px solid #dce4ec; border-radius: 4px; background: #fff; }
|
.user-right { min-height: calc(100vh - 190px); }
|
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
|
.selected-count { align-self: center; color: #5c6f82; font-size: 12px; }
|
.batch-summary { margin-bottom: 10px; color: #31465d; font-size: 14px; }
|
.batch-role-list { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 12px; max-height: 320px; margin-top: 14px; overflow: auto; }
|
.pager { margin-top: 12px; text-align: right; }
|
.panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-bottom: 14px; padding-bottom: 10px; border-bottom: 1px solid #edf1f5; }
|
.panel-head h3 { margin: 0; color: #31465d; font-size: 15px; }
|
.selected-user { color: #5c6f82; font-size: 13px; }
|
.empty-tip { padding: 60px 0; color: #93a2b1; text-align: center; }
|
.role-list { display: flex; flex-direction: column; gap: 4px; max-height: calc(100vh - 320px); overflow: auto; }
|
.role-item { display: flex; align-items: center; height: 34px; margin-right: 0; }
|
.role-name { color: #31465d; }
|
.role-code { margin-left: 8px; color: #93a2b1; font-size: 12px; }
|
.save-button { width: 100%; margin-top: 16px; }
|
.role-tag { margin: 2px 4px 2px 0; }
|
.muted { color: #a3afbc; }
|
@media (max-width: 1100px) { .user-layout { grid-template-columns: 1fr; } }
|
</style>
|