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:
parent
284499745a
commit
29451277d1
@ -15,6 +15,7 @@ import {
|
||||
} from 'lucide-react';
|
||||
import { useAuth } from '../contexts/AuthContext';
|
||||
import { http } from '../api';
|
||||
import { commonApi } from '../api/common';
|
||||
|
||||
export type AccountRole = 'purchaser' | 'textile' | 'washing';
|
||||
|
||||
@ -82,23 +83,7 @@ export function AccountProfileCard({
|
||||
if (!auth.user) return;
|
||||
|
||||
try {
|
||||
// TODO: 头像上传接口待后端实现
|
||||
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 || '';
|
||||
const publicUrl = await commonApi.uploadFile(file, 'avatars', 'avatars');
|
||||
|
||||
await http.patch('/auth/me', { image_url: publicUrl });
|
||||
|
||||
|
||||
@ -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 { useResponsive } from '../../hooks/useResponsive';
|
||||
import { http } from '../../api';
|
||||
import { commonApi } from '../../api/common';
|
||||
import type { Product, ProductYarnRatio } from '../../types';
|
||||
|
||||
interface ProductFormData {
|
||||
@ -379,25 +380,7 @@ export function ProductForm({
|
||||
setUploading(true);
|
||||
|
||||
try {
|
||||
// TODO: 文件上传接口待后端实现
|
||||
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 || '';
|
||||
const publicUrl = await commonApi.uploadFile(file, 'product_images', 'products');
|
||||
setPreviewUrl(publicUrl);
|
||||
setFormData(prev => ({ ...prev, image_url: publicUrl }));
|
||||
setUploading(false);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user