editPer.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <el-dialog custom-class="custom-dialog" title="分配数据权限" v-model="isShowDialog" width="500px">
  3. <el-form ref="formRef" :model="ruleForm" label-width="90px">
  4. <el-form-item label="角色名称" prop="name">
  5. <el-input v-model="ruleForm.name" disabled placeholder="请输入账户名称" clearable></el-input>
  6. </el-form-item>
  7. <el-form-item label="权限范围" prop="dataScope">
  8. <el-select v-model="ruleForm.dataScope" placeholder="请选择" clearable class="w100">
  9. <el-option label="全部数据权限" :value="1" />
  10. <el-option label="自定数据权限" :value="2" />
  11. <el-option label="本组织数据权限" :value="3" />
  12. <el-option label="本组织及以下数据权限" :value="4" />
  13. </el-select>
  14. </el-form-item>
  15. <el-form-item label="数据权限" prop="deptIds" v-if="ruleForm.dataScope===2">
  16. <div class="tree">
  17. <el-tree ref="treeRef" :data="deptData" show-checkbox default-expand-all node-key="deptId" highlight-current :props="defaultProps" check-on-click-node :expand-on-click-node="false" />
  18. </div>
  19. </el-form-item>
  20. </el-form>
  21. <template #footer>
  22. <span class="dialog-footer">
  23. <el-button @click="onCancel" size="default">取 消</el-button>
  24. <el-button type="primary" @click="onSubmit" size="default">确定</el-button>
  25. </span>
  26. </template>
  27. </el-dialog>
  28. </template>
  29. <script lang="ts">
  30. import { reactive, toRefs, defineComponent, ref, nextTick } from 'vue';
  31. import api from '/@/api/system';
  32. import { ElMessage } from 'element-plus';
  33. interface DialogRow {
  34. id?: number;
  35. name: string;
  36. dataScope: 1 | 2 | 3 | 4; // 数据范围(1:全部数据权限 2:自定数据权限 3:本组织数据权限 4:本组织及以下数据权限)
  37. deptIds: number[]; // 组织id
  38. }
  39. const baseForm: DialogRow = {
  40. id: undefined,
  41. name: '',
  42. dataScope: 1,
  43. deptIds: [],
  44. };
  45. export default defineComponent({
  46. props: {
  47. deptData: {
  48. type: Array,
  49. default: () => [],
  50. },
  51. },
  52. setup() {
  53. const postList = ref([]);
  54. const formRef = ref<HTMLElement | null>(null);
  55. const defaultProps = {
  56. children: 'children',
  57. label: 'deptName',
  58. };
  59. const treeRef = ref();
  60. const state = reactive({
  61. isShowDialog: false,
  62. ruleForm: {
  63. ...baseForm,
  64. },
  65. });
  66. // 打开弹窗
  67. const openDialog = async (row?: any) => {
  68. resetForm();
  69. const { id, name, dataScope, deptIds } = await api.role.getRole(row.id);
  70. state.ruleForm = { id, name, dataScope, deptIds: deptIds || [] };
  71. state.isShowDialog = true;
  72. nextTick(() => {
  73. if (deptIds && deptIds.length) {
  74. treeRef.value.setCheckedKeys(deptIds);
  75. }
  76. });
  77. };
  78. // 关闭弹窗
  79. const closeDialog = () => {
  80. state.isShowDialog = false;
  81. };
  82. // 取消
  83. const onCancel = () => {
  84. closeDialog();
  85. };
  86. // 新增
  87. const onSubmit = () => {
  88. if (state.ruleForm.dataScope === 2) {
  89. state.ruleForm.deptIds = treeRef.value.getCheckedKeys(true);
  90. } else {
  91. state.ruleForm.deptIds = [];
  92. }
  93. api.role.dataScope(state.ruleForm).then(() => {
  94. ElMessage.success('设置数据权限成功');
  95. closeDialog();
  96. });
  97. };
  98. const resetForm = () => {
  99. state.ruleForm = {
  100. ...baseForm,
  101. };
  102. };
  103. return {
  104. openDialog,
  105. closeDialog,
  106. defaultProps,
  107. treeRef,
  108. onCancel,
  109. onSubmit,
  110. postList,
  111. formRef,
  112. ...toRefs(state),
  113. };
  114. },
  115. });
  116. </script>
  117. <style lang="scss" scoped>
  118. .tree {
  119. width: 100%;
  120. max-height: 50vh;
  121. overflow: auto;
  122. padding-bottom: 5px;
  123. }
  124. </style>