refactor: 轮播图模态框

This commit is contained in:
ilhamtahir committed 2024-12-04 21:00:21 +08:00
1 parent 0558261ba7
commit 73fb136b91
5 files changed
+40 -33

No files matched your search

+2 -2
View File
@@ -39,13 +39,13 @@ const props = defineProps({
const emits = defineEmits(['on-success']) const emits = defineEmits(['on-success'])
watch(() => props.imageUrl,(newVal) => { watch(() => props.imageUrl,(newVal) => {
if(newVal){ if (!newVal) return
if (newVal.url !== '') { if (newVal.url !== '') {
file1.value = [{...newVal}] file1.value = [{...newVal}]
} else { } else {
file1.value = [] file1.value = []
} }
}
},{ },{
deep:true, deep:true,
immediate:true immediate:true
@@ -60,9 +60,9 @@ const props = defineProps<{
article?: Article | {} article?: Article | {}
categoryList: Category[] | [] categoryList: Category[] | []
}>() }>()
const form = ref<FormInstanceFunctions | null>(null) const formRef = ref<FormInstanceFunctions | null>(null)
const header = computed(() => `${props.isEdit ? '编辑' : '创建'}${props.headerTitle || ''}`) const headerText = computed(() => `${props.isEdit ? '编辑' : '创建'}${props.headerTitle || ''}`)
const confirmBtn = computed(() => (props.isEdit ? '保存' : '确定')) const confirmBtnText = computed(() => (props.isEdit ? '保存' : '确定'))
const articleData: FormProps['data'] = reactive({ categoryName: '' }) const articleData: FormProps['data'] = reactive({ categoryName: '' })
const popupVisible = ref(false) const popupVisible = ref(false)
@@ -1,14 +1,13 @@
<template> <template>
<t-dialog <TDialog
width="70%" width="980px"
:header="header" :header="headerText"
:confirmBtn="confirmBtn" :confirmBtn="confirmBtnText"
:closeOnOverlayClick="false" :closeOnOverlayClick="false"
@close="handleCloseDialog" @close="handleCloseDialog"
@confirm="fetchSaveCarousalData" @confirm="fetchSaveCarousalData"
> >
<t-space direction="vertical" style="width: 100%"> <TForm v-if="carousalData" ref="formRef" :data="carousalData" :rules="rules" resetType="initial" label-width="100">
<TForm ref="form" :data="carousalData" :rules="rules" resetType="initial" label-width="100">
<TFormItem label="轮播图:" name="imageUrl"> <TFormItem label="轮播图:" name="imageUrl">
<ImageUpload @on-success="handleSuccessImg" :image-url="imgUrl"/> <ImageUpload @on-success="handleSuccessImg" :image-url="imgUrl"/>
</TFormItem> </TFormItem>
@@ -24,25 +23,33 @@
<t-input v-model.trim="carousalData.targetUrl" clearable placeholder="请输入网址路径" /> <t-input v-model.trim="carousalData.targetUrl" clearable placeholder="请输入网址路径" />
</TFormItem> </TFormItem>
</TForm> </TForm>
</t-space> </TDialog>
</t-dialog>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { computed, ref, reactive, watch } from 'vue' import { computed, ref, reactive, watch } from 'vue'
import type { FormInstanceFunctions, FormProps } from 'tdesign-vue-next' import type { FormInstanceFunctions, FormProps } from 'tdesign-vue-next'
import ImageUpload from '@/components/ImageUpload.vue' import ImageUpload from '@/components/ImageUpload.vue'
import { createCarousal, updateCarousalItem } from '@/api/carousal' import { createCarousal, updateCarousalItem } from '@/api/carousal'
// Todo: 改为ts方式
const props = defineProps<{ const props = defineProps<{
isEdit?: Boolean | null isEdit?: Boolean | null
headerTitle?: String | null headerTitle?: String | null
carousal: {} | number carousal: {} | number
}>() }>()
const emit = defineEmits(['success']) const emit = defineEmits<{
const form = ref<FormInstanceFunctions | null>(null) success: [string]
const header = computed(() => `${props.isEdit ? '编辑' : '创建'}${props.headerTitle || ''}`) }>()
const formRef = ref<FormInstanceFunctions | null>(null)
const headerText = computed(() => `${props.isEdit ? '编辑' : '创建'}${props.headerTitle || ''}`)
const confirmBtnText = computed(() => (props.isEdit ? '保存' : '确定'))
const formType = computed(() => `${props.isEdit ? 'update' : 'add'}`) const formType = computed(() => `${props.isEdit ? 'update' : 'add'}`)
const confirmBtn = computed(() => (props.isEdit ? '保存' : '确定'))
const carousalData: FormProps['data'] = ref({}) const carousalData = ref<FormProps['data']>({
imageUrl: '',
targetType: 'article',
targetId: '',
targetUrl: '',
})
const imgUrl = ref({}) const imgUrl = ref({})
const rules = computed(() => { const rules = computed(() => {
const commonRules = { const commonRules = {
@@ -104,14 +111,15 @@ watch(
) )
const fetchSaveCarousalData = async () => { const fetchSaveCarousalData = async () => {
/* TODO: 保存分类数据 */ /* TODO: 保存分类数据 */
if (form.value) { if (formRef.value) {
const valid = await form.value.validate() const valid = await formRef.value.validate()
// console.log(valid,'valid') // console.log(valid,'valid')
if (valid && typeof valid === 'boolean') { if (valid && typeof valid === 'boolean') {
/* TODO: 校验通过保存分类数据 */ /* TODO: 校验通过保存分类数据 */
handleFormTypeData() handleFormTypeData()
// console.log(formType.value,'formType') // console.log(formType.value,'formType')
let res let res
switch (formType.value){ switch (formType.value){
case 'create': case 'create':
res = await createCarousal(carousalData.value) res = await createCarousal(carousalData.value)
@@ -143,9 +151,9 @@ const fetchSaveCarousalData = async () => {
} }
const handleCloseDialog = () => { const handleCloseDialog = () => {
// 数据&&规则校验结果重置 // 数据&&规则校验结果重置
if (form.value) { if (formRef.value) {
form.value.reset() formRef.value.reset()
form.value.clearValidate() formRef.value.clearValidate()
} }
} }
</script> </script>
+6 -7
View File
@@ -40,12 +40,12 @@ const isEdit = ref<boolean>(false)
const currentTableData = ref<CarousalResult | null>(null) const currentTableData = ref<CarousalResult | null>(null)
const editClick = (row: object): void => { const handleEdit = (row: object): void => {
isEdit.value = true isEdit.value = true
currentTableData.value = Object.assign({}, row) currentTableData.value = Object.assign({}, row)
carousalDialogVisible.value = true carousalDialogVisible.value = true
} }
const showClick = async (res: CarousalResult): Promise<void> => { const handleShow = async (res: CarousalResult): Promise<void> => {
if (res.status === 'active') { if (res.status === 'active') {
await inactiveCarousal(res.id) await inactiveCarousal(res.id)
} else { } else {
@@ -53,7 +53,7 @@ const showClick = async (res: CarousalResult): Promise<void> => {
} }
await fetchData() await fetchData()
} }
const delClick = async (row: CarousalResult): Promise<void> => { const handleDelete = async (row: CarousalResult): Promise<void> => {
await deleteCarousal(row.id) await deleteCarousal(row.id)
await fetchData() await fetchData()
} }
@@ -62,7 +62,6 @@ const handleCreateCarousal = (): void => {
carousalDialogVisible.value = true carousalDialogVisible.value = true
currentTableData.value = {} currentTableData.value = {}
} }
const handleSuccessDialog = async (): Promise<void> => { const handleSuccessDialog = async (): Promise<void> => {
await fetchData() await fetchData()
carousalDialogVisible.value = false carousalDialogVisible.value = false
@@ -109,11 +108,11 @@ const handleSuccessDialog = async (): Promise<void> => {
</TSpace> </TSpace>
</template> </template>
<template #operation="{ row }"> <template #operation="{ row }">
<TButton variant="text" size="small" theme="primary" @click="editClick(row)">编辑</TButton> <TButton variant="text" size="small" theme="primary" @click="handleEdit(row)">编辑</TButton>
<TButton variant="text" size="small" theme="primary" @click="showClick(row)">{{ <TButton variant="text" size="small" theme="primary" @click="handleShow(row)">{{
row.status === 'active' ? '关闭显示' : '开启显示' row.status === 'active' ? '关闭显示' : '开启显示'
}}</TButton> }}</TButton>
<TButton variant="text" size="small" theme="danger" @click="delClick(row)">删除</TButton> <TButton variant="text" size="small" theme="danger" @click="handleDelete(row)">删除</TButton>
</template> </template>
</TTable> </TTable>
<CarousalDialog <CarousalDialog
@@ -39,9 +39,9 @@ const props = defineProps<{
headerTitle?: string headerTitle?: string
category: Category | null category: Category | null
}>() }>()
const form = ref<FormInstanceFunctions | null>(null) const formRef = ref<FormInstanceFunctions | null>(null)
const header = computed(() => `${props.category ? '编辑' : '创建'}${props.headerTitle || ''}`) const headerText = computed(() => `${props.category ? '编辑' : '创建'}${props.headerTitle || ''}`)
const confirmBtn = computed(() => (props.category ? '保存' : '确定')) const confirmBtnText = computed(() => (props.category ? '保存' : '确定'))
const categoryData: FormProps['data'] = reactive({ name: '', order: 1 }) const categoryData: FormProps['data'] = reactive({ name: '', order: 1 })
const rules: FormProps['rules'] = { const rules: FormProps['rules'] = {
name: [ name: [