Commit 44f0d35f authored by 徐一鸣's avatar 徐一鸣

分配角色fixed

parent 7aa0cb63
...@@ -16,9 +16,9 @@ ...@@ -16,9 +16,9 @@
</el-button> </el-button>
</template> </template>
<div class="apass_table" slot="list"> <div class="apass_table" slot="list">
<el-table class="user_roles_table" :border="false" :data="roleData"> <el-table class="user_roles_table" :border="false" :data="roleList">
<el-table-column width="50" align="right"></el-table-column> <el-table-column width="50" align="right"></el-table-column>
<el-table-column> <el-table-column width="300">
<template slot="header"> <template slot="header">
<span style="margin-left: 25px;">角色</span> <span style="margin-left: 25px;">角色</span>
</template> </template>
...@@ -50,13 +50,14 @@ export default { ...@@ -50,13 +50,14 @@ export default {
components: { apassList }, components: { apassList },
data: () => ({ data: () => ({
roleTotal: 0, roleTotal: 0,
roleData: [], roleList: [],
showDialog: false, showDialog: false,
userRoles: null,
roles: null, roles: null,
}), }),
computed: { computed: {
roleIds() { roleIds() {
return this.roleData.map((item) => item.role_id); return this.roleList.map((item) => item.role_id);
}, },
}, },
methods: { methods: {
...@@ -65,13 +66,13 @@ export default { ...@@ -65,13 +66,13 @@ export default {
if (this.roles === null) { if (this.roles === null) {
this.getUserRoles(() => { this.getUserRoles(() => {
this.getRoles(filters); this.getRoleList(filters);
}); });
} else { } else {
this.getRoles(filters); this.getRoleList(filters);
} }
}, },
getRoles(filters) { getRoleList(filters) {
this.$http this.$http
.get(`/apaas/backmgt/role/list`, { .get(`/apaas/backmgt/role/list`, {
params: { params: {
...@@ -86,20 +87,22 @@ export default { ...@@ -86,20 +87,22 @@ export default {
}); });
this.roleTotal = data.total; this.roleTotal = data.total;
this.roleData = data.data; this.roleList = data.data;
}) })
.catch((error) => { .catch((error) => {
console.log(error); console.log(error);
}); });
}, },
getUserRoles(callback) { getUserRoles(cb) {
this.$http this.$http
.get(`/apaas/backmgt/user/roles/${this.$route.params.id}`) .get(`/apaas/backmgt/user/roles/${this.$route.params.id}`)
.then(({ data }) => { .then(({ data }) => {
this.roles = data.data.map((item) => item.role_id); let userRoles = data.data.map((item) => item.role_id);
// console.log(this.roles);
this.userRoles = userRoles;
this.roles = userRoles;
typeof callback === "function" && callback(); typeof cb === "function" && cb();
}) })
.catch((error) => { .catch((error) => {
console.log(error); console.log(error);
...@@ -111,7 +114,7 @@ export default { ...@@ -111,7 +114,7 @@ export default {
return this.roleIds.indexOf(item) === -1; return this.roleIds.indexOf(item) === -1;
}) })
.concat( .concat(
this.roleData this.roleList
.filter((item) => { .filter((item) => {
return item.selected; return item.selected;
}) })
...@@ -123,18 +126,48 @@ export default { ...@@ -123,18 +126,48 @@ export default {
// console.log(this.roles); // console.log(this.roles);
}, },
setUserRoles() { setUserRoles() {
if (this.roles.length > 0) { let add = [];
this.$http let remove = [];
.post(`/apaas/backmgt/user/addRole`, {
this.roles.forEach((item) => {
if (this.userRoles.indexOf(item) === -1) {
add.push(item);
}
});
this.userRoles.forEach((item) => {
if (this.roles.indexOf(item) === -1) {
remove.push(item);
}
});
let requests = [];
if (add.length > 0) {
requests.push(
this.$http.post(`/apaas/backmgt/user/addRole`, {
user_id: this.$route.params.id,
role_id: add,
})
);
}
if (remove.length > 0) {
requests.push(
this.$http.post(`/apaas/backmgt/role/userDel`, {
user_id: this.$route.params.id, user_id: this.$route.params.id,
role_id: this.roles, role_id: remove,
}) })
);
}
if (requests.length > 0) {
Promise.all(requests)
.then(({ data }) => { .then(({ data }) => {
this.$message({ this.$message({
message: `分配角色成功.`, message: `分配角色成功.`,
type: "success", type: "success",
}); });
this.$router.push("/authority/users");
}) })
.catch((error) => { .catch((error) => {
this.$message({ this.$message({
...@@ -144,10 +177,12 @@ export default { ...@@ -144,10 +177,12 @@ export default {
}); });
} else { } else {
this.$message({ this.$message({
message: "用户角色不能为空!", message: "您没有进行任何修改.",
type: "warning", type: "warning",
}); });
} }
// this.$router.push("/authority/users");
}, },
}, },
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment