<template>
<el-dialog :visible.sync="dialogFormVisible" :title="titleText" width="330px" append-to-body>
<el-scrollbar>
<el-tree
v-loading="loading"
ref="tree"
:props="defaultProps"
:data="resourceTreeList"
:default-expanded-keys="defaultExpanded"
:default-checked-keys="defaultChecked"
show-checkbox
node-key="id"
@check-change="handleCheckChange"/>
</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 { getDeptTreeByRole } from '@/api/dept'
import { dataAuthor } from '@/api/role'
export default {
name: 'DataPerm',
data() {
return {
roleId: '', // 角色id
roleName: '', // 角色名称
dialogFormVisible: false, // 对话框是否显示
defaultProps: {
label: 'name',
children: 'children'
},
resourceTreeList: null, // 资源树列表数据
defaultExpanded: [], // 默认展开的项
defaultChecked: [], // 默认选中的树
loading: false // 加载动态效果
}
},
computed: {
titleText: function() {
return this.roleName + ''
}
},
mounted: function() {
// this.fetchResourceData()
},
methods: {
// 初始化对话框
initDialog: function(dialogFormVisible, row) {
this.loading = true
this.roleId = row.id
this.roleName = row.name
this.dialogFormVisible = dialogFormVisible
this.fetchResourceTree()
},
// 获取资源列表
fetchResourceTree: function() {
console.log('fetchResourceTree')
getDeptTreeByRole(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()
dataAuthor(this.roleId, this.ids).then(response => {
if (response.code === 200) {
this.$message.success('数据权限配置成功')
this.dialogFormVisible = false
}
})
console.log('授权成功')
},
handleCheckChange() {
console.log('handleCheckChange')
}
}
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.el-dialog{
min-width:350px !important;
}
.el-tree{
max-height:300px;
}
.el-select{
width: 100%;
}
</style>