index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <ContentWrap>
  3. <!-- 列表 -->
  4. <vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
  5. <template #toolbar_buttons>
  6. <XButton
  7. type="primary"
  8. preIcon="ep:zoom-in"
  9. :title="t('action.add')"
  10. @click="handleCreate()"
  11. />
  12. </template>
  13. <template #actionbtns_default="{ row }">
  14. <XTextButton preIcon="ep:edit" :title="t('action.edit')" @click="handleUpdate(row.id)" />
  15. <XTextButton preIcon="ep:delete" :title="t('action.del')" @click="handleDelete(row.id)" />
  16. </template>
  17. </vxe-grid>
  18. </ContentWrap>
  19. <XModal v-model="dialogVisible" :title="dialogTitle">
  20. <!-- 对话框(添加 / 修改) -->
  21. <Form
  22. v-if="['create', 'update'].includes(actionType)"
  23. :schema="allSchemas.formSchema"
  24. :rules="rules"
  25. ref="formRef"
  26. >
  27. <template #menuIds>
  28. <el-card class="w-120">
  29. <template #header>
  30. <div class="card-header">
  31. 全选/全不选:
  32. <el-switch
  33. v-model="treeNodeAll"
  34. inline-prompt
  35. active-text="是"
  36. inactive-text="否"
  37. @change="handleCheckedTreeNodeAll()"
  38. />
  39. </div>
  40. </template>
  41. <el-tree
  42. ref="treeRef"
  43. node-key="id"
  44. show-checkbox
  45. :props="defaultProps"
  46. :data="menuOptions"
  47. empty-text="加载中,请稍后"
  48. />
  49. </el-card>
  50. </template>
  51. </Form>
  52. <!-- 操作按钮 -->
  53. <template #footer>
  54. <!-- 按钮:保存 -->
  55. <XButton
  56. v-if="['create', 'update'].includes(actionType)"
  57. type="primary"
  58. :title="t('action.save')"
  59. :loading="loading"
  60. @click="submitForm()"
  61. />
  62. <!-- 按钮:关闭 -->
  63. <XButton :loading="loading" :title="t('dialog.close')" @click="dialogVisible = false" />
  64. </template>
  65. </XModal>
  66. </template>
  67. <script setup lang="ts" name="TenantPackage">
  68. import { onMounted, ref, unref } from 'vue'
  69. import { handleTree, defaultProps } from '@/utils/tree'
  70. import { useI18n } from '@/hooks/web/useI18n'
  71. import { useMessage } from '@/hooks/web/useMessage'
  72. import { useVxeGrid } from '@/hooks/web/useVxeGrid'
  73. import { VxeGridInstance } from 'vxe-table'
  74. import { FormExpose } from '@/components/Form'
  75. import { ElCard, ElSwitch, ElTree } from 'element-plus'
  76. // 业务相关的 import
  77. import { rules, allSchemas } from './tenantPackage.data'
  78. import * as TenantPackageApi from '@/api/system/tenantPackage'
  79. import { listSimpleMenusApi } from '@/api/system/menu'
  80. const { t } = useI18n() // 国际化
  81. const message = useMessage() // 消息弹窗
  82. const menuOptions = ref<any[]>([]) // 树形结构
  83. const menuExpand = ref(false)
  84. const menuNodeAll = ref(false)
  85. const treeRef = ref<InstanceType<typeof ElTree>>()
  86. const treeNodeAll = ref(false)
  87. const xGrid = ref<VxeGridInstance>() // 列表 Grid Ref
  88. const formRef = ref<FormExpose>() // 表单 Ref
  89. const loading = ref(false) // 遮罩层
  90. const actionType = ref('') // 操作按钮的类型
  91. const dialogVisible = ref(false) // 是否显示弹出层
  92. const dialogTitle = ref('edit') // 弹出层标题
  93. // 全选/全不选
  94. const handleCheckedTreeNodeAll = () => {
  95. treeRef.value!.setCheckedNodes(treeNodeAll.value ? menuOptions.value : [])
  96. }
  97. const getTree = async () => {
  98. const res = await listSimpleMenusApi()
  99. menuOptions.value = handleTree(res)
  100. }
  101. const { gridOptions, getList, deleteData } = useVxeGrid<TenantPackageApi.TenantPackageVO>({
  102. allSchemas: allSchemas,
  103. getListApi: TenantPackageApi.getTenantPackageTypePageApi,
  104. deleteApi: TenantPackageApi.deleteTenantPackageTypeApi
  105. })
  106. // 设置标题
  107. const setDialogTile = (type: string) => {
  108. dialogTitle.value = t('action.' + type)
  109. actionType.value = type
  110. dialogVisible.value = true
  111. }
  112. // 新增操作
  113. const handleCreate = () => {
  114. //重置菜单树
  115. unref(treeRef)?.setCheckedKeys([])
  116. menuExpand.value = false
  117. menuNodeAll.value = false
  118. setDialogTile('create')
  119. }
  120. // 修改操作
  121. const handleUpdate = async (rowId: number) => {
  122. setDialogTile('update')
  123. // 设置数据
  124. const res = await TenantPackageApi.getTenantPackageApi(rowId)
  125. unref(formRef)?.setValues(res)
  126. // 设置选中
  127. unref(treeRef)?.setCheckedKeys(res.menuIds)
  128. }
  129. // 删除操作
  130. const handleDelete = async (rowId: number) => {
  131. await deleteData(xGrid, rowId)
  132. }
  133. // 提交按钮
  134. const submitForm = async () => {
  135. const elForm = unref(formRef)?.getElFormRef()
  136. if (!elForm) return
  137. elForm.validate(async (valid) => {
  138. if (valid) {
  139. loading.value = true
  140. // 提交请求
  141. try {
  142. const data = unref(formRef)?.formModel as TenantPackageApi.TenantPackageVO
  143. data.menuIds = treeRef.value!.getCheckedKeys(false) as number[]
  144. if (actionType.value === 'create') {
  145. await TenantPackageApi.createTenantPackageTypeApi(data)
  146. message.success(t('common.createSuccess'))
  147. } else {
  148. await TenantPackageApi.updateTenantPackageTypeApi(data)
  149. message.success(t('common.updateSuccess'))
  150. }
  151. // 操作成功,重新加载列表
  152. dialogVisible.value = false
  153. } finally {
  154. loading.value = false
  155. // 刷新列表
  156. await getList(xGrid)
  157. }
  158. }
  159. })
  160. }
  161. // ========== 初始化 ==========
  162. onMounted(async () => {
  163. await getTree()
  164. })
  165. // getList()
  166. </script>