11import { SuperComponent , wxComponent } from '../common/src/index' ;
22import props from './props' ;
3- import { UploadFile } from './type' ;
3+ import { UploadFile , SizeLimitObj } from './type' ;
44import config from '../common/config' ;
5- import { isOverSize } from '../common/utils' ;
5+ import { isOverSize , isWxWork , isPC } from '../common/utils' ;
66import { isObject } from '../common/validator' ;
77
88const { prefix } = config ;
@@ -111,6 +111,20 @@ export default class Upload extends SuperComponent {
111111 return parseInt ( `${ Date . now ( ) } ${ Math . floor ( Math . random ( ) * 900 + 100 ) } ` , 10 ) . toString ( 36 ) + extName ;
112112 }
113113
114+ checkFileSize ( size : number , sizeLimit : SizeLimitObj | number , fileType ?: string ) : boolean {
115+ if ( isOverSize ( size , sizeLimit ) ) {
116+ let title = `${ fileType === 'video' ? '视频' : '图片' } 大小超过限制` ;
117+
118+ if ( isObject ( sizeLimit ) ) {
119+ const { size : limitSize , message : limitMessage } = sizeLimit as SizeLimitObj ;
120+ title = limitMessage ?. replace ( '{sizeLimit}' , String ( limitSize ) ) ;
121+ }
122+ wx . showToast ( { icon : 'none' , title } ) ;
123+ return true ;
124+ }
125+ return false ;
126+ }
127+
114128 onDelete ( e : any ) {
115129 const { index } = e . currentTarget . dataset ;
116130 this . deleteHandle ( index ) ;
@@ -278,13 +292,13 @@ export default class Upload extends SuperComponent {
278292 } ,
279293
280294 uploadFiles ( files : UploadFile [ ] ) {
281- return new Promise ( ( resolve ) => {
295+ return Promise . resolve ( ) . then ( ( ) => {
282296 // 开始调用上传函数
283297 const task = this . data . requestMethod ( files ) ;
284298 if ( task instanceof Promise ) {
285299 return task ;
286300 }
287- resolve ( { } ) ;
301+ return Promise . resolve ( { } ) ;
288302 } ) ;
289303 } ,
290304
@@ -323,50 +337,77 @@ export default class Upload extends SuperComponent {
323337 chooseMedia ( mediaType ) {
324338 const { customLimit } = this . data ;
325339 const { config, sizeLimit } = this . properties ;
326- wx . chooseMedia ( {
327- count : Math . min ( 20 , customLimit ) ,
328- mediaType,
329- ...config ,
330- success : ( res ) => {
331- const files = [ ] ;
332-
333- // 支持单/多文件
334- res . tempFiles . forEach ( ( temp ) => {
335- const { size, fileType, tempFilePath, width, height, duration, thumbTempFilePath, ...res } = temp ;
336-
337- if ( isOverSize ( size , sizeLimit ) ) {
338- let title = `${ fileType === 'image' ? '图片' : '视频' } 大小超过限制` ;
339340
340- if ( typeof sizeLimit !== 'number' ) {
341- title = sizeLimit . message . replace ( '{sizeLimit}' , sizeLimit ?. size ) ;
342- }
343- wx . showToast ( { icon : 'none' , title } ) ;
344- return ;
345- }
341+ if ( isWxWork || isPC ) {
342+ wx . chooseImage ( {
343+ count : Math . min ( 20 , customLimit ) ,
344+ ...config ,
345+ success : ( res ) => {
346+ const files = [ ] ;
347+
348+ res . tempFiles . forEach ( ( temp ) => {
349+ const { path, size } = temp ;
350+
351+ if ( this . checkFileSize ( size , sizeLimit , 'image' ) ) return ;
352+
353+ const name = this . getRandFileName ( path ) ;
354+ files . push ( {
355+ name,
356+ type : 'image' ,
357+ url : path ,
358+ size : size ,
359+ percent : 0 ,
360+ } ) ;
361+ } ) ;
346362
347- const name = this . getRandFileName ( tempFilePath ) ;
348- files . push ( {
349- name,
350- type : this . getFileType ( mediaType , tempFilePath , fileType ) ,
351- url : tempFilePath ,
352- size : size ,
353- width : width ,
354- height : height ,
355- duration : duration ,
356- thumb : thumbTempFilePath ,
357- percent : 0 ,
358- ...res ,
363+ this . afterSelect ( files ) ;
364+ } ,
365+ fail : ( err ) => {
366+ this . triggerFailEvent ( err ) ;
367+ } ,
368+ complete : ( res ) => {
369+ this . triggerEvent ( 'complete' , res ) ;
370+ } ,
371+ } ) ;
372+ } else {
373+ wx . chooseMedia ( {
374+ count : Math . min ( 20 , customLimit ) ,
375+ mediaType,
376+ ...config ,
377+ success : ( res ) => {
378+ const files = [ ] ;
379+
380+ // 支持单/多文件
381+ res . tempFiles . forEach ( ( temp ) => {
382+ const { size, fileType, tempFilePath, width, height, duration, thumbTempFilePath, ...res } = temp ;
383+
384+ if ( this . checkFileSize ( size , sizeLimit , fileType ) ) return ;
385+
386+ const name = this . getRandFileName ( tempFilePath ) ;
387+ files . push ( {
388+ name,
389+ type : this . getFileType ( mediaType , tempFilePath , fileType ) ,
390+ url : tempFilePath ,
391+ size : size ,
392+ width : width ,
393+ height : height ,
394+ duration : duration ,
395+ thumb : thumbTempFilePath ,
396+ percent : 0 ,
397+ ...res ,
398+ } ) ;
359399 } ) ;
360- } ) ;
361- this . afterSelect ( files ) ;
362- } ,
363- fail : ( err ) => {
364- this . triggerFailEvent ( err ) ;
365- } ,
366- complete : ( res ) => {
367- this . triggerEvent ( 'complete' , res ) ;
368- } ,
369- } ) ;
400+
401+ this . afterSelect ( files ) ;
402+ } ,
403+ fail : ( err ) => {
404+ this . triggerFailEvent ( err ) ;
405+ } ,
406+ complete : ( res ) => {
407+ this . triggerEvent ( 'complete' , res ) ;
408+ } ,
409+ } ) ;
410+ }
370411 } ,
371412
372413 chooseMessageFile ( mediaType ) {
@@ -383,15 +424,7 @@ export default class Upload extends SuperComponent {
383424 res . tempFiles . forEach ( ( temp ) => {
384425 const { size, type : fileType , path : tempFilePath , ...res } = temp ;
385426
386- if ( isOverSize ( size , sizeLimit ) ) {
387- let title = `${ fileType === 'image' ? '图片' : '视频' } 大小超过限制` ;
388-
389- if ( typeof sizeLimit !== 'number' ) {
390- title = sizeLimit . message . replace ( '{sizeLimit}' , sizeLimit ?. size ) ;
391- }
392- wx . showToast ( { icon : 'none' , title } ) ;
393- return ;
394- }
427+ if ( this . checkFileSize ( size , sizeLimit , fileType ) ) return ;
395428
396429 const name = this . getRandFileName ( tempFilePath ) ;
397430 files . push ( {
0 commit comments