1211 lines
38 KiB
Vue
1211 lines
38 KiB
Vue
<!-- 角色管理(系统管理) -->
|
||
<!-- eslint-disable vue/no-v-model-argument -->
|
||
<template>
|
||
<div class="page-container">
|
||
<section class="panel">
|
||
<div class="panel-top">
|
||
<h2>角色列表</h2>
|
||
</div>
|
||
|
||
<div class="filter-block">
|
||
<el-form :model="searchForm" class="filter-form" @submit.prevent>
|
||
<div class="filter-grid">
|
||
<el-form-item label="关键词">
|
||
<el-input v-model="searchForm.keyword" placeholder="角色名称" clearable />
|
||
</el-form-item>
|
||
</div>
|
||
<div class="filter-actions-row">
|
||
<span class="filter-hint">按角色名称模糊查询。</span>
|
||
<div class="filter-actions-end">
|
||
<el-button class="btn-ghost" @click="handleReset"
|
||
><el-icon><RefreshLeft /></el-icon>重置</el-button
|
||
>
|
||
<el-button class="btn-main" type="primary" @click="handleSearch"
|
||
><el-icon><Search /></el-icon>搜索</el-button
|
||
>
|
||
</div>
|
||
</div>
|
||
</el-form>
|
||
</div>
|
||
|
||
<div class="list-toolbar">
|
||
<div class="list-toolbar-row">
|
||
<div class="list-toolbar-selected">
|
||
已选 <span class="sel-num">{{ selectedRows.length }}</span> 项
|
||
</div>
|
||
<el-button class="btn-main" type="primary" @click="handleAdd">
|
||
<el-icon><Plus /></el-icon>
|
||
新增角色
|
||
</el-button>
|
||
<el-button
|
||
class="btn-ghost"
|
||
type="danger"
|
||
:disabled="selectedRows.length === 0"
|
||
@click="handleBatchDelete"
|
||
>
|
||
批量删除
|
||
</el-button>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="list-body list-wrap">
|
||
<el-table
|
||
class="list-table"
|
||
:data="tableData"
|
||
v-loading="loading"
|
||
stripe
|
||
row-key="id"
|
||
@selection-change="handleSelectionChange"
|
||
@row-dblclick="handleView"
|
||
>
|
||
<el-table-column type="selection" min-width="55" align="center" header-align="center" />
|
||
<el-table-column
|
||
type="index"
|
||
label="序号"
|
||
min-width="68"
|
||
align="center"
|
||
header-align="center"
|
||
:index="(index: number) => (pagination.page - 1) * pagination.pageSize + index + 1"
|
||
/>
|
||
<el-table-column
|
||
prop="roleName"
|
||
label="角色名称"
|
||
min-width="140"
|
||
align="center"
|
||
header-align="center"
|
||
/>
|
||
<el-table-column
|
||
prop="dataScope"
|
||
label="数据权限"
|
||
min-width="120"
|
||
align="center"
|
||
header-align="center"
|
||
>
|
||
<template #default="{ row }">
|
||
{{ dataScopeLabel(row.dataScope) }}
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column
|
||
prop="status"
|
||
label="状态"
|
||
min-width="100"
|
||
align="center"
|
||
header-align="center"
|
||
>
|
||
<template #default="{ row }">
|
||
<el-tag :type="dictStore.getTagType('sys_status', row.status)" size="small">
|
||
{{ dictStore.getLabel('sys_status', row.status) }}
|
||
</el-tag>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column
|
||
prop="createTime"
|
||
label="创建时间"
|
||
min-width="180"
|
||
align="center"
|
||
header-align="center"
|
||
/>
|
||
<el-table-column
|
||
label="操作"
|
||
min-width="200"
|
||
fixed="right"
|
||
align="center"
|
||
header-align="center"
|
||
>
|
||
<template #default="{ row }">
|
||
<div style="display: flex; justify-content: center; gap: 8px">
|
||
<el-button class="btn-op-item primary" @click="handleMenuConfig(row)"
|
||
>菜单配置</el-button
|
||
>
|
||
<el-button class="btn-op-item primary" @click="handleDataScope(row)"
|
||
><el-icon><DataAnalysis /></el-icon>数据权限</el-button
|
||
>
|
||
<el-dropdown trigger="hover" @command="(cmd: string) => handleDropdownCommand(cmd, row)">
|
||
<el-button class="btn-op-item">
|
||
<el-icon><More /></el-icon>
|
||
</el-button>
|
||
<template #dropdown>
|
||
<el-dropdown-menu>
|
||
<el-dropdown-item command="view">
|
||
<el-icon><View /></el-icon>查看
|
||
</el-dropdown-item>
|
||
<el-dropdown-item command="edit">
|
||
<el-icon><Edit /></el-icon>编辑
|
||
</el-dropdown-item>
|
||
<el-dropdown-item command="delete" divided>
|
||
<el-icon><Delete /></el-icon>删除
|
||
</el-dropdown-item>
|
||
</el-dropdown-menu>
|
||
</template>
|
||
</el-dropdown>
|
||
</div>
|
||
</template>
|
||
</el-table-column>
|
||
</el-table>
|
||
<div class="pagination list-pager-bar">
|
||
<el-pagination
|
||
:current-page="pagination.page"
|
||
:page-size="pagination.pageSize"
|
||
:total="pagination.total"
|
||
:page-sizes="[10, 20, 50]"
|
||
layout="total, sizes, prev, pager, next"
|
||
@size-change="
|
||
(val: number) => ((pagination.page = 1), (pagination.pageSize = val), loadData())
|
||
"
|
||
@current-change="(val: number) => ((pagination.page = val), loadData())"
|
||
/>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 新增/编辑角色弹窗 -->
|
||
<el-dialog
|
||
v-model="dialogVisible"
|
||
:title="dialogTitle"
|
||
width="720px"
|
||
class="dialog-form-layout"
|
||
align-center
|
||
destroy-on-close
|
||
:close-on-click-modal="false"
|
||
>
|
||
<el-form
|
||
ref="formRef"
|
||
:model="formData"
|
||
:rules="formRules"
|
||
label-position="top"
|
||
class="role-dialog-form"
|
||
scroll-to-error
|
||
:scroll-into-view-options="{ behavior: 'smooth', block: 'center' }"
|
||
>
|
||
<div class="form-grid-2">
|
||
<el-form-item label="角色名称" prop="roleName">
|
||
<el-input v-model="formData.roleName" placeholder="请输入角色名称" />
|
||
</el-form-item>
|
||
<el-form-item label="状态" prop="status">
|
||
<el-select v-model="formData.status" style="width: 100%">
|
||
<el-option label="正常" value="1" />
|
||
<el-option label="停用" value="0" />
|
||
</el-select>
|
||
</el-form-item>
|
||
<el-form-item label="备注" style="grid-column: 1 / -1">
|
||
<el-input v-model="formData.remark" type="textarea" :rows="2" placeholder="可选" />
|
||
</el-form-item>
|
||
</div>
|
||
</el-form>
|
||
<template #footer>
|
||
<el-button class="btn-ghost" @click="dialogVisible = false">
|
||
<el-icon><Close /></el-icon>
|
||
取消
|
||
</el-button>
|
||
<el-button class="btn-main" type="primary" @click="handleSubmit" :loading="submitLoading">
|
||
<el-icon><Check /></el-icon>
|
||
确定
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<!-- 菜单配置弹窗 -->
|
||
<el-dialog
|
||
v-model="menuDialogVisible"
|
||
title="菜单配置"
|
||
width="700px"
|
||
class="dialog-form-layout"
|
||
align-center
|
||
destroy-on-close
|
||
:close-on-click-modal="false"
|
||
>
|
||
<DialogFormBody :loading="menuDialogLoading">
|
||
<!-- 父子关联勾选:选父级全选子级,选子级自动带上父级(与 check-strictly 互斥,故不加) -->
|
||
<el-tree
|
||
ref="menuTreeRef"
|
||
:data="menuTreeData"
|
||
show-checkbox
|
||
node-key="id"
|
||
:props="{ label: 'menuName', children: 'children' }"
|
||
:default-checked-keys="menuCheckedKeys"
|
||
style="max-height: 500px; overflow-y: auto"
|
||
/>
|
||
</DialogFormBody>
|
||
<template #footer>
|
||
<el-button class="btn-ghost" @click="menuDialogVisible = false">
|
||
<el-icon><Close /></el-icon>
|
||
取消
|
||
</el-button>
|
||
<el-button
|
||
class="btn-main"
|
||
type="primary"
|
||
@click="handleMenuSubmit"
|
||
:loading="menuSubmitLoading"
|
||
>
|
||
<el-icon><Check /></el-icon>
|
||
确定
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<!-- 数据权限配置弹窗 -->
|
||
<el-dialog
|
||
v-model="dataScopeDialogVisible"
|
||
title="数据权限配置"
|
||
width="1120px"
|
||
class="dialog-form-layout"
|
||
align-center
|
||
destroy-on-close
|
||
:close-on-click-modal="false"
|
||
>
|
||
<el-form label-position="top" class="data-scope-form">
|
||
<el-form-item label="数据权限范围">
|
||
<el-select v-model="dataScopeForm.dataScope" style="width: 100%">
|
||
<el-option label="全部数据" value="1" />
|
||
<el-option label="本部门数据" value="2" />
|
||
<el-option label="本部门及以下数据" value="3" />
|
||
<el-option label="仅本人数据" value="4" />
|
||
<el-option label="自定义" value="5" />
|
||
</el-select>
|
||
</el-form-item>
|
||
|
||
<template v-if="dataScopeForm.dataScope === '5'">
|
||
<el-form-item label="自定义范围" class="data-scope-custom-form-item">
|
||
<div class="data-scope-custom-grid">
|
||
<div class="data-scope-custom-col">
|
||
<div>
|
||
<div class="data-scope-col-title">选择部门</div>
|
||
<div class="data-scope-col-tip">
|
||
仅通过复选框勾选授权范围;点击部门名称可筛选右侧用户
|
||
</div>
|
||
</div>
|
||
<div class="data-scope-tree-wrap">
|
||
<el-tree
|
||
ref="deptTreeRef"
|
||
:data="deptTreeData"
|
||
show-checkbox
|
||
highlight-current
|
||
node-key="id"
|
||
:props="{ label: 'deptName', children: 'children' }"
|
||
:default-checked-keys="scopeDeptCheckedKeys"
|
||
default-expand-all
|
||
:check-on-click-node="false"
|
||
:check-on-click-leaf="false"
|
||
check-strictly
|
||
@check="syncDataScopeDeptCheckedFromTree"
|
||
@node-click="handleDataScopeDeptNodeClick"
|
||
/>
|
||
</div>
|
||
</div>
|
||
<div class="data-scope-custom-col">
|
||
<div class="data-scope-col-head-row">
|
||
<span class="data-scope-col-title">选择用户</span>
|
||
<el-button
|
||
v-if="dataScopeUserFilterDeptId"
|
||
class="btn-ghost"
|
||
size="small"
|
||
@click="clearDataScopeUserDeptFilter"
|
||
>
|
||
清除部门筛选
|
||
</el-button>
|
||
</div>
|
||
<el-input
|
||
v-model="dataScopeUserKeyword"
|
||
class="data-scope-user-filter"
|
||
clearable
|
||
placeholder="按姓名、工号、联系方式过滤"
|
||
/>
|
||
<div class="data-scope-user-list-wrap">
|
||
<el-checkbox-group
|
||
v-if="dataScopeFilteredUsers.length > 0"
|
||
v-model="scopeUserCheckedKeys"
|
||
>
|
||
<div
|
||
v-for="u in dataScopeFilteredUsers"
|
||
:key="u.id"
|
||
class="data-scope-user-row"
|
||
>
|
||
<el-checkbox :value="u.id">{{ userOptionLabel(u) }}</el-checkbox>
|
||
</div>
|
||
</el-checkbox-group>
|
||
<el-empty v-else :description="dataScopeUserEmptyDesc" :image-size="40" />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="data-scope-selection-summary">
|
||
<template v-if="dataScopeHasCustomSelectionSummary">
|
||
<div v-if="dataScopeSelectedDeptRows.length > 0" class="data-scope-summary-block">
|
||
<div class="data-scope-summary-heading">已选部门</div>
|
||
<ul class="data-scope-summary-list">
|
||
<li v-for="row in dataScopeSelectedDeptRows" :key="'d-' + row.id">
|
||
{{ row.label }}
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
<div
|
||
v-if="dataScopeSelectedUserSummaries.length > 0"
|
||
class="data-scope-summary-block"
|
||
>
|
||
<div class="data-scope-summary-heading">已选用户</div>
|
||
<ul class="data-scope-summary-list">
|
||
<li v-for="row in dataScopeSelectedUserSummaries" :key="'u-' + row.id">
|
||
{{ row.label }}
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</template>
|
||
<p v-else class="data-scope-summary-empty">暂未勾选部门或用户</p>
|
||
</div>
|
||
</el-form-item>
|
||
</template>
|
||
</el-form>
|
||
<template #footer>
|
||
<el-button class="btn-ghost" @click="dataScopeDialogVisible = false">
|
||
<el-icon><Close /></el-icon>
|
||
取消
|
||
</el-button>
|
||
<el-button
|
||
class="btn-main"
|
||
type="primary"
|
||
@click="handleDataScopeSubmit"
|
||
:loading="dataScopeSubmitLoading"
|
||
>
|
||
<el-icon><Check /></el-icon>
|
||
确定
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<SystemViewDialog
|
||
v-model:visible="viewVisible"
|
||
:title="viewRow?.roleName || '角色详情'"
|
||
:icon="Stamp"
|
||
width="860px"
|
||
:meta="`数据权限:${dataScopeLabel(viewDataScopeCode || viewRow?.dataScope || '')}${viewRow?.createTime ? ' · ' + viewRow.createTime : ''}`"
|
||
:sections="viewSections"
|
||
>
|
||
<template #menuTree>
|
||
<div class="role-view-tree-block" v-loading="viewDetailLoading">
|
||
<div v-if="viewMenuTreeData.length" class="role-view-tree-scroll">
|
||
<el-tree
|
||
:key="'menu-' + viewTreeRenderKey"
|
||
ref="viewMenuTreeRef"
|
||
class="role-view-readonly-tree"
|
||
show-checkbox
|
||
:data="viewMenuTreeData"
|
||
node-key="id"
|
||
:props="{ label: 'menuName', children: 'children' }"
|
||
:default-checked-keys="viewMenuCheckedKeys"
|
||
default-expand-all
|
||
/>
|
||
</div>
|
||
<el-empty v-else-if="!viewDetailLoading" description="暂无菜单数据" :image-size="40" />
|
||
</div>
|
||
</template>
|
||
<template #customDeptTree>
|
||
<div class="role-view-tree-block" v-loading="viewDetailLoading">
|
||
<div v-if="viewDeptTreeData.length" class="role-view-tree-scroll">
|
||
<el-tree
|
||
:key="'dept-' + viewTreeRenderKey"
|
||
ref="viewDeptTreeRef"
|
||
class="role-view-readonly-tree"
|
||
show-checkbox
|
||
check-strictly
|
||
:data="viewDeptTreeData"
|
||
node-key="id"
|
||
:props="{ label: 'deptName', children: 'children' }"
|
||
:default-checked-keys="viewDeptCheckedKeys"
|
||
default-expand-all
|
||
/>
|
||
</div>
|
||
<el-empty v-else-if="!viewDetailLoading" description="暂无部门数据" :image-size="40" />
|
||
</div>
|
||
</template>
|
||
</SystemViewDialog>
|
||
</section>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, reactive, computed, onMounted, nextTick, watch } from 'vue'
|
||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||
import {
|
||
Plus,
|
||
Close,
|
||
Check,
|
||
DataAnalysis,
|
||
View,
|
||
Stamp,
|
||
RefreshLeft,
|
||
Search,
|
||
More,
|
||
Edit,
|
||
Delete
|
||
} from '@element-plus/icons-vue'
|
||
import DialogFormBody from '@/components/business/dialog-form-body/index.vue'
|
||
import SystemViewDialog, {
|
||
type ViewSection
|
||
} from '@/components/business/system-view-dialog/index.vue'
|
||
import { MAX_ADMIN_LIST_PAGE_SIZE } from '@/utils/fetch-paged-list'
|
||
import { withFullscreenLoading, fullscreenLoadingText } from '@/utils/fullscreen-loading'
|
||
import { buildPathLabelMapFromTreeRoots } from '@/utils/tree-path'
|
||
|
||
import { useDictStore } from '@/store/modules/dict'
|
||
import {
|
||
fetchRoleList,
|
||
fetchCreateRole,
|
||
fetchUpdateRole,
|
||
fetchDeleteRole,
|
||
fetchRoleMenuIds,
|
||
fetchUpdateRoleMenus,
|
||
fetchRoleDataScope,
|
||
fetchUpdateRoleDataScope,
|
||
fetchMenuTree,
|
||
fetchDeptTree,
|
||
fetchUserList
|
||
} from '@/api/admin'
|
||
|
||
defineOptions({ name: 'SystemRole' })
|
||
|
||
const dictStore = useDictStore()
|
||
|
||
const dataScopeLabel = (code: string) => dictStore.getLabel('sys_data_scope', code)
|
||
const dataScopeTagType = (code: string) => dictStore.getTagType('sys_data_scope', code)
|
||
|
||
const searchForm = reactive<{ keyword: string }>({ keyword: '' })
|
||
const tableData = ref<Api.Role.RoleItem[]>([])
|
||
const selectedRows = ref<Api.Role.RoleItem[]>([])
|
||
const loading = ref(false)
|
||
const pagination = reactive({ page: 1, pageSize: 10, total: 0 })
|
||
|
||
// ---- 新增/编辑弹窗 ----
|
||
const dialogVisible = ref(false)
|
||
const dialogTitle = ref('新增角色')
|
||
const isEdit = ref(false)
|
||
const submitLoading = ref(false)
|
||
const formRef = ref()
|
||
const editingId = ref<string | null>(null)
|
||
|
||
const formData = reactive({
|
||
roleName: '',
|
||
status: '1',
|
||
orderNum: 0,
|
||
remark: ''
|
||
})
|
||
|
||
const formRules = {
|
||
roleName: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
|
||
}
|
||
|
||
// ---- 菜单配置弹窗 ----
|
||
const menuDialogVisible = ref(false)
|
||
const menuDialogLoading = ref(false)
|
||
const menuSubmitLoading = ref(false)
|
||
const menuTreeRef = ref()
|
||
const menuTreeData = ref<Api.Menu.MenuItem[]>([])
|
||
const menuCheckedKeys = ref<string[]>([])
|
||
const menuEditingRoleId = ref<string>('')
|
||
|
||
// ---- 数据权限弹窗 ----
|
||
const dataScopeDialogVisible = ref(false)
|
||
const dataScopeSubmitLoading = ref(false)
|
||
const dataScopeEditingRoleId = ref<string>('')
|
||
const deptTreeRef = ref()
|
||
const deptTreeData = ref<Api.Dept.DeptItem[]>([])
|
||
const allUsers = ref<
|
||
Array<{
|
||
id: string
|
||
nickName: string
|
||
userNo: string
|
||
deptId: string
|
||
deptName?: string
|
||
phoneNo: string
|
||
status: string
|
||
}>
|
||
>([])
|
||
const scopeDeptCheckedKeys = ref<string[]>([])
|
||
const scopeUserCheckedKeys = ref<string[]>([])
|
||
/** 左侧部门树当前行(用于筛选右侧用户,不含子部门勾选逻辑) */
|
||
const dataScopeUserFilterDeptId = ref<string | null>(null)
|
||
const dataScopeUserKeyword = ref('')
|
||
const dataScopeForm = reactive({
|
||
dataScope: '1'
|
||
})
|
||
|
||
function findDeptNodeById(nodes: Api.Dept.DeptItem[], id: string): Api.Dept.DeptItem | null {
|
||
for (const n of nodes) {
|
||
if (n.id === id) return n
|
||
if (n.children?.length) {
|
||
const found = findDeptNodeById(n.children, id)
|
||
if (found) return found
|
||
}
|
||
}
|
||
return null
|
||
}
|
||
|
||
function collectDeptSubtreeIds(node: Api.Dept.DeptItem): string[] {
|
||
const ids = [node.id]
|
||
for (const c of node.children || []) ids.push(...collectDeptSubtreeIds(c))
|
||
return ids
|
||
}
|
||
|
||
/** 收集部门树全部节点 id(异步灌入数据后仍保持整树展开) */
|
||
function collectAllDeptIds(nodes: Api.Dept.DeptItem[]): string[] {
|
||
const ids: string[] = []
|
||
const walk = (list: Api.Dept.DeptItem[]) => {
|
||
for (const n of list) {
|
||
ids.push(n.id)
|
||
if (n.children?.length) walk(n.children)
|
||
}
|
||
}
|
||
walk(nodes)
|
||
return ids
|
||
}
|
||
|
||
async function expandDataScopeDeptTree() {
|
||
await nextTick()
|
||
const tree = deptTreeRef.value
|
||
const keys = collectAllDeptIds(deptTreeData.value)
|
||
if (tree && keys.length && typeof tree.setExpandedKeys === 'function') {
|
||
tree.setExpandedKeys(keys)
|
||
}
|
||
}
|
||
|
||
const dataScopeDeptFilterIdSet = computed(() => {
|
||
const id = dataScopeUserFilterDeptId.value
|
||
if (!id) return null
|
||
const node = findDeptNodeById(deptTreeData.value, id)
|
||
if (!node) return new Set<string>([id])
|
||
return new Set(collectDeptSubtreeIds(node))
|
||
})
|
||
|
||
const dataScopeFilteredUsers = computed(() => {
|
||
const kw = dataScopeUserKeyword.value.trim().toLowerCase()
|
||
const deptSet = dataScopeDeptFilterIdSet.value
|
||
return allUsers.value.filter((u) => {
|
||
if (deptSet && !deptSet.has(u.deptId)) return false
|
||
if (!kw) return true
|
||
const name = (u.nickName || '').toLowerCase()
|
||
const no = (u.userNo || '').toLowerCase()
|
||
const phone = (u.phoneNo || '').toLowerCase()
|
||
return name.includes(kw) || no.includes(kw) || phone.includes(kw)
|
||
})
|
||
})
|
||
|
||
const dataScopeUserEmptyDesc = computed(() => {
|
||
if (allUsers.value.length === 0) return '暂无用户数据'
|
||
const hasDept = Boolean(dataScopeUserFilterDeptId.value)
|
||
const kw = dataScopeUserKeyword.value.trim()
|
||
if (hasDept && kw) return '该部门下无匹配用户'
|
||
if (hasDept) return '该部门下暂无用户'
|
||
if (kw) return '无匹配用户'
|
||
return '无匹配用户'
|
||
})
|
||
|
||
/** 与 el-tree 勾选状态同步,供已选摘要与提交一致 */
|
||
function syncDataScopeDeptCheckedFromTree() {
|
||
const keys = deptTreeRef.value?.getCheckedKeys?.() as string[] | undefined
|
||
scopeDeptCheckedKeys.value = Array.isArray(keys) ? [...keys] : []
|
||
}
|
||
|
||
const dataScopeDeptPathLabelMap = computed(() =>
|
||
buildPathLabelMapFromTreeRoots(deptTreeData.value, {
|
||
idKey: 'id',
|
||
nameKey: 'deptName',
|
||
childrenKey: 'children'
|
||
})
|
||
)
|
||
|
||
const dataScopeSelectedDeptRows = computed(() => {
|
||
const map = dataScopeDeptPathLabelMap.value
|
||
return scopeDeptCheckedKeys.value.map((id) => ({
|
||
id,
|
||
label: map.get(id) || findDeptNodeById(deptTreeData.value, id)?.deptName || id
|
||
}))
|
||
})
|
||
|
||
const dataScopeSelectedUserSummaries = computed(() =>
|
||
scopeUserCheckedKeys.value.map((id) => {
|
||
const u = allUsers.value.find((x) => x.id === id)
|
||
return { id, label: u ? userOptionLabel(u) : id }
|
||
})
|
||
)
|
||
|
||
const dataScopeHasCustomSelectionSummary = computed(
|
||
() =>
|
||
dataScopeSelectedDeptRows.value.length > 0 || dataScopeSelectedUserSummaries.value.length > 0
|
||
)
|
||
|
||
function userStatusText(status: string | undefined) {
|
||
return status === '1' ? '正常' : '停用'
|
||
}
|
||
|
||
function userOptionLabel(u: {
|
||
id: string
|
||
nickName: string
|
||
userNo: string
|
||
phoneNo?: string
|
||
status?: string
|
||
}) {
|
||
const name = u.nickName || u.userNo || u.id
|
||
const no = u.userNo || '—'
|
||
const phone = u.phoneNo || '—'
|
||
return `${name}(${no}、${phone}、${userStatusText(u.status)})`
|
||
}
|
||
|
||
function handleDataScopeDeptNodeClick(data: Api.Dept.DeptItem) {
|
||
dataScopeUserFilterDeptId.value = data.id
|
||
deptTreeRef.value?.setCurrentKey?.(data.id)
|
||
}
|
||
|
||
function clearDataScopeUserDeptFilter() {
|
||
dataScopeUserFilterDeptId.value = null
|
||
deptTreeRef.value?.setCurrentKey?.(null)
|
||
}
|
||
|
||
const handleSelectionChange = (rows: Api.Role.RoleItem[]) => {
|
||
selectedRows.value = rows
|
||
}
|
||
|
||
async function loadData() {
|
||
loading.value = true
|
||
try {
|
||
const res = await fetchRoleList({
|
||
page: pagination.page,
|
||
pageSize: pagination.pageSize,
|
||
keyword: searchForm.keyword || undefined
|
||
})
|
||
tableData.value = res.list || []
|
||
pagination.total = res.total ?? 0
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
function handleSearch() {
|
||
pagination.page = 1
|
||
loadData()
|
||
}
|
||
|
||
function handleReset() {
|
||
searchForm.keyword = ''
|
||
pagination.page = 1
|
||
loadData()
|
||
}
|
||
|
||
function resetForm() {
|
||
formData.roleName = ''
|
||
formData.status = '1'
|
||
formData.orderNum = 0
|
||
formData.remark = ''
|
||
editingId.value = null
|
||
}
|
||
|
||
function handleAdd() {
|
||
isEdit.value = false
|
||
dialogTitle.value = '新增角色'
|
||
resetForm()
|
||
dialogVisible.value = true
|
||
formRef.value?.clearValidate?.()
|
||
}
|
||
|
||
function handleEdit(row: Api.Role.RoleItem) {
|
||
isEdit.value = true
|
||
dialogTitle.value = '编辑角色'
|
||
editingId.value = row.id
|
||
formData.roleName = row.roleName
|
||
formData.status = row.status || '1'
|
||
formData.orderNum = row.orderNum ?? 0
|
||
formData.remark = row.remark || ''
|
||
dialogVisible.value = true
|
||
formRef.value?.clearValidate?.()
|
||
}
|
||
|
||
async function handleSubmit() {
|
||
await formRef.value?.validate?.()
|
||
submitLoading.value = true
|
||
try {
|
||
const payload = {
|
||
roleName: formData.roleName.trim(),
|
||
status: formData.status,
|
||
orderNum: formData.orderNum,
|
||
remark: formData.remark?.trim() || undefined
|
||
}
|
||
if (isEdit.value && editingId.value) {
|
||
await fetchUpdateRole(editingId.value, payload)
|
||
} else {
|
||
await fetchCreateRole({ ...payload, dataScope: '4' })
|
||
}
|
||
dialogVisible.value = false
|
||
await loadData()
|
||
} catch {
|
||
/* handled globally */
|
||
} finally {
|
||
submitLoading.value = false
|
||
}
|
||
}
|
||
|
||
function handleDropdownCommand(command: string, row: any) {
|
||
switch (command) {
|
||
case 'view':
|
||
handleView(row)
|
||
break
|
||
case 'edit':
|
||
handleEdit(row)
|
||
break
|
||
case 'delete':
|
||
handleDelete(row)
|
||
break
|
||
}
|
||
}
|
||
|
||
async function handleDelete(row: Api.Role.RoleItem) {
|
||
await ElMessageBox.confirm(`确定删除角色「${row.roleName}」吗?`, '提示', { type: 'warning' })
|
||
await fetchDeleteRole(row.id)
|
||
await loadData()
|
||
}
|
||
|
||
async function handleBatchDelete() {
|
||
const ids = selectedRows.value.map((r) => r.id)
|
||
if (!ids.length) {
|
||
ElMessage.warning('请至少选择一项')
|
||
return
|
||
}
|
||
await ElMessageBox.confirm(`确定删除选中的 ${ids.length} 个角色吗?`, '提示', {
|
||
type: 'warning'
|
||
})
|
||
await withFullscreenLoading(async () => {
|
||
for (const id of ids) {
|
||
await fetchDeleteRole(id)
|
||
}
|
||
ElMessage.success('批量删除成功')
|
||
selectedRows.value = []
|
||
await loadData()
|
||
}, fullscreenLoadingText.batchDelete)
|
||
}
|
||
|
||
// ---- 菜单配置 ----
|
||
async function handleMenuConfig(row: Api.Role.RoleItem) {
|
||
menuEditingRoleId.value = row.id
|
||
menuDialogVisible.value = true
|
||
menuDialogLoading.value = true
|
||
try {
|
||
const [tree, res] = await Promise.all([fetchMenuTree(), fetchRoleMenuIds(row.id)])
|
||
menuTreeData.value = tree || []
|
||
menuCheckedKeys.value = res?.menuIds || []
|
||
} finally {
|
||
menuDialogLoading.value = false
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 提交角色菜单:只持久化叶子菜单 ID(去重),避免树控件偶发重复 key。
|
||
* 父子关联下「半选」父节点在 getHalfCheckedKeys;若与 getCheckedKeys 一起入库,重开弹窗会把父节点当全选,误勾整棵子树。
|
||
* 侧栏按 menuCode 过滤时会保留有权限子项的父级,无需单独存父菜单行。
|
||
*/
|
||
async function handleMenuSubmit() {
|
||
menuSubmitLoading.value = true
|
||
try {
|
||
const tree = menuTreeRef.value
|
||
const rawKeys: string[] = tree?.getCheckedKeys(true) || []
|
||
const leafKeys = [...new Set(rawKeys)]
|
||
await fetchUpdateRoleMenus(menuEditingRoleId.value, leafKeys)
|
||
menuDialogVisible.value = false
|
||
} finally {
|
||
menuSubmitLoading.value = false
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 分页拉取租户用户列表供「数据权限」多选;单页 pageSize 超过 100 时接口会 422。
|
||
*/
|
||
async function fetchAllUsersForDataScope(): Promise<
|
||
Array<{
|
||
id: string
|
||
nickName: string
|
||
userNo: string
|
||
deptId: string
|
||
deptName?: string
|
||
phoneNo: string
|
||
status: string
|
||
}>
|
||
> {
|
||
const pageSize = MAX_ADMIN_LIST_PAGE_SIZE
|
||
const raw: any[] = []
|
||
let page = 1
|
||
for (;;) {
|
||
const res = await fetchUserList({ page, pageSize })
|
||
const list = res?.list || []
|
||
raw.push(...list)
|
||
if (list.length < pageSize) break
|
||
if (typeof res?.total === 'number' && raw.length >= res.total) break
|
||
page += 1
|
||
}
|
||
return raw.map((u: any) => ({
|
||
id: u.id,
|
||
deptId: u.deptId || '',
|
||
nickName: u.nickName || u.realName || '',
|
||
userNo: u.userNo || '',
|
||
deptName: u.deptName,
|
||
phoneNo: u.phoneNo || '',
|
||
status: u.status || '0'
|
||
}))
|
||
}
|
||
|
||
// ---- 数据权限配置 ----
|
||
async function handleDataScope(row: Api.Role.RoleItem) {
|
||
dataScopeEditingRoleId.value = row.id
|
||
dataScopeForm.dataScope = row.dataScope || '1'
|
||
scopeDeptCheckedKeys.value = []
|
||
scopeUserCheckedKeys.value = []
|
||
dataScopeUserFilterDeptId.value = null
|
||
dataScopeUserKeyword.value = ''
|
||
dataScopeDialogVisible.value = true
|
||
|
||
const [, deptTree, users] = await Promise.all([
|
||
fetchRoleDataScope(row.id).then((res) => {
|
||
if (res?.scopeDetails) {
|
||
scopeDeptCheckedKeys.value = res.scopeDetails
|
||
.filter((d) => d.scopeType === 'dept')
|
||
.map((d) => d.scopeValue)
|
||
scopeUserCheckedKeys.value = res.scopeDetails
|
||
.filter((d) => d.scopeType === 'user')
|
||
.map((d) => d.scopeValue)
|
||
}
|
||
dataScopeForm.dataScope = res?.dataScope || row.dataScope || '1'
|
||
}),
|
||
fetchDeptTree(),
|
||
fetchAllUsersForDataScope()
|
||
])
|
||
|
||
deptTreeData.value = deptTree || []
|
||
allUsers.value = users
|
||
await expandDataScopeDeptTree()
|
||
}
|
||
|
||
async function handleDataScopeSubmit() {
|
||
dataScopeSubmitLoading.value = true
|
||
try {
|
||
const details: Array<{ scopeType: string; scopeValue: string }> = []
|
||
|
||
if (dataScopeForm.dataScope === '5') {
|
||
const deptKeys: string[] = deptTreeRef.value?.getCheckedKeys() || []
|
||
deptKeys.forEach((id: string) => details.push({ scopeType: 'dept', scopeValue: id }))
|
||
scopeUserCheckedKeys.value.forEach((id: string) =>
|
||
details.push({ scopeType: 'user', scopeValue: id })
|
||
)
|
||
}
|
||
|
||
await fetchUpdateRoleDataScope(dataScopeEditingRoleId.value, {
|
||
dataScope: dataScopeForm.dataScope,
|
||
scopeDetails: details.length > 0 ? details : undefined
|
||
})
|
||
dataScopeDialogVisible.value = false
|
||
await loadData()
|
||
} finally {
|
||
dataScopeSubmitLoading.value = false
|
||
}
|
||
}
|
||
|
||
watch(
|
||
() => dataScopeForm.dataScope,
|
||
async (v) => {
|
||
if (v !== '5' || !dataScopeDialogVisible.value) return
|
||
await expandDataScopeDeptTree()
|
||
}
|
||
)
|
||
|
||
// ---- 查看弹窗 ----
|
||
const viewVisible = ref(false)
|
||
const viewRow = ref<Api.Role.RoleItem | null>(null)
|
||
const viewDetailLoading = ref(false)
|
||
const viewDataScopeCode = ref('')
|
||
const viewMenuTreeData = ref<Api.Menu.MenuItem[]>([])
|
||
const viewMenuCheckedKeys = ref<string[]>([])
|
||
const viewDeptTreeData = ref<Api.Dept.DeptItem[]>([])
|
||
const viewDeptCheckedKeys = ref<string[]>([])
|
||
const viewCustomUserLabels = ref<string[]>([])
|
||
const viewMenuTreeRef = ref()
|
||
const viewDeptTreeRef = ref()
|
||
const viewTreeRenderKey = ref(0)
|
||
|
||
const viewSections = computed((): ViewSection[] => {
|
||
const row = viewRow.value
|
||
if (!row) return []
|
||
const loadingText = '加载中…'
|
||
const scopeCode = viewDataScopeCode.value || row.dataScope
|
||
const dataFields: ViewSection['fields'] = [
|
||
{
|
||
label: '权限范围',
|
||
value: viewDetailLoading.value ? loadingText : dataScopeLabel(scopeCode),
|
||
tagType: viewDetailLoading.value ? undefined : dataScopeTagType(scopeCode)
|
||
}
|
||
]
|
||
if (scopeCode === '5') {
|
||
dataFields.push({
|
||
label: '自定义用户',
|
||
span: true,
|
||
...(viewDetailLoading.value
|
||
? { value: loadingText }
|
||
: viewCustomUserLabels.value.length
|
||
? { items: viewCustomUserLabels.value }
|
||
: { value: '—' })
|
||
})
|
||
}
|
||
const sections: ViewSection[] = [
|
||
{
|
||
title: '基本信息',
|
||
fields: [
|
||
{ label: '角色名称', value: row.roleName },
|
||
{
|
||
label: '状态',
|
||
value: dictStore.getLabel('sys_status', row.status),
|
||
tagType: dictStore.getTagType('sys_status', row.status) as 'success' | 'info'
|
||
},
|
||
{ label: '创建时间', value: row.createTime },
|
||
{ label: '备注', value: row.remark, span: true }
|
||
]
|
||
},
|
||
{
|
||
title: '菜单权限',
|
||
titleStyle: 'field',
|
||
fields: [],
|
||
slot: 'menuTree'
|
||
},
|
||
{
|
||
title: '数据权限',
|
||
fields: dataFields,
|
||
...(scopeCode === '5'
|
||
? {
|
||
slot: 'customDeptTree',
|
||
slotTitle: '自定义部门',
|
||
slotTitleStyle: 'field' as const
|
||
}
|
||
: {})
|
||
}
|
||
]
|
||
return sections
|
||
})
|
||
|
||
/** 异步灌入勾选数据后重挂载树并同步勾选态(只读展示) */
|
||
async function syncViewReadonlyTreeChecks() {
|
||
viewTreeRenderKey.value += 1
|
||
await nextTick()
|
||
viewMenuTreeRef.value?.setCheckedKeys?.(viewMenuCheckedKeys.value, true)
|
||
viewDeptTreeRef.value?.setCheckedKeys?.(viewDeptCheckedKeys.value)
|
||
}
|
||
|
||
async function loadViewDetail(row: Api.Role.RoleItem) {
|
||
viewDetailLoading.value = true
|
||
viewDataScopeCode.value = row.dataScope || ''
|
||
viewMenuTreeData.value = []
|
||
viewMenuCheckedKeys.value = []
|
||
viewDeptTreeData.value = []
|
||
viewDeptCheckedKeys.value = []
|
||
viewCustomUserLabels.value = []
|
||
try {
|
||
const [menuTree, menuRes, scopeRes] = await Promise.all([
|
||
fetchMenuTree(),
|
||
fetchRoleMenuIds(row.id),
|
||
fetchRoleDataScope(row.id)
|
||
])
|
||
viewMenuTreeData.value = menuTree || []
|
||
viewMenuCheckedKeys.value = [...(menuRes?.menuIds || [])]
|
||
|
||
const scope = scopeRes?.dataScope || row.dataScope
|
||
viewDataScopeCode.value = scope
|
||
if (scope === '5') {
|
||
const [deptTree, users] = await Promise.all([fetchDeptTree(), fetchAllUsersForDataScope()])
|
||
const details = scopeRes?.scopeDetails || []
|
||
viewDeptTreeData.value = deptTree || []
|
||
viewDeptCheckedKeys.value = details
|
||
.filter((d) => d.scopeType === 'dept')
|
||
.map((d) => d.scopeValue)
|
||
const userLabels = details
|
||
.filter((d) => d.scopeType === 'user')
|
||
.map((d) => {
|
||
const u = users.find((x) => x.id === d.scopeValue)
|
||
return u ? userOptionLabel(u) : d.scopeValue
|
||
})
|
||
viewCustomUserLabels.value = userLabels
|
||
}
|
||
await syncViewReadonlyTreeChecks()
|
||
} finally {
|
||
viewDetailLoading.value = false
|
||
}
|
||
}
|
||
|
||
function handleView(row: Api.Role.RoleItem) {
|
||
viewRow.value = row
|
||
viewVisible.value = true
|
||
loadViewDetail(row)
|
||
}
|
||
// 输入检索条件时自动搜索(防抖 300ms)
|
||
watchDebounced(
|
||
searchForm,
|
||
() => {
|
||
pagination.page = 1
|
||
loadData()
|
||
},
|
||
{ debounce: 300, deep: true }
|
||
)
|
||
|
||
onMounted(() => {
|
||
loadData()
|
||
})
|
||
</script>
|
||
|
||
<style scoped lang="scss">
|
||
.role-dialog-form,
|
||
.data-scope-form {
|
||
:deep(.el-form-item) {
|
||
margin-bottom: 0;
|
||
}
|
||
}
|
||
|
||
.data-scope-custom-form-item {
|
||
margin-top: 12px;
|
||
|
||
:deep(.el-form-item__content) {
|
||
display: block;
|
||
}
|
||
}
|
||
|
||
.data-scope-custom-grid {
|
||
display: grid;
|
||
grid-template-columns: 1fr 1fr;
|
||
gap: 16px;
|
||
width: 100%;
|
||
}
|
||
|
||
.data-scope-custom-col {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 8px;
|
||
min-width: 0;
|
||
}
|
||
|
||
.data-scope-col-title {
|
||
padding-left: 10px;
|
||
font-size: 12px;
|
||
font-weight: 600;
|
||
color: var(--el-text-color-regular);
|
||
border-left: 3px solid #10b981;
|
||
}
|
||
|
||
.data-scope-col-tip {
|
||
margin-top: 4px;
|
||
font-size: 11px;
|
||
line-height: 1.45;
|
||
color: var(--mint-text-muted, var(--el-text-color-secondary));
|
||
}
|
||
|
||
.data-scope-col-head-row {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 8px;
|
||
}
|
||
|
||
.data-scope-tree-wrap {
|
||
max-height: 280px;
|
||
padding: 8px;
|
||
overflow: auto;
|
||
border: 1px solid var(--el-border-color-lighter);
|
||
border-radius: 8px;
|
||
}
|
||
|
||
.data-scope-user-filter {
|
||
width: 100%;
|
||
}
|
||
|
||
.data-scope-user-list-wrap {
|
||
max-height: 236px;
|
||
padding: 8px;
|
||
overflow: auto;
|
||
border: 1px solid var(--el-border-color-lighter);
|
||
border-radius: 8px;
|
||
}
|
||
|
||
.data-scope-user-row {
|
||
padding: 4px 0;
|
||
|
||
:deep(.el-checkbox__label) {
|
||
line-height: 1.45;
|
||
white-space: normal;
|
||
}
|
||
}
|
||
|
||
.data-scope-selection-summary {
|
||
margin-top: 12px;
|
||
padding: 10px 12px;
|
||
border: 1px solid var(--el-border-color-lighter);
|
||
border-radius: 8px;
|
||
background: var(--el-fill-color-blank);
|
||
}
|
||
|
||
.data-scope-summary-block + .data-scope-summary-block {
|
||
margin-top: 10px;
|
||
padding-top: 10px;
|
||
border-top: 1px solid var(--el-border-color-lighter);
|
||
}
|
||
|
||
.data-scope-summary-heading {
|
||
margin-bottom: 6px;
|
||
padding-left: 10px;
|
||
font-size: 12px;
|
||
font-weight: 600;
|
||
color: var(--el-text-color-regular);
|
||
border-left: 3px solid #10b981;
|
||
}
|
||
|
||
.data-scope-summary-list {
|
||
display: grid;
|
||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||
gap: 8px 12px;
|
||
margin: 0;
|
||
padding: 0;
|
||
list-style: none;
|
||
font-size: 12px;
|
||
line-height: 1.45;
|
||
color: var(--el-text-color-primary);
|
||
}
|
||
|
||
.data-scope-summary-list li {
|
||
min-width: 0;
|
||
padding: 6px 8px;
|
||
overflow-wrap: anywhere;
|
||
word-break: break-word;
|
||
border-radius: 6px;
|
||
background: var(--el-fill-color-light);
|
||
}
|
||
|
||
.data-scope-summary-empty {
|
||
margin: 0;
|
||
font-size: 12px;
|
||
line-height: 1.5;
|
||
color: var(--mint-text-muted, var(--el-text-color-secondary));
|
||
}
|
||
|
||
.role-view-tree-block {
|
||
min-height: 48px;
|
||
}
|
||
|
||
.role-view-tree-scroll {
|
||
max-height: 320px;
|
||
overflow-y: auto;
|
||
padding: 8px;
|
||
border: 1px solid var(--el-border-color-lighter);
|
||
border-radius: 8px;
|
||
}
|
||
|
||
.role-view-readonly-tree {
|
||
:deep(.el-tree-node__content) {
|
||
cursor: default;
|
||
}
|
||
|
||
:deep(.el-checkbox) {
|
||
pointer-events: none;
|
||
}
|
||
|
||
:deep(.el-tree-node__expand-icon) {
|
||
pointer-events: auto;
|
||
}
|
||
}
|
||
</style>
|