@@ -26,14 +26,14 @@ export default function Home() {
26
26
const [ selectedFiles , setSelectedFiles ] = useState ( [ ] ) ;
27
27
const [ uploadedImages , setUploadedImages ] = useState ( [ ] ) ;
28
28
const [ uploadedFilesNum , setUploadedFilesNum ] = useState ( 0 ) ;
29
- const [ selectedImage , setSelectedImage ] = useState ( null ) ;
29
+ const [ selectedImage , setSelectedImage ] = useState ( null ) ; // 添加状态用于跟踪选中的放大图片
30
30
const [ activeTab , setActiveTab ] = useState ( 'preview' ) ;
31
31
const [ uploading , setUploading ] = useState ( false ) ;
32
32
const [ IP , setIP ] = useState ( '' ) ;
33
33
const [ Total , setTotal ] = useState ( '?' ) ;
34
34
const [ selectedOption , setSelectedOption ] = useState ( 'tgchannel' ) ; // 初始选择第一个选项
35
- const [ isAuthapi , setisAuthapi ] = useState ( false ) ;
36
- const [ Loginuser , setLoginuser ] = useState ( '' ) ;
35
+ const [ isAuthapi , setisAuthapi ] = useState ( false ) ; // 初始选择第一个选项
36
+ const [ Loginuser , setLoginuser ] = useState ( '' ) ; // 初始选择第一个选项
37
37
const [ boxType , setBoxtype ] = useState ( "img" ) ;
38
38
39
39
const origin = typeof window !== 'undefined' ? window . location . origin : '' ;
@@ -129,6 +129,7 @@ export default function Home() {
129
129
const newFiles = event . target . files ;
130
130
const filteredFiles = Array . from ( newFiles ) . filter ( file =>
131
131
! selectedFiles . find ( selFile => selFile . name === file . name ) ) ;
132
+ // 过滤掉已经在 uploadedImages 数组中存在的文件
132
133
const uniqueFiles = filteredFiles . filter ( file =>
133
134
! uploadedImages . find ( upImg => upImg . name === file . name )
134
135
) ;
@@ -170,10 +171,11 @@ export default function Home() {
170
171
formData . append ( formFieldName , file ) ;
171
172
172
173
try {
173
- const targetUrl = selectedOption === "tgchannel"
174
+ const targetUrl = selectedOption === "tgchannel" || selectedOption === "r2"
174
175
? `/api/enableauthapi/${ selectedOption } `
175
176
: `/api/${ selectedOption } ` ;
176
177
178
+ // const response = await fetch("https://img.131213.xyz/api/tencent", {
177
179
const response = await fetch ( targetUrl , {
178
180
method : 'POST' ,
179
181
body : formData ,
@@ -190,10 +192,37 @@ export default function Home() {
190
192
setUploadedImages ( ( prevImages ) => [ ...prevImages , file ] ) ;
191
193
setSelectedFiles ( ( prevFiles ) => prevFiles . filter ( f => f !== file ) ) ;
192
194
successCount ++ ;
193
- } else if ( response . status === 401 ) {
194
- toast . error ( ' 未授权,请登录 !' ) ;
195
195
} else {
196
- toast . error ( `上传 ${ file . name } 图片时出错` ) ;
196
+ // 尝试从响应中提取错误信息
197
+ let errorMsg ;
198
+ try {
199
+ const errorData = await response . json ( ) ;
200
+ errorMsg = errorData . message || `上传 ${ file . name } 图片时出错` ;
201
+ } catch ( jsonError ) {
202
+ // 如果解析 JSON 失败,使用默认错误信息
203
+ errorMsg = `上传 ${ file . name } 图片时发生未知错误` ;
204
+ }
205
+
206
+ // 细化状态码处理
207
+ switch ( response . status ) {
208
+ case 400 :
209
+ toast . error ( `请求无效: ${ errorMsg } ` ) ;
210
+ break ;
211
+ case 403 :
212
+ toast . error ( `无权限访问资源: ${ errorMsg } ` ) ;
213
+ break ;
214
+ case 404 :
215
+ toast . error ( `资源未找到: ${ errorMsg } ` ) ;
216
+ break ;
217
+ case 500 :
218
+ toast . error ( `服务器错误: ${ errorMsg } ` ) ;
219
+ break ;
220
+ case 401 :
221
+ toast . error ( `未授权: ${ errorMsg } ` ) ;
222
+ break ;
223
+ default :
224
+ toast . error ( `上传 ${ file . name } 图片时出错: ${ errorMsg } ` ) ;
225
+ }
197
226
}
198
227
} catch ( error ) {
199
228
toast . error ( `上传 ${ file . name } 图片时出错` ) ;
@@ -460,9 +489,11 @@ export default function Home() {
460
489
{ renderButton ( ) }
461
490
</ header >
462
491
< div className = "mt-[60px] w-9/10 sm:w-9/10 md:w-9/10 lg:w-9/10 xl:w-3/5 2xl:w-2/3" >
492
+
463
493
< div className = "flex flex-row" >
464
494
< div className = "flex flex-col" >
465
- < div className = "text-gray-800 text-lg" > 图片或视频上传</ div >
495
+ < div className = "text-gray-800 text-lg" > 图片或视频上传
496
+ </ div >
466
497
< div className = "mb-4 text-sm text-gray-500" >
467
498
上传文件最大 5 MB;本站已托管 < span className = "text-cyan-600" > { Total } </ span > 张图片; 你访问本站的IP是:< span className = "text-cyan-600" > { IP } </ span >
468
499
</ div >
@@ -475,6 +506,7 @@ export default function Home() {
475
506
className = "text-lg p-2 border rounded text-center w-auto sm:w-auto md:w-auto lg:w-auto xl:w-auto 2xl:w-36" >
476
507
< option value = "tg" > TG(会失效)</ option >
477
508
< option value = "tgchannel" > TG_Channel</ option >
509
+ < option value = "r2" > R2</ option >
478
510
< option value = "vviptuangou" > vviptuangou</ option >
479
511
< option value = "58img" > 58img</ option >
480
512
< option value = "tencent" > tencent</ option >
@@ -543,7 +575,9 @@ export default function Home() {
543
575
544
576
{ selectedFiles . length === 0 && (
545
577
< div className = "absolute -z-10 left-0 top-0 w-full h-full flex items-center justify-center" >
578
+
546
579
< div className = "text-gray-500" >
580
+
547
581
拖拽文件到这里或将屏幕截图复制并粘贴到此处上传
548
582
</ div >
549
583
</ div >
@@ -585,6 +619,8 @@ export default function Home() {
585
619
< div className = "md:col-span-1 col-span-5" >
586
620
< div
587
621
className = { `w-full bg-green-500 cursor-pointer h-10 flex items-center justify-center text-white ${ uploading ? 'pointer-events-none opacity-50' : '' } ` }
622
+ // className={`bg-green-500 text-white rounded-full w-6 h-6 flex items-center justify-center cursor-pointer mx-2 ${uploading ? 'pointer-events-none opacity-50' : ''}`}
623
+
588
624
onClick = { ( ) => handleUpload ( ) }
589
625
>
590
626
< FontAwesomeIcon icon = { faUpload } style = { { width : '20px' , height : '20px' } } className = "mr-2" />
0 commit comments