<template>
<div class="dashboard-container">
<div class="app-container">
<el-card>
<el-tabs>
<el-tab-pane label="角色管理">
<el-row style="height: 60px">
<el-button
v-if="checkPermBtn('add-roles')"
size="small"
type="primary"
@click="addRoleFn"
>新增角色</el-button>
</el-row>
<el-table border :data="tableData">
<el-table-column type="index" label="序号" width="120" />
<el-table-column label="角色名称" width="240" prop="name" />
<el-table-column label="描述" prop="description" />
<el-table-column label="操作">
<template v-slot="{row}">
<el-button size="small" type="success" @click="assignPermFn(row.id)">分配权限</el-button>
<el-button v-if="checkPermBtn('edit-roles')" size="small" type="primary" @click="editFn(row.id)">编辑</el-button>
<el-button v-if="checkPermBtn('del-roles')" size="small" type="danger" @click="delFn(row.id)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-row
type="flex"
justify="center"
align="middle"
style="height: 60px"
>
<el-pagination
layout="prev, pager, next"
:page-size="pagesize"
:total="100"
@current-change="currentChangeFn"
/>
</el-row>
<el-dialog title="新增角色" :visible="showDialog" width="70%" @click="closeFn">
<el-form ref="form" :model="formData" :rules="rules" label-width="120px">
<el-form-item label="角色名称" prop="name">
<el-input v-model="formData.name" />
</el-form-item>
<el-form-item label="角色描述" prop="description">
<el-input v-model="formData.description" />
</el-form-item>
</el-form>
<span slot="footer">
<el-button @click="closeFn">取消</el-button>
<el-button type="primary" @click="confirmFn">确定</el-button>
</span>
</el-dialog>
</el-tab-pane>
<el-tab-pane label="公司信息">
<el-form label-width="120px" style="margin-top: 50px">
<el-form-item label="公司名称">
<el-input v-model="company.name" disabled style="width: 400px" />
</el-form-item>
<el-form-item label="公司地址">
<el-input v-model="company.companyAddress" disabled style="width: 400px" />
</el-form-item>
<el-form-item label="邮箱">
<el-input v-model="company.mailbox" disabled style="width: 400px" />
</el-form-item>
<el-form-item label="备注">
<el-input
v-model="company.remarks"
type="textarea"
:rows="3"
disabled
style="width: 400px"
/>
</el-form-item>
</el-form>
</el-tab-pane>
</el-tabs>
</el-card>
</div>
<el-dialog title="分配权限" :visible="visibleDialog">
<el-tree
ref="tree"
:data="permList"
:props="{label: 'name'}"
default-expand-all
show-checkbox
node-key="id"
:check-strictly="true"
/>
<template #footer>
<el-row type="flex" justify="center">
<el-button type="primary" size="small" @click="confirmAssignPermissionFn">确定</el-button>
<el-button size="small" @click="closePermFn">取消</el-button>
</el-row>
</template>
</el-dialog>
</div>
</template>
<script>
import { getRoleList, addRoleList, deleteRole, getRoleDetailById, updateRole, getCompany } from '@/api/setting'
import { permission } from '@/api/permission'
import { listToTreeData } from '@/utils/index'
import { assignperm } from '@/api/setting'
export default {
data() {
return {
checkPermList: [],
visibleDialog: false,
tableData: [],
total: 0,
page: 1,
pagesize: 10,
showDialog: false,
company: {},
permList: [],
curId: '',
formData: {
name: '',
description: ''
},
rules: {
name: [{
required: true, message: '角色名称不能为空!', trigger: 'blur'
}, {
min: 2, max: 10, message: '角色名称长度2到50', trigger: 'blur'
}],
description: [{
required: true, message: '角色描述不能为空!', trigger: 'blur'
}, {
min: 2, max: 10, message: '角色描述长度2到50', trigger: 'blur'
}]
}
}
},
async created() {
this.getRoleList()
const id = this.$store.state.user.userInfo.companyId
this.company = await getCompany(id)
const res = await permission()
this.permList = listToTreeData(res, '0')
},
methods: {
async confirmAssignPermissionFn() {
this.checkPermList = this.$refs.tree.getCheckedKeys()
await assignperm({
id: this.curId,
permIds: this.checkPermList
})
this.closePermFn()
},
closePermFn() {
this.visibleDialog = false
this.$refs.tree.getCheckedKeys([])
},
async assignPermFn(id) {
this.curId = id
this.visibleDialog = true
const res = await getRoleDetailById(id)
this.checkPermList = res.permIds
this.$refs.tree.setCheckedKeys(this.checkPermList)
},
addRoleFn() {
this.showDialog = true
},
closeFn() {
this.formData = {
name: '',
description: ''
}
this.$refs.form.resetFields()
this.showDialog = false
},
async confirmFn() {
await this.$refs.form.validate()
if (this.formData.id) {
await updateRole(this.formData)
} else {
await addRoleList(this.formData)
}
this.getRoleList()
this.closeFn()
},
async getRoleList() {
const res = await getRoleList({
page: this.page,
pagesize: this.pagesize
})
this.tableData = res.rows
this.total = res.total
},
currentChangeFn(page) {
this.page = page
this.getRoleList()
},
delFn(id) {
console.log(id)
this.$confirm('是否确定删除?', '删除', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(async() => {
await deleteRole(id)
this.getRoleList()
}).catch(() => {
console.log('取消删除')
})
},
async editFn(id) {
this.showDialog = true
this.formData = await getRoleDetailById(id)
}
}
}
</script>
<style>
</style>