fix: use authenticated upload in ProductForm and AccountProfileCard

Both components had raw fetch to /api/v1/common/upload without the
Authorization header. Replace with commonApi.uploadFile which handles
token refresh and auth headers.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Chever John 2026-06-30 20:39:15 +08:00
parent 284499745a
commit 29451277d1
No known key found for this signature in database
GPG Key ID: 2894D52ED1211DF0
2 changed files with 4 additions and 36 deletions

View File

@ -15,6 +15,7 @@ import {
} from 'lucide-react'; } from 'lucide-react';
import { useAuth } from '../contexts/AuthContext'; import { useAuth } from '../contexts/AuthContext';
import { http } from '../api'; import { http } from '../api';
import { commonApi } from '../api/common';
export type AccountRole = 'purchaser' | 'textile' | 'washing'; export type AccountRole = 'purchaser' | 'textile' | 'washing';
@ -82,23 +83,7 @@ export function AccountProfileCard({
if (!auth.user) return; if (!auth.user) return;
try { try {
// TODO: 头像上传接口待后端实现 const publicUrl = await commonApi.uploadFile(file, 'avatars', 'avatars');
const formDataObj = new FormData();
formDataObj.append('file', file);
formDataObj.append('bucket', 'avatars');
const res = await fetch('/api/v1/common/upload', {
method: 'POST',
body: formDataObj,
});
if (!res.ok) {
alert('头像上传失败');
return;
}
const json = await res.json();
const publicUrl = json.data?.url || '';
await http.patch('/auth/me', { image_url: publicUrl }); await http.patch('/auth/me', { image_url: publicUrl });

View File

@ -3,6 +3,7 @@ import { motion } from 'framer-motion';
import { X, Package, Tag, Hash, Palette, DollarSign, Layers, Plus, Minus, Image as ImageIcon, Upload, Trash2, Search } from 'lucide-react'; import { X, Package, Tag, Hash, Palette, DollarSign, Layers, Plus, Minus, Image as ImageIcon, Upload, Trash2, Search } from 'lucide-react';
import { useResponsive } from '../../hooks/useResponsive'; import { useResponsive } from '../../hooks/useResponsive';
import { http } from '../../api'; import { http } from '../../api';
import { commonApi } from '../../api/common';
import type { Product, ProductYarnRatio } from '../../types'; import type { Product, ProductYarnRatio } from '../../types';
interface ProductFormData { interface ProductFormData {
@ -379,25 +380,7 @@ export function ProductForm({
setUploading(true); setUploading(true);
try { try {
// TODO: 文件上传接口待后端实现 const publicUrl = await commonApi.uploadFile(file, 'product_images', 'products');
const formDataObj = new FormData();
formDataObj.append('file', file);
formDataObj.append('bucket', 'product_images');
formDataObj.append('path_prefix', 'products');
const res = await fetch('/api/v1/common/upload', {
method: 'POST',
body: formDataObj,
});
if (!res.ok) {
alert('图片上传失败');
setUploading(false);
return;
}
const json = await res.json();
const publicUrl = json.data?.url || '';
setPreviewUrl(publicUrl); setPreviewUrl(publicUrl);
setFormData(prev => ({ ...prev, image_url: publicUrl })); setFormData(prev => ({ ...prev, image_url: publicUrl }));
setUploading(false); setUploading(false);