add:增加编辑、删除等功能

This commit is contained in:
zlong committed 2024-11-21 17:39:36 +08:00
1 parent b3ae608daa
commit b4541b6f78
5 files changed
+171 -91

No files matched your search

@@ -8,9 +8,9 @@
@confirm="fetchSaveClassifyData"
>
<t-space direction="vertical" style="width: 100%">
<TForm ref="form" :data="carousalData" :rules="rules" resetType="initial">
<TForm ref="form" :data="carousalData" :rules="rules" resetType="initial" label-width="100">
<TFormItem label="轮播图:" name="imageUrl">
<ImageUpload @on-success="handleSuccessImg"/>
<ImageUpload @on-success="handleSuccessImg" :image-url="imgUrl"/>
</TFormItem>
<TFormItem label="跳转类型:" name="targetType" >
<t-radio-group :default-value="carousalData.targetType" v-model="carousalData.targetType">
@@ -32,17 +32,19 @@ import type { Classify } from '@/model/classify'
import { computed, ref, reactive, watch } from 'vue'
import type { FormInstanceFunctions, FormProps } from 'tdesign-vue-next'
import ImageUpload from '@/components/ImageUpload.vue'
import { createCarousal } from '@/api/carousal'
import { createCarousal, updateCarousalItem } from '@/api/carousal'
const props = defineProps<{
isEdit?: Boolean | null
headerTitle?: String | null
classify: Classify | {}
count:number
classify: Classify | {} | number
}>()
const emit = defineEmits(['success'])
const form = ref<FormInstanceFunctions | null>(null)
const header = computed(() => `${props.isEdit ? '编辑' : '创建'}${props.headerTitle || ''}`)
const formType = computed(() => `${props.isEdit ? 'update' : 'add'}`)
const confirmBtn = computed(() => (props.isEdit ? '保存' : '确定'))
const carousalData: FormProps['data'] = reactive({})
const carousalData: FormProps['data'] = ref({})
const imgUrl = ref({})
const rules = computed(() => {
const commonRules = {
imageUrl: [
@@ -68,82 +70,32 @@ const rules = computed(() => {
},
],
};
return carousalData.targetType === 'article'
return carousalData.value.targetType === 'article'
? {...commonRules, targetId: [{ required: true, message: '文章 id 不能为空', type: 'error', trigger: 'blur' }, { required: true, message: '文章 id 不能为空', type: 'error', trigger: 'change' }, { whitespace: true, message: '网址路径不能为空' }] }
: {...commonRules, targetUrl: [{ required: true, message: '网址路径不能为空', type: 'error', trigger: 'blur' }, { required: true, message: '网址路径不能为空', type: 'error', trigger: 'change' }, { whitespace: true, message: '网址路径不能为空' }] };
});
// const rules: FormProps['rules'] = {
// imageUrl:[
// {
// required: true,
// message:'轮播图必须是上传',
// type:'error',
// trigger: 'change'
// }
// ],
// targetType:[
// {
// required: true,
// message: '请选择跳转类型',
// type: 'error',
// trigger: 'blur'
// },
// {
// required: true,
// message: '请选择跳转类型',
// type: 'error',
// trigger: 'change'
// },
// ],
// targetId:[
// {
// required: true,
// message: '文章id不能为空',
// type: 'error',
// trigger: 'blur'
// },
// {
// required: true,
// message: '文章id不能为空',
// type: 'error',
// trigger: 'change'
// },
// {
// whitespace: true,
// message: '网址路径不能为空'
// },
// ],
// targetUrl:[
// {
// required: true,
// message: '网址路径不能为空',
// type: 'error',
// trigger: 'blur'
// },
// {
// required: true,
// message: '网址路径不能为空',
// type: 'error',
// trigger: 'change'
// },
// {
// whitespace: true,
// message: '网址路径不能为空'
// },
// ],
// }
const handleSuccessImg = (res:string) => {
carousalData.imageUrl = res
console.log(res,'图片地址')
carousalData.value.imageUrl = res
}
const typeOption = reactive([
{ value: 'article', label: '文章' },
{ value: 'url', label: '地址' },
])
const handleFormTypeData = () => {
if (carousalData.value.targetType === 'article') {
delete carousalData.value.targetUrl
} else if (carousalData.value.targetType === 'url') {
delete carousalData.value.targetId
}
}
watch(
() => props.classify,
(newClassify) => {
Object.assign(carousalData, newClassify)
console.log(carousalData)
carousalData.value = Object.assign({}, newClassify)
const carousalObj = Object.assign({},newClassify)
imgUrl.value.url = carousalObj.imageUrl || ''
},
{
deep: true,
@@ -157,13 +109,33 @@ const fetchSaveClassifyData = async () => {
// console.log(valid,'valid')
if (valid && typeof valid === 'boolean') {
/* TODO: 校验通过保存分类数据 */
if (carousalData.targetType === 'article') {
delete carousalData.targetUrl
} else if (carousalData.targetType === 'url') {
delete carousalData.targetId
handleFormTypeData()
// console.log(formType.value,'formType')
let res
switch (formType.value){
case 'create':
res = await createCarousal(carousalData.value)
emit('success', true)
break;
case 'update':
const fieldsToKeep = ["imageUrl", "targetType", "targetUrl", "targetId"];
const requestObj = {};
for (const field of fieldsToKeep) {
if (carousalData.value[field]!== null && carousalData.value[field]!== undefined) {
requestObj[field] = carousalData.value[field];
}
}
handleFormTypeData()
res = await updateCarousalItem(carousalData.value.id,requestObj)
emit('success', res)
break;
default:
res = await createCarousal(carousalData.value)
break;
}
const createCarousalApi = createCarousal(carousalData)
console.log(createCarousalApi,'createCarousalApi')
// const createCarousalApi = createCarousal(carousalData.value)
// console.log(createCarousalApi,'createCarousalApi')
return
}
}