index.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <script lang="ts" setup>
  2. import type { VbenFormProps } from '@vben/common-ui';
  3. import type { VxeGridListeners, VxeGridProps } from '#/adapter/vxe-table';
  4. import { h } from 'vue';
  5. import { Page, useVbenModal } from '@vben/common-ui';
  6. import { MdiDelete, MdiDetail, MdiEdit } from '@vben/icons';
  7. import { parseQueryValues } from '@vben/utils';
  8. import { ElMessage, ElMessageBox, ElTag } from 'element-plus';
  9. import { useVbenVxeGrid } from '#/adapter/vxe-table';
  10. import { deleteMenuApi, getMenuListApi, initPermissionApi } from '#/api/menu';
  11. import { $t } from '#/locales';
  12. import MenuForm from './form.vue';
  13. const formOptions: VbenFormProps = {
  14. // 默认展开
  15. collapsed: true,
  16. // 控制表单是否显示折叠按钮
  17. showCollapseButton: true,
  18. // 按下回车时是否提交表单
  19. submitOnEnter: true,
  20. schema: [
  21. {
  22. component: 'Input',
  23. fieldName: 'menu_name',
  24. label: '菜单名称',
  25. componentProps: {
  26. placeholder: $t('ui.placeholder.input'),
  27. clearable: true,
  28. },
  29. },
  30. // {
  31. // component: 'Input',
  32. // fieldName: 'menu_sort',
  33. // label: '菜单分类',
  34. // componentProps: {
  35. // placeholder: $t('ui.placeholder.input'),
  36. // clearable: true,
  37. // },
  38. // },
  39. {
  40. component: 'Select',
  41. fieldName: 'menu_type',
  42. label: '菜单类型',
  43. componentProps: {
  44. placeholder: $t('ui.placeholder.select'),
  45. clearable: true,
  46. clearable: true,
  47. options: [
  48. { label: '目录', value: 1 },
  49. { label: '菜单', value: 2 },
  50. { label: '按钮', value: 3 },
  51. { label: 'API接口', value: 4 },
  52. ],
  53. },
  54. },
  55. ],
  56. wrapperClass: 'grid-cols-1 md:grid-cols-3 lg:grid-cols-5',
  57. };
  58. const gridOptions: VxeGridProps<any> = {
  59. toolbarConfig: {
  60. custom: true,
  61. export: true,
  62. refresh: true,
  63. zoom: true,
  64. },
  65. height: 'auto',
  66. exportConfig: {},
  67. pagerConfig: {},
  68. rowConfig: {
  69. isHover: true,
  70. },
  71. stripe: true,
  72. proxyConfig: {
  73. response: {
  74. result: 'Data',
  75. total: 'Total',
  76. },
  77. ajax: {
  78. query: async ({ page }, formValues) => {
  79. return await getMenuListApi({
  80. pageindex: page.currentPage,
  81. rows: page.pageSize,
  82. ...parseQueryValues(formValues),
  83. });
  84. },
  85. },
  86. },
  87. columns: [
  88. { title: '菜单名称', field: 'menu_name', sortable: true },
  89. { title: '备注', field: 'remark', sortable: true },
  90. // { title: '菜单分类', field: 'menu_sort' },
  91. {
  92. title: '菜单类型',
  93. field: 'menu_type',
  94. sortable: true,
  95. slots: {
  96. default: ({ row }) => {
  97. const type = row.menu_type;
  98. const typeMap = {
  99. 1: { text: '目录', type: 'primary' },
  100. 2: { text: '菜单', type: 'success' },
  101. 3: { text: '按钮', type: 'warning' },
  102. 4: { text: 'API接口', type: 'info' },
  103. };
  104. return h(
  105. ElTag,
  106. {
  107. // @ts-ignore 运行时类型安全,但TypeScript无法正确推断
  108. type: typeMap[type]?.type || 'info',
  109. effect: 'light',
  110. size: 'small',
  111. },
  112. // @ts-ignore 运行时类型安全,但TypeScript无法正确推断
  113. () => typeMap[type]?.text || '未知',
  114. );
  115. },
  116. },
  117. },
  118. { title: '菜单标识', field: 'menu_key', sortable: true },
  119. { title: '路径', field: 'path', sortable: true },
  120. { title: '组件', field: 'component', sortable: true },
  121. { title: '图标', field: 'icon', sortable: true },
  122. {
  123. title: '状态',
  124. field: 'visible',
  125. sortable: true,
  126. slots: {
  127. default: ({ row }) => {
  128. const status = row.visible;
  129. return h(
  130. ElTag,
  131. {
  132. type: status === 0 ? 'success' : 'danger',
  133. effect: 'dark',
  134. size: 'small',
  135. round: true,
  136. },
  137. () => (status === 0 ? '显示' : '隐藏'),
  138. );
  139. },
  140. },
  141. },
  142. { title: '排序', field: 'order_num', sortable: true },
  143. {
  144. title: '操作',
  145. field: 'action',
  146. fixed: 'right',
  147. slots: { default: 'action' },
  148. width: 150,
  149. },
  150. ],
  151. };
  152. const gridEvents: VxeGridListeners<any> = {
  153. cellDblclick: ({ row }) => {
  154. handleDetail(row);
  155. },
  156. };
  157. const [Grid, gridApi] = useVbenVxeGrid({
  158. gridEvents,
  159. gridOptions,
  160. formOptions,
  161. });
  162. const [Modal, modalApi] = useVbenModal({
  163. fullscreenButton: false,
  164. closeOnClickModal: false,
  165. closeOnPressEscape: false,
  166. connectedComponent: MenuForm,
  167. });
  168. /* 创建 */
  169. function handleCreate() {
  170. modalApi.setState({ showCancelButton: true });
  171. modalApi.setData({ formType: 'create' }).open();
  172. }
  173. /* 编辑 */
  174. function handleEdit(row: any) {
  175. modalApi.setState({ showCancelButton: true });
  176. modalApi.setData({ formType: 'edit', row }).open();
  177. }
  178. /* 详情 */
  179. function handleDetail(row: any) {
  180. modalApi.setState({ showCancelButton: false });
  181. modalApi.setData({ formType: 'detail', row }).open();
  182. }
  183. /* 删除 */
  184. async function handleDelete(row: any) {
  185. try {
  186. await ElMessageBox.confirm('确认要删除该菜单吗?', '提示', {
  187. confirmButtonText: '确定',
  188. cancelButtonText: '取消',
  189. type: 'warning',
  190. });
  191. await deleteMenuApi({ 'menu_id.value': row.menu_id });
  192. ElMessage.success('删除成功');
  193. gridApi.reload();
  194. } catch (error) {
  195. console.error(error);
  196. }
  197. }
  198. /* 初始化权限 */
  199. async function handleInitPermission() {
  200. try {
  201. await ElMessageBox.confirm('确认要初始化权限吗?', '提示', {
  202. confirmButtonText: '确定',
  203. cancelButtonText: '取消',
  204. type: 'warning',
  205. });
  206. await initPermissionApi();
  207. ElMessage.success('初始化成功');
  208. gridApi.reload();
  209. } catch (error) {
  210. console.error(error);
  211. }
  212. }
  213. function handleFinish() {
  214. gridApi.reload();
  215. }
  216. </script>
  217. <template>
  218. <Page auto-content-height>
  219. <Grid table-title="菜单列表">
  220. <template #toolbar-tools>
  221. <el-button type="warning" @click="handleInitPermission">
  222. 初始化权限
  223. </el-button>
  224. <el-button type="primary" @click="handleCreate"> 新增 </el-button>
  225. </template>
  226. <template #action="{ row }">
  227. <el-tooltip
  228. class="box-item"
  229. effect="dark"
  230. content="详情"
  231. placement="top"
  232. >
  233. <el-button
  234. round
  235. @click="() => handleDetail(row)"
  236. :icon="MdiDetail"
  237. class="!p-2"
  238. />
  239. </el-tooltip>
  240. <el-tooltip
  241. class="box-item"
  242. effect="dark"
  243. content="编辑"
  244. placement="top"
  245. >
  246. <el-button
  247. round
  248. @click="() => handleEdit(row)"
  249. :icon="MdiEdit"
  250. class="!p-2"
  251. />
  252. </el-tooltip>
  253. <el-tooltip
  254. class="box-item"
  255. effect="dark"
  256. content="删除"
  257. placement="top"
  258. >
  259. <el-button
  260. round
  261. @click="() => handleDelete(row)"
  262. :icon="MdiDelete"
  263. class="!p-2"
  264. />
  265. </el-tooltip>
  266. </template>
  267. </Grid>
  268. <Modal @finish="handleFinish" />
  269. </Page>
  270. </template>