adminOperation.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. isplatform: null,
  100. };
  101. },
  102. mounted() {
  103. this.onReSet();
  104. this.init();
  105. console.log(this.$route)
  106. },
  107. activated() {
  108. this.onReSet();
  109. this.init();
  110. },
  111. methods: {
  112. init() {
  113. this.pageType = this.$route.query.type;
  114. this.id = this.$route.query.id;
  115. this.page = this.$route.query.page;
  116. this.isplatform = this.$route.query.isplatform;
  117. if (this.pageType == "create") {
  118. this.$refs.tree.setCheckedKeys([4229]);
  119. }
  120. this.lookSilder();
  121. },
  122. onSubmit() {
  123. let tempIds = this.$refs.tree.getCheckedKeys();
  124. let halfIds = this.$refs.tree.getHalfCheckedKeys();
  125. let allIds = [...tempIds, ...halfIds];
  126. let params = {
  127. organId: this.organId,
  128. roleDesc: this.result.roleDesc,
  129. roleName: this.result.roleName,
  130. menuIds: allIds,
  131. }
  132. if (this.isplatform) {
  133. params.tenantId = -1;
  134. }
  135. if (this.pageType == "update") {
  136. roleUpdate({
  137. id: this.id,
  138. ...params
  139. }).then((res) => {
  140. this.messageTips("修改", res);
  141. });
  142. } else if (this.pageType == "create") {
  143. roleAdd({
  144. ...params
  145. }).then((res) => {
  146. this.messageTips("添加", res);
  147. });
  148. }
  149. },
  150. messageTips(title, res) {
  151. if (res.code == 200) {
  152. this.$message.success(`${title}成功`);
  153. this.$store.dispatch("delVisitedViews", this.$route);
  154. if (this.isplatform) {
  155. this.$router.push({
  156. path: "/platformAdminManger",
  157. query: { page: this.page },
  158. });
  159. } else {
  160. this.$router.push({
  161. path: "/adminManager",
  162. query: { page: this.page },
  163. });
  164. }
  165. } else {
  166. this.$message.error(res.msg);
  167. }
  168. },
  169. async lookSilder() {
  170. // 修复反写没有loading request返回的不是Promise await无效
  171. getSilder({ hid: 0 }).then(async (silderList) => {
  172. let tempData = [];
  173. if (silderList.code == 200) {
  174. this.silderList = silderList.data;
  175. tempData = this.setTableData(silderList.data);
  176. this.data = tempData;
  177. // console.log(this.data)
  178. }
  179. // console.log(this.pageType)
  180. if (this.pageType == "update") {
  181. let roleInfo = await getRoleInfo({ id: this.id });
  182. if (roleInfo.code == 200) {
  183. let roleData = roleInfo.data;
  184. // 是否是全部选中
  185. this.checkAll = roleData.menuIds.length >= this.slideCount;
  186. // 是否是半选
  187. this.isIndeterminate =
  188. roleData.menuIds.length > 0 &&
  189. roleData.menuIds.length < this.slideCount;
  190. let tSplice = this.getParent(roleData.menuIds, tempData);
  191. roleData.menuIds = tSplice;
  192. this.result = roleData;
  193. }
  194. } else {
  195. this.onReSet();
  196. }
  197. });
  198. },
  199. onTreeCheck() {
  200. let checkTree = this.$refs.tree.getCheckedKeys();
  201. this.checkAll = checkTree.length >= this.slideCount;
  202. this.isIndeterminate =
  203. checkTree.length > 0 && checkTree.length < this.slideCount;
  204. },
  205. onCheckAll(val) {
  206. if (val) {
  207. // 先去掉半选
  208. this.isIndeterminate = false;
  209. this.$refs.tree.setCheckedNodes(this.data);
  210. } else {
  211. this.$refs.tree.setCheckedNodes([]);
  212. }
  213. },
  214. //递归获取到所有的为子级的ID
  215. getParent(checkIds, data) {
  216. let removeIds = JSON.parse(JSON.stringify(checkIds));
  217. this.getAllChildIds(data);
  218. let tempAllChildIds = this.allChildIds;
  219. for (let i = checkIds.length; i > 0; i--) {
  220. if (!tempAllChildIds.includes(checkIds[i - 1])) {
  221. removeIds.splice(i - 1, 1);
  222. }
  223. }
  224. return removeIds;
  225. },
  226. getAllChildIds(data) {
  227. // 获取所有最子集编号
  228. let child = this.allChildIds;
  229. let tempList = [];
  230. data.forEach((item, index) => {
  231. let temp = [];
  232. if (item.children && item.children.length > 0) {
  233. temp = this.getAllChildIds(item.children);
  234. } else {
  235. child.push(item.id);
  236. }
  237. });
  238. },
  239. setTableData(result) {
  240. let list = [];
  241. list = result.map((res) => {
  242. if (!this.filterIds.includes(res.id)) {
  243. let tempList = {};
  244. tempList = {
  245. id: res.id,
  246. name: res.name,
  247. label: res.name,
  248. type: res.type,
  249. path: res.path,
  250. permission: res.permission,
  251. icon: res.icon,
  252. parentId: res.parentId,
  253. };
  254. this.slideCount++;
  255. if (res.sysMenus && res.sysMenus.length > 0) {
  256. tempList.children = this.setTableData(res.sysMenus);
  257. }
  258. return tempList;
  259. }
  260. return null;
  261. });
  262. let tempList = [];
  263. list.forEach((item) => {
  264. if (item) {
  265. tempList.push(item);
  266. }
  267. });
  268. return tempList;
  269. },
  270. onReSet() {
  271. this.$refs.tree.setCheckedNodes([]);
  272. this.result = {
  273. roleName: null,
  274. roleDesc: null,
  275. };
  276. this.isIndeterminate = false;
  277. this.checkAll = false;
  278. },
  279. onCancel() {
  280. this.$store.dispatch("delVisitedViews", this.$route);
  281. // this.$router.push({
  282. // path: "/adminManager",
  283. // query: {
  284. // page: this.$route.query.page,
  285. // },
  286. // });
  287. if (this.isplatform) {
  288. this.$router.push({
  289. path: "/platformAdminManger",
  290. query: { page: this.page },
  291. });
  292. } else {
  293. this.$router.push({
  294. path: "/adminManager",
  295. query: { page: this.page },
  296. });
  297. }
  298. },
  299. seachRoles() {
  300. this.$refs.tree.filter(this.seachRoleValue);
  301. },
  302. filterNode(value, data) {
  303. console.log(data);
  304. if (!value) return true;
  305. return data.label.indexOf(value) !== -1;
  306. },
  307. onReSetRole() {
  308. this.seachRoleValue = "";
  309. this.data = this.setTableData(this.silderList);
  310. },
  311. },
  312. };
  313. </script>
  314. <style lang="scss" scoped>
  315. .el-row {
  316. margin-top: 40px;
  317. }
  318. .el-col {
  319. display: flex;
  320. align-items: center;
  321. margin-bottom: 20px;
  322. justify-content: flex-end;
  323. margin-right: 50%;
  324. }
  325. .el-input-group {
  326. width: 200px;
  327. margin: 0 20px;
  328. }
  329. ::v-deep .el-tree-node__content {
  330. height: 40px !important;
  331. }
  332. </style>