role_privilege_settings.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <template>
  2. <div>
  3. <section class="content" style="margin-top: -10px;">
  4. <div class="box box-info">
  5. <!-- 头信息 -->
  6. <div class="box-header with-border">
  7. <h3 class="box-title">鉴权管理 &gt; 角色权限配置</h3>
  8. <div class="box-tools pull-right">
  9. <button
  10. type="button"
  11. class="btn btn-box-tool"
  12. data-widget="collapse"
  13. >
  14. <i class="fa fa-minus"></i>
  15. </button>
  16. </div>
  17. </div>
  18. <!-- 正文信息 -->
  19. <div class="box-body">
  20. <!-- 选择 -->
  21. <el-form
  22. :inline="true"
  23. :model="form"
  24. label-position="right"
  25. label-width="100px"
  26. >
  27. <el-row>
  28. <el-form-item label="顶级机构" class="pull-left">
  29. <el-select
  30. class="input_width_lg"
  31. v-model="form.orgId"
  32. placeholder="请选择"
  33. @change="rootOrgChanged"
  34. :disabled="!isSuperAdmin"
  35. >
  36. <el-option
  37. v-for="item in orgList"
  38. :label="item.name"
  39. :value="item.id"
  40. :key="item.id"
  41. >
  42. </el-option>
  43. </el-select>
  44. </el-form-item>
  45. <el-form-item label="角色" class="pull-left">
  46. <el-select
  47. class="input_width_lg"
  48. v-model="form.roleId"
  49. placeholder="请选择"
  50. @change="change"
  51. >
  52. <el-option
  53. v-for="item in roleList"
  54. :label="item.roleName"
  55. :value="item.roleId"
  56. :key="item.roleId"
  57. >
  58. </el-option>
  59. </el-select>
  60. </el-form-item>
  61. <el-form-item label="权限组" class="pull-left">
  62. <el-select
  63. class="input_width_lg"
  64. v-model="form.privilegeGroupId"
  65. placeholder="请选择"
  66. @change="change"
  67. >
  68. <el-option
  69. v-for="item in privilegeGroupList"
  70. :label="item.name"
  71. :value="item.id"
  72. :key="item.id"
  73. >
  74. </el-option>
  75. </el-select>
  76. </el-form-item>
  77. </el-row>
  78. </el-form>
  79. <div style="margin-bottom:10px;margin-left: 50px;">
  80. <el-button
  81. :plain="true"
  82. type="success"
  83. :disabled="!treeChanged"
  84. @click="save"
  85. >保 存</el-button
  86. >
  87. </div>
  88. <!-- 权限树 -->
  89. <div style="width: 50%;margin-left: 50px;">
  90. <el-tree
  91. :data="treeData"
  92. :props="defaultProps"
  93. show-checkbox
  94. node-key="id"
  95. ref="tree"
  96. highlight-current
  97. :check-strictly="true"
  98. :default-expanded-keys="[-1]"
  99. :default-checked-keys="checkedKeys"
  100. @check-change="treeChange"
  101. :expand-on-click-node="true"
  102. >
  103. </el-tree>
  104. </div>
  105. </div>
  106. </div>
  107. </section>
  108. </div>
  109. </template>
  110. <script>
  111. import { mapState } from "vuex";
  112. import { core_api } from "../constants/constants.js";
  113. export default {
  114. data() {
  115. return {
  116. completed: false,
  117. isSuperAdmin: false,
  118. form: {
  119. orgId: null,
  120. roleId: null,
  121. privilegeGroupId: null
  122. },
  123. orgList: [],
  124. roleList: [],
  125. privilegeGroupList: [],
  126. treeChanged: false,
  127. treeData: [],
  128. checkedKeys: [],
  129. uncheckChildren_nodeId: null,
  130. defaultProps: {
  131. children: "children",
  132. label: "label"
  133. }
  134. };
  135. },
  136. computed: {
  137. ...mapState({ user: state => state.user })
  138. },
  139. methods: {
  140. /*初始化*/
  141. init() {
  142. for (let role of this.user.roleList) {
  143. if (role.roleCode == "SUPER_ADMIN") {
  144. this.isSuperAdmin = true;
  145. break;
  146. }
  147. }
  148. var url1 = core_api + "/org/getRootOrgList";
  149. var url2 =
  150. core_api + "/rolePrivilege/getRoles?includeSuperAdmin=" + false;
  151. var url3 = core_api + "/rolePrivilege/getPrivilegeGroupList";
  152. Promise.all([
  153. this.$http.get(url1),
  154. this.$http.post(url2),
  155. this.$http.get(url3)
  156. ]).then(([resp1, resp2, resp3]) => {
  157. this.orgList = resp1.data;
  158. this.form.orgId = this.user.rootOrgId;
  159. this.roleList = resp2.data;
  160. if (0 < this.roleList.length) {
  161. this.form.roleId = this.roleList[0].roleId;
  162. }
  163. this.privilegeGroupList = resp3.data;
  164. if (0 < this.privilegeGroupList.length) {
  165. this.form.privilegeGroupId = this.privilegeGroupList[0].id;
  166. }
  167. this.initTree(
  168. this.form.orgId,
  169. this.form.roleId,
  170. this.form.privilegeGroupId
  171. );
  172. });
  173. },
  174. /*初始化权限树*/
  175. initTree(orgId, roleId, privilegeGroupId) {
  176. var url1 =
  177. core_api + "/rolePrivilege/getPrivilegeTree/" + privilegeGroupId;
  178. var url2 =
  179. core_api + "/rolePrivilege/getPrivilegeIdList/" + orgId + "/" + roleId;
  180. Promise.all([this.$http.get(url1), this.$http.get(url2)]).then(
  181. ([resp1, resp2]) => {
  182. console.log("initTree(). treeData:", resp1.data.children);
  183. console.log("initTree(). checkedKeys:", resp2.data);
  184. this.treeData = resp1.data.children;
  185. this.checkedKeys = resp2.data;
  186. this.completed = true;
  187. }
  188. );
  189. },
  190. /*change事件*/
  191. change() {
  192. if (!this.completed) {
  193. return;
  194. }
  195. this.initTree(
  196. this.form.orgId,
  197. this.form.roleId,
  198. this.form.privilegeGroupId
  199. );
  200. },
  201. rootOrgChanged() {
  202. if (!this.completed) {
  203. return;
  204. }
  205. var url =
  206. core_api +
  207. "/rolePrivilege/getRoles?includeSuperAdmin=" +
  208. false +
  209. "&rootOrgId=" +
  210. this.form.orgId;
  211. this.$http
  212. .post(url)
  213. .then(response => {
  214. this.roleList = response.data;
  215. if (0 < this.roleList.length) {
  216. this.form.roleId = this.roleList[0].roleId;
  217. }
  218. })
  219. .catch(response => {
  220. if (response.status == 500) {
  221. this.$notify({
  222. showClose: true,
  223. message: response.data.desc,
  224. type: "error"
  225. });
  226. }
  227. });
  228. this.initTree(
  229. this.form.orgId,
  230. this.form.roleId,
  231. this.form.privilegeGroupId
  232. );
  233. },
  234. treeChange(node, checked) {
  235. console.log("[tree change] node:", node);
  236. if (checked && !this.checkedKeys.contains(node.id)) {
  237. this.checkedKeys.push(node.id);
  238. }
  239. if (!checked && this.checkedKeys.contains(node.id)) {
  240. this.checkedKeys.remove(node.id);
  241. }
  242. var checkChildren = this.uncheckChildren_nodeId != node.id;
  243. this.uncheckChildren_nodeId = null;
  244. if (checked) {
  245. if (node.parentId) {
  246. this.uncheckChildren_nodeId = node.parentId;
  247. this.$refs.tree.setChecked(node.parentId, true, false);
  248. }
  249. if (checkChildren) {
  250. if (node.children && 0 < node.children.length) {
  251. for (const cur of node.children) {
  252. this.$refs.tree.setChecked(cur.id, true, false);
  253. }
  254. }
  255. }
  256. } else {
  257. if (node.children && 0 < node.children.length) {
  258. for (const cur of node.children) {
  259. this.$refs.tree.setChecked(cur.id, false, false);
  260. }
  261. }
  262. }
  263. this.treeChanged = true;
  264. },
  265. save() {
  266. console.log("save(). checkedKeys:", this.checkedKeys);
  267. var url = core_api + "/rolePrivilege/updateRolePrivilegeRelations";
  268. this.$http
  269. .post(url, {
  270. rootOrgId: this.form.orgId,
  271. roleId: this.form.roleId,
  272. privilegeGroupId: this.form.privilegeGroupId,
  273. privilegeIdSet: this.checkedKeys
  274. })
  275. .then(() => {
  276. this.$notify({
  277. message: "更新成功",
  278. type: "success"
  279. });
  280. this.treeChanged = false;
  281. })
  282. .catch(response => {
  283. if (response.status == 500) {
  284. this.$notify({
  285. showClose: true,
  286. message: response.data.desc,
  287. type: "error"
  288. });
  289. }
  290. });
  291. }
  292. },
  293. created() {
  294. this.init();
  295. }
  296. };
  297. </script>
  298. <style scoped>
  299. .el-radio-group {
  300. margin-top: 5px;
  301. }
  302. </style>