Newer
Older
newBaseFront / src / views / system / role / functionPerm.vue
StephanieGitHub on 12 Aug 2021 2 KB MOD: vue-cli2代码迁移
<template>
  <el-dialog v-loading="dialogLoading" :visible.sync="dialogFormVisible" :title="titleText" width="330px" append-to-body>
    <el-scrollbar>
      <el-tree
        ref="tree"
        v-loading="loading"
        :props="defaultProps"
        :data="resourceTreeList"
        :default-expanded-keys="defaultExpanded"
        :default-checked-keys="defaultChecked"
        check-strictly
        show-checkbox
        node-key="id"
      />
    </el-scrollbar>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">
        保存
      </el-button>
      <el-button @click="dialogFormVisible = false">
        取消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { toTreeList, getShowItem } from '@/utils/structure'
import { getResourceListByRole } from '@/api/system/resource'
import { funcAuthor } from '@/api/system/role'

export default {
  name: 'FunctionPerm',
  data() {
    return {
      roleName: '', // 角色名称
      roleId: '', // 角色id
      dialogFormVisible: false, // 对话框是否显示
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      resourceTreeList: [], // 资源树列表数据
      defaultExpanded: [], // 默认展开的项
      defaultChecked: [], // 默认选中的树
      ids: [],
      loading: false, // 加载动态效果
      dialogLoading: false
    }
  },
  computed: {
    titleText: function() {
      return this.roleName + ''
    }
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogFormVisible, row) {
      this.loading = true
      this.roleName = row.name
      this.roleId = row.id
      this.dialogFormVisible = dialogFormVisible
      this.fetchResourceTree()
    },
    // 获取资源列表
    fetchResourceTree: function() {
      getResourceListByRole(this.roleId).then(response => {
        if (response.data.list) {
          this.resourceTreeList = toTreeList(response.data.list)
          const temp = getShowItem(response.data.list) // 获取展开项和选中项
          this.defaultExpanded = temp[0]
          this.defaultChecked = temp[1]
          this.loading = false
        }
      })
    },
    // 保存数据
    saveData: function() {
      this.ids = this.$refs.tree.getCheckedKeys()
      this.dialogLoading = true
      funcAuthor(this.roleId, this.ids).then(response => {
        if (response.code === 200) {
          this.dialogLoading = false
          this.$message.success('权限配置成功')
          this.dialogFormVisible = false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

  .el-dialog{
    min-width:350px !important;
  }
  .el-tree{
    max-height:300px;
  }
  .el-select{
    width: 100%;
  }
</style>