import React,{useEffect,useState} from 'react'; import {EditorContent,useEditor} from '@tiptap/react'; import StarterKit from '@tiptap/starter-kit'; import Link from '@tiptap/extension-link'; import Image from '@tiptap/extension-image'; import {ArrowLeft,Bold,Heading2,Heading3,Image as ImageIcon,Italic,Link as LinkIcon,List,Plus,Save,Trash2,Undo2,Redo2} from 'lucide-react'; import {API,assetUrl} from './api'; const SITE='SIT0000000000001'; async function post(path:string,body:unknown){const r=await fetch(API+path,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});if(!r.ok)throw new Error((await r.json()).message||'请求失败');return r.json()} type Attribute={attributeCode?:string;attributeName:string;value:string;unit:string;sortNo:number}; type Specification={groupName:string;name:string;value:string;unit:string;sortNo:number}; type Heading={level:number;text:string;sortNo:number}; type Props={productCode?:string;onBack:()=>void}; export default function ProductEditor({productCode,onBack}:Props){ const [form,setForm]=useState({productName:'',slug:'',model:'',summary:'',primaryCategoryCode:'',coverMediaCode:'',coverMediaUrl:'',featured:false,inquiryEnabled:true,sortNo:30,status:'DRAFT'}); const [attributes,setAttributes]=useState([]);const [specifications,setSpecifications]=useState([]);const [categories,setCategories]=useState([]); const [seo,setSeo]=useState({title:'',description:'',keywords:'',h1:'',canonicalUrl:'',robots:'index,follow',ogTitle:'',ogDescription:'',ogImageUrl:'',status:'DRAFT'});const [headings,setHeadings]=useState([]); const [saving,setSaving]=useState(false);const [notice,setNotice]=useState(''); 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;try{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()).message||'上传失败');const d=await r.json();setForm({...form,coverMediaCode:d.mediaCode,coverMediaUrl:d.url})}catch(e){setNotice(e instanceof Error?e.message:'上传失败')}}; 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
/

{productCode?'编辑产品':'新增产品'}

{notice&&{notice}}

基础信息

setForm({...form,productName:e.target.value})}/>setForm({...form,slug:e.target.value.toLowerCase().replace(/[^a-z0-9-]/g,'-')})}/>setForm({...form,model:e.target.value})}/>setForm({...form,sortNo:Number(e.target.value)})}/>