menu.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. <template>
  2. <div>
  3. <div class="gva-table-box">
  4. <div class="gva-btn-list">
  5. <el-button
  6. type="primary"
  7. icon="plus"
  8. @click="addMenu(0)"
  9. >新增根菜单</el-button>
  10. </div>
  11. <!-- 由于此处菜单跟左侧列表一一对应所以不需要分页 pageSize默认999 -->
  12. <el-table
  13. :data="tableData"
  14. row-key="ID"
  15. >
  16. <el-table-column
  17. align="left"
  18. label="ID"
  19. min-width="100"
  20. prop="ID"
  21. />
  22. <el-table-column
  23. align="left"
  24. label="展示名称"
  25. min-width="120"
  26. prop="authorityName"
  27. >
  28. <template #default="scope">
  29. <span>{{ scope.row.meta.title }}</span>
  30. </template>
  31. </el-table-column>
  32. <el-table-column
  33. align="left"
  34. label="图标"
  35. min-width="140"
  36. prop="authorityName"
  37. >
  38. <template #default="scope">
  39. <div
  40. v-if="scope.row.meta.icon"
  41. class="icon-column"
  42. >
  43. <el-icon>
  44. <component :is="scope.row.meta.icon" />
  45. </el-icon>
  46. <span>{{ scope.row.meta.icon }}</span>
  47. </div>
  48. </template>
  49. </el-table-column>
  50. <el-table-column
  51. align="left"
  52. label="路由Name"
  53. show-overflow-tooltip
  54. min-width="160"
  55. prop="name"
  56. />
  57. <el-table-column
  58. align="left"
  59. label="路由Path"
  60. show-overflow-tooltip
  61. min-width="160"
  62. prop="path"
  63. />
  64. <el-table-column
  65. align="left"
  66. label="是否隐藏"
  67. min-width="100"
  68. prop="hidden"
  69. >
  70. <template #default="scope">
  71. <span>{{ scope.row.hidden ? "隐藏" : "显示" }}</span>
  72. </template>
  73. </el-table-column>
  74. <el-table-column
  75. align="left"
  76. label="父节点"
  77. min-width="90"
  78. prop="parentId"
  79. />
  80. <el-table-column
  81. align="left"
  82. label="排序"
  83. min-width="70"
  84. prop="sort"
  85. />
  86. <el-table-column
  87. align="left"
  88. label="文件路径"
  89. min-width="360"
  90. prop="component"
  91. />
  92. <el-table-column
  93. align="left"
  94. fixed="right"
  95. label="操作"
  96. width="300"
  97. >
  98. <template #default="scope">
  99. <el-button
  100. type="primary"
  101. link
  102. icon="plus"
  103. @click="addMenu(scope.row.ID)"
  104. >添加子菜单</el-button>
  105. <el-button
  106. type="primary"
  107. link
  108. icon="edit"
  109. @click="editMenu(scope.row.ID)"
  110. >编辑</el-button>
  111. <el-button
  112. type="primary"
  113. link
  114. icon="delete"
  115. @click="deleteMenu(scope.row.ID)"
  116. >删除</el-button>
  117. </template>
  118. </el-table-column>
  119. </el-table>
  120. </div>
  121. <el-drawer
  122. v-model="dialogFormVisible"
  123. size="60%"
  124. :before-close="handleClose"
  125. :show-close="false"
  126. >
  127. <template #header>
  128. <div class="flex justify-between items-center">
  129. <span class="text-lg">{{ dialogTitle }}</span>
  130. <div>
  131. <el-button @click="closeDialog">取 消</el-button>
  132. <el-button
  133. type="primary"
  134. @click="enterDialog"
  135. >确 定</el-button>
  136. </div>
  137. </div>
  138. </template>
  139. <warning-bar title="新增菜单,需要在角色管理内配置权限才可使用" />
  140. <el-form
  141. v-if="dialogFormVisible"
  142. ref="menuForm"
  143. :inline="true"
  144. :model="form"
  145. :rules="rules"
  146. label-position="top"
  147. label-width="85px"
  148. >
  149. <el-form-item
  150. label="路由Name"
  151. prop="path"
  152. style="width: 30%"
  153. >
  154. <el-input
  155. v-model="form.name"
  156. autocomplete="off"
  157. placeholder="唯一英文字符串"
  158. @change="changeName"
  159. />
  160. </el-form-item>
  161. <el-form-item
  162. prop="path"
  163. style="width: 30%"
  164. >
  165. <template #label>
  166. <span style="display: inline-flex; align-items: center">
  167. <span>路由Path</span>
  168. <el-checkbox
  169. v-model="checkFlag"
  170. style="margin-left: 12px; height: auto"
  171. >添加参数</el-checkbox>
  172. </span>
  173. </template>
  174. <el-input
  175. v-model="form.path"
  176. :disabled="!checkFlag"
  177. autocomplete="off"
  178. placeholder="建议只在后方拼接参数"
  179. />
  180. </el-form-item>
  181. <el-form-item
  182. label="是否隐藏"
  183. style="width: 30%"
  184. >
  185. <el-select
  186. v-model="form.hidden"
  187. placeholder="是否在列表隐藏"
  188. >
  189. <el-option
  190. :value="false"
  191. label="否"
  192. />
  193. <el-option
  194. :value="true"
  195. label="是"
  196. />
  197. </el-select>
  198. </el-form-item>
  199. <el-form-item
  200. label="父节点ID"
  201. style="width: 30%"
  202. >
  203. <el-cascader
  204. v-model="form.parentId"
  205. style="width: 100%"
  206. :disabled="!isEdit"
  207. :options="menuOption"
  208. :props="{
  209. checkStrictly: true,
  210. label: 'title',
  211. value: 'ID',
  212. disabled: 'disabled',
  213. emitPath: false,
  214. }"
  215. :show-all-levels="false"
  216. filterable
  217. />
  218. </el-form-item>
  219. <el-form-item
  220. label="文件路径"
  221. prop="component"
  222. style="width: 60%"
  223. >
  224. <el-input
  225. v-model="form.component"
  226. autocomplete="off"
  227. placeholder="页面:view/xxx/xx.vue 插件:plugin/xx/xx.vue"
  228. @blur="fmtComponent"
  229. />
  230. <span style="font-size: 12px; margin-right: 12px">如果菜单包含子菜单,请创建router-view二级路由页面或者</span><el-button
  231. style="margin-top: 4px"
  232. @click="form.component = 'view/routerHolder.vue'"
  233. >点我设置</el-button>
  234. </el-form-item>
  235. <el-form-item
  236. label="展示名称"
  237. prop="meta.title"
  238. style="width: 30%"
  239. >
  240. <el-input
  241. v-model="form.meta.title"
  242. autocomplete="off"
  243. />
  244. </el-form-item>
  245. <el-form-item
  246. label="图标"
  247. prop="meta.icon"
  248. style="width: 30%"
  249. >
  250. <icon
  251. :meta="form.meta"
  252. style="width: 100%"
  253. />
  254. </el-form-item>
  255. <el-form-item
  256. label="排序标记"
  257. prop="sort"
  258. style="width: 30%"
  259. >
  260. <el-input
  261. v-model.number="form.sort"
  262. autocomplete="off"
  263. />
  264. </el-form-item>
  265. <el-form-item
  266. prop="meta.activeName"
  267. style="width: 30%"
  268. >
  269. <template #label>
  270. <div>
  271. <span> 高亮菜单 </span>
  272. <el-tooltip
  273. content="注:当到达此路由时候,指定左侧菜单指定name会处于活跃状态(亮起),可为空,为空则为本路由Name。"
  274. placement="top"
  275. effect="light"
  276. >
  277. <el-icon><QuestionFilled /></el-icon>
  278. </el-tooltip>
  279. </div>
  280. </template>
  281. <el-input
  282. v-model="form.meta.activeName"
  283. :placeholder="form.name"
  284. autocomplete="off"
  285. />
  286. </el-form-item>
  287. <el-form-item
  288. label="KeepAlive"
  289. prop="meta.keepAlive"
  290. style="width: 30%"
  291. >
  292. <el-select
  293. v-model="form.meta.keepAlive"
  294. style="width: 100%"
  295. placeholder="是否keepAlive缓存页面"
  296. >
  297. <el-option
  298. :value="false"
  299. label="否"
  300. />
  301. <el-option
  302. :value="true"
  303. label="是"
  304. />
  305. </el-select>
  306. </el-form-item>
  307. <el-form-item
  308. label="CloseTab"
  309. prop="meta.closeTab"
  310. style="width: 30%"
  311. >
  312. <el-select
  313. v-model="form.meta.closeTab"
  314. style="width: 100%"
  315. placeholder="是否自动关闭tab"
  316. >
  317. <el-option
  318. :value="false"
  319. label="否"
  320. />
  321. <el-option
  322. :value="true"
  323. label="是"
  324. />
  325. </el-select>
  326. </el-form-item>
  327. <el-form-item style="width: 30%">
  328. <template #label>
  329. <div>
  330. <span> 是否为基础页面 </span>
  331. <el-tooltip
  332. content="此项选择为是,则不会展示左侧菜单以及顶部信息。"
  333. placement="top"
  334. effect="light"
  335. >
  336. <el-icon><QuestionFilled /></el-icon>
  337. </el-tooltip>
  338. </div>
  339. </template>
  340. <el-select
  341. v-model="form.meta.defaultMenu"
  342. style="width: 100%"
  343. placeholder="是否为基础页面"
  344. >
  345. <el-option
  346. :value="false"
  347. label="否"
  348. />
  349. <el-option
  350. :value="true"
  351. label="是"
  352. />
  353. </el-select>
  354. </el-form-item>
  355. </el-form>
  356. <div>
  357. <div class="flex items-center gap-2">
  358. <el-button
  359. type="primary"
  360. icon="edit"
  361. @click="addParameter(form)"
  362. >新增菜单参数</el-button>
  363. </div>
  364. <el-table
  365. :data="form.parameters"
  366. style="width: 100%; margin-top: 12px"
  367. >
  368. <el-table-column
  369. align="left"
  370. prop="type"
  371. label="参数类型"
  372. width="180"
  373. >
  374. <template #default="scope">
  375. <el-select
  376. v-model="scope.row.type"
  377. placeholder="请选择"
  378. >
  379. <el-option
  380. key="query"
  381. value="query"
  382. label="query"
  383. />
  384. <el-option
  385. key="params"
  386. value="params"
  387. label="params"
  388. />
  389. </el-select>
  390. </template>
  391. </el-table-column>
  392. <el-table-column
  393. align="left"
  394. prop="key"
  395. label="参数key"
  396. width="180"
  397. >
  398. <template #default="scope">
  399. <div>
  400. <el-input v-model="scope.row.key" />
  401. </div>
  402. </template>
  403. </el-table-column>
  404. <el-table-column
  405. align="left"
  406. prop="value"
  407. label="参数值"
  408. >
  409. <template #default="scope">
  410. <div>
  411. <el-input v-model="scope.row.value" />
  412. </div>
  413. </template>
  414. </el-table-column>
  415. <el-table-column align="left">
  416. <template #default="scope">
  417. <div>
  418. <el-button
  419. type="danger"
  420. icon="delete"
  421. @click="deleteParameter(form.parameters, scope.$index)"
  422. >删除</el-button>
  423. </div>
  424. </template>
  425. </el-table-column>
  426. </el-table>
  427. <div class="flex items-center gap-2 mt-3">
  428. <el-button
  429. type="primary"
  430. icon="edit"
  431. @click="addBtn(form)"
  432. >新增可控按钮
  433. </el-button>
  434. <el-icon
  435. class="cursor-pointer"
  436. @click="
  437. toDoc('https://www.gin-vue-admin.com/guide/web/button-auth.html')
  438. "
  439. ><QuestionFilled /></el-icon>
  440. </div>
  441. <el-table
  442. :data="form.menuBtn"
  443. style="width: 100%; margin-top: 12px"
  444. >
  445. <el-table-column
  446. align="left"
  447. prop="name"
  448. label="按钮名称"
  449. width="180"
  450. >
  451. <template #default="scope">
  452. <div>
  453. <el-input v-model="scope.row.name" />
  454. </div>
  455. </template>
  456. </el-table-column>
  457. <el-table-column
  458. align="left"
  459. prop="name"
  460. label="备注"
  461. width="180"
  462. >
  463. <template #default="scope">
  464. <div>
  465. <el-input v-model="scope.row.desc" />
  466. </div>
  467. </template>
  468. </el-table-column>
  469. <el-table-column align="left">
  470. <template #default="scope">
  471. <div>
  472. <el-button
  473. type="danger"
  474. icon="delete"
  475. @click="deleteBtn(form.menuBtn, scope.$index)"
  476. >删除</el-button>
  477. </div>
  478. </template>
  479. </el-table-column>
  480. </el-table>
  481. </div>
  482. </el-drawer>
  483. </div>
  484. </template>
  485. <script setup>
  486. import {
  487. updateBaseMenu,
  488. getMenuList,
  489. addBaseMenu,
  490. deleteBaseMenu,
  491. getBaseMenuById,
  492. } from '@/api/menu'
  493. import icon from '@/view/superAdmin/menu/icon.vue'
  494. import WarningBar from '@/components/warningBar/warningBar.vue'
  495. import { canRemoveAuthorityBtnApi } from '@/api/authorityBtn'
  496. import { reactive, ref } from 'vue'
  497. import { ElMessage, ElMessageBox } from 'element-plus'
  498. import { QuestionFilled, VideoCameraFilled } from '@element-plus/icons-vue'
  499. import { toDoc } from '@/utils/doc'
  500. defineOptions({
  501. name: 'Menus',
  502. })
  503. const rules = reactive({
  504. path: [{ required: true, message: '请输入菜单name', trigger: 'blur' }],
  505. component: [{ required: true, message: '请输入文件路径', trigger: 'blur' }],
  506. 'meta.title': [
  507. { required: true, message: '请输入菜单展示名称', trigger: 'blur' },
  508. ],
  509. })
  510. const page = ref(1)
  511. const total = ref(0)
  512. const pageSize = ref(999)
  513. const tableData = ref([])
  514. const searchInfo = ref({})
  515. // 查询
  516. const getTableData = async() => {
  517. const table = await getMenuList({
  518. page: page.value,
  519. pageSize: pageSize.value,
  520. ...searchInfo.value,
  521. })
  522. if (table.code === 0) {
  523. tableData.value = table.data.list
  524. total.value = table.data.total
  525. page.value = table.data.page
  526. pageSize.value = table.data.pageSize
  527. }
  528. }
  529. getTableData()
  530. // 新增参数
  531. const addParameter = (form) => {
  532. if (!form.parameters) {
  533. form.parameters = []
  534. }
  535. form.parameters.push({
  536. type: 'query',
  537. key: '',
  538. value: '',
  539. })
  540. }
  541. const fmtComponent = () => {
  542. form.value.component = form.value.component.replace(/\\/g, '/')
  543. }
  544. // 删除参数
  545. const deleteParameter = (parameters, index) => {
  546. parameters.splice(index, 1)
  547. }
  548. // 新增可控按钮
  549. const addBtn = (form) => {
  550. if (!form.menuBtn) {
  551. form.menuBtn = []
  552. }
  553. form.menuBtn.push({
  554. name: '',
  555. desc: '',
  556. })
  557. }
  558. // 删除可控按钮
  559. const deleteBtn = async(btns, index) => {
  560. const btn = btns[index]
  561. if (btn.ID === 0) {
  562. btns.splice(index, 1)
  563. return
  564. }
  565. const res = await canRemoveAuthorityBtnApi({ id: btn.ID })
  566. if (res.code === 0) {
  567. btns.splice(index, 1)
  568. }
  569. }
  570. const form = ref({
  571. ID: 0,
  572. path: '',
  573. name: '',
  574. hidden: false,
  575. parentId: 0,
  576. component: '',
  577. meta: {
  578. activeName: '',
  579. title: '',
  580. icon: '',
  581. defaultMenu: false,
  582. closeTab: false,
  583. keepAlive: false,
  584. },
  585. parameters: [],
  586. menuBtn: [],
  587. })
  588. const changeName = () => {
  589. form.value.path = form.value.name
  590. }
  591. const handleClose = (done) => {
  592. initForm()
  593. done()
  594. }
  595. // 删除菜单
  596. const deleteMenu = (ID) => {
  597. ElMessageBox.confirm('此操作将永久删除所有角色下该菜单, 是否继续?', '提示', {
  598. confirmButtonText: '确定',
  599. cancelButtonText: '取消',
  600. type: 'warning',
  601. })
  602. .then(async() => {
  603. const res = await deleteBaseMenu({ ID })
  604. if (res.code === 0) {
  605. ElMessage({
  606. type: 'success',
  607. message: '删除成功!',
  608. })
  609. if (tableData.value.length === 1 && page.value > 1) {
  610. page.value--
  611. }
  612. getTableData()
  613. }
  614. })
  615. .catch(() => {
  616. ElMessage({
  617. type: 'info',
  618. message: '已取消删除',
  619. })
  620. })
  621. }
  622. // 初始化弹窗内表格方法
  623. const menuForm = ref(null)
  624. const checkFlag = ref(false)
  625. const initForm = () => {
  626. checkFlag.value = false
  627. menuForm.value.resetFields()
  628. form.value = {
  629. ID: 0,
  630. path: '',
  631. name: '',
  632. hidden: false,
  633. parentId: 0,
  634. component: '',
  635. meta: {
  636. title: '',
  637. icon: '',
  638. defaultMenu: false,
  639. closeTab: false,
  640. keepAlive: false,
  641. },
  642. }
  643. }
  644. // 关闭弹窗
  645. const dialogFormVisible = ref(false)
  646. const closeDialog = () => {
  647. initForm()
  648. dialogFormVisible.value = false
  649. }
  650. // 添加menu
  651. const enterDialog = async() => {
  652. menuForm.value.validate(async(valid) => {
  653. if (valid) {
  654. let res
  655. if (isEdit.value) {
  656. res = await updateBaseMenu(form.value)
  657. } else {
  658. res = await addBaseMenu(form.value)
  659. }
  660. if (res.code === 0) {
  661. ElMessage({
  662. type: 'success',
  663. message: isEdit.value ? '编辑成功' : '添加成功!',
  664. })
  665. getTableData()
  666. }
  667. initForm()
  668. dialogFormVisible.value = false
  669. }
  670. })
  671. }
  672. const menuOption = ref([
  673. {
  674. ID: '0',
  675. title: '根菜单',
  676. },
  677. ])
  678. const setOptions = () => {
  679. menuOption.value = [
  680. {
  681. ID: 0,
  682. title: '根目录',
  683. },
  684. ]
  685. setMenuOptions(tableData.value, menuOption.value, false)
  686. }
  687. const setMenuOptions = (menuData, optionsData, disabled) => {
  688. menuData &&
  689. menuData.forEach((item) => {
  690. if (item.children && item.children.length) {
  691. const option = {
  692. title: item.meta.title,
  693. ID: item.ID,
  694. disabled: disabled || item.ID === form.value.ID,
  695. children: [],
  696. }
  697. setMenuOptions(
  698. item.children,
  699. option.children,
  700. disabled || item.ID === form.value.ID
  701. )
  702. optionsData.push(option)
  703. } else {
  704. const option = {
  705. title: item.meta.title,
  706. ID: item.ID,
  707. disabled: disabled || item.ID === form.value.ID,
  708. }
  709. optionsData.push(option)
  710. }
  711. })
  712. }
  713. // 添加菜单方法,id为 0则为添加根菜单
  714. const isEdit = ref(false)
  715. const dialogTitle = ref('新增菜单')
  716. const addMenu = (id) => {
  717. dialogTitle.value = '新增菜单'
  718. form.value.parentId = id
  719. isEdit.value = false
  720. setOptions()
  721. dialogFormVisible.value = true
  722. }
  723. // 修改菜单方法
  724. const editMenu = async(id) => {
  725. dialogTitle.value = '编辑菜单'
  726. const res = await getBaseMenuById({ id })
  727. form.value = res.data.menu
  728. isEdit.value = true
  729. setOptions()
  730. dialogFormVisible.value = true
  731. }
  732. </script>
  733. <style scoped lang="scss">
  734. .warning {
  735. color: #dc143c;
  736. }
  737. .icon-column {
  738. display: flex;
  739. align-items: center;
  740. .el-icon {
  741. margin-right: 8px;
  742. }
  743. }
  744. </style>