<template>
|
<el-dialog title="编辑用户权限" :close-on-click-modal="false" :modal-append-to-body="false"
|
:visible.sync="visible">
|
<div class="userAuthority-form">
|
<el-form ref="ruleForm" size="mini" label-width="100px" class="demo-ruleForm">
|
<el-form-item label="姓名:" prop="userName">
|
<el-input :disabled="true" v-model="info.userName"></el-input>
|
</el-form-item>
|
<el-form-item label="用户账号:" prop="userAccount">
|
<el-input autocomplete="off" :disabled="true" v-model="info.userAccount">
|
</el-input>
|
</el-form-item>
|
<el-form-item label="权限配置:" prop="roles">
|
<el-checkbox :indeterminate="isIndeterminate" v-model="checkAll"
|
@change="handleCheckAllChange">全选</el-checkbox>
|
<el-checkbox-group v-model="valueCheckedAry" @change="handlecheckedRolesChange">
|
<el-row>
|
<el-col :span="8" v-for="item in roleList" :key="item.id">
|
<el-checkbox class="checkbox" :label="item.id" :key="item.id">
|
{{item.name}}</el-checkbox>
|
</el-col>
|
</el-row>
|
</el-checkbox-group>
|
</el-form-item>
|
</el-form>
|
</div>
|
<div slot="footer" class="dialog-footer text-center">
|
<el-button type="primary" @click="submitForm" size="mini">确 定
|
</el-button>
|
<el-button size="mini" @click="cancel">取 消</el-button>
|
</div>
|
</el-dialog>
|
</template>
|
|
<script>
|
import { mapState } from 'vuex'
|
import { sysRoleIds, bindUserRole } from '../../api/userAuthority'
|
export default {
|
name: "newInfo",
|
props: {
|
// 显示隐藏
|
visible: {
|
type: Boolean,
|
required: true
|
},
|
// 角色相关信息
|
info: {
|
type: Object,
|
required: true
|
},
|
},
|
computed: {
|
// 获取登录信息
|
...mapState(['userInfo'])
|
},
|
data() {
|
return {
|
isIndeterminate: false,
|
// 管理员列表
|
roleList: [],
|
// 全部选中的val的列表
|
allValAry: [],
|
// 设置 indeterminate 状态,只负责样式控制: true-显示正方形,false-显示钩或者空(非正方形的)
|
indeterminate: false,
|
// 指定当前是否选中
|
checkAll: false,
|
// 选中的角色数组
|
valueCheckedAry: [],
|
}
|
},
|
watch: {
|
visible(newV) {
|
if (newV) {
|
// 回显当前角色之前选中的角色
|
this.valueCheckedAry = this.info.roles ? this.info.roles : []
|
// 去回显样式
|
this.handlecheckedRolesChange()
|
}
|
}
|
},
|
mounted() {
|
// 获取角色列表
|
this.getRoleList()
|
},
|
methods: {
|
/**
|
* 获取角色列表
|
*/
|
async getRoleList() {
|
let params = {
|
userId: this.userInfo.userId,
|
systemId: process.env.VUE_APP_CLIENT_ID
|
}
|
const res = await sysRoleIds(params)
|
if (res.code === '200' && res.data) {
|
this.roleList = [...res.data.roleAll]
|
// 全部选中的ids集合
|
const idsList = []
|
res.data.roleAll.forEach(item => {
|
idsList.push(item.id)
|
})
|
this.allValAry = idsList
|
}
|
},
|
/**
|
* 全选改变
|
*/
|
handleCheckAllChange(val) {
|
Object.assign(this, {
|
valueCheckedAry: val ? this.allValAry : [],
|
indeterminate: false,
|
checkAll: val,
|
});
|
},
|
/**
|
* 多选框改变
|
* @params:
|
* checkedValues:选中的角色
|
*/
|
handlecheckedRolesChange() {
|
this.indeterminate = !!this.valueCheckedAry.length && this.valueCheckedAry.length < this.roleList.length;
|
this.checkAll = this.valueCheckedAry.length === this.roleList.length;
|
},
|
/**
|
* 提交
|
* @params:
|
*/
|
async submitForm() {
|
if (!this.valueCheckedAry || !this.valueCheckedAry.length) {
|
this.$message.error('请选择角色!')
|
return false
|
}
|
const res = await bindUserRole({
|
roleCodes: this.valueCheckedAry, // 角色IDS
|
userId: this.info.id, // 用户ID
|
systemId: process.env.VUE_APP_CLIENT_ID
|
})
|
if (res.code == 200) {
|
this.$message.success('角色设置成功!')
|
this.$emit('ok')
|
// 还原
|
this.restoreSettings()
|
} else {
|
this.$message.error(res.data.message)
|
}
|
},
|
/**
|
* 还原最初设置
|
*/
|
restoreSettings() {
|
this.indeterminate = false
|
this.checkAll = false
|
this.valueCheckedAry = []
|
},
|
/**
|
* 关闭弹窗
|
*/
|
cancel() {
|
this.restoreSettings();
|
this.$emit("cancel")
|
}
|
}
|
}
|
</script>
|
|
<style>
|
</style>
|