index.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <script setup lang="ts">
  2. import { onMounted, ref, unref } from 'vue'
  3. import dayjs from 'dayjs'
  4. import { handleTree } from '@/utils/tree'
  5. import { DICT_TYPE } from '@/utils/dict'
  6. import { useTable } from '@/hooks/web/useTable'
  7. import { useI18n } from '@/hooks/web/useI18n'
  8. import { FormExpose } from '@/components/Form'
  9. import { TenantPackageVO } from '@/api/system/tenantPackage/types'
  10. import { ElMessage, ElCard, ElSwitch, ElTree } from 'element-plus'
  11. import { rules, allSchemas } from './tenantPackage.data'
  12. import * as TenantPackageApi from '@/api/system/tenantPackage'
  13. import { listSimpleMenusApi } from '@/api/system/menu'
  14. const { t } = useI18n() // 国际化
  15. const defaultProps = {
  16. children: 'children',
  17. label: 'name',
  18. value: 'id'
  19. }
  20. // ========== 创建菜单树结构 ==========
  21. const menuOptions = ref<any[]>([]) // 树形结构
  22. const treeRef = ref<InstanceType<typeof ElTree>>()
  23. const treeNodeAll = ref(false)
  24. // 全选/全不选
  25. const handleCheckedTreeNodeAll = () => {
  26. treeRef.value!.setCheckedNodes(treeNodeAll.value ? menuOptions.value : [])
  27. }
  28. const getTree = async () => {
  29. const res = await listSimpleMenusApi()
  30. menuOptions.value = handleTree(res)
  31. }
  32. const menuExpand = ref(false)
  33. const menuNodeAll = ref(false)
  34. // ========== 列表相关 ==========
  35. const { register, tableObject, methods } = useTable<TenantPackageVO>({
  36. getListApi: TenantPackageApi.getTenantPackageTypePageApi,
  37. delListApi: TenantPackageApi.deleteTenantPackageTypeApi
  38. })
  39. const { getList, setSearchParams, delList } = methods
  40. // ========== CRUD 相关 ==========
  41. const loading = ref(false) // 遮罩层
  42. const formRef = ref<FormExpose>() // 表单 Ref
  43. const actionType = ref('') // 操作按钮的类型
  44. const dialogVisible = ref(false) // 是否显示弹出层
  45. const dialogTitle = ref('edit') // 弹出层标题
  46. // 设置标题
  47. const setDialogTile = (type: string) => {
  48. dialogTitle.value = t('action.' + type)
  49. actionType.value = type
  50. dialogVisible.value = true
  51. }
  52. // 新增操作
  53. const handleCreate = () => {
  54. //重置菜单树
  55. unref(treeRef)?.setCheckedKeys([])
  56. menuExpand.value = false
  57. menuNodeAll.value = false
  58. setDialogTile('create')
  59. }
  60. // 修改操作
  61. const handleUpdate = async (row: any) => {
  62. setDialogTile('update')
  63. // 设置数据
  64. const res = await TenantPackageApi.getTenantPackageApi(row.id)
  65. unref(formRef)?.setValues(res)
  66. // 设置选中
  67. unref(treeRef)?.setCheckedKeys(res.menuIds)
  68. }
  69. // 提交按钮
  70. const submitForm = async () => {
  71. const elForm = unref(formRef)?.getElFormRef()
  72. if (!elForm) return
  73. elForm.validate(async (valid) => {
  74. if (valid) {
  75. loading.value = true
  76. // 提交请求
  77. try {
  78. const data = unref(formRef)?.formModel as TenantPackageVO
  79. data.menuIds = treeRef.value!.getCheckedKeys(false) as string[]
  80. if (actionType.value === 'create') {
  81. await TenantPackageApi.createTenantPackageTypeApi(data)
  82. ElMessage.success(t('common.createSuccess'))
  83. } else {
  84. await TenantPackageApi.updateTenantPackageTypeApi(data)
  85. ElMessage.success(t('common.updateSuccess'))
  86. }
  87. // 操作成功,重新加载列表
  88. dialogVisible.value = false
  89. await getList()
  90. } finally {
  91. loading.value = false
  92. }
  93. }
  94. })
  95. }
  96. // ========== 初始化 ==========
  97. onMounted(async () => {
  98. await getList()
  99. await getTree()
  100. })
  101. // getList()
  102. </script>
  103. <template>
  104. <!-- 搜索工作区 -->
  105. <ContentWrap>
  106. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  107. </ContentWrap>
  108. <ContentWrap>
  109. <!-- 操作工具栏 -->
  110. <div class="mb-10px">
  111. <el-button type="primary" @click="handleCreate">
  112. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  113. </el-button>
  114. </div>
  115. <!-- 列表 -->
  116. <Table
  117. :columns="allSchemas.tableColumns"
  118. :selection="false"
  119. :data="tableObject.tableList"
  120. :loading="tableObject.loading"
  121. :pagination="{
  122. total: tableObject.total
  123. }"
  124. v-model:pageSize="tableObject.pageSize"
  125. v-model:currentPage="tableObject.currentPage"
  126. @register="register"
  127. >
  128. <template #status="{ row }">
  129. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  130. </template>
  131. <template #createTime="{ row }">
  132. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  133. </template>
  134. <template #action="{ row }">
  135. <el-button link type="primary" @click="handleUpdate(row)">
  136. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  137. </el-button>
  138. <el-button link type="primary" @click="delList(row.id, false)">
  139. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  140. </el-button>
  141. </template>
  142. </Table>
  143. </ContentWrap>
  144. <Dialog v-model="dialogVisible" :title="dialogTitle" maxHeight="500px" width="50%">
  145. <!-- 对话框(添加 / 修改) -->
  146. <Form
  147. v-if="['create', 'update'].includes(actionType)"
  148. :schema="allSchemas.formSchema"
  149. :rules="rules"
  150. ref="formRef"
  151. >
  152. <template #menuIds>
  153. <el-card class="box-card">
  154. <template #header>
  155. <div class="card-header">
  156. 全选/全不选:
  157. <el-switch
  158. v-model="treeNodeAll"
  159. inline-prompt
  160. active-text="是"
  161. inactive-text="否"
  162. @change="handleCheckedTreeNodeAll()"
  163. />
  164. </div>
  165. </template>
  166. <el-tree
  167. ref="treeRef"
  168. node-key="id"
  169. show-checkbox
  170. :props="defaultProps"
  171. :data="menuOptions"
  172. empty-text="加载中,请稍后"
  173. />
  174. </el-card>
  175. </template>
  176. </Form>
  177. <!-- 操作按钮 -->
  178. <template #footer>
  179. <el-button
  180. v-if="['create', 'update'].includes(actionType)"
  181. type="primary"
  182. :loading="loading"
  183. @click="submitForm"
  184. >
  185. {{ t('action.save') }}
  186. </el-button>
  187. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  188. </template>
  189. </Dialog>
  190. </template>