<script lang="ts" setup>
|
import type { TreeActionType } from '@jnpf/ui';
|
|
import { nextTick, onMounted, reactive, ref, toRefs, unref } from 'vue';
|
|
import { useMessage } from '@jnpf/hooks';
|
import { BasicTree } from '@jnpf/ui';
|
|
import { getCommonAuthValues, updateCommonAuthList } from '#/api/permission/auth';
|
|
interface State {
|
loading: boolean;
|
objectId: string;
|
treeData: any[];
|
selectedIds: any[];
|
btnLoading: boolean;
|
}
|
const props = defineProps({
|
id: { type: String, default: '' },
|
type: { type: String, default: 'Portal' },
|
objectType: { type: String, default: 'role' },
|
});
|
|
const { createMessage } = useMessage();
|
const treeRef = ref<Nullable<TreeActionType>>(null);
|
const state = reactive<State>({
|
loading: false,
|
objectId: '',
|
selectedIds: [],
|
treeData: [],
|
btnLoading: false,
|
});
|
const { treeData, loading, btnLoading } = toRefs(state);
|
|
defineExpose({ handleExpandAll, handleCheckAll, handleSubmit, btnLoading, loading });
|
|
function init() {
|
state.objectId = props.id;
|
state.btnLoading = false;
|
getAuthorizeList();
|
}
|
function getAuthorizeList() {
|
state.loading = true;
|
state.treeData = [];
|
state.selectedIds = [];
|
getTree().setCheckedKeys([]);
|
if (!props.type || !state.objectId) return;
|
getCommonAuthValues(props.type, state.objectId, { objectType: props.objectType })
|
.then((res) => {
|
state.treeData = res.data.list || [];
|
const parentKeys = getTree().getParentKeys(state.treeData, true);
|
const dataIds = [...new Set([...res.data.ids, ...state.selectedIds])];
|
state.selectedIds = dataIds;
|
const checkedKeys = dataIds.filter((o) => !parentKeys.includes(o));
|
nextTick(() => {
|
getTree().setCheckedKeys(checkedKeys);
|
state.loading = false;
|
});
|
})
|
.catch(() => {
|
state.loading = false;
|
});
|
}
|
function onCheck(checkedKeys, e) {
|
const dataIds = [...checkedKeys, ...e.halfCheckedKeys];
|
state.selectedIds = dataIds;
|
}
|
function handleCheckAll(boo) {
|
getTree().checkAll(boo);
|
nextTick(() => {
|
const checkedKeys: any[] = getTree().getCheckedKeys() as any[];
|
state.selectedIds = checkedKeys;
|
});
|
}
|
function handleExpandAll(boo) {
|
getTree().expandAll(boo);
|
}
|
function getTree() {
|
const tree = unref(treeRef);
|
if (!tree) throw new Error('tree is null!');
|
return tree;
|
}
|
function handleSubmit() {
|
state.btnLoading = true;
|
updateCommonAuthList(props.type, state.objectId, { ids: state.selectedIds, objectType: props.objectType })
|
.then((res) => {
|
createMessage.success(res.msg);
|
state.btnLoading = false;
|
})
|
.catch(() => {
|
state.btnLoading = false;
|
});
|
}
|
|
onMounted(() => {
|
init();
|
});
|
</script>
|
<template>
|
<div class="auth-main-container">
|
<div class="main h-full overflow-hidden" v-loading="loading">
|
<BasicTree
|
v-show="treeData.length"
|
:tree-data="treeData"
|
ref="treeRef"
|
checkable
|
block-node
|
click-row-to-expand
|
default-expand-all
|
@check="onCheck"
|
class="overflow-auto" />
|
<div class="no-data" v-show="!treeData.length && !loading">
|
<jnpf-empty />
|
</div>
|
</div>
|
</div>
|
</template>
|