import { useState } from 'react' import { View, Text, Image } from '@tarojs/components' import Taro from '@tarojs/taro' import { cn } from '@/utils/cn' import { Button } from '@/components/ui/button' import { uploadFromSelect, type UploadResult } from '@/utils/minio' interface AvatarUploadProps { currentAvatar?: string onUploadSuccess?: (result: UploadResult) => void onUploadError?: (error: Error) => void size?: number editable?: boolean } export function AvatarUpload({ currentAvatar, onUploadSuccess, onUploadError, size = 96, editable = true }: AvatarUploadProps) { const [uploading, setUploading] = useState(false) const [progress, setProgress] = useState(0) const handleChooseImage = async () => { if (!editable || uploading) return try { setUploading(true) setProgress(0) const result = await uploadFromSelect( 'avatars', { sourceType: ['album', 'camera'], count: 1 }, { onProgress: (event) => { setProgress(event.progress) if (event.stage === 'uploading') { Taro.showLoading({ title: `上传中...${event.progress}%` }) } }, onComplete: () => { Taro.hideLoading() Taro.showToast({ title: '上传成功', icon: 'success' }) }, onError: (error) => { Taro.hideLoading() onUploadError?.(error) Taro.showToast({ title: '上传失败', icon: 'none' }) } } ) onUploadSuccess?.(result) } catch (error) { console.error('头像上传失败:', error) onUploadError?.(error as Error) } finally { setUploading(false) setProgress(0) } } const avatarSize = size const iconSize = Math.floor(size / 4) return ( {uploading && ( {progress}% )} {editable && !uploading && ( )} {uploading && ( )} ) }