diff --git a/web-admin/src/ContentEditor.tsx b/web-admin/src/ContentEditor.tsx index 3e2e3dd..277689b 100644 --- a/web-admin/src/ContentEditor.tsx +++ b/web-admin/src/ContentEditor.tsx @@ -53,6 +53,7 @@ export default function ContentEditor({ contentType, contentCode, onBack }: Prop setUploading(true); setNotice(""); const body = new FormData(); body.append("siteCode", SITE); body.append("altText", form.title); body.append("file", file); const response = await fetch(API + "/media/upload-image", { method: "POST", body }); + if (response.status === 413) throw new Error("图片超过服务器上传限制,请联系管理员调整 Nginx 上传上限"); if (!response.ok) throw new Error((await response.json().catch(() => ({}))).message || "上传失败"); const data = await response.json(); setForm((current) => ({ ...current, coverMediaCode: data.mediaCode, coverMediaUrl: data.url })); diff --git a/web-admin/src/ProductEditor.tsx b/web-admin/src/ProductEditor.tsx index 512c0c8..bcbd1d9 100644 --- a/web-admin/src/ProductEditor.tsx +++ b/web-admin/src/ProductEditor.tsx @@ -22,7 +22,7 @@ export default function ProductEditor({productCode,onBack}:Props){ const editor=useEditor({extensions:[StarterKit,Link.configure({openOnClick:false}),Image],content:'
'}); useEffect(()=>{post('/products/categories/list',{siteCode:SITE}).then(x=>setCategories(x.items||[]));if(!productCode)return;post('/products/detail',{siteCode:SITE,productCode}).then(d=>{setForm({...d,coverMediaUrl:d.coverMediaUrl||''});setAttributes(d.attributes||[]);setSpecifications(d.specifications||[]);editor?.commands.setContent(d.productContent||'')});post('/seo/detail',{siteCode:SITE,pageType:'PRODUCT_DETAIL',targetCode:productCode}).then(d=>{setSeo({title:d.title,description:d.description,keywords:d.keywords||'',h1:d.h1,canonicalUrl:d.canonicalUrl||'',robots:d.robots,ogTitle:d.ogTitle||'',ogDescription:d.ogDescription||'',ogImageUrl:d.ogImageUrl||'',status:d.status});setHeadings(d.headings||[])}).catch(()=>{})},[productCode,editor]); /** 校验并上传产品主图,选择相同文件时也允许重新触发上传。 */ - const upload=async(file?:File)=>{if(!file)return;if(file.size>10*1024*1024){setNotice('图片不能超过 10MB');return}try{setUploading(true);setNotice('');const body=new FormData();body.append('siteCode',SITE);body.append('altText',form.productName);body.append('file',file);const r=await fetch(API+'/media/upload-image',{method:'POST',body});if(!r.ok)throw new Error((await r.json().catch(()=>({}))).message||'上传失败');const d=await r.json();setForm(current=>({...current,coverMediaCode:d.mediaCode,coverMediaUrl:d.url}));setNotice('产品主图上传成功')}catch(e){setNotice(e instanceof Error?e.message:'上传失败')}finally{setUploading(false);if(coverInputRef.current)coverInputRef.current.value=''}}; + const upload=async(file?:File)=>{if(!file)return;if(file.size>10*1024*1024){setNotice('图片不能超过 10MB');return}try{setUploading(true);setNotice('');const body=new FormData();body.append('siteCode',SITE);body.append('altText',form.productName);body.append('file',file);const r=await fetch(API+'/media/upload-image',{method:'POST',body});if(r.status===413)throw new Error('图片超过服务器上传限制,请联系管理员调整 Nginx 上传上限');if(!r.ok)throw new Error((await r.json().catch(()=>({}))).message||'上传失败');const d=await r.json();setForm(current=>({...current,coverMediaCode:d.mediaCode,coverMediaUrl:d.url}));setNotice('产品主图上传成功')}catch(e){setNotice(e instanceof Error?e.message:'上传失败')}finally{setUploading(false);if(coverInputRef.current)coverInputRef.current.value=''}}; const save=async()=>{if(!editor)return;try{setSaving(true);setNotice('');const result=await post('/products/save',{productCode,siteCode:SITE,primaryCategoryCode:form.primaryCategoryCode,productName:form.productName,slug:form.slug,model:form.model,summary:form.summary,productContent:editor.getHTML(),coverMediaCode:form.coverMediaCode,featured:form.featured,inquiryEnabled:form.inquiryEnabled,sortNo:form.sortNo,status:form.status,specifications,attributes});await post('/seo/save',{siteCode:SITE,pageType:'PRODUCT_DETAIL',targetCode:result.productCode,title:seo.title||form.productName,description:seo.description||form.summary,keywords:seo.keywords,h1:seo.h1||form.productName,canonicalUrl:seo.canonicalUrl||`/products/${form.slug}`,robots:seo.robots,ogTitle:seo.ogTitle,ogDescription:seo.ogDescription,ogImageUrl:seo.ogImageUrl,structuredData:null,status:form.status,headings});setNotice('产品与 SEO 已保存');if(!productCode)setTimeout(onBack,500)}catch(e){setNotice(e instanceof Error?e.message:'保存失败')}finally{setSaving(false)}}; const addLink=()=>{const href=window.prompt('请输入链接地址');if(href)editor?.chain().focus().setLink({href}).run()};const addImage=()=>{const src=window.prompt('请输入图片地址');if(src)editor?.chain().focus().setImage({src}).run()}; return