adminOperation.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <div class="m-container">
  3. <h2>
  4. <el-page-header
  5. @back="onCancel"
  6. :content="(pageType == 'create' ? '添加' : '修改') + '角色'"
  7. ></el-page-header>
  8. </h2>
  9. <div class="m-core">
  10. <el-form ref="form" label-width="120px" style="width: 500px">
  11. <el-form-item label="角色名称" prop="roleName">
  12. <el-input v-model.trim="result.roleName"></el-input>
  13. </el-form-item>
  14. <el-form-item label="角色描述">
  15. <el-input type="textarea" v-model.trim="result.roleDesc"></el-input>
  16. </el-form-item>
  17. <el-form-item label="搜索">
  18. <el-input
  19. style="width: 210px"
  20. v-model.trim="seachRoleValue"
  21. clearable
  22. ></el-input>
  23. <el-button style="margin-left: 10px" type="danger" @click="seachRoles"
  24. >搜索</el-button
  25. >
  26. <!-- <el-button type="primary"
  27. @click="onReSetRole">重置</el-button> -->
  28. </el-form-item>
  29. <el-form-item label="基本权限">
  30. <el-checkbox
  31. :indeterminate="isIndeterminate"
  32. @change="onCheckAll"
  33. v-model.trim="checkAll"
  34. >全选</el-checkbox
  35. >
  36. <el-tree
  37. :data="data"
  38. show-checkbox
  39. node-key="id"
  40. @check="onTreeCheck"
  41. :filter-node-method="filterNode"
  42. ref="tree"
  43. accordion
  44. highlight-current
  45. :default-checked-keys="result.menuIds"
  46. :props="defaultProps"
  47. >
  48. <div slot-scope="{ node, data }">
  49. {{ node.label }}
  50. <el-tag v-if="data.type == 1" effect="dark">按钮</el-tag>
  51. </div>
  52. </el-tree>
  53. </el-form-item>
  54. <el-form-item>
  55. <el-button @click="onSubmit" type="primary"
  56. >立即{{ pageType == "create" ? "创建" : "修改" }}</el-button
  57. >
  58. <el-button @click="onReSet('form')">重置</el-button>
  59. </el-form-item>
  60. </el-form>
  61. </div>
  62. </div>
  63. </template>
  64. <script>
  65. import { getUserRole } from "@/api/systemManage";
  66. import store from "@/store";
  67. import { getSilder } from "@/api/silder";
  68. import {
  69. roleGetMenus,
  70. getRoleInfo,
  71. roleUpdate,
  72. roleAdd,
  73. } from "@/api/systemManage";
  74. export default {
  75. name: "adminOperation",
  76. data() {
  77. return {
  78. organId: null,
  79. pageType: null,
  80. id: null,
  81. page: null,
  82. isIndeterminate: false,
  83. data: [],
  84. defaultProps: {
  85. children: "children",
  86. label: "label",
  87. },
  88. result: {
  89. roleName: null,
  90. roleDesc: null,
  91. },
  92. checkAll: false,
  93. splice: [],
  94. silderList: [],
  95. allChildIds: [], // 所有子编号
  96. slideCount: 0,
  97. seachRoleValue: "", //权限搜索字段
  98. filterIds: [4794, 4743, 3266, 3268, 3306, 3307, 3309, 3784, 4627],
  99. };
  100. },
  101. mounted() {
  102. this.onReSet();
  103. this.init();
  104. },
  105. activated() {
  106. this.onReSet();
  107. this.init();
  108. },
  109. methods: {
  110. init() {
  111. this.pageType = this.$route.query.type;
  112. this.id = this.$route.query.id;
  113. this.page = this.$route.query.page;
  114. if (this.pageType == "create") {
  115. this.$refs.tree.setCheckedKeys([4229]);
  116. }
  117. this.lookSilder();
  118. },
  119. onSubmit() {
  120. let tempIds = this.$refs.tree.getCheckedKeys();
  121. let halfIds = this.$refs.tree.getHalfCheckedKeys();
  122. let allIds = [...tempIds, ...halfIds];
  123. if (this.pageType == "update") {
  124. roleUpdate({
  125. id: this.id,
  126. organId: this.organId,
  127. roleDesc: this.result.roleDesc,
  128. roleName: this.result.roleName,
  129. menuIds: allIds,
  130. }).then((res) => {
  131. this.messageTips("修改", res);
  132. });
  133. } else if (this.pageType == "create") {
  134. roleAdd({
  135. organId: this.organId,
  136. roleDesc: this.result.roleDesc,
  137. roleName: this.result.roleName,
  138. menuIds: allIds,
  139. }).then((res) => {
  140. this.messageTips("添加", res);
  141. });
  142. }
  143. },
  144. messageTips(title, res) {
  145. if (res.code == 200) {
  146. this.$message.success("修改成功");
  147. this.$store.dispatch("delVisitedViews", this.$route);
  148. this.$router.push({
  149. path: "/adminManager",
  150. query: { page: this.page },
  151. });
  152. } else {
  153. this.$message.error(res.msg);
  154. }
  155. },
  156. async lookSilder() {
  157. // 修复反写没有loading request返回的不是Promise await无效
  158. getSilder({ hid: 0 }).then(async (silderList) => {
  159. let tempData = [];
  160. if (silderList.code == 200) {
  161. this.silderList = silderList.data;
  162. tempData = this.setTableData(silderList.data);
  163. this.data = tempData;
  164. // console.log(this.data)
  165. }
  166. // console.log(this.pageType)
  167. if (this.pageType == "update") {
  168. let roleInfo = await getRoleInfo({ id: this.id });
  169. if (roleInfo.code == 200) {
  170. let roleData = roleInfo.data;
  171. // 是否是全部选中
  172. this.checkAll = roleData.menuIds.length >= this.slideCount;
  173. // 是否是半选
  174. this.isIndeterminate =
  175. roleData.menuIds.length > 0 &&
  176. roleData.menuIds.length < this.slideCount;
  177. let tSplice = this.getParent(roleData.menuIds, tempData);
  178. roleData.menuIds = tSplice;
  179. this.result = roleData;
  180. }
  181. } else {
  182. this.onReSet();
  183. }
  184. });
  185. },
  186. onTreeCheck() {
  187. let checkTree = this.$refs.tree.getCheckedKeys();
  188. this.checkAll = checkTree.length >= this.slideCount;
  189. this.isIndeterminate =
  190. checkTree.length > 0 && checkTree.length < this.slideCount;
  191. },
  192. onCheckAll(val) {
  193. if (val) {
  194. // 先去掉半选
  195. this.isIndeterminate = false;
  196. this.$refs.tree.setCheckedNodes(this.data);
  197. } else {
  198. this.$refs.tree.setCheckedNodes([]);
  199. }
  200. },
  201. //递归获取到所有的为子级的ID
  202. getParent(checkIds, data) {
  203. let removeIds = JSON.parse(JSON.stringify(checkIds));
  204. this.getAllChildIds(data);
  205. let tempAllChildIds = this.allChildIds;
  206. for (let i = checkIds.length; i > 0; i--) {
  207. if (!tempAllChildIds.includes(checkIds[i - 1])) {
  208. removeIds.splice(i - 1, 1);
  209. }
  210. }
  211. return removeIds;
  212. },
  213. getAllChildIds(data) {
  214. // 获取所有最子集编号
  215. let child = this.allChildIds;
  216. let tempList = [];
  217. data.forEach((item, index) => {
  218. let temp = [];
  219. if (item.children && item.children.length > 0) {
  220. temp = this.getAllChildIds(item.children);
  221. } else {
  222. child.push(item.id);
  223. }
  224. });
  225. },
  226. setTableData(result) {
  227. let list = [];
  228. list = result.map((res) => {
  229. if (!this.filterIds.includes(res.id)) {
  230. let tempList = {};
  231. tempList = {
  232. id: res.id,
  233. name: res.name,
  234. label: res.name,
  235. type: res.type,
  236. path: res.path,
  237. permission: res.permission,
  238. icon: res.icon,
  239. parentId: res.parentId,
  240. };
  241. this.slideCount++;
  242. if (res.sysMenus && res.sysMenus.length > 0) {
  243. tempList.children = this.setTableData(res.sysMenus);
  244. }
  245. return tempList;
  246. }
  247. return null;
  248. });
  249. let tempList = [];
  250. list.forEach((item) => {
  251. if (item) {
  252. tempList.push(item);
  253. }
  254. });
  255. return tempList;
  256. },
  257. onReSet() {
  258. this.$refs.tree.setCheckedNodes([]);
  259. this.result = {
  260. roleName: null,
  261. roleDesc: null,
  262. };
  263. this.isIndeterminate = false;
  264. this.checkAll = false;
  265. },
  266. onCancel() {
  267. this.$store.dispatch("delVisitedViews", this.$route);
  268. this.$router.push({
  269. path: "/adminManager",
  270. query: {
  271. page: this.$route.query.page,
  272. },
  273. });
  274. },
  275. seachRoles() {
  276. this.$refs.tree.filter(this.seachRoleValue);
  277. },
  278. filterNode(value, data) {
  279. console.log(data);
  280. if (!value) return true;
  281. return data.label.indexOf(value) !== -1;
  282. },
  283. onReSetRole() {
  284. this.seachRoleValue = "";
  285. this.data = this.setTableData(this.silderList);
  286. },
  287. },
  288. };
  289. </script>
  290. <style lang="scss" scoped>
  291. .el-row {
  292. margin-top: 40px;
  293. }
  294. .el-col {
  295. display: flex;
  296. align-items: center;
  297. margin-bottom: 20px;
  298. justify-content: flex-end;
  299. margin-right: 50%;
  300. }
  301. .el-input-group {
  302. width: 200px;
  303. margin: 0 20px;
  304. }
  305. /deep/.el-tree-node__content {
  306. height: 40px !important;
  307. }
  308. </style>