menu.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <template>
  2. <basic-container>
  3. <avue-crud :option="option"
  4. :table-loading="loading"
  5. :data="data"
  6. ref="crud"
  7. v-model="form"
  8. :permission="permissionList"
  9. :before-open="beforeOpen"
  10. :before-close="beforeClose"
  11. @row-del="rowDel"
  12. @row-update="rowUpdate"
  13. @row-save="rowSave"
  14. @search-change="searchChange"
  15. @search-reset="searchReset"
  16. @selection-change="selectionChange"
  17. @current-change="currentChange"
  18. @size-change="sizeChange"
  19. @refresh-change="refreshChange"
  20. @on-load="onLoad"
  21. @tree-load="treeLoad">
  22. <template slot="menuLeft">
  23. <el-button type="danger"
  24. size="small"
  25. icon="el-icon-delete"
  26. v-if="permission.menu_delete"
  27. plain
  28. @click="handleDelete">删 除
  29. </el-button>
  30. </template>
  31. <template slot-scope="scope" slot="menu">
  32. <el-button
  33. type="text"
  34. icon="el-icon-circle-plus-outline"
  35. size="small"
  36. @click.stop="handleAdd(scope.row,scope.index)"
  37. v-if="userInfo.role_name.includes('admin') && scope.row.category === 1"
  38. >新增子项
  39. </el-button>
  40. </template>
  41. <template slot-scope="{row}"
  42. slot="source">
  43. <div style="text-align:center">
  44. <i :class="row.source"/>
  45. </div>
  46. </template>
  47. </avue-crud>
  48. </basic-container>
  49. </template>
  50. <script>
  51. import {getLazyList, remove, update, add, getMenu} from "@/api/system/menu";
  52. import {mapGetters} from "vuex";
  53. import iconList from "@/config/iconList";
  54. import func from "@/util/func";
  55. import {getMenuTree} from "@/api/system/menu";
  56. export default {
  57. data() {
  58. return {
  59. form: {},
  60. query: {},
  61. loading: true,
  62. selectionList: [],
  63. parentId: 0,
  64. page: {
  65. pageSize: 10,
  66. currentPage: 1,
  67. total: 0,
  68. },
  69. option: {
  70. lazy: true,
  71. tip: false,
  72. simplePage: true,
  73. searchShow: true,
  74. searchMenuSpan: 6,
  75. dialogWidth: "60%",
  76. tree: true,
  77. border: true,
  78. index: true,
  79. selection: true,
  80. viewBtn: true,
  81. menuWidth: 300,
  82. dialogClickModal: false,
  83. column: [
  84. {
  85. label: "菜单名称",
  86. prop: "name",
  87. search: true,
  88. rules: [
  89. {
  90. required: true,
  91. message: "请输入菜单名称",
  92. trigger: "blur"
  93. }
  94. ]
  95. },
  96. {
  97. label: "路由地址",
  98. prop: "path",
  99. rules: [
  100. {
  101. required: true,
  102. message: "请输入路由地址",
  103. trigger: "blur"
  104. }
  105. ]
  106. },
  107. {
  108. label: "上级菜单",
  109. prop: "parentId",
  110. type: "tree",
  111. dicData: [],
  112. hide: true,
  113. addDisabled: false,
  114. props: {
  115. label: "title"
  116. },
  117. rules: [
  118. {
  119. required: false,
  120. message: "请选择上级菜单",
  121. trigger: "click"
  122. }
  123. ]
  124. },
  125. {
  126. label: "菜单图标",
  127. prop: "source",
  128. type: "icon",
  129. slot: true,
  130. iconList: iconList,
  131. rules: [
  132. {
  133. required: true,
  134. message: "请输入菜单图标",
  135. trigger: "click"
  136. }
  137. ]
  138. },
  139. {
  140. label: "菜单编号",
  141. prop: "code",
  142. search: true,
  143. rules: [
  144. {
  145. required: true,
  146. message: "请输入菜单编号",
  147. trigger: "blur"
  148. }
  149. ]
  150. },
  151. {
  152. label: "菜单类型",
  153. prop: "category",
  154. type: "radio",
  155. dicData: [
  156. {
  157. label: "菜单",
  158. value: 1
  159. },
  160. {
  161. label: "按钮",
  162. value: 2
  163. }
  164. ],
  165. hide: true,
  166. rules: [
  167. {
  168. required: true,
  169. message: "请选择菜单类型",
  170. trigger: "blur"
  171. }
  172. ]
  173. },
  174. {
  175. label: "菜单别名",
  176. prop: "alias",
  177. search: true,
  178. rules: [
  179. {
  180. required: true,
  181. message: "请输入菜单别名",
  182. trigger: "blur"
  183. }
  184. ]
  185. },
  186. {
  187. label: "新窗口",
  188. prop: "isOpen",
  189. type: "radio",
  190. disabled: false,
  191. dicData: [
  192. {
  193. label: "否",
  194. value: 1
  195. },
  196. {
  197. label: "是",
  198. value: 2
  199. }
  200. ],
  201. value: 1,
  202. rules: [
  203. {
  204. required: true,
  205. message: "请选择新窗口打开",
  206. trigger: "blur"
  207. }
  208. ]
  209. },
  210. {
  211. label: "菜单排序",
  212. prop: "sort",
  213. type: "number",
  214. row: true,
  215. span: 24,
  216. rules: [
  217. {
  218. required: true,
  219. message: "请输入菜单排序",
  220. trigger: "blur"
  221. }
  222. ]
  223. },
  224. {
  225. label: "菜单备注",
  226. prop: "remark",
  227. type: "textarea",
  228. span: 24,
  229. minRows: 2,
  230. hide: true
  231. }
  232. ]
  233. },
  234. data: []
  235. };
  236. },
  237. watch: {
  238. 'form.category'() {
  239. const category = func.toInt(this.form.category);
  240. this.$refs.crud.option.column.filter(item => {
  241. if (item.prop === "path") {
  242. item.rules[0].required = category === 1;
  243. }
  244. if (item.prop === 'isOpen') {
  245. item.disabled = category === 2;
  246. }
  247. });
  248. },
  249. },
  250. computed: {
  251. ...mapGetters(["userInfo", "permission"]),
  252. permissionList() {
  253. return {
  254. addBtn: this.vaildData(this.permission.menu_add, false),
  255. viewBtn: this.vaildData(this.permission.menu_view, false),
  256. delBtn: this.vaildData(this.permission.menu_delete, false),
  257. editBtn: this.vaildData(this.permission.menu_edit, false)
  258. };
  259. },
  260. ids() {
  261. let ids = [];
  262. this.selectionList.forEach(ele => {
  263. ids.push(ele.id);
  264. });
  265. return ids.join(",");
  266. }
  267. },
  268. methods: {
  269. initData() {
  270. getMenuTree().then(res => {
  271. const column = this.findObject(this.option.column, "parentId");
  272. column.dicData = res.data.data;
  273. });
  274. },
  275. handleAdd(row) {
  276. this.parentId = row.id;
  277. const column = this.findObject(this.option.column, "parentId");
  278. column.value = row.id;
  279. column.addDisabled = true;
  280. this.$refs.crud.rowAdd();
  281. },
  282. rowSave(row, done, loading) {
  283. add(row).then((res) => {
  284. // 获取新增数据的相关字段
  285. const data = res.data.data;
  286. row.id = data.id;
  287. this.$message({
  288. type: "success",
  289. message: "操作成功!"
  290. });
  291. // 数据回调进行刷新
  292. done(row);
  293. }, error => {
  294. window.console.log(error);
  295. loading();
  296. });
  297. },
  298. rowUpdate(row, index, done, loading) {
  299. update(row).then(() => {
  300. this.$message({
  301. type: "success",
  302. message: "操作成功!"
  303. });
  304. // 数据回调进行刷新
  305. done(row);
  306. }, error => {
  307. window.console.log(error);
  308. loading();
  309. });
  310. },
  311. rowDel(row, index, done) {
  312. this.$confirm("确定将选择数据删除?", {
  313. confirmButtonText: "确定",
  314. cancelButtonText: "取消",
  315. type: "warning"
  316. })
  317. .then(() => {
  318. return remove(row.id);
  319. })
  320. .then(() => {
  321. this.$message({
  322. type: "success",
  323. message: "操作成功!"
  324. });
  325. // 数据回调进行刷新
  326. done(row);
  327. });
  328. },
  329. handleDelete() {
  330. if (this.selectionList.length === 0) {
  331. this.$message.warning("请选择至少一条数据");
  332. return;
  333. }
  334. this.$confirm("确定将选择数据删除?", {
  335. confirmButtonText: "确定",
  336. cancelButtonText: "取消",
  337. type: "warning"
  338. })
  339. .then(() => {
  340. return remove(this.ids);
  341. })
  342. .then(() => {
  343. // 刷新表格数据并重载
  344. this.data = [];
  345. this.parentId = 0;
  346. this.$refs.crud.refreshTable();
  347. this.$refs.crud.toggleSelection();
  348. // 表格数据重载
  349. this.onLoad(this.page);
  350. this.$message({
  351. type: "success",
  352. message: "操作成功!"
  353. });
  354. });
  355. },
  356. searchReset() {
  357. this.query = {};
  358. this.parentId = 0;
  359. this.onLoad(this.page);
  360. },
  361. searchChange(params, done) {
  362. this.query = params;
  363. this.parentId = '';
  364. this.page.currentPage = 1;
  365. this.onLoad(this.page, params);
  366. done();
  367. },
  368. selectionChange(list) {
  369. this.selectionList = list;
  370. },
  371. selectionClear() {
  372. this.selectionList = [];
  373. this.$refs.crud.toggleSelection();
  374. },
  375. beforeOpen(done, type) {
  376. if (["add", "edit"].includes(type)) {
  377. this.initData();
  378. }
  379. if (["edit", "view"].includes(type)) {
  380. getMenu(this.form.id).then(res => {
  381. this.form = res.data.data;
  382. });
  383. }
  384. done();
  385. },
  386. beforeClose(done) {
  387. this.parentId = "";
  388. const column = this.findObject(this.option.column, "parentId");
  389. column.value = "";
  390. column.addDisabled = false;
  391. done();
  392. },
  393. currentChange(currentPage) {
  394. this.page.currentPage = currentPage;
  395. },
  396. sizeChange(pageSize) {
  397. this.page.pageSize = pageSize;
  398. },
  399. refreshChange() {
  400. this.onLoad(this.page, this.query);
  401. },
  402. onLoad(page, params = {}) {
  403. this.loading = true;
  404. getLazyList(this.parentId, Object.assign(params, this.query)).then(res => {
  405. this.data = res.data.data;
  406. this.loading = false;
  407. this.selectionClear();
  408. });
  409. },
  410. treeLoad(tree, treeNode, resolve) {
  411. const parentId = tree.id;
  412. getLazyList(parentId).then(res => {
  413. resolve(res.data.data);
  414. });
  415. }
  416. }
  417. };
  418. </script>
  419. <style>
  420. </style>