adminOperation.vue 9.3 KB

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