index.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. onMounted(async () => {
  99. await getList()
  100. await getTree()
  101. })
  102. // getList()
  103. </script>
  104. <template>
  105. <!-- 搜索工作区 -->
  106. <ContentWrap>
  107. <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  108. </ContentWrap>
  109. <ContentWrap>
  110. <!-- 操作工具栏 -->
  111. <div class="mb-10px">
  112. <el-button type="primary" @click="handleCreate">
  113. <Icon icon="ep:zoom-in" class="mr-5px" /> {{ t('action.add') }}
  114. </el-button>
  115. </div>
  116. <!-- 列表 -->
  117. <Table
  118. :columns="allSchemas.tableColumns"
  119. :selection="false"
  120. :data="tableObject.tableList"
  121. :loading="tableObject.loading"
  122. :pagination="{
  123. total: tableObject.total
  124. }"
  125. v-model:pageSize="tableObject.pageSize"
  126. v-model:currentPage="tableObject.currentPage"
  127. @register="register"
  128. >
  129. <template #status="{ row }">
  130. <DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
  131. </template>
  132. <template #createTime="{ row }">
  133. <span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
  134. </template>
  135. <template #action="{ row }">
  136. <el-button link type="primary" @click="handleUpdate(row)">
  137. <Icon icon="ep:edit" class="mr-1px" /> {{ t('action.edit') }}
  138. </el-button>
  139. <el-button link type="primary" @click="delList(row.id, false)">
  140. <Icon icon="ep:delete" class="mr-1px" /> {{ t('action.del') }}
  141. </el-button>
  142. </template>
  143. </Table>
  144. </ContentWrap>
  145. <Dialog v-model="dialogVisible" :title="dialogTitle" maxHeight="500px" width="50%">
  146. <!-- 对话框(添加 / 修改) -->
  147. <Form
  148. v-if="['create', 'update'].includes(actionType)"
  149. :schema="allSchemas.formSchema"
  150. :rules="rules"
  151. ref="formRef"
  152. >
  153. <template #menuIds>
  154. <el-card class="box-card">
  155. <template #header>
  156. <div class="card-header">
  157. <el-checkbox>展开/折叠</el-checkbox>
  158. <el-checkbox>全选/全不选</el-checkbox>
  159. </div>
  160. </template>
  161. <!-- default-expand-all-->
  162. <el-tree
  163. ref="treeRef"
  164. node-key="id"
  165. v-model="menuParentId"
  166. :props="defaultProps"
  167. :data="menuOptions"
  168. show-checkbox
  169. :check-strictly="menuCheckStrictly"
  170. empty-text="加载菜单中..."
  171. />
  172. </el-card>
  173. </template>
  174. </Form>
  175. <!-- 操作按钮 -->
  176. <template #footer>
  177. <el-button
  178. v-if="['create', 'update'].includes(actionType)"
  179. type="primary"
  180. :loading="loading"
  181. @click="submitForm"
  182. >
  183. {{ t('action.save') }}
  184. </el-button>
  185. <el-button @click="dialogVisible = false">{{ t('dialog.close') }}</el-button>
  186. </template>
  187. </Dialog>
  188. </template>