增加文件系统存储功能,修复上传功能

Change-Id: I7b76c0a78fa0a02018fa76e932d283e29b35d4be
diff --git a/Merge/front/src/api/posts_wzy.js b/Merge/front/src/api/posts_wzy.js
index d901500..4992f95 100644
--- a/Merge/front/src/api/posts_wzy.js
+++ b/Merge/front/src/api/posts_wzy.js
@@ -36,11 +36,11 @@
  * 发布新帖
  * POST /posts
  */
-export async function createPost(payload) {
+export async function createPost(formData) {
   const res = await fetch(`${BASE}/posts`, {
     method: 'POST',
-    headers: { 'Content-Type': 'application/json' },
-    body: JSON.stringify(payload)
+    // 不设置Content-Type,让浏览器自动设置multipart/form-data
+    body: formData
   })
   if (!res.ok) {
     const err = await res.json().catch(() => null)
@@ -53,11 +53,12 @@
  * 修改帖子
  * PUT /posts/{postId}
  */
-export async function updatePost(postId, payload) {
+export async function updatePost(postId, formData) {
   const res = await fetch(`${BASE}/posts/${postId}`, {
     method: 'PUT',
-    headers: { 'Content-Type': 'application/json' },
-    body: JSON.stringify(payload)
+    // 如果是FormData则不设置Content-Type,否则设置JSON
+    headers: formData instanceof FormData ? {} : { 'Content-Type': 'application/json' },
+    body: formData instanceof FormData ? formData : JSON.stringify(formData)
   })
   if (!res.ok) throw new Error(`updatePost(${postId}): ${res.status}`)
   // 204 No Content
diff --git a/Merge/front/src/components/CreatePost.jsx b/Merge/front/src/components/CreatePost.jsx
index c11e247..2b365db 100644
--- a/Merge/front/src/components/CreatePost.jsx
+++ b/Merge/front/src/components/CreatePost.jsx
@@ -51,12 +51,12 @@
       .catch(err => setError(err.message))
       .finally(() => setLoading(false))
   }, [isEdit, postId])
-  // 上传回调
+
+  // 上传回调 - 保存原始文件对象
   const handleUploadComplete = async uploadedFiles => {
-    // TODO: 真正上传到服务器后替换为服务端 URL
-    const urls = await Promise.all(
-      uploadedFiles.map(f => URL.createObjectURL(f))
-    )
+    setFiles(uploadedFiles)
+    // 为预览创建临时URLs
+    const urls = uploadedFiles.map(f => URL.createObjectURL(f))
     setMediaUrls(urls)
     setStep('detail')
   }
@@ -72,25 +72,42 @@
       return
     }
     setError(null)
+    
     try {
+      // 创建FormData对象
+      const formData = new FormData()
+      
+      // 添加文本字段
+      formData.append('user_id', currentUserId)
+      formData.append('title', title.trim())
+      formData.append('content', content.trim())
+      formData.append('status', status)
+      if (topicId) {
+        formData.append('topic_id', topicId)
+      }
+      
       if (isEdit) {
-        await updatePost(postId, {
-          title: title.trim(),
-          content: content.trim(),
-          topic_id: topicId || undefined,
-          media_urls: mediaUrls,
-          status
-        })
+        // 编辑模式:如果有新文件,添加新文件;否则保留现有URLs
+        if (files.length > 0) {
+          files.forEach((file, index) => {
+            formData.append(`media_${index}`, file)
+          })
+          formData.append('media_count', files.length)
+        } else {
+          // 保留现有的media_urls
+          formData.append('existing_media_urls', JSON.stringify(mediaUrls))
+        }
+        
+        await updatePost(postId, formData)
         alert('更新成功!')
       } else {
-        await createPost({
-          user_id: currentUserId,
-          topic_id: topicId || undefined,
-          title: title.trim(),
-          content: content.trim(),
-          media_urls: mediaUrls,
-          status
+        // 创建模式:添加文件
+        files.forEach((file, index) => {
+          formData.append(`media_${index}`, file)
         })
+        formData.append('media_count', files.length)
+        
+        await createPost(formData)
         alert('发布成功!')
       }
       navigate('/notebooks', { replace: true })