diff --git a/Cunkebao/app/content/[id]/materials/edit/[materialId]/page.tsx b/Cunkebao/app/content/[id]/materials/edit/[materialId]/page.tsx index 174e36ac..f22c944f 100644 --- a/Cunkebao/app/content/[id]/materials/edit/[materialId]/page.tsx +++ b/Cunkebao/app/content/[id]/materials/edit/[materialId]/page.tsx @@ -29,7 +29,7 @@ interface Material { content: string coverImage: string | null resUrls: string[] - urls: string[] + urls: ({ desc?: string; image?: string; url?: string } | string)[] createTime: string createMomentTime: number time: string @@ -43,6 +43,8 @@ interface Material { comment: string | null icon: string | null videoUrl?: string + sendTime: string + contentType: string } const isImageUrl = (url: string) => { @@ -66,12 +68,14 @@ export default function EditMaterialPage({ params }: { params: Promise<{ id: str const [images, setImages] = useState([]) const [previewUrls, setPreviewUrls] = useState([]) const [originalMaterial, setOriginalMaterial] = useState(null) - const [materialType, setMaterialType] = useState(1) // 默认为图片类型 + const [sendTime, setSendTime] = useState("") + const [contentType, setContentType] = useState(1) const [url, setUrl] = useState("") + const [desc, setDesc] = useState("") + const [image, setImage] = useState("") const [title, setTitle] = useState("") const [iconUrl, setIconUrl] = useState("") const [videoUrl, setVideoUrl] = useState("") - const [publishTime, setPublishTime] = useState("") const [comment, setComment] = useState("") // 获取素材详情 @@ -85,19 +89,35 @@ export default function EditMaterialPage({ params }: { params: Promise<{ id: str const material = response.data setOriginalMaterial(material) setContent(material.content) - setTitle(material.title || "") - setIconUrl(material.icon || "") - setVideoUrl(material.videoUrl || "") setComment(material.comment || "") - - // 设置素材类型 - setMaterialType(Number(material.type) || 1) - - // 如果是链接类型,设置URL - if (material.type === "2" && material.urls && material.urls.length > 0) { - setUrl(material.urls[0]) + setSendTime(material.sendTime || "") + setContentType(Number(material.contentType) || 1) + // 链接类型 + if (Number(material.contentType) === 2 && material.urls && material.urls.length > 0) { + const first = material.urls[0]; + if (typeof first === "object" && first !== null) { + setDesc(first.desc || ""); + setImage(first.image || ""); + setUrl(first.url || ""); + } else { + setDesc(""); + setImage(""); + setUrl(typeof first === "string" ? first : ""); + } } + // 视频类型 + if (Number(material.contentType) === 3 && material.urls && material.urls.length > 0) { + const first = material.urls[0]; + if (typeof first === "string") { + setVideoUrl(first); + } else if (typeof first === "object" && first !== null && first.url) { + setVideoUrl(first.url); + } else { + setVideoUrl(""); + } + } + // 处理图片 const imageUrls: string[] = [] @@ -161,13 +181,13 @@ export default function EditMaterialPage({ params }: { params: Promise<{ id: str e.preventDefault() // 根据不同类型校验不同字段 - if (materialType === 1 && images.length === 0) { + if (contentType === 1 && images.length === 0) { showToast("请上传图片", "error") return - } else if (materialType === 2 && !url) { - showToast("请输入链接地址", "error") + } else if (contentType === 2 && (!url || !desc)) { + showToast("请输入描述和链接地址", "error") return - } else if ((materialType === 4 || materialType === 6) && !content) { + } else if ((contentType === 4 || contentType === 6) && !content) { showToast("请输入文本内容", "error") return } @@ -176,21 +196,19 @@ export default function EditMaterialPage({ params }: { params: Promise<{ id: str try { const payload: any = { id: resolvedParams.materialId, - type: materialType, + contentType: contentType, content: content, - title: materialType === 2 ? title : undefined, - icon: materialType === 2 ? iconUrl : undefined, - videoUrl: materialType === 3 ? videoUrl : undefined, comment: comment, + sendTime: sendTime, } // 根据类型添加不同的字段 - if (materialType === 1) { + if (contentType === 1) { payload.resUrls = images - } else if (materialType === 2) { - payload.urls = [url] - } else if (materialType === 3) { - payload.urls = [url] + } else if (contentType === 2) { + payload.urls = [{ desc, image, url }] + } else if (contentType === 3) { + payload.urls = videoUrl ? [videoUrl] : [] } const response = await api.post('/v1/content/library/update-item', payload) @@ -245,8 +263,8 @@ export default function EditMaterialPage({ params }: { params: Promise<{ id: str id="publish-time" type="datetime-local" step="60" - value={publishTime} - onChange={(e) => setPublishTime(e.target.value)} + value={sendTime} + onChange={(e) => setSendTime(e.target.value)} className="w-full h-12 rounded-2xl border-gray-300 focus:border-blue-500 focus:ring-2 focus:ring-blue-100 px-4 text-base placeholder:text-gray-300" placeholder="请选择发布时间" style={{ width: 'auto' }} @@ -261,8 +279,8 @@ export default function EditMaterialPage({ params }: { params: Promise<{ id: str