UserRoleModal.vue 8.7 KB
<template>
  <a-drawer
    :title="title"
    :maskClosable="true"
    width=650
    placement="right"
    :closable="true"
    @close="close"
    :visible="visible"
    style="overflow: auto;padding-bottom: 53px;">
    <a-tabs v-model:activeKey="activeKey">
      <a-tab-pane key="1" tab="菜单权限">
        <a-form>
          <a-form-item label='所拥有的权限'>
            <a-tree
              checkable
              @check="onCheck"
              :checkedKeys="checkedKeys"
              :treeData="treeData"
              @expand="onExpand"
              @select="onTreeNodeSelect"
              :selectedKeys="selectedKeys"
              :expandedKeys="expandedKeysss"
              :checkStrictly="checkStrictly">
              <span slot="hasDatarule" slot-scope="{slotTitle,ruleFlag}">
                {{ slotTitle }}<a-icon type="align-left" style="margin-left:5px;color: red;"></a-icon>
              </span>
            </a-tree>
          </a-form-item>
        </a-form>
      </a-tab-pane>
      <a-tab-pane key="2" tab="数据权限" force-render>
        <a-form>
          <a-form-item label='所拥有的权限'>
            <a-tree
              checkable
              @check="onCheck2"
              :checkedKeys="checkedKeys2"
              :treeData="treeData2"
              @expand="onExpand2"
              @select="onTreeNodeSelect2"
              :selectedKeys="selectedKeys2"
              :expandedKeys="expandedKeysss2"
              :checkStrictly="checkStrictly2">
              <span slot="hasDatarule" slot-scope="{slotTitle,ruleFlag}">
                {{ slotTitle }}<a-icon v-if="ruleFlag" type="align-left" style="margin-left:5px;color: red;"></a-icon>
              </span>
            </a-tree>
          </a-form-item>
        </a-form>
      </a-tab-pane>
    </a-tabs>

    <div class="drawer-bootom-button">
      <a-dropdown style="float: left" :trigger="['click']" placement="topCenter">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="switchCheckStrictly(1)">父子关联</a-menu-item>
          <a-menu-item key="2" @click="switchCheckStrictly(2)">取消关联</a-menu-item>
          <a-menu-item key="3" @click="checkALL">全部勾选</a-menu-item>
          <a-menu-item key="4" @click="cancelCheckALL">取消全选</a-menu-item>
          <a-menu-item key="5" @click="expandAll">展开所有</a-menu-item>
          <a-menu-item key="6" @click="closeAll">合并所有</a-menu-item>
        </a-menu>
        <a-button>
          树操作
          <a-icon type="up" />
        </a-button>
      </a-dropdown>
      <a-popconfirm title="确定放弃编辑?" @confirm="close" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit(false)" type="primary" :loading="loading" ghost style="margin-right: 0.8rem">仅保存
      </a-button>
      <a-button @click="handleSubmit(true)" type="primary" :loading="loading">保存并关闭</a-button>
    </div>

    <role-datarule-modal ref="datarule"></role-datarule-modal>

  </a-drawer>

</template>
<script>
import { queryDataTreeListForRole, queryRoleDataPermission, queryRolePermission, queryTreeListForRole, saveRolePermission } from '@/api/api'
import RoleDataruleModal from './RoleDataruleModal.vue'

export default {
  name: 'RoleModal',
  components: {
    RoleDataruleModal
  },
  data() {
    return {
      roleId: '',
      title: '角色权限配置',
      visible: false,
      loading: false,
      activeKey: '1',

      treeData: [],
      defaultCheckedKeys: [],
      selectedKeys: [],
      checkedKeys: [],
      expandedKeysss: [],
      allTreeKeys: [],
      autoExpandParent: true,
      checkStrictly: true,

      treeData2: [],
      defaultCheckedKeys2: [],
      selectedKeys2: [],
      checkedKeys2: [],
      expandedKeysss2: [],
      allTreeKeys2: [],
      autoExpandParent2: true,
      checkStrictly2: true,
    }
  },
  methods: {
    onTreeNodeSelect(id) {
      if (id && id.length > 0) {
        this.selectedKeys = id
      }
      this.$refs.datarule.show(this.selectedKeys[0], this.roleId)
    },
    onTreeNodeSelect2(id) {
      if (id && id.length > 0) {
        this.selectedKeys2 = id
      }
      this.$refs.datarule.show(this.selectedKeys2[0], this.roleId)
    },
    onCheck(o) {
      if (this.checkStrictly) {
        this.checkedKeys = o.checked
      } else {
        this.checkedKeys = o
      }
    },
    onCheck2(o) {
      if (this.checkStrictly2) {
        this.checkedKeys2 = o.checked
      } else {
        this.checkedKeys2 = o
      }
    },
    show(roleId) {
      this.roleId = roleId
      this.visible = true
    },
    close() {
      this.reset()
      this.$emit('close')
      this.visible = false
    },
    onExpand(expandedKeys) {
      this.expandedKeysss = expandedKeys
      this.autoExpandParent = false
    },
    onExpand2(expandedKeys) {
      this.expandedKeysss2 = expandedKeys
      this.autoExpandParent2 = false
    },
    reset() {
      this.expandedKeysss = []
      this.expandedKeysss2 = []
      this.checkedKeys = []
      this.checkedKeys2 = []
      this.defaultCheckedKeys = []
      this.defaultCheckedKeys2 = []
      this.loading = false
    },
    expandAll() {
      let activeKey = this.activeKey
      if (activeKey === '1') {
        this.expandedKeysss = this.allTreeKeys
      } else if (activeKey === '2') {
        this.expandedKeysss2 = this.allTreeKeys2
      }
    },
    closeAll() {
      let activeKey = this.activeKey
      if (activeKey === '1') {
        this.expandedKeysss = []
      } else if (activeKey === '2') {
        this.expandedKeysss2 = []
      }
    },
    checkALL() {
      let activeKey = this.activeKey
      if (activeKey === '1') {
        this.checkedKeys = this.allTreeKeys
      } else if (activeKey === '2') {
        this.checkedKeys2 = this.allTreeKeys2
      }
    },
    cancelCheckALL() {
      let activeKey = this.activeKey
      if (activeKey === '1') {
        this.checkedKeys = []
      } else if (activeKey === '2') {
        this.checkedKeys2 = []
      }
    },
    switchCheckStrictly(v) {
      let activeKey = this.activeKey
      if (v === 1) {
        if (activeKey === '1') {
          this.checkStrictly = false
        } else if (activeKey === '2') {
          this.checkStrictly2 = false
        }
      } else if (v === 2) {
        if (activeKey === '1') {
          this.checkStrictly = true
        } else if (activeKey === '2') {
          this.checkStrictly2 = true
        }
      }
    },
    handleCancel() {
      this.close()
    },
    handleSubmit(exit) {
      let that = this
      let params = {
        roleId: that.roleId,
        permissionIds: that.checkedKeys.join(','),
        lastpermissionIds: that.defaultCheckedKeys.join(','),
        permissionIds2: that.checkedKeys2.join(','),
        lastpermissionIds2: that.defaultCheckedKeys2.join(',')
      }
      that.loading = true
      saveRolePermission(params).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loading = false
          if (exit) {
            that.close()
          }
        } else {
          that.$message.error(res.message)
          that.loading = false
          if (exit) {
            that.close()
          }
        }
        this.loadData()
      })
    },
    loadData() {
      queryTreeListForRole().then((res) => {
        this.treeData = res.result.treeList
        this.treeData2 = res.result.treeList2
        this.allTreeKeys = res.result.ids
        this.allTreeKeys2 = res.result.ids2
        queryRolePermission({ roleId: this.roleId }).then((res) => {
          this.checkedKeys = [...res.result.permissionIds]
          this.defaultCheckedKeys = [...res.result.permissionIds]
          this.checkedKeys2 = [...res.result.dataPermissionIds]
          this.defaultCheckedKeys2 = [...res.result.dataPermissionIds]
          //this.expandedKeysss = this.allTreeKeys 注释为了加载数据后,控件不展开
        })
      })

      /*queryDataTreeListForRole().then((res) => {
        this.treeData2 = res.result.treeList
        this.allTreeKeys2 = res.result.ids
        queryRoleDataPermission({ roleId: this.roleId }).then((res) => {
          this.checkedKeys2 = [...res.result]
          this.defaultCheckedKeys2 = [...res.result]
          //this.expandedKeysss = this.allTreeKeys2 注释为了加载数据后,控件不展开
        })
      })*/
    }
  },
  watch: {
    visible() {
      if (this.visible) {
        this.loadData()
      }
    }
  }
}

</script>
<style lang="less" scoped>
.drawer-bootom-button {
  position: absolute;
  bottom: 0;
  width: 100%;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: right;
  left: 0;
  background: #fff;
  border-radius: 0 0 2px 2px;
}

</style>