1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| import { PureComponent } from 'react' import Taro from '@tarojs/taro' import { View, Image } from '@tarojs/components'
import closePng from '../../assets/images/close.png' import plusPng from '../../assets/images/plus.png' import './index.scss'
export default class ImageUpload extends PureComponent { static defaultProps = { imageFiles: [], maxLength: 4, onChange() { }, onImageClick() { }, }
constructor(props) { super(props) this.picUrls = [] this.state = { uploadUrl: process.env.BASE_URL + '/api/upload', token: '', uploadCurrent: 0 } }
componentDidMount() { this.getToken() }
async getToken() { try { const res = await Taro.request({ url: process.env.BASE_URL + '/api/token', method: 'GET' }) if (res) { this.setState({ token: res.data?.token }) } } catch (err) { console.log(err) } }
chooseImage() { const { imageFiles, maxLength } = this.props const limit = maxLength - imageFiles.length Taro.chooseImage({ count: limit, success: (res) => { this.uploadImage(res.tempFilePaths) } }) }
uploadImage(tempFilePaths) { const { uploadCurrent, uploadUrl, token } = this.state const { onChange, imageFiles } = this.props Taro.uploadFile({ url: uploadUrl, filePath: tempFilePaths[uploadCurrent], name: 'file', formData: { 'token': token }, success: (res) => { if (res.errMsg === "uploadFile:ok") { const imageUrl = process.env.BASE_URL + res.data.src this.picUrls.push(imageUrl) } else { console.error('图片上传失败') } }, fail: () => { console.error('图片上传失败') }, complete: () => { if (uploadCurrent === tempFilePaths.length - 1) { const totalFiles = imageFiles.concat(this.picUrls) onChange(totalFiles) this.picUrls = [] this.setState({ uploadCurrent: 0 }) } else { this.setState({ uploadCurrent: uploadCurrent + 1 }, () => { this.uploadImage(tempFilePaths) }) } } }) }
handlePlusClick() { this.chooseImage() }
handleImageDelete(index) { const { imageFiles, onChange } = this.props const copyFiles = JSON.parse(JSON.stringify(imageFiles)) copyFiles.splice(index, 1) onChange(copyFiles) }
render() { const { onImageClick, imageFiles, maxLength } = this.props return ( <View className='image-upload'> {imageFiles.map((item, index) => ( <View key={item + index} className='image-upload__item' > <Image className='image-upload__image' src={item} mode='aspectFill' onClick={onImageClick.bind(this, item)} ></Image> <Image onClick={this.handleImageDelete.bind(this, index)} className='image-upload__close' src={closePng} ></Image> </View> ))} {imageFiles.length < maxLength && <View className='image-upload__item'> <View className='image-upload__plus' onClick={this.handlePlusClick.bind(this)} > <Image className='image-upload__plus--image' src={plusPng} ></Image> </View> </View>} </View> ) } }
|