role_privilege_settings.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <template>
  2. <section class="content">
  3. <div class="box box-info">
  4. <div class="box-body">
  5. <!-- 选择 -->
  6. <el-form :inline="true" :model="form" label-position="right">
  7. <el-row>
  8. <el-form-item label="学校" class="pull-left">
  9. <el-select
  10. class="input_width_lg"
  11. v-model="form.orgId"
  12. placeholder="请选择"
  13. @change="rootOrgChanged"
  14. :disabled="!isSuperAdmin"
  15. >
  16. <el-option
  17. v-for="item in orgList"
  18. :label="item.name"
  19. :value="item.id"
  20. :key="item.id"
  21. />
  22. </el-select>
  23. </el-form-item>
  24. <el-form-item label="角色" class="pull-left">
  25. <el-select
  26. class="input_width_lg"
  27. v-model="form.roleId"
  28. placeholder="请选择"
  29. @change="change"
  30. >
  31. <el-option
  32. v-for="item in roleList"
  33. :label="item.roleName"
  34. :value="item.roleId"
  35. :key="item.roleId"
  36. />
  37. </el-select>
  38. </el-form-item>
  39. <el-form-item label="权限组" class="pull-left">
  40. <el-select
  41. class="input_width_lg"
  42. v-model="form.privilegeGroupId"
  43. placeholder="请选择"
  44. @change="change"
  45. >
  46. <el-option
  47. v-for="item in privilegeGroupList"
  48. :label="item.name"
  49. :value="item.id"
  50. :key="item.id"
  51. />
  52. </el-select>
  53. </el-form-item>
  54. </el-row>
  55. </el-form>
  56. <div style="margin-bottom:10px;">
  57. <el-button type="primary" :disabled="!treeChanged" @click="save">
  58. 保 存
  59. </el-button>
  60. </div>
  61. <!-- 权限树 -->
  62. <div style="width: 50%;">
  63. <el-tree
  64. class="el-tree"
  65. :data="treeData"
  66. :props="defaultProps"
  67. show-checkbox
  68. node-key="id"
  69. ref="tree"
  70. highlight-current
  71. :check-strictly="true"
  72. :default-expanded-keys="checkedKeys"
  73. :default-checked-keys="checkedKeys"
  74. @check="nodeCheck"
  75. :expand-on-click-node="true"
  76. />
  77. </div>
  78. </div>
  79. </div>
  80. </section>
  81. </template>
  82. <script>
  83. import { mapState } from "vuex";
  84. import { CORE_API } from "@/constants/constants.js";
  85. export default {
  86. name: "RolePrivilegeSettings",
  87. data() {
  88. return {
  89. form: {
  90. orgId: null,
  91. roleId: null,
  92. privilegeGroupId: null
  93. },
  94. orgList: [],
  95. roleList: [],
  96. privilegeGroupList: [],
  97. treeChanged: false,
  98. treeData: [],
  99. checkedKeys: [],
  100. uncheckChildren_nodeId: null,
  101. defaultProps: {
  102. children: "children",
  103. label: "label"
  104. }
  105. };
  106. },
  107. computed: {
  108. ...mapState({ user: state => state.user }),
  109. isSuperAdmin() {
  110. return this.user.roleList.some(role => role.roleCode == "SUPER_ADMIN");
  111. }
  112. },
  113. methods: {
  114. /*初始化*/
  115. init() {
  116. var url1 = CORE_API + "/org/getRootOrgList";
  117. var url2 =
  118. CORE_API + "/rolePrivilege/getRoles?includeSuperAdmin=" + false;
  119. var url3 = CORE_API + "/rolePrivilege/getPrivilegeGroupList";
  120. Promise.all([
  121. this.$httpWithMsg.get(url1),
  122. this.$httpWithMsg.post(url2),
  123. this.$httpWithMsg.get(url3)
  124. ]).then(([resp1, resp2, resp3]) => {
  125. this.orgList = resp1.data;
  126. this.form.orgId = this.user.rootOrgId;
  127. this.roleList = resp2.data;
  128. if (0 < this.roleList.length) {
  129. this.form.roleId = this.roleList[0].roleId;
  130. }
  131. this.privilegeGroupList = resp3.data;
  132. if (0 < this.privilegeGroupList.length) {
  133. this.form.privilegeGroupId = this.privilegeGroupList[0].id;
  134. }
  135. this.initTree(
  136. this.form.orgId,
  137. this.form.roleId,
  138. this.form.privilegeGroupId
  139. );
  140. });
  141. },
  142. /*初始化权限树*/
  143. initTree(orgId, roleId, privilegeGroupId) {
  144. var url1 =
  145. CORE_API + "/rolePrivilege/getPrivilegeTree/" + privilegeGroupId;
  146. var url2 =
  147. CORE_API + "/rolePrivilege/getPrivilegeIdList/" + orgId + "/" + roleId;
  148. Promise.all([
  149. this.$httpWithMsg.get(url1),
  150. this.$httpWithMsg.get(url2)
  151. ]).then(([resp1, resp2]) => {
  152. console.log("initTree(). treeData:", resp1.data.children);
  153. console.log("initTree(). checkedKeys:", resp2.data);
  154. this.treeData = resp1.data.children;
  155. this.checkedKeys = resp2.data;
  156. });
  157. },
  158. /*change事件*/
  159. change() {
  160. this.initTree(
  161. this.form.orgId,
  162. this.form.roleId,
  163. this.form.privilegeGroupId
  164. );
  165. },
  166. rootOrgChanged() {
  167. var url =
  168. CORE_API +
  169. "/rolePrivilege/getRoles?includeSuperAdmin=false&rootOrgId=" +
  170. this.form.orgId;
  171. this.$httpWithMsg.post(url).then(response => {
  172. this.roleList = response.data;
  173. if (0 < this.roleList.length) {
  174. this.form.roleId = this.roleList[0].roleId;
  175. }
  176. this.initTree(
  177. this.form.orgId,
  178. this.form.roleId,
  179. this.form.privilegeGroupId
  180. );
  181. });
  182. },
  183. nodeCheck({ id, parentId, children }, { checkedKeys }) {
  184. // console.log("[tree change] node:", node);
  185. const checked = checkedKeys.includes(id);
  186. // 当前node的状态
  187. if (checked) {
  188. this.checkedKeys = [...this.checkedKeys, id];
  189. } else {
  190. this.checkedKeys = this.checkedKeys.filter(id0 => id0 !== id);
  191. }
  192. // 选中状态下对子节点的影响:递归选中所有子孙节点
  193. if (checked && children) {
  194. this.checkedKeys = [...this.checkedKeys, ...children.map(v => v.id)];
  195. for (const child of children) {
  196. this.checkedKeys = [...this.checkedKeys, child.id];
  197. for (const child of child.children || []) {
  198. // 树最多只有三个层级,这里就不写递归了
  199. this.checkedKeys = [...this.checkedKeys, child.id];
  200. }
  201. }
  202. }
  203. // 选中状态下对父节点的影响:递归选中所有父辈节点
  204. if (checked && parentId) {
  205. this.checkedKeys = [...this.checkedKeys, parentId];
  206. const parent1 = this.$refs.tree.getNode(parentId).data;
  207. // this.$refs.tree.setChecked(parentId, true, false);
  208. if (parent1.parentId) {
  209. // 第二个父节点
  210. this.checkedKeys = [...this.checkedKeys, parent1.parentId];
  211. // this.$refs.tree.setChecked(parent1.parentId, true, false);
  212. }
  213. }
  214. // 取消选中状态下对子节点的影响:递归取消选中所有子孙节点
  215. if (!checked && children) {
  216. this.checkedKeys = this.checkedKeys.filter(id0 => id0 !== id);
  217. // console.log(this.checkedKeys);
  218. for (const child of children) {
  219. this.checkedKeys = this.checkedKeys.filter(id => id !== child.id);
  220. // console.log(this.checkedKeys);
  221. for (const child of child.children || []) {
  222. // 树最多只有三个层级,这里就不写递归了
  223. this.checkedKeys = this.checkedKeys.filter(id => id !== child.id);
  224. }
  225. }
  226. }
  227. this.checkedKeys = [...new Set(this.checkedKeys)];
  228. this.$refs.tree.setCheckedKeys(this.checkedKeys);
  229. // 取消选中状态下对父节点的影响:无影响
  230. // if (checked && !this.checkedKeys.includes(node.id)) {
  231. // this.checkedKeys.push(node.id);
  232. // }
  233. // if (!checked && this.checkedKeys.includes(node.id)) {
  234. // this.checkedKeys = this.checkedKeys.filter(v => v != node.id);
  235. // }
  236. // var checkChildren = this.uncheckChildren_nodeId != node.id;
  237. // this.uncheckChildren_nodeId = null;
  238. // if (checked) {
  239. // if (node.parentId) {
  240. // this.uncheckChildren_nodeId = node.parentId;
  241. // this.$refs.tree.setChecked(node.parentId, true, false);
  242. // }
  243. // if (checkChildren) {
  244. // if (node.children && 0 < node.children.length) {
  245. // for (const cur of node.children) {
  246. // this.$refs.tree.setChecked(cur.id, true, false);
  247. // }
  248. // }
  249. // }
  250. // } else {
  251. // if (node.children && 0 < node.children.length) {
  252. // for (const cur of node.children) {
  253. // this.$refs.tree.setChecked(cur.id, false, false);
  254. // }
  255. // }
  256. // }
  257. this.treeChanged = true;
  258. },
  259. save() {
  260. console.log("save(). checkedKeys:", this.checkedKeys);
  261. var url = CORE_API + "/rolePrivilege/updateRolePrivilegeRelations";
  262. this.$httpWithMsg
  263. .post(url, {
  264. rootOrgId: this.form.orgId,
  265. roleId: this.form.roleId,
  266. privilegeGroupId: this.form.privilegeGroupId,
  267. privilegeIdSet: this.checkedKeys
  268. })
  269. .then(() => {
  270. this.$notify({
  271. message: "更新成功",
  272. type: "success"
  273. });
  274. this.treeChanged = false;
  275. });
  276. }
  277. },
  278. created() {
  279. this.init();
  280. }
  281. };
  282. </script>
  283. <style scoped>
  284. .el-tree >>> label {
  285. margin-bottom: 0;
  286. }
  287. </style>