paltformOperation.vue 9.3 KB

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