index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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, ElCheckbox, 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([]) // 树形结构
  22. const treeRef = ref<InstanceType<typeof ElTree>>()
  23. const getTree = async () => {
  24. const res = await listSimpleMenusApi()
  25. menuOptions.value = handleTree(res)
  26. }
  27. let menuCheckStrictly = true
  28. const menuExpand = ref(false)
  29. const menuNodeAll = ref(false)
  30. // ========== 列表相关 ==========
  31. const { register, tableObject, methods } = useTable<TenantPackageVO>({
  32. getListApi: TenantPackageApi.getTenantPackageTypePageApi,
  33. delListApi: TenantPackageApi.deleteTenantPackageTypeApi
  34. })
  35. const { getList, setSearchParams, delList } = methods
  36. // ========== CRUD 相关 ==========
  37. const loading = ref(false) // 遮罩层
  38. const formRef = ref<FormExpose>() // 表单 Ref
  39. const actionType = ref('') // 操作按钮的类型
  40. const dialogVisible = ref(false) // 是否显示弹出层
  41. const dialogTitle = ref('edit') // 弹出层标题
  42. const menuParentId = ref()
  43. // 设置标题
  44. const setDialogTile = (type: string) => {
  45. dialogTitle.value = t('action.' + type)
  46. actionType.value = type
  47. dialogVisible.value = true
  48. }
  49. // 新增操作
  50. const handleCreate = () => {
  51. setDialogTile('create')
  52. // 重置表单
  53. unref(formRef)?.getElFormRef()?.resetFields()
  54. //重置菜单树
  55. unref(treeRef)?.setCheckedKeys([])
  56. menuCheckStrictly = true
  57. menuExpand.value = false
  58. menuNodeAll.value = false
  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. // 设置为严格,避免设置父节点自动选中子节点,解决半选中问题
  68. menuCheckStrictly = true
  69. // 设置选中
  70. unref(treeRef)?.setCheckedKeys(res.menuIds)
  71. // 设置为非严格,继续使用半选中
  72. menuCheckStrictly = false
  73. }
  74. // 提交按钮
  75. const submitForm = async () => {
  76. loading.value = true
  77. // 提交请求
  78. try {
  79. const data = unref(formRef)?.formModel as TenantPackageVO
  80. data.menuIds = treeRef.value!.getCheckedKeys(false) as string[]
  81. if (actionType.value === 'create') {
  82. await TenantPackageApi.createTenantPackageTypeApi(data)
  83. ElMessage.success(t('common.createSuccess'))
  84. console.log('new data')
  85. } else {
  86. await TenantPackageApi.updateTenantPackageTypeApi(data)
  87. ElMessage.success(t('common.updateSuccess'))
  88. console.log('edit data')
  89. }
  90. // 操作成功,重新加载列表
  91. dialogVisible.value = false
  92. await getList()
  93. } finally {
  94. loading.value = false
  95. }
  96. }
  97. // 删除操作
  98. const handleDelete = (row: TenantPackageVO) => {
  99. delList(row.id, false)
  100. }
  101. // ========== 初始化 ==========
  102. onMounted(async () => {
  103. await getList()
  104. await getTree()
  105. })
  106. // getList()
  107. </script>
  108. <template>
  109. <!-- 搜索工作区 -->
  110. <ContentWrap>
  111. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  112. </ContentWrap>
  113. <ContentWrap>
  114. <!-- 操作工具栏 -->
  115. <div class="mb-10px">
  116. <el-button type="primary" @click="handleCreate">
  117. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  118. </el-button>
  119. </div>
  120. <!-- 列表 -->
  121. <Table
  122. :columns="allSchemas.tableColumns"
  123. :selection="false"
  124. :data="tableObject.tableList"
  125. :loading="tableObject.loading"
  126. :pagination="{
  127. total: tableObject.total
  128. }"
  129. v-model:pageSize="tableObject.pageSize"
  130. v-model:currentPage="tableObject.currentPage"
  131. @register="register"
  132. >
  133. <template #status="{ row }">
  134. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  135. </template>
  136. <template #createTime="{ row }">
  137. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  138. </template>
  139. <template #action="{ row }">
  140. <el-button link type="primary" @click="handleUpdate(row)">
  141. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  142. </el-button>
  143. <el-button link type="primary" @click="handleDelete(row)">
  144. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  145. </el-button>
  146. </template>
  147. </Table>
  148. </ContentWrap>
  149. <Dialog v-model="dialogVisible" :title="dialogTitle" maxHeight="500px" width="50%">
  150. <!-- 对话框(添加 / 修改) -->
  151. <Form
  152. v-if="['create', 'update'].includes(actionType)"
  153. :schema="allSchemas.formSchema"
  154. :rules="rules"
  155. ref="formRef"
  156. >
  157. <template #menuIds>
  158. <el-card class="box-card">
  159. <template #header>
  160. <div class="card-header">
  161. <el-checkbox>展开/折叠</el-checkbox>
  162. <el-checkbox>全选/全不选</el-checkbox>
  163. </div>
  164. </template>
  165. <!-- default-expand-all-->
  166. <el-tree
  167. ref="treeRef"
  168. node-key="id"
  169. v-model="menuParentId"
  170. :props="defaultProps"
  171. :data="menuOptions"
  172. show-checkbox
  173. :check-strictly="menuCheckStrictly"
  174. empty-text="加载菜单中..."
  175. />
  176. </el-card>
  177. </template>
  178. </Form>
  179. <!-- 操作按钮 -->
  180. <template #footer>
  181. <el-button
  182. v-if="['create', 'update'].includes(actionType)"
  183. type="primary"
  184. :loading="loading"
  185. @click="submitForm"
  186. >
  187. {{ t('action.save') }}
  188. </el-button>
  189. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  190. </template>
  191. </Dialog>
  192. </template>