feat: 完善用户权限

This commit is contained in:
xingyu
2022-08-01 19:03:27 +08:00
parent 72db9341f6
commit 85287e1f7f
8 changed files with 210 additions and 40 deletions

View File

@ -1,14 +1,6 @@
<script setup lang="ts">
import { onMounted, reactive, ref, unref } from 'vue'
import dayjs from 'dayjs'
import { DICT_TYPE, getDictOptions } from '@/utils/dict'
import { useTable } from '@/hooks/web/useTable'
import { useI18n } from '@/hooks/web/useI18n'
import { FormExpose } from '@/components/Form'
import type { RoleVO } from '@/api/system/role/types'
import { rules, allSchemas } from './role.data'
import * as RoleApi from '@/api/system/role'
import Dialog from '@/components/Dialog/src/Dialog.vue'
import {
ElForm,
ElFormItem,
@ -20,6 +12,18 @@ import {
ElCard,
ElSwitch
} from 'element-plus'
import { DICT_TYPE, getDictOptions } from '@/utils/dict'
import { useTable } from '@/hooks/web/useTable'
import { useI18n } from '@/hooks/web/useI18n'
import { FormExpose } from '@/components/Form'
import { rules, allSchemas } from './role.data'
import type { RoleVO } from '@/api/system/role/types'
import type {
PermissionAssignRoleDataScopeReqVO,
PermissionAssignRoleMenuReqVO
} from '@/api/system/permission/types'
import * as RoleApi from '@/api/system/role'
import * as PermissionApi from '@/api/system/permission'
import { listSimpleMenusApi } from '@/api/system/menu'
import { listSimpleDeptApi } from '@/api/system/dept'
import { handleTree } from '@/utils/tree'
@ -95,6 +99,7 @@ const handleDetail = async (row: RoleVO) => {
// ========== 数据权限 ==========
const dataScopeForm = reactive({
id: 0,
name: '',
code: '',
dataScope: 0,
@ -111,33 +116,62 @@ const dialogScopeVisible = ref(false)
const dialogScopeTitle = ref('数据权限')
const actionScopeType = ref('')
const dataScopeDictDatas = ref()
const defaultCheckedKeys = ref()
// 选项
const checkStrictly = ref(true)
const treeNodeAll = ref(false)
// 全选/全不选
const handleCheckedTreeNodeAll = () => {
treeRef.value!.setCheckedNodes(treeNodeAll.value ? treeOptions.value : [])
}
// 权限操作
const handleScope = async (type: string, row: RoleVO) => {
checkStrictly.value = true
treeNodeAll.value = false
dataScopeForm.dataScope = 0
dataScopeForm.id = row.id
dataScopeForm.name = row.name
dataScopeForm.code = row.code
if (type === 'menu') {
const menuRes = await listSimpleMenusApi()
treeOptions.value = handleTree(menuRes)
} else if (type === 'dept') {
const role = await PermissionApi.listRoleMenusApi(row.id)
console.info(role)
if (role) {
// treeRef.value!.setCheckedKeys(role as unknown as Array<number>)
defaultCheckedKeys.value = role
}
} else if (type === 'data') {
const deptRes = await listSimpleDeptApi()
treeOptions.value = handleTree(deptRes)
const role = await RoleApi.getRoleApi(row.id)
console.info(role)
dataScopeForm.dataScope = role.dataScope
if (role.dataScopeDeptIds) {
// treeRef.value!.setCheckedKeys(role.dataScopeDeptIds as unknown as Array<number>, false)
defaultCheckedKeys.value = role.dataScopeDeptIds
}
}
actionScopeType.value = type
dialogScopeVisible.value = true
}
// 全选/全不选
const handleCheckedTreeNodeAll = () => {
treeRef.value?.setCheckedNodes(treeNodeAll.value ? treeOptions.value : [])
}
// TODO:保存
const submitScope = () => {
console.info()
// 保存权限
const submitScope = async () => {
const keys = treeRef.value!.getCheckedKeys(false) as unknown as Array<number>
console.info(keys)
if ('data' === actionScopeType.value) {
const data = ref<PermissionAssignRoleDataScopeReqVO>({
roleId: dataScopeForm.id,
dataScope: dataScopeForm.dataScope,
dataScopeDeptIds: dataScopeForm.dataScope !== SystemDataScopeEnum.DEPT_CUSTOM ? [] : keys
})
await PermissionApi.assignRoleDataScopeApi(data.value)
} else if ('menu' === actionScopeType.value) {
const data = ref<PermissionAssignRoleMenuReqVO>({
roleId: dataScopeForm.id,
menuIds: keys
})
await PermissionApi.assignRoleMenuApi(data.value)
}
ElMessage.success(t('common.updateSuccess'))
dialogScopeVisible.value = false
}
const init = () => {
dataScopeDictDatas.value = getDictOptions(DICT_TYPE.SYSTEM_DATA_SCOPE)
@ -308,7 +342,7 @@ onMounted(() => {
ref="treeRef"
node-key="id"
show-checkbox
default-expand-all
:default-checked-keys="defaultCheckedKeys"
:check-strictly="!checkStrictly"
:props="defaultProps"
:data="treeOptions"