index.vue 6.1 KB

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