index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <template>
  2. <div class="table-box">
  3. <ProTable
  4. ref="proTable"
  5. title="菜单列表"
  6. row-key="menuId"
  7. :columns="columns"
  8. :pagination="false"
  9. :request-api="listMenuApi"
  10. :init-param="initParam"
  11. :data-callback="dataCallback"
  12. >
  13. <!-- 表格 header 按钮 -->
  14. <template #tableHeader>
  15. <el-button type="primary" v-auth="['system:menu:add']" @click="openDialog(1, '菜单新增')">新增</el-button>
  16. </template>
  17. <!-- 菜单图标 -->
  18. <template #icon="scope">
  19. <!-- <el-icon :size="18" v-if="scope.row.icon">
  20. <component :is="scope.row.icon"></component>
  21. </el-icon> -->
  22. <svg-icon v-if="scope.row.icon" :name="scope.row.icon" />
  23. </template>
  24. <!-- 表格操作 -->
  25. <template #operation="scope">
  26. <el-button type="primary" link icon="EditPen" v-auth="['system:menu:edit']" @click="openDialog(2, '菜单编辑', scope.row)"> 编辑 </el-button>
  27. <el-button type="primary" link icon="CirclePlus" v-auth="['system:menu:add']" @click="openDialog(1, '菜单新增')"> 新增 </el-button>
  28. <el-button type="primary" link icon="Delete" v-auth="['system:menu:remove']" @click="deleteMenu(scope.row)"> 删除 </el-button>
  29. </template>
  30. </ProTable>
  31. <FormDialog ref="formDialogRef" :items-options="itemsOptions" :model="model" />
  32. </div>
  33. </template>
  34. <script setup lang="tsx" name="MenuManage">
  35. import { useHandleData } from '@/hooks/useHandleData'
  36. import FormDialog from '@/components/FormDialog/index.vue'
  37. import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
  38. import { handleTree } from '@/utils/common'
  39. import { listMenuApi, delMenuApi, addMenuApi, updateMenuApi, getMenuApi } from '@/api/modules/system/menu'
  40. const { proxy } = getCurrentInstance() as ComponentInternalInstance
  41. const { sys_normal_disable, sys_yes_no, sys_show_hide } = toRefs<any>(proxy?.useDict('sys_normal_disable', 'sys_yes_no', 'sys_show_hide'))
  42. // ProTable 实例
  43. const proTable = ref<ProTableInstance>()
  44. // 表单model
  45. const model = ref({})
  46. // 如果表格需要初始化请求参数,直接定义传给 ProTable (之后每次请求都会自动带上该参数,此参数更改之后也会一直带上,改变此参数会自动刷新表格数据)
  47. const initParam = reactive({})
  48. const menuOptions = ref<any[]>([])
  49. const menuTypeOptions = ref<any[]>([
  50. { value: 'M', label: '目录' },
  51. { value: 'C', label: '菜单' },
  52. { value: 'F', label: '按钮' }
  53. ])
  54. // dataCallback 是对于返回的表格数据做处理,如果你后台返回的数据不是 list && total && pageNum && pageSize 这些字段,可以在这里进行处理成这些字段
  55. const dataCallback = (res: any) => {
  56. const data = handleTree(res, 'menuId')
  57. menuOptions.value = []
  58. const menu: any = { menuId: 0, menuName: '主类目', children: [] }
  59. menu.children = data
  60. menuOptions.value.push(menu)
  61. setItemsOptions()
  62. return data
  63. }
  64. // 删除用户信息
  65. const deleteMenu = async (params: any) => {
  66. await useHandleData(delMenuApi, params.menuId, `删除【${params.menuName}】菜单`)
  67. proTable.value?.getTableList()
  68. }
  69. const formDialogRef = ref<InstanceType<typeof FormDialog> | null>(null)
  70. // 打开弹框的功能
  71. const openDialog = async (type: number, title: string, row?: any) => {
  72. let res = { data: {} }
  73. if (row?.menuId) {
  74. res = await getMenuApi(row?.menuId || null)
  75. }
  76. model.value = type == 1 ? {} : res.data
  77. const params = {
  78. title,
  79. width: 720,
  80. isEdit: type !== 3,
  81. api: type == 1 ? addMenuApi : updateMenuApi,
  82. getTableList: proTable.value?.getTableList
  83. }
  84. formDialogRef.value?.openDialog(params)
  85. }
  86. // 表格配置项
  87. const columns = reactive<ColumnProps<any>[]>([
  88. {
  89. prop: 'menuName',
  90. label: '菜单名称',
  91. align: 'left',
  92. search: {
  93. el: 'input'
  94. },
  95. width: 180
  96. },
  97. {
  98. prop: 'orderNum',
  99. label: '显示排序',
  100. search: {
  101. el: 'input'
  102. },
  103. width: 120
  104. },
  105. {
  106. prop: 'path',
  107. label: '路由地址',
  108. search: {
  109. el: 'input'
  110. },
  111. width: 120
  112. },
  113. {
  114. prop: 'status',
  115. label: '状态',
  116. tag: true,
  117. enum: sys_normal_disable,
  118. search: {
  119. el: 'tree-select'
  120. }
  121. },
  122. {
  123. prop: 'perms',
  124. label: '权限标识',
  125. search: {
  126. el: 'input'
  127. },
  128. width: 120
  129. },
  130. {
  131. prop: 'icon',
  132. label: '图标',
  133. search: {
  134. el: 'input'
  135. },
  136. width: 120
  137. },
  138. {
  139. prop: 'createTime',
  140. label: '创建时间',
  141. search: {
  142. el: 'date-picker',
  143. props: { type: 'datetimerange', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
  144. },
  145. width: 120
  146. },
  147. { prop: 'operation', label: '操作', width: 230, fixed: 'right' }
  148. ])
  149. // 表单配置项
  150. let itemsOptions = ref<ProForm.ItemsOptions[]>([])
  151. const setItemsOptions = () => {
  152. itemsOptions.value = [
  153. {
  154. label: '菜单类型',
  155. prop: 'menuType',
  156. span: 12,
  157. rules: [{ required: true, message: '菜单类型不能为空', trigger: 'change' }],
  158. compOptions: {
  159. elTagName: 'radio-button',
  160. enum: menuTypeOptions.value
  161. }
  162. },
  163. {
  164. label: '菜单状态',
  165. prop: 'status',
  166. rules: [{ required: true, message: '菜单状态不能为空', trigger: 'change' }],
  167. span: 12,
  168. compOptions: {
  169. elTagName: 'radio-button',
  170. value: '0',
  171. enum: sys_normal_disable.value
  172. }
  173. },
  174. {
  175. label: '上级菜单',
  176. rules: [{ required: true, message: '上级菜单不能为空', trigger: 'change' }],
  177. prop: 'parentId',
  178. compOptions: {
  179. elTagName: 'tree-select',
  180. placeholder: '请选择上级菜单',
  181. props: { label: 'menuName', value: 'menuId' },
  182. enum: menuOptions.value,
  183. checkStrictly: true
  184. }
  185. },
  186. {
  187. label: '菜单名称',
  188. prop: 'menuName',
  189. span: 12,
  190. rules: [{ required: true, message: '菜单名称不能为空', trigger: 'blur' }],
  191. compOptions: {
  192. elTagName: 'input',
  193. placeholder: '请输入菜单名称'
  194. }
  195. },
  196. {
  197. label: '显示排序',
  198. prop: 'orderNum',
  199. span: 12,
  200. compOptions: {
  201. elTagName: 'input-number',
  202. min: 0,
  203. controlsPosition: 'right'
  204. }
  205. },
  206. {
  207. label: '图标',
  208. prop: 'icon',
  209. show: val => {
  210. return val?.menuType !== 'F'
  211. },
  212. compOptions: {
  213. elTagName: 'icon'
  214. }
  215. },
  216. {
  217. label: '是否外链',
  218. prop: 'isFrame',
  219. span: 12,
  220. show: val => {
  221. return val?.menuType !== 'F'
  222. },
  223. tooltip: '选择是外链则路由地址需要以`http(s)://`开头',
  224. compOptions: {
  225. elTagName: 'radio-button',
  226. enum: sys_yes_no.value,
  227. enumKey: 'sys_yes_no'
  228. }
  229. },
  230. {
  231. label: '显示状态',
  232. prop: 'visible',
  233. tooltip: '选择隐藏则路由将不会出现在侧边栏,但仍然可以访问',
  234. span: 12,
  235. show: val => {
  236. return val?.menuType !== 'F'
  237. },
  238. compOptions: {
  239. elTagName: 'radio-button',
  240. value: '0',
  241. enum: sys_show_hide.value
  242. }
  243. },
  244. {
  245. label: '路由地址',
  246. rules: [{ required: true, message: '路由地址不能为空', trigger: 'blur' }],
  247. prop: 'path',
  248. show: val => {
  249. return val?.menuType !== 'F'
  250. },
  251. tooltip: '访问的路由地址,如:`user`,如外网地址需内链访问则以`http(s)://`开头',
  252. compOptions: {
  253. elTagName: 'input',
  254. placeholder: '请输入路由地址'
  255. }
  256. },
  257. {
  258. label: '路由参数',
  259. prop: 'query',
  260. show: val => {
  261. return val?.menuType !== 'F'
  262. },
  263. tooltip: '访问路由的默认传递参数,如:`{"id": 1, "name": "eco"}`',
  264. compOptions: {
  265. elTagName: 'input',
  266. placeholder: '请输入路由地址'
  267. }
  268. },
  269. {
  270. label: '组件路径',
  271. rules: [{ required: true, message: '组件路径不能为空', trigger: 'blur' }],
  272. prop: 'component',
  273. tooltip: '访问的组件路径,如:`system/user/index`,默认在`views`目录下',
  274. span: 12,
  275. show: val => {
  276. return val?.menuType == 'C'
  277. },
  278. compOptions: {
  279. elTagName: 'input',
  280. placeholder: '请输入组件路径'
  281. }
  282. },
  283. {
  284. label: '路由名称',
  285. // rules: [{ required: true, message: '路由名称不能为空', trigger: 'blur' }],
  286. prop: 'componentName',
  287. tooltip: '路由名称,匹配路由内Name属性,如:UserManage',
  288. span: 12,
  289. show: val => {
  290. return val?.menuType == 'C'
  291. },
  292. compOptions: {
  293. elTagName: 'input',
  294. placeholder: '请输入路由名称'
  295. }
  296. },
  297. {
  298. label: '权限标识',
  299. prop: 'perms',
  300. tooltip: '控制器中定义的权限字符,如:system:user:list',
  301. span: 12,
  302. show: val => {
  303. return val?.menuType !== 'M'
  304. },
  305. compOptions: {
  306. elTagName: 'input',
  307. placeholder: '请输入权限标识'
  308. }
  309. },
  310. {
  311. label: '是否缓存',
  312. prop: 'isCache',
  313. span: 12,
  314. show: val => {
  315. return val?.menuType == 'C'
  316. },
  317. compOptions: {
  318. elTagName: 'radio-button',
  319. enum: sys_show_hide.value,
  320. enumKey: 'sys_yes_no'
  321. }
  322. }
  323. ]
  324. }
  325. </script>