import React , { useState , useEffect } from 'react' ; import Qiniu from './components/qiniu' ; import { useStore } from 'react-redux' ; import { getQiNiuToken } from '@app/redux/actions/qiniu' ; interface Props { text ? : string ; multiple ? : boolean ; beforeUpload ? : ( s : object ) => void ; upload ? : ( url : string , s : object ) => void ; accept ? : string ; } export default function ( { text = '上传图片' , multiple = true , beforeUpload = ( ) => { } , upload = ( ) => { } , accept = "image/gif, image/jpeg, image/png" } ) { const [ token , setToken ] = useState ( '' ) ; const [ url , setUrl ] = useState ( '' ) ; const store = useStore ( ) ; const _getQiNiuToken = ( ) => getQiNiuToken ( ) ( store . dispatch , store . getState ) ; useEffect ( ( ) => { if ( token ) return ; _getQiNiuToken ( ) . then ( ( [ err , res ] ) => { if ( res ) { setToken ( res . token ) ; setUrl ( res . url ) ; } } ) ; } ) ; const HandleOnUpload = function ( files ) { beforeUpload ( files ) ; files . map ( ( item ) => { item . upload ( ) . then ( ( res ) => { res . text ( ) . then ( ( res ) => { res = JSON . parse ( res ) ; upload ( url + '/' + res . key , item ) ; } ) ; } ) ; } ) ; } ; if ( ! token ) return ( < span > < /span>); return ( < Qiniu text = { text } multiple = { multiple } accept = { accept } token = { token } onUpload = { HandleOnUpload } / > ) ; }