perf: 重构post

This commit is contained in:
xingyu 2023-02-01 16:45:37 +08:00
parent 654fce751a
commit d2805b1dca
2 changed files with 104 additions and 82 deletions

View File

@ -0,0 +1,93 @@
<template>
<!-- 弹窗 -->
<XModal id="postModel" :loading="modelLoading" v-model="modelVisible" :title="modelTitle">
<!-- 表单添加/修改 -->
<Form
ref="formRef"
v-if="['create', 'update'].includes(actionType)"
:schema="allSchemas.formSchema"
:rules="rules"
/>
<!-- 表单详情 -->
<Descriptions
v-if="actionType === 'detail'"
:schema="allSchemas.detailSchema"
:data="detailData"
/>
<template #footer>
<!-- 按钮保存 -->
<XButton
v-if="['create', 'update'].includes(actionType)"
type="primary"
:title="t('action.save')"
:loading="actionLoading"
@click="submitForm()"
/>
<!-- 按钮关闭 -->
<XButton :loading="actionLoading" :title="t('dialog.close')" @click="modelVisible = false" />
</template>
</XModal>
</template>
<script setup lang="ts">
import type { FormExpose } from '@/components/Form'
import * as PostApi from '@/api/system/post'
import { rules, allSchemas } from './post.data'
const { t } = useI18n() //
const message = useMessage() //
const emit = defineEmits(['success', 'openModal'])
//
const modelVisible = ref(false) //
const modelTitle = ref('edit') //
const modelLoading = ref(false) // loading
const actionType = ref('') //
const actionLoading = ref(false) // Loading
const formRef = ref<FormExpose>() // Ref
const detailData = ref() // Ref
const openModal = async (type: string, rowId?: number) => {
modelLoading.value = true
modelTitle.value = t('action.' + type)
actionType.value = type
modelVisible.value = true
//
if (rowId) {
const res = await PostApi.getPostApi(rowId)
if (type === 'update') {
unref(formRef)?.setValues(res)
} else if (type === 'detail') {
detailData.value = res
}
}
modelLoading.value = false
}
// /
const submitForm = async () => {
const elForm = unref(formRef)?.getElFormRef()
if (!elForm) return
elForm.validate(async (valid) => {
if (valid) {
actionLoading.value = true
//
try {
const data = unref(formRef)?.formModel as PostApi.PostVO
if (actionType.value === 'create') {
await PostApi.createPostApi(data)
message.success(t('common.createSuccess'))
} else {
await PostApi.updatePostApi(data)
message.success(t('common.updateSuccess'))
}
modelVisible.value = false
} finally {
actionLoading.value = false
emit('success')
}
}
})
}
defineExpose({ openModal: openModal })
</script>

View File

@ -9,7 +9,7 @@
preIcon="ep:zoom-in" preIcon="ep:zoom-in"
:title="t('action.add')" :title="t('action.add')"
v-hasPermi="['system:post:create']" v-hasPermi="['system:post:create']"
@click="openModel('create')" @click="openModal('create')"
/> />
<!-- 操作导出 --> <!-- 操作导出 -->
<XButton <XButton
@ -25,13 +25,13 @@
<XTextButton <XTextButton
preIcon="ep:edit" preIcon="ep:edit"
v-hasPermi="['system:post:update']" v-hasPermi="['system:post:update']"
@click="openModel('update', row.id)" @click="openModal('update', row.id)"
/> />
<!-- 操作详情 --> <!-- 操作详情 -->
<XTextButton <XTextButton
preIcon="ep:view" preIcon="ep:view"
v-hasPermi="['system:post:query']" v-hasPermi="['system:post:query']"
@click="openModel('detail', row.id)" @click="openModal('detail', row.id)"
/> />
<!-- 操作删除 --> <!-- 操作删除 -->
<XTextButton <XTextButton
@ -42,43 +42,16 @@
</template> </template>
</XTable> </XTable>
</ContentWrap> </ContentWrap>
<!-- 弹窗 --> <PostModal ref="modalRef" @success="success" />
<XModal id="postModel" :loading="modelLoading" v-model="modelVisible" :title="modelTitle">
<!-- 表单添加/修改 -->
<Form
ref="formRef"
v-if="['create', 'update'].includes(actionType)"
:schema="allSchemas.formSchema"
:rules="rules"
/>
<!-- 表单详情 -->
<Descriptions
v-if="actionType === 'detail'"
:schema="allSchemas.detailSchema"
:data="detailData"
/>
<template #footer>
<!-- 按钮保存 -->
<XButton
v-if="['create', 'update'].includes(actionType)"
type="primary"
:title="t('action.save')"
:loading="actionLoading"
@click="submitForm()"
/>
<!-- 按钮关闭 -->
<XButton :loading="actionLoading" :title="t('dialog.close')" @click="modelVisible = false" />
</template>
</XModal>
</template> </template>
<script setup lang="ts" name="Post"> <script setup lang="ts" name="Post">
import type { FormExpose } from '@/components/Form'
// import // import
import * as PostApi from '@/api/system/post' import * as PostApi from '@/api/system/post'
import { rules, allSchemas } from './post.data' import { allSchemas } from './post.data'
import PostModal from './PostModal.vue'
const { t } = useI18n() // const { t } = useI18n() //
const message = useMessage() // const modalRef = ref()
// //
const [registerTable, { reload, deleteData, exportList }] = useXTable({ const [registerTable, { reload, deleteData, exportList }] = useXTable({
allSchemas: allSchemas, allSchemas: allSchemas,
@ -86,56 +59,12 @@ const [registerTable, { reload, deleteData, exportList }] = useXTable({
deleteApi: PostApi.deletePostApi, deleteApi: PostApi.deletePostApi,
exportListApi: PostApi.exportPostApi exportListApi: PostApi.exportPostApi
}) })
//
const modelVisible = ref(false) //
const modelTitle = ref('edit') //
const modelLoading = ref(false) // loading
const actionType = ref('') //
const actionLoading = ref(false) // Loading
const formRef = ref<FormExpose>() // Ref
const detailData = ref() // Ref
const openModel = async (type: string, rowId?: number) => { const openModal = (type: string, rowId?: number) => {
modelLoading.value = true modalRef.value.openModal(type, rowId)
modelTitle.value = t('action.' + type)
actionType.value = type
modelVisible.value = true
//
if (rowId) {
const res = await PostApi.getPostApi(rowId)
if (type === 'update') {
unref(formRef)?.setValues(res)
} else if (type === 'detail') {
detailData.value = res
}
}
modelLoading.value = false
} }
// / const success = () => {
const submitForm = async () => {
const elForm = unref(formRef)?.getElFormRef()
if (!elForm) return
elForm.validate(async (valid) => {
if (valid) {
actionLoading.value = true
//
try {
const data = unref(formRef)?.formModel as PostApi.PostVO
if (actionType.value === 'create') {
await PostApi.createPostApi(data)
message.success(t('common.createSuccess'))
} else {
await PostApi.updatePostApi(data)
message.success(t('common.updateSuccess'))
}
modelVisible.value = false
} finally {
actionLoading.value = false
//
reload() reload()
} }
}
})
}
</script> </script>