美文网首页
小程序总结(二十一)- 基于Taro + typescript的

小程序总结(二十一)- 基于Taro + typescript的

作者: 自律财富自由 | 来源:发表于2020-01-17 16:09 被阅读0次

    可选择多张图片进行上传(接口是一张一张的上传的)

    
    <View className="textarea-title">上传照片</View>
                            <View className="patient-document-pic-box">
                                {files.map((file, index) => 
                                    <View className="temp-file-pic-box" key={'file' + index}>
                                        <Image className="temp-file-pic" src={file.path}></Image>
                                        <Image className="close-icon" src='https://kano.guahao.cn/Efm199642756' onClick={this.onDeletePhoto.bind(this, index)}></Image>
                                        {this.state.showLoadingLayer && !file.isUploaded &&
                                        <View className="pic-layer">
                                            <View className="layer-loading">
                                                <View className="layer-progress"></View>
                                                {this.state.showUploadingText && <Text className="layer-text">正在上传</Text>}
                                            </View>
                                        </View>}
                                    </View>
                                )}
                                {
                                    this.state.files.length < 9 &&
                                    <View className="camera-pic-box" onClick={this.onChooseImg.bind(this)}>
                                        <Image className="camera-pic" src='https://kano.guahao.cn/GEm199642253'></Image>
                                        <Text className="add-text">添加照片</Text>
                                    </View>
                                }
                            </View>
    
    
    .patient-document-pic-box, .temp-file-box {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            .temp-file-pic-box {
                position: relative;
                .temp-file-pic {
                    width: 150px;
                    height: 150px;
                    margin-right: 20px;
                    margin-bottom: 20px;
                    flex-shrink: 0;
                }
                .close-icon {
                    width: 40px;
                    height: 40px;
                    position: absolute;
                    top: -20px;
                    right: 0px;
                    z-index: 2;
                }
                .pic-layer {
                    width: 150px;
                    height: 150px;
                    background: rgba(0,0,0,.6);
                    position: absolute;
                    top: 0;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    .layer-text {
                        font-size: 18px;
                        color: #fff;
                        opacity: 0.8;
                        position: absolute;
                        left: 50%;
                        transform: translateX(-50%);
                    }
                    .layer-loading {
                        width: 60px;
                        height: 60px;
                        position: relative;
                        .layer-progress {
                            width: 60px;
                            height: 60px;
                            border-radius: 50%;
                            border: 5px solid #8F8E8C;
                            border-top: 5px solid #fff;
                            position: absolute;
                            left: -5rpx;
                            top: -3rpx;
                            -webkit-animation: spin 2s linear infinite;
                            animation: spin 2s linear infinite;
                        }
                        .layer-text {
                            position: absolute;
                            z-index: 2;
                            top: 50%;
                            transform: translate(-50%,-50%);
                            left: 50%;
                            word-break: keep-all;
                        }
                    }
                }
            }
            .camera-pic-box {
                position: relative;
                top: -14px;
                width: 149px;
                height: 149px;
                border: 1Px solid #ddd;
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                .camera-pic {
                    width: 45px;
                    height: 37px;
                }
                .add-text {
                    font-size: 18px;
                    color: #9B9B9B;
                }
            }
        }
    
    
    // 关于typescript的类型定义你可以去掉
    onChooseImg (): void {
            const filesLength = this.state.files.length
            this.setState({
                fileIndex: 0
            })
            if (this.state.files.length < 9) {
                Taro.chooseImage({
                    sourceType: ['album', 'camera'],
                    sizeType: ['compressed'],
                    count: 9 - filesLength
                }).then((res:any) => {
                    this.setState({
                        files: this.state.files.concat(res.tempFiles).slice(0,9)
                    })
                    this.setState({
                        showLoadingLayer: true,
                        showUploadingText: true,
                        showWaitingUploadText: true
                    })
                    // 选中图片之后就要上传
                    /*这里我一开始直接遍历res.tempFiles数组,后来发现,我上传的跟我选中的图片无法对上,所以这里不能直接循环遍历选中的图片数组*/
                    this.onUpload(res.tempFiles, res.tempFiles[this.state.fileIndex])
                }).catch(e=> {console.log(e)})
            } else {
                Taro.showToast({
                    title: '最多只能上传9张图片!',
                    icon: 'none'
                })
            }
        }
         // 这里只在本地删除
        onDeletePhoto (index:number): void {
            this.state.files.splice(index, 1)
            this.setState({
                files: this.state.files
            })
            for (let i = 0; i < this.state.recordFiles.length; i++) {
                if (index === i) {
                    this.state.recordFiles.splice(index, 1)
                    this.setState({
                        recordFiles: this.state.recordFiles
                    })
                    break;
                }
            }
        }
        onUpload (tempArr: Array<IFile>, file: IFile): void {
            const fileObj = file
        
            file && Taro.uploadFile({
                url: `这里换成你的上传图片的接口地址`,
                filePath: file.path,
                name: 'file',
                // 根据情况看是否需要传token
                header: {
                    "Content-Type": "multipart/form-data",
                    Authorization: 'Bearer ' + Taro.getStorageSync('UserInfo').accessToken
                },
                // complete比success先执行
                complete: () => {
                    if (this.state.fileIndex === tempArr.length) {
                        this.setState((preState: any) => {
                            preState.showLoadingLayer = false
                            preState.showUploadingText = false
                            preState.showWaitingUploadText = false
                        })
                    } else {
                        let { fileIndex } = this.state
                        fileIndex = fileIndex + 1 // 避免index = 0上传两次
                        this.onUpload(tempArr, tempArr[fileIndex])
                    }
                },
                success: (res) => {
                    let file = JSON.parse(res.data).obj
                    // 控制蒙层的显示
                    for (let i = 0; i < this.state.files.length; i++) {
                        if (this.state.files[i].path === fileObj.path) {
                            this.state.files[i].isUploaded = true
                            break;
                        }
                    }
                    this.setState((preState:any) => {
                        preState.recordFiles = preState.recordFiles.concat(file[0].url)
                        preState.fileIndex = preState.fileIndex + 1
                    }, () => {
                        console.log('success fileIndex = ', this.state.fileIndex)
                    })
                }
            })
        }
    

    需要在state中定义的数据:

    state = {
      files: [],
      recordFiles: [],
      showLoadingLayer: false,
      showUploadingText: false,
      fileIndex: 0 // 当前上传的图片索引
    }
    

    相关文章

      网友评论

          本文标题:小程序总结(二十一)- 基于Taro + typescript的

          本文链接:https://www.haomeiwen.com/subject/uokczctx.html