1.0.3 的相关功能

This commit is contained in:
浅念
2025-08-19 16:18:15 +08:00
parent 0e84bedb67
commit 70d26f0829
10 changed files with 774 additions and 167 deletions
+21 -2
View File
@@ -27,7 +27,7 @@
</template> -->
</el-segmented>
<!-- 按钮权限:后端控制模式,使用自定义指令 -->
<ElButton
<!-- <ElButton
v-auth="'system@menu_add'"
@click="showModel('menu', null)"
v-ripple
@@ -37,12 +37,17 @@
</ElButton>
<ElButton @click="toggleExpand" v-ripple>
{{ isExpanded ? '收起' : '展开' }}
</ElButton>
</ElButton> -->
<!-- 按钮权限:前端控制模式,使用 hasAuth 方法 -->
<!-- <ElButton v-if="hasAuth('B_CODE1')" @click="showModel('menu', null, true)" v-ripple>
添加菜单
</ElButton> -->
</template>
<template #right>
<div class="btnadds" @click="showModel('menu', null)" v-auth="'system@menu_add'">
<i class="iconfont-sys">&#xe83e;</i>
</div>
</template>
</ArtTableHeader>
<!-- 表格 -->
<ArtTable
@@ -748,4 +753,18 @@
}
}
}
.btnadds {
display: flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
margin-left: 10px;
color: var(--art-gray-700);
cursor: pointer;
background-color: rgba(var(--art-gray-200-rgb), 0.8);
border-radius: 6px;
transition: color 0.3s;
transition: all 0.3s;
}
</style>
+190 -75
View File
@@ -7,7 +7,9 @@
<div style="width: 12px"></div>
<ElCol :xs="24" :sm="12" :lg="6" class="el-col2">
<ElButton v-ripple>搜索</ElButton>
<ElButton @click="showDialog('add')" v-ripple v-if="hasAuth('system@role_add')">新增角色</ElButton>
<ElButton @click="showDialog('add')" v-ripple v-if="hasAuth('system@role_add')"
>新增角色</ElButton
>
</ElCol>
</ElRow>
@@ -36,11 +38,12 @@
<ElTableColumn fixed="right" label="操作" width="200px">
<template #default="scope">
<ElRow>
<el-button
<el-button link @click="showPermissionDialog(scope.row)">
<!-- <el-button
link
@click="showPermissionDialog(scope.row)"
v-if="hasAuth('system@role_perm')"
>
> -->
菜单权限
</el-button>
<el-button
@@ -92,39 +95,38 @@
</template>
</ElDialog>
<ElDialog
v-model="permissionDialog"
title="菜单权限"
width="520px"
align-center
class="el-dialog-border"
>
<ElScrollbar height="70vh">
<ElTree
ref="treeRef"
:data="processedMenuList"
show-checkbox
node-key="id"
:default-expand-all="isExpandAll"
:props="defaultProps"
@check="handleTreeCheck"
<ElDialog v-model="permissionDialog" title="菜单权限" align-center>
<el-tabs v-model="activeName" class="demo-tabs">
<el-tab-pane
v-for="sys in systemsList"
:key="sys.id"
:label="sys.name"
:name="String(sys.id)"
>
<template #default="{ data }">
<div style="display: flex; align-items: center">
<span v-if="data.isAuth">
{{ data.label }}
</span>
<span v-else>{{ defaultProps.label(data) }}</span>
</div>
</template>
</ElTree>
</ElScrollbar>
<ElScrollbar height="70vh">
<ElTree
:ref="(el:any) => setTreeRef(sys.id, el)"
:data="processedMenuBySystem[sys.id] || []"
show-checkbox
node-key="id"
:default-expand-all="isExpandAll"
:props="defaultProps"
@check="() => handleTreeCheck(sys.id)"
>
<template #default="{ data }">
<div style="display: flex; align-items: center">
<span v-if="data.isAuth">
{{ data.label }}
</span>
<span v-else>{{ defaultProps.label(data) }}</span>
</div>
</template>
</ElTree>
</ElScrollbar>
</el-tab-pane>
</el-tabs>
<template #footer>
<div class="dialog-footer">
<ElButton @click="toggleExpandAll">{{ isExpandAll ? '全部收起' : '全部展开' }}</ElButton>
<ElButton @click="toggleSelectAll" style="margin-left: 8px">{{
isSelectAll ? '取消全选' : '全部选择'
}}</ElButton>
<ElButton type="primary" @click="savePermission">保存</ElButton>
</div>
</template>
@@ -138,17 +140,29 @@
import { formatMenuTitle } from '@/router/utils/utils'
import { systemService } from '@/api/systemApi'
import { useAuth } from '@/composables/useAuth'
import { UserService } from '@/api/usersApi'
const { hasAuth } = useAuth()
// import { ButtonMoreItem } from '@/components/core/forms/ArtButtonMore.vue'
// import { Role, ROLE_LIST_DATA } from '@/mock/temp/formData'
const loading = ref(false)
defineOptions({ name: 'Role' })
const activeName = ref('1')
const dialogVisible = ref(false)
const permissionDialog = ref(false)
// 权限树数据(来自接口 getPermissionMenuList)
const permissionMenuList = ref<any[]>([])
const permissionMenuListwork = ref<any[]>([])
const permissionMenuListcrm = ref<any[]>([])
// 精简后的多系统映射
const permissionMenuBySystem = reactive<Record<number, any[]>>({})
const processedMenuBySystem = reactive<Record<number, any[]>>({})
const treeRefs = reactive<Record<number, any>>({})
const setTreeRef = (systemId: number, el: any) => {
if (el) treeRefs[systemId] = el
}
const treeRef = ref()
const treeRefwork = ref()
const treeRefcrm = ref()
const isExpandAll = ref(true)
const isSelectAll = ref(false)
const pagination = reactive({
@@ -162,7 +176,12 @@
name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
des: [{ required: true, message: '请输入角色描述', trigger: 'blur' }]
})
//系统
const systemsList = ref<any>()
const getsystemsList = async () => {
const response = await UserService.getsystemsList()
systemsList.value = response || []
}
const form = reactive<any>({
id: '',
name: '',
@@ -234,25 +253,46 @@
permissionDialog.value = true
roleId.value = row.id
try {
const res = await systemService.getPermissionMenuList({ role_id: row.id })
// 兼容不同返回结构(data/records/直接数组)
const list = (res as any)?.data ?? (res as any)?.records ?? (res as any)
permissionMenuList.value = Array.isArray(list) ? list : []
// 根据接口返回的 selected 字段设置默认选中
await getsystemsList()
// 并行请求所选用户的各系统权限
const sysIds = (systemsList.value || []).map((s: any) => s.id)
const results = await Promise.all(
sysIds.map((id: number) =>
systemService.getPermissionMenuList({ role_id: row.id, system_id: id })
)
)
sysIds.forEach((id: number, idx: number) => {
const raw = results[idx] as any
const list = raw?.data ?? raw?.records ?? raw
permissionMenuBySystem[id] = Array.isArray(list) ? list : []
processedMenuBySystem[id] = buildProcessedMenu(permissionMenuBySystem[id])
})
// 兼容旧变量,避免其它逻辑引用报错
permissionMenuList.value = permissionMenuBySystem[1] || []
permissionMenuListwork.value = permissionMenuBySystem[2] || []
permissionMenuListcrm.value = permissionMenuBySystem[3] || []
// 设置默认选中
nextTick(() => {
const keys = buildDefaultCheckedKeys(permissionMenuList.value)
treeRef.value?.setCheckedKeys(keys)
handleTreeCheck()
sysIds.forEach((id: number) => {
const keys = buildDefaultCheckedKeys(permissionMenuBySystem[id] || [])
treeRefs[id]?.setCheckedKeys?.(keys)
handleTreeCheck(id)
})
})
} catch (e) {
permissionMenuList.value = []
permissionMenuListwork.value = []
permissionMenuListcrm.value = []
}
}
// 构建默认选中的 key 列表(与 node-key="id" 一致)
const buildDefaultCheckedKeys = (source: any[]): Array<string | number> => {
const keys: Array<string | number> = []
const walk = (node: any) => {
if (node?.meta?.selected) {
const hasChildren = Array.isArray(node?.children) && node.children.length > 0
const hasAuths = Array.isArray(node?.meta?.authList) && node.meta.authList.length > 0
// 仅当没有子节点且没有权限子项时,才勾选父节点,避免联动导致子级被全选
if (node?.meta?.selected && !hasChildren && !hasAuths) {
keys.push(node.id)
}
if (Array.isArray(node?.meta?.authList)) {
@@ -262,7 +302,7 @@
}
})
}
if (Array.isArray(node?.children)) node.children.forEach(walk)
if (hasChildren) node.children.forEach(walk)
}
source.forEach(walk)
return keys
@@ -272,7 +312,6 @@
const processNode = (node: any) => {
// 规范化 id,确保树节点唯一键与 node-key 对应
const processed: any = { ...node, id: node.id }
// 如果有 authList,将其转换为子节点
if (node.meta && node.meta.authList && node.meta.authList.length) {
const authNodes = node.meta.authList.map((auth: any) => ({
@@ -285,27 +324,97 @@
isAuth: true,
checked: auth.selected || false
}))
processed.children = processed.children ? [...processed.children, ...authNodes] : authNodes
}
// 递归处理子节点
if (processed.children) {
processed.children = processed.children.map(processNode)
}
return processed
}
const source = permissionMenuList.value || []
return source.map(processNode)
})
const processedMenuListwork = computed(() => {
const processNode = (node: any) => {
// 规范化 id,确保树节点唯一键与 node-key 对应
const processed: any = { ...node, id: node.id }
// 如果有 authList,将其转换为子节点
if (node.meta && node.meta.authList && node.meta.authList.length) {
const authNodes = node.meta.authList.map((auth: any) => ({
// 为权限子节点提供稳定唯一 id(数字+字符串),与 node-key="id" 匹配
id: `${node.id}__auth__${auth.authMark}`,
name: `${node.name}_${auth.authMark}`,
label: auth.title,
auth_mark: auth.authMark,
auth_id: auth.id,
isAuth: true,
checked: auth.selected || false
}))
processed.children = processed.children ? [...processed.children, ...authNodes] : authNodes
}
// 递归处理子节点
if (processed.children) {
processed.children = processed.children.map(processNode)
}
return processed
}
const source = permissionMenuListwork.value || []
return source.map(processNode)
})
const processedMenuListcrm = computed(() => {
const processNode = (node: any) => {
// 规范化 id,确保树节点唯一键与 node-key 对应
const processed: any = { ...node, id: node.id }
// 如果有 authList,将其转换为子节点
if (node.meta && node.meta.authList && node.meta.authList.length) {
const authNodes = node.meta.authList.map((auth: any) => ({
// 为权限子节点提供稳定唯一 id(数字+字符串),与 node-key="id" 匹配
id: `${node.id}__auth__${auth.authMark}`,
name: `${node.name}_${auth.authMark}`,
label: auth.title,
auth_mark: auth.authMark,
auth_id: auth.id,
isAuth: true,
checked: auth.selected || false
}))
processed.children = processed.children ? [...processed.children, ...authNodes] : authNodes
}
// 递归处理子节点
if (processed.children) {
processed.children = processed.children.map(processNode)
}
return processed
}
const source = permissionMenuListcrm.value || []
return source.map(processNode)
})
const defaultProps = {
children: 'children',
label: (data: any) => formatMenuTitle(data.meta?.title) || ''
}
// 通用:处理节点,展开权限为子节点
const processNode = (node: any) => {
const processed: any = { ...node, id: node.id }
if (node.meta && node.meta.authList && node.meta.authList.length) {
const authNodes = node.meta.authList.map((auth: any) => ({
id: `${node.id}__auth__${auth.authMark}`,
name: `${node.name}_${auth.authMark}`,
label: auth.title,
auth_mark: auth.authMark,
auth_id: auth.id,
isAuth: true,
checked: auth.selected || false
}))
processed.children = processed.children ? [...processed.children, ...authNodes] : authNodes
}
if (processed.children) processed.children = processed.children.map(processNode)
return processed
}
const buildProcessedMenu = (source: any[]) => (source || []).map(processNode)
const deleteRole = async (row: any) => {
ElMessageBox.confirm('确定删除该角色吗?', '删除确认', {
confirmButtonText: '确定',
@@ -335,46 +444,33 @@
dialogVisible.value = false
formEl.resetFields()
}
const getSelectedIds = () => {
const tree = treeRef.value
if (!tree) return { menuIds: [], authIds: [] }
const getSelectedIdsBySystem = (systemId: number) => {
const tree = treeRefs[systemId]
if (!tree) return [] as (string | number)[]
const checkedNodes = tree.getCheckedNodes?.(false) || []
const halfCheckedNodes = tree.getHalfCheckedNodes?.() || []
const allNodes = [...checkedNodes, ...halfCheckedNodes]
const menuIds = Array.from(
new Set(
allNodes
.filter((n: any) => !n.isAuth && n.id !== undefined && n.id !== null)
.map((n: any) => n.id)
)
new Set(allNodes.filter((n: any) => !n.isAuth && n.id != null).map((n: any) => n.id))
)
const authIds = Array.from(
new Set(
checkedNodes.filter((n: any) => n.isAuth && n.auth_id != null).map((n: any) => n.auth_id)
)
)
const ids = Array.from(new Set([...(menuIds as any[]), ...(authIds as any[])]))
return { menuIds, authIds, ids }
return Array.from(new Set([...(menuIds as any[]), ...(authIds as any[])]))
}
const savePermission = async () => {
const { ids } = getSelectedIds()
// console.log('选中的菜单ID:', menuIds)
// console.log('选中的权限ID:', authIds)
// console.log('合并后的全部ID:', ids)
const sysIds = (systemsList.value || []).map((s: any) => s.id)
const idall = Array.from(new Set(sysIds.flatMap((id: number) => getSelectedIdsBySystem(id))))
console.log('idall:', idall)
const res = await systemService.roleAuth({
role_id: roleId.value,
resources: ids
resources: idall
})
ElMessage.success(res?.message || '权限保存成功')
permissionDialog.value = false
}
const toggleExpandAll = () => {
const tree = treeRef.value
if (!tree) return
@@ -420,13 +516,32 @@
return keys
}
const handleTreeCheck = () => {
const tree = treeRef.value
const handleTreeCheck = (systemId?: number) => {
const sysId = systemId ?? Number(activeName.value)
const tree = treeRefs[sysId] || treeRef.value
if (!tree) return
const checkedKeys = tree.getCheckedKeys?.() || []
const allKeys = getAllNodeKeys(processedMenuBySystem[sysId] || [])
isSelectAll.value = checkedKeys.length === allKeys.length && allKeys.length > 0
}
const handleTreeCheckwork = () => {
const tree = treeRefwork.value
if (!tree) return
// 使用树组件的getCheckedKeys方法获取选中的节点
const checkedKeys = tree.getCheckedKeys()
const allKeys = getAllNodeKeys(processedMenuList.value)
const allKeys = getAllNodeKeys(processedMenuListwork.value)
// 判断是否全选:选中的节点数量等于总节点数量
isSelectAll.value = checkedKeys.length === allKeys.length && allKeys.length > 0
}
const handleTreeCheckcrm = () => {
const tree = treeRefcrm.value
if (!tree) return
// 使用树组件的getCheckedKeys方法获取选中的节点
const checkedKeys = tree.getCheckedKeys()
const allKeys = getAllNodeKeys(processedMenuListcrm.value)
// 判断是否全选:选中的节点数量等于总节点数量
isSelectAll.value = checkedKeys.length === allKeys.length && allKeys.length > 0