160 lines
5.4 KiB
Vue
Raw Normal View History

2022-08-02 10:16:07 +08:00
<template>
2022-09-01 18:49:41 +08:00
<div class="conent">
<!-- 点击新增弹出表单 -->
<div class="from">
<el-dialog title="新增角色" :visible.sync="dialogVisible2" width="30%" :close-on-click-modal="false">
<div>
<el-form label-width="80px">
<el-form-item label="角色名称">
<el-input v-model="newrole"></el-input>
</el-form-item>
<div class="from-btn">
<el-button type="danger" @click="dialogVisible2 = false">取消</el-button>
<el-button @click="onSubmit">确认</el-button>
</div>
</el-form>
</div>
</el-dialog>
</div>
2022-08-02 10:16:07 +08:00
2022-09-01 18:49:41 +08:00
<!-- 新增按钮 -->
<el-button type="primary" @click="dialogVisible2 = true">新增</el-button>
<!-- 角色列表 -->
2022-09-08 01:02:07 +08:00
<div class="table" style="margin-top: 10px">
2022-09-01 18:49:41 +08:00
<el-table :data="tableList" style="width: 100%">
<el-table-column prop="id" label="ID"> </el-table-column>
<el-table-column prop="name" label="角色名称"> </el-table-column>
<el-table-column label="权限内容">
<template slot-scope="scope">
<span v-for="item in scope.row.permissions" :key="item.id">{{ item.name }}&nbsp;</span>
</template>
</el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button @click="onEdit(scope.row)">编辑</el-button>
</template>
</el-table-column>
</el-table>
2022-08-06 20:03:35 +08:00
</div>
2022-09-01 18:49:41 +08:00
<!-- 点击编辑弹出权限框 -->
<el-dialog title="编辑权限" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false">
<el-tree v-if="isShowtree" :data="Role_Permission" show-checkbox :default-expand-all="false" node-key="id"
ref="tree" highlight-current :props="defaultProps" :default-checked-keys="chekedKeys"
v-model="permissionIds">
</el-tree>
<span slot="footer" class="dialog-footer">
<el-button @click="cancel()"> </el-button>
<el-button type="primary" @click="getCheckedKeys()"> </el-button>
</span>
</el-dialog>
2022-08-02 20:19:15 +08:00
</div>
2022-08-02 10:16:07 +08:00
</template>
<script>
2022-09-01 18:49:41 +08:00
import {
2022-08-25 11:21:14 +08:00
roleList,
roleAdd,
jurisdiction,
jurisdictionEdit,
2022-09-01 18:49:41 +08:00
} from "../../api/role.js";
export default {
2022-08-25 11:21:14 +08:00
data() {
2022-09-01 18:49:41 +08:00
return {
id: "", //点击角色的id
tableList: [], //列表数据
rolePermissions: [],
newrole: "", //添加角色框input
dialogVisible: false, //编辑按钮打开表单变量
dialogVisible2: false, //新增按钮打开表单变量
permissionIds: [], //复选框选中权限的ID
allpermissionIds: {}, //id数组添加进对象
Role_Permission: [], //权限列表数据
defaultProps: {
children: "children",
label: "name",
},
isShowtree: false,
chekedKeys: [],
};
2022-08-06 20:03:35 +08:00
},
2022-08-25 11:21:14 +08:00
mounted() {
2022-09-01 18:49:41 +08:00
this.getroleList();
this.getJurisdiction();
2022-08-15 19:55:44 +08:00
},
2022-08-25 11:21:14 +08:00
methods: {
2022-09-01 18:49:41 +08:00
//角色表单数据请求
getroleList() {
roleList().then((res) => {
this.tableList = res.data.data;
});
},
2022-08-06 20:03:35 +08:00
2022-09-01 18:49:41 +08:00
//角色权限数据请求
getJurisdiction() {
jurisdiction().then((res) => {
this.Role_Permission = res.data.data;
2022-08-25 11:21:14 +08:00
});
2022-09-01 18:49:41 +08:00
},
2022-08-25 11:21:14 +08:00
2022-09-01 18:49:41 +08:00
// 点击新增
onSubmit() {
let roleName = {
name: this.newrole,
};
roleAdd(roleName).then((res) => {
this.dialogVisible2 = false;
this.getroleList();
if (res.status == 201) {
this.$message({
message: "角色添加成功!",
type: "success",
});
}
});
},
2022-08-25 11:21:14 +08:00
2022-09-01 18:49:41 +08:00
// 点击编辑
onEdit(row) {
this.id = row.id;
let arr = row.permissions.map((item) => {
return item.id;
});
this.chekedKeys = arr;
this.dialogVisible = true;
this.isShowtree = true;
},
2022-08-25 11:21:14 +08:00
2022-09-01 18:49:41 +08:00
// 点击取消
cancel() {
2022-08-25 11:21:14 +08:00
this.dialogVisible = false;
2022-09-01 18:49:41 +08:00
this.isShowtree = false;
},
//编辑权限确认
getCheckedKeys() {
let id = this.id; //选中角色的ID
this.permissionIds = this.$refs.tree.getCheckedKeys(); //选中权限的ID
this.allpermissionIds = {
permissionIds: this.permissionIds,
};
jurisdictionEdit(id, this.allpermissionIds).then((res) => {
if (res.status === 200) {
this.dialogVisible = false;
this.getroleList();
this.$message({
message: "权限修改成功!",
type: "success",
});
}
2022-08-25 11:21:14 +08:00
});
2022-09-01 18:49:41 +08:00
},
2022-08-06 20:03:35 +08:00
},
2022-09-01 18:49:41 +08:00
};
2022-08-02 10:16:07 +08:00
</script>
<style lang="scss" scoped>
2022-09-01 18:49:41 +08:00
</style>