index.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <doc-alert title="功能权限" url="https://doc.iocoder.cn/resource-permission" />
  3. <doc-alert title="菜单路由" url="https://doc.iocoder.cn/vue3/route/" />
  4. <!-- 搜索工作栏 -->
  5. <ContentWrap>
  6. <el-form
  7. ref="queryFormRef"
  8. :inline="true"
  9. :model="queryParams"
  10. class="-mb-15px"
  11. label-width="68px"
  12. >
  13. <el-form-item label="菜单名称" prop="name">
  14. <el-input
  15. v-model="queryParams.name"
  16. class="!w-240px"
  17. clearable
  18. placeholder="请输入菜单名称"
  19. @keyup.enter="handleQuery"
  20. />
  21. </el-form-item>
  22. <el-form-item label="状态" prop="status">
  23. <el-select
  24. v-model="queryParams.status"
  25. class="!w-240px"
  26. clearable
  27. placeholder="请选择菜单状态"
  28. >
  29. <el-option
  30. v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
  31. :key="dict.value"
  32. :label="dict.label"
  33. :value="dict.value"
  34. />
  35. </el-select>
  36. </el-form-item>
  37. <el-form-item>
  38. <el-button @click="handleQuery">
  39. <Icon class="mr-5px" icon="ep:search" />
  40. 搜索
  41. </el-button>
  42. <el-button @click="resetQuery">
  43. <Icon class="mr-5px" icon="ep:refresh" />
  44. 重置
  45. </el-button>
  46. <el-button
  47. v-hasPermi="['system:menu:create']"
  48. plain
  49. type="primary"
  50. @click="openForm('create')"
  51. >
  52. <Icon class="mr-5px" icon="ep:plus" />
  53. 新增
  54. </el-button>
  55. <el-button plain @click="refreshMenu">
  56. <Icon class="mr-5px" icon="ep:refresh" />
  57. 刷新菜单缓存
  58. </el-button>
  59. </el-form-item>
  60. </el-form>
  61. </ContentWrap>
  62. <!-- 列表 -->
  63. <ContentWrap>
  64. <div style="height: 700px">
  65. <!-- AutoResizer 自动调节大小 -->
  66. <el-auto-resizer>
  67. <template #default="{ height, width }">
  68. <!-- Virtualized Table 虚拟化表格:高性能,解决表格在大数据量下的卡顿问题 -->
  69. <el-table-v2
  70. v-loading="loading"
  71. :columns="columns"
  72. :data="list"
  73. :width="width"
  74. :height="height"
  75. expand-column-key="name"
  76. />
  77. </template>
  78. </el-auto-resizer>
  79. </div>
  80. </ContentWrap>
  81. <!-- 表单弹窗:添加/修改 -->
  82. <MenuForm ref="formRef" @success="getList" />
  83. </template>
  84. <script lang="ts" setup>
  85. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  86. import { handleTree } from '@/utils/tree'
  87. import * as MenuApi from '@/api/system/menu'
  88. import { MenuVO } from '@/api/system/menu'
  89. import MenuForm from './MenuForm.vue'
  90. import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
  91. import { h } from 'vue'
  92. import { Column, ElButton } from 'element-plus'
  93. import { Icon } from '@/components/Icon'
  94. import { hasPermission } from '@/directives/permission/hasPermi'
  95. import { CommonStatusEnum } from '@/utils/constants'
  96. defineOptions({ name: 'SystemMenu' })
  97. const { wsCache } = useCache()
  98. const { t } = useI18n() // 国际化
  99. const message = useMessage() // 消息弹窗
  100. // 表格的 column 字段
  101. const columns: Column[] = [
  102. {
  103. dataKey: 'name',
  104. title: '菜单名称',
  105. width: 250
  106. },
  107. {
  108. dataKey: 'icon',
  109. title: '图标',
  110. width: 150,
  111. cellRenderer: ({ rowData }) => {
  112. return h(Icon, {
  113. icon: rowData.icon
  114. })
  115. }
  116. },
  117. {
  118. dataKey: 'sort',
  119. title: '排序',
  120. width: 100
  121. },
  122. {
  123. dataKey: 'permission',
  124. title: '权限标识',
  125. width: 240
  126. },
  127. {
  128. dataKey: 'component',
  129. title: '组件路径',
  130. width: 240
  131. },
  132. {
  133. dataKey: 'componentName',
  134. title: '组件名称',
  135. width: 240
  136. },
  137. {
  138. dataKey: 'status',
  139. title: '状态',
  140. width: 160,
  141. cellRenderer: ({ rowData }) => {
  142. return h(ElSwitch, {
  143. modelValue: rowData.status,
  144. activeValue: CommonStatusEnum.ENABLE,
  145. inactiveValue: CommonStatusEnum.DISABLE,
  146. loading: menuStatusUpdating.value[rowData.id],
  147. disabled: !hasPermission(['system:menu:update']),
  148. onChange: (val) => handleStatusChanged(rowData, val as number)
  149. })
  150. }
  151. },
  152. {
  153. dataKey: 'operation',
  154. title: '操作',
  155. width: 200,
  156. cellRenderer: ({ rowData }) => {
  157. return h(
  158. 'div',
  159. [
  160. hasPermission(['system:menu:update']) &&
  161. h(
  162. ElButton,
  163. {
  164. link: true,
  165. type: 'primary',
  166. onClick: () => openForm('update', rowData.id)
  167. },
  168. '修改'
  169. ),
  170. hasPermission(['system:menu:create']) &&
  171. h(
  172. ElButton,
  173. {
  174. link: true,
  175. type: 'primary',
  176. onClick: () => openForm('create', undefined, rowData.id)
  177. },
  178. '新增'
  179. ),
  180. hasPermission(['system:menu:delete']) &&
  181. h(
  182. ElButton,
  183. {
  184. link: true,
  185. type: 'danger',
  186. onClick: () => handleDelete(rowData.id)
  187. },
  188. '删除'
  189. )
  190. ].filter(Boolean)
  191. )
  192. }
  193. }
  194. ]
  195. const loading = ref(true) // 列表的加载中
  196. const list = ref<any>([]) // 列表的数据
  197. const queryParams = reactive({
  198. name: undefined,
  199. status: undefined
  200. })
  201. const queryFormRef = ref() // 搜索的表单
  202. /** 查询列表 */
  203. const getList = async () => {
  204. loading.value = true
  205. try {
  206. const data = await MenuApi.getMenuList(queryParams)
  207. list.value = handleTree(data)
  208. } finally {
  209. loading.value = false
  210. }
  211. }
  212. /** 搜索按钮操作 */
  213. const handleQuery = () => {
  214. getList()
  215. }
  216. /** 重置按钮操作 */
  217. const resetQuery = () => {
  218. queryFormRef.value.resetFields()
  219. handleQuery()
  220. }
  221. /** 添加/修改操作 */
  222. const formRef = ref()
  223. const openForm = (type: string, id?: number, parentId?: number) => {
  224. formRef.value.open(type, id, parentId)
  225. }
  226. /** 刷新菜单缓存按钮操作 */
  227. const refreshMenu = async () => {
  228. try {
  229. await message.confirm('即将更新缓存刷新浏览器!', '刷新菜单缓存')
  230. // 清空,从而触发刷新
  231. wsCache.delete(CACHE_KEY.USER)
  232. wsCache.delete(CACHE_KEY.ROLE_ROUTERS)
  233. // 刷新浏览器
  234. location.reload()
  235. } catch {}
  236. }
  237. /** 删除按钮操作 */
  238. const handleDelete = async (id: number) => {
  239. try {
  240. // 删除的二次确认
  241. await message.delConfirm()
  242. // 发起删除
  243. await MenuApi.deleteMenu(id)
  244. message.success(t('common.delSuccess'))
  245. // 刷新列表
  246. await getList()
  247. } catch {}
  248. }
  249. /** 开启/关闭菜单的状态 */
  250. const menuStatusUpdating = ref({}) // 菜单状态更新中的 menu 映射。key:菜单编号,value:是否更新中
  251. const handleStatusChanged = async (menu: MenuVO, val: number) => {
  252. // 1. 标记 menu.id 更新中
  253. menuStatusUpdating.value[menu.id] = true
  254. try {
  255. // 2. 发起更新状态
  256. menu.status = val
  257. await MenuApi.updateMenu(menu)
  258. } finally {
  259. // 3. 标记 menu.id 更新完成
  260. menuStatusUpdating.value[menu.id] = false
  261. }
  262. }
  263. /** 初始化 **/
  264. onMounted(() => {
  265. getList()
  266. })
  267. </script>