index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <template>
  2. <div class="app-container">
  3. <div class="head-container">
  4. <!-- 搜索 -->
  5. <el-input
  6. v-model="listQuery.Filter"
  7. clearable
  8. size="small"
  9. placeholder="搜索..."
  10. style="width: 200px"
  11. class="filter-item"
  12. @keyup.enter.native="handleFilter"
  13. />
  14. <el-button
  15. class="filter-item"
  16. size="mini"
  17. type="success"
  18. icon="el-icon-search"
  19. @click="handleFilter"
  20. >搜索</el-button
  21. >
  22. <div style="padding: 6px 0">
  23. <el-button
  24. class="filter-item"
  25. size="mini"
  26. type="primary"
  27. icon="el-icon-plus"
  28. @click="handleCreate"
  29. v-permission="['BaseService.Job.Create']"
  30. >新增</el-button
  31. >
  32. <el-button
  33. class="filter-item"
  34. size="mini"
  35. type="success"
  36. icon="el-icon-edit"
  37. v-permission="['BaseService.Job.Update']"
  38. @click="handleUpdate()"
  39. >修改</el-button
  40. >
  41. <el-button
  42. slot="reference"
  43. class="filter-item"
  44. type="danger"
  45. icon="el-icon-delete"
  46. size="mini"
  47. v-permission="['BaseService.Job.Delete']"
  48. @click="handleDelete()"
  49. >删除</el-button
  50. >
  51. </div>
  52. </div>
  53. <el-dialog
  54. :visible.sync="dialogFormVisible"
  55. :close-on-click-modal="false"
  56. :title="formTitle"
  57. @close="cancel()"
  58. width="600px"
  59. >
  60. <el-form
  61. ref="form"
  62. :inline="true"
  63. :model="form"
  64. :rules="rules"
  65. size="small"
  66. label-width="80px"
  67. >
  68. <el-form-item label="菜单类型" prop="categoryId">
  69. <el-select
  70. v-model="form.categoryId"
  71. placeholder="请选择"
  72. style="width: 184px;"
  73. :disabled="isEdit"
  74. >
  75. <el-option label="菜单" :value="1"></el-option>
  76. <el-option label="按钮" :value="2"></el-option>
  77. </el-select>
  78. </el-form-item>
  79. <el-form-item label="上级菜单" prop="menuType">
  80. <treeselect
  81. v-model="form.pid"
  82. :load-options="loadMenus"
  83. :options="menus"
  84. :disabled="isEdit"
  85. style="width: 184px;"
  86. placeholder="根目录"
  87. />
  88. </el-form-item>
  89. <el-form-item label="菜单名称" prop="name">
  90. <el-input v-model="form.name" placeholder="请输入菜单名称" style="width: 184px;" />
  91. </el-form-item>
  92. <el-form-item label="显示名称" prop="label">
  93. <el-input v-model="form.label" placeholder="请输入显示名称" style="width: 184px;" />
  94. </el-form-item>
  95. <el-form-item label="菜单排序" prop="sort">
  96. <el-input-number
  97. v-model="form.sort"
  98. controls-position="right"
  99. :min="0"
  100. style="width: 184px;"
  101. />
  102. </el-form-item>
  103. <el-form-item label="菜单图标" prop="icon" v-if="form.categoryId==1">
  104. <el-popover
  105. placement="bottom-start"
  106. width="460"
  107. trigger="click"
  108. @show="$refs['iconSelect'].reset()"
  109. >
  110. <IconSelect ref="iconSelect" @selected="selected" />
  111. <el-input slot="reference" v-model="form.icon" placeholder="点击选择图标" style="width: 184px;" readonly>
  112. <svg-icon
  113. v-if="form.icon"
  114. slot="prefix"
  115. :icon-class="form.icon"
  116. class="el-input__icon"
  117. style="height: 32px;width: 16px;"
  118. />
  119. <i v-else slot="prefix" class="el-icon-search el-input__icon" />
  120. </el-input>
  121. </el-popover>
  122. </el-form-item>
  123. <el-form-item label="路由地址" prop="path" v-if="form.categoryId==1">
  124. <el-input v-model="form.path" placeholder="请输入路由地址" style="width: 184px;" />
  125. </el-form-item>
  126. <el-form-item label="组件路径" prop="component" v-if="form.categoryId==1">
  127. <el-input v-model="form.component" placeholder="请输入组件路径" style="width: 184px;" />
  128. </el-form-item>
  129. <el-form-item label="权限标识">
  130. <el-input
  131. v-model="form.permission"
  132. placeholder="请输入权限标识"
  133. maxlength="50"
  134. style="width: 184px;"
  135. />
  136. </el-form-item>
  137. </el-form>
  138. <div slot="footer" class="dialog-footer">
  139. <el-button size="small" type="text" @click="cancel">取消</el-button>
  140. <el-button
  141. size="small"
  142. v-loading="formLoading"
  143. type="primary"
  144. @click="save"
  145. >确认</el-button
  146. >
  147. </div>
  148. </el-dialog>
  149. <el-table
  150. ref="multipleTable"
  151. v-loading="listLoading"
  152. :data="list"
  153. size="small"
  154. style="width: 90%"
  155. row-key="id"
  156. :tree-props="{children: 'children'}"
  157. >
  158. <el-table-column
  159. label="菜单名"
  160. prop="label"
  161. sortable="custom"
  162. >
  163. <template slot-scope="{ row }">
  164. <span class="link-type" @click="handleUpdate(row)">{{
  165. row.label
  166. }}</span>
  167. </template>
  168. </el-table-column>
  169. <el-table-column prop="icon" label="图标" align="center" width="100">
  170. <template slot-scope="scope">
  171. <svg-icon :icon-class="scope.row.icon" />
  172. </template>
  173. </el-table-column>
  174. <el-table-column label="排序" prop="sort" align="center" />
  175. <el-table-column label="菜单类型" prop="categoryId" align="center">
  176. <template slot-scope="scope">
  177. <span>{{scope.row.categoryId | displayCategory}}</span>
  178. </template>
  179. </el-table-column>
  180. <el-table-column label="组件路径" prop="component" align="center" />
  181. <el-table-column label="操作" align="center">
  182. <template slot-scope="{ row }">
  183. <el-button
  184. type="text"
  185. size="mini"
  186. @click="handleUpdate(row)"
  187. icon="el-icon-edit"
  188. >修改</el-button>
  189. <el-button
  190. type="text"
  191. size="mini"
  192. @click="handleDelete(row)"
  193. icon="el-icon-delete"
  194. >删除</el-button>
  195. </template>
  196. </el-table-column>
  197. </el-table>
  198. <pagination
  199. v-show="totalCount > 0"
  200. :total="totalCount"
  201. :page.sync="page"
  202. :limit.sync="listQuery.MaxResultCount"
  203. @pagination="getList"
  204. />
  205. </div>
  206. </template>
  207. <script>
  208. import permission from "@/directive/permission/index.js";
  209. import Treeselect from "@riophae/vue-treeselect";
  210. import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  211. import { LOAD_CHILDREN_OPTIONS } from "@riophae/vue-treeselect";
  212. import IconSelect from "@/components/IconSelect";
  213. const defaultForm = {
  214. id: null,
  215. pid:null,
  216. categoryId:1,
  217. name: null,
  218. path: '',
  219. sort: 999,
  220. icon: '',
  221. };
  222. export default {
  223. name: "Menu",
  224. components: { IconSelect, Treeselect },
  225. directives: { permission },
  226. filters: {
  227. displayCategory(categoryId) {
  228. const categoryMap = {
  229. 1: "菜单",
  230. 2: "按钮",
  231. };
  232. return categoryMap[categoryId];
  233. }
  234. },
  235. data() {
  236. return {
  237. rules: {
  238. name: [{ required: true, message: "请输入菜单名", trigger: "blur" }],
  239. label: [{ required: true, message: "请输入显示名", trigger: "blur" }],
  240. sort: [{ required: true, message: "请输入序号", trigger: "blur" }]
  241. },
  242. form: Object.assign({}, defaultForm),
  243. list: [],
  244. menus:[],
  245. totalCount: 0,
  246. listLoading: true,
  247. formLoading: false,
  248. listQuery: {
  249. Filter: "",
  250. Sorting: "",
  251. SkipCount: 0,
  252. MaxResultCount: 10,
  253. },
  254. page: 1,
  255. dialogFormVisible: false,
  256. multipleSelection: [],
  257. formTitle: "",
  258. isEdit: false,
  259. };
  260. },
  261. created() {
  262. this.getList();
  263. },
  264. methods: {
  265. selected(name) {
  266. this.form.icon = name;
  267. },
  268. getList() {
  269. this.listLoading = true;
  270. this.$axios.gets("/api/base/menu/all", this.listQuery).then((response) => {
  271. this.list = response.items.filter(_=>_.pid==null)
  272. this.setChildren(this.list,response.items)
  273. this.listLoading = false;
  274. });
  275. },
  276. setChildren(roots, items) {
  277. roots.forEach(element => {
  278. items.forEach(item => {
  279. if (item.pid == element.id) {
  280. if(!element.children)
  281. element.children=[]
  282. element.children.push(item);
  283. }
  284. });
  285. if (element.children) {
  286. this.setChildren(element.children, items);
  287. }
  288. });
  289. },
  290. fetchData(id) {
  291. this.$axios.gets("/api/base/menu/" + id).then((response) => {
  292. this.form = response;
  293. this.menus.push({id:response.pid, label:response.parentLabel})
  294. });
  295. },
  296. loadMenus({ action, parentNode, callback }) {
  297. if (action === LOAD_CHILDREN_OPTIONS) {
  298. this.$axios
  299. .gets("/api/base/menu/loadMenus", { id: parentNode.id })
  300. .then(response => {
  301. parentNode.children = response.items.map(function(obj) {
  302. if (!obj.leaf) {
  303. obj.children = null;
  304. }
  305. return obj;
  306. });
  307. setTimeout(() => {
  308. callback();
  309. }, 100);
  310. });
  311. }
  312. },
  313. handleFilter() {
  314. this.page = 1;
  315. this.getList();
  316. },
  317. save() {
  318. this.$refs.form.validate((valid) => {
  319. if (valid) {
  320. this.formLoading = true;
  321. this.form.roleNames = this.checkedRole;
  322. if (this.isEdit) {
  323. this.$axios
  324. .puts("/api/base/menu/" + this.form.id, this.form)
  325. .then((response) => {
  326. this.formLoading = false;
  327. this.$notify({
  328. title: "成功",
  329. message: "更新成功",
  330. type: "success",
  331. duration: 2000,
  332. });
  333. this.dialogFormVisible = false;
  334. this.getList();
  335. })
  336. .catch(() => {
  337. this.formLoading = false;
  338. });
  339. } else {
  340. this.$axios
  341. .posts("/api/base/menu", this.form)
  342. .then((response) => {
  343. this.formLoading = false;
  344. this.$notify({
  345. title: "成功",
  346. message: "新增成功",
  347. type: "success",
  348. duration: 2000,
  349. });
  350. this.dialogFormVisible = false;
  351. this.getList();
  352. })
  353. .catch(() => {
  354. this.formLoading = false;
  355. });
  356. }
  357. }
  358. });
  359. },
  360. handleCreate() {
  361. this.formTitle = "新增菜单";
  362. this.isEdit = false;
  363. this.dialogFormVisible = true;
  364. this.$axios.gets("/api/base/menu/loadMenus").then(response => {
  365. this.menus = response.items.map(function(obj) {
  366. if (!obj.leaf) {
  367. obj.children = null;
  368. }
  369. return obj;
  370. });
  371. });
  372. },
  373. handleDelete(row) {
  374. var params = [];
  375. let alert = "";
  376. if (row) {
  377. params.push(row.id);
  378. alert = row.name;
  379. } else {
  380. if (this.multipleSelection.length === 0) {
  381. this.$message({
  382. message: "未选择",
  383. type: "warning",
  384. });
  385. return;
  386. }
  387. this.multipleSelection.forEach((element) => {
  388. let id = element.id;
  389. params.push(id);
  390. });
  391. alert = "选中项";
  392. }
  393. this.$confirm("是否删除" + alert + "?", "提示", {
  394. confirmButtonText: "确定",
  395. cancelButtonText: "取消",
  396. type: "warning",
  397. })
  398. .then(() => {
  399. this.$axios.posts("/api/base/menu/delete", params).then((response) => {
  400. this.$notify({
  401. title: "成功",
  402. message: "删除成功",
  403. type: "success",
  404. duration: 2000,
  405. });
  406. this.getList();
  407. });
  408. })
  409. .catch(() => {
  410. this.$message({
  411. type: "info",
  412. message: "已取消删除",
  413. });
  414. });
  415. },
  416. handleUpdate(row) {
  417. this.formTitle = "修改菜单";
  418. this.isEdit = true;
  419. if (row) {
  420. this.fetchData(row.id);
  421. this.dialogFormVisible = true;
  422. } else {
  423. if (this.multipleSelection.length != 1) {
  424. this.$message({
  425. message: "编辑必须选择单行",
  426. type: "warning",
  427. });
  428. return;
  429. } else {
  430. this.fetchData(this.multipleSelection[0].id);
  431. this.dialogFormVisible = true;
  432. }
  433. }
  434. },
  435. sortChange(data) {
  436. const { prop, order } = data;
  437. if (!prop || !order) {
  438. this.handleFilter();
  439. return;
  440. }
  441. this.listQuery.Sorting = prop + " " + order;
  442. this.handleFilter();
  443. },
  444. handleSelectionChange(val) {
  445. this.multipleSelection = val;
  446. },
  447. handleRowClick(row, column, event) {
  448. this.$refs.multipleTable.clearSelection();
  449. this.$refs.multipleTable.toggleRowSelection(row);
  450. },
  451. cancel() {
  452. this.form = Object.assign({}, defaultForm);
  453. this.dialogFormVisible = false;
  454. this.$refs.form.clearValidate();
  455. },
  456. changeEnabled(data, val) {
  457. data.active = val ? "启用" : "停用";
  458. this.$confirm("是否" + data.active + data.name + "?", "提示", {
  459. confirmButtonText: "确定",
  460. cancelButtonText: "取消",
  461. type: "warning",
  462. })
  463. .then(() => {
  464. this.$axios
  465. .puts("/api/base/menu/" + data.id, data)
  466. .then((response) => {
  467. this.$notify({
  468. title: "成功",
  469. message: "更新成功",
  470. type: "success",
  471. duration: 2000,
  472. });
  473. })
  474. .catch(() => {
  475. data.enabled = !data.enabled;
  476. });
  477. })
  478. .catch(() => {
  479. data.enabled = !data.enabled;
  480. });
  481. },
  482. },
  483. };
  484. </script>