Commit dd3fce0a by 李宁

1

1 parent 640f1611
......@@ -26,3 +26,14 @@ export function createOrUpdateRole(data) {
data,
})
}
/**
* 根据角色ID获取角色权限
*/
export function queryRolePermission(data) {
return request({
url: '/crm/getRoleFunction',
data,
})
}
......@@ -102,9 +102,29 @@ const isSelected = computed(() =>
const isIndeterminate = computed(() => {
if (isSelected.value || !hasChildren.value) return false
return props.permission.children!.some(c =>
props.selectedPermissions.includes(c.id)
)
// 获取所有子权限ID
const getAllChildIds = (children: Permission[]): string[] => {
const ids: string[] = []
for (const child of children) {
ids.push(child.id)
if (child.children) {
ids.push(...getAllChildIds(child.children))
}
}
return ids
}
const allChildIds = getAllChildIds(props.permission.children!)
if (allChildIds.length === 0) return false
// 检查是否所有子权限都被选中
const allSelected = allChildIds.every(id => props.selectedPermissions.includes(id))
if (allSelected) return false
// 检查是否有部分子权限被选中
const someSelected = allChildIds.some(id => props.selectedPermissions.includes(id))
return someSelected
})
</script>
......
......@@ -212,18 +212,16 @@ export interface Permission {
}
export interface Role {
id: string
name: string
description?: string
level: RoleLevel
id: Number
roleName: string
status: RoleStatus
remark?: string
permissionIds?: string[]
permissions?: string[]
status: RoleStatus
createTime?: string
}
export type RoleLevel = '地市级' | '区县级' | '一线人员'
export type RoleStatus = '启用' | '禁用'
export type RoleStatus = 1 | 0
// Props
interface Props {
......@@ -255,7 +253,7 @@ const roleStatusEnabled = ref(true)
const expandedPermissions = ref<Set<string>>(new Set())
// 计算属性
const roleStatus = computed((): RoleStatus => roleStatusEnabled.value ? '启用' : '禁用')
const roleStatus = computed((): RoleStatus => roleStatusEnabled.value ? '1' : '0')
const topLevelPermissions = computed(() =>
props.permissions.filter(p => !p.parentId)
......@@ -264,11 +262,13 @@ const topLevelPermissions = computed(() =>
const filteredRoles = ref([])
// 获取角色的权限ID列表,兼容不同的字段名
const getRolePermissionIds = (role: Role): string[] => {
return role.permissionIds || role.permissions || []
}
const getRolePermissionIds = async (role: Role)=> {
const response = await $api.queryRolePermission({
id: role.id
})
return ['1','2','3','4']
}
// 打开新增对话框
const handleOpenAddDialog = () => {
......@@ -283,19 +283,23 @@ const handleOpenAddDialog = () => {
}
// 打开编辑对话框
const handleOpenEditDialog = (role: Role) => {
const handleOpenEditDialog = async (role: Role) => {
editingRole.value = role
roleName.value = role.name
roleDescription.value = role.description || ''
roleName.value = role.roleName
roleDescription.value = role.remark || ''
roleStatusEnabled.value = role.status === 1
selectedPermissions.value = [...getRolePermissionIds(role)]
roleStatusEnabled.value = role.status === '启用'
selectedPermissions.value = await getRolePermissionIds(role)
expandedPermissions.value = new Set(props.permissions.filter(p => !p.parentId).map(p => p.id))
console.log(selectedPermissions)
console.log(expandedPermissions)
isDialogOpen.value = true
}
// 保存角色
const handleSave = () => {
const handleSave = async () => {
if (!roleName.value.trim()) {
ElMessage.error('请输入角色名称')
return
......@@ -309,22 +313,47 @@ const handleSave = () => {
const roleData = {
name: roleName.value.trim(),
description: roleDescription.value.trim(),
level: roleLevel.value,
permissionIds: selectedPermissions.value,
status: roleStatus.value
}
if (editingRole.value) {
emit('updateRole', editingRole.value.id, roleData)
ElMessage.success('角色更新成功')
console.log(roleData)
const response = await $api.createOrUpdateRole({
roleId: '',
roleName: roleName.value.trim(),
remark: roleDescription.value.trim(),
status: roleStatus.value,
functionIds: selectedPermissions.value
})
if (response.c === 0) {
handleFilter()
ElMessage.success('创建成功');
} else {
emit('addRole', roleData)
ElMessage.success('角色创建成功')
ElMessage.error(response.m);
}
isDialogOpen.value = false
}
// 获取所有子权限ID
const getAllChildPermissionIds = (permissionId: string): string[] => {
const permission = findPermissionById(permissionId)
if (!permission || !permission.children) return []
const childIds: string[] = []
const collectChildIds = (perms: Permission[]) => {
for (const p of perms) {
childIds.push(p.id)
if (p.children) {
collectChildIds(p.children)
}
}
}
collectChildIds(permission.children)
return childIds
}
// 切换权限选择
const handleTogglePermission = (permissionId: string) => {
const permission = findPermissionById(permissionId)
......@@ -340,8 +369,18 @@ const handleTogglePermission = (permissionId: string) => {
})
newSelected = newSelected.filter(id => id !== permissionId)
} else {
// 选择:自动选择所有父权限
// 选择:自动选择所有父权限和所有子权限
newSelected.push(permissionId)
// 自动选择所有子权限
const childIds = getAllChildPermissionIds(permissionId)
childIds.forEach(id => {
if (!newSelected.includes(id)) {
newSelected.push(id)
}
})
// 自动选择所有父权限
let current = permission
while (current.parentId) {
if (!newSelected.includes(current.parentId)) {
......@@ -352,6 +391,8 @@ const handleTogglePermission = (permissionId: string) => {
}
selectedPermissions.value = newSelected
console.log(newSelected)
}
// 查找权限
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!