uniapp uview2上传文件图片并获取返回识别数据

js yekong

html

<u-upload width='120rpx' height='100rpx' :fileList="fileList2"
                                    @afterRead="afterRead($event,'BILL')" @delete="deletePic" name="2" :maxCount="1">
                                    <view class="flex-col items-center image-wrapper">
                                        <image
                                            src="https://piaolala.oss-cn-beijing.aliyuncs.com/static/remote/434dd1e89a45ad088a61fa777bf619ae.png"
                                            class="image_4" />
                                    </view>
                                </u-upload>

js

<script>
    import configs from '../../config/config.js'
    export default {
        data() {
            return {
                fileList1: [],
                fileList2: [],
                show: false,
                status: 0,
                data2: [],
                checked: false,
                data: {
                    attachIds: [],
                    billAcceptAcct: "",
                    billAcceptName: "",
                    billAmount: "",
                    billExpireDate: "",
                    billNo: "",
                    entId: ""
                }
            };
        },
        methods: {
            // 删除图片
            deletePic(event) {
                console.log(event)
                this[`fileList${event.name}`] = []
            },
            
            // 新增图片
            async afterRead(event, type) {
                // 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
                let lists = [].concat(event.file)
                let fileListLen = this[`fileList${event.name}`].length
                lists.map((item) => {
                    this[`fileList${event.name}`].push({
                        ...item,
                        status: 'uploading',
                        message: '上传中'
                    })
                })
                for (let i = 0; i < lists.length; i++) {
                    const result = await this.uploadFilePromise(lists[i].thumb, type)
                    let item = this[`fileList${event.name}`][fileListLen]
                    this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
                        status: 'success',
                        message: '',
                        url: result
                    }))
                    fileListLen++
                }
            },
            getBILL(data) {
                console.log(data)
                var that = this;
                let data2 = JSON.parse(data)
                let data3 = JSON.parse(data2.data.text)
                console.log(data3)
                console.log('ssss')
                if (data3.billAcceptAcct) {
                    that.data.attachIds = data3.attachIds
                    that.data.billAcceptAcct = data3.billAcceptAcct
                    that.data.billAcceptName = data3.billAcceptName
                    that.data.billAmount = data3.billAmount
                    that.data.billExpireDate = data3.billExpireDate
                    that.data.billNo = data3.billNo
                    that.data.entId = data3.entId
                }
            },
            uploadFilePromise(url, type) {
                var that = this;
                return new Promise((resolve, reject) => {
                    if (uni.getStorageSync('token')) {
                        let a = uni.uploadFile({
                            url: configs.upload, // 仅为示例,非真实的接口地址
                            filePath: url,
                            name: 'file',
                            header: {
                                Authorization: uni.getStorageSync('token')
                            },
                            formData: {
                                attachType: type
                            },
                            success: (res) => {
                                console.log(JSON.parse(res.data))
                                console.log(1234)
                                if (JSON.parse(res.data).code == 401) {
                                    uni.redirectTo({
                                        url: '/pages/login/login'
                                    })
                                }
                                if (type == 'BILL') {
                                    that.getBILL(res.data)
                                }
                                setTimeout(() => {
                                    resolve(JSON.parse(res.data).data)
                                }, 1000)
                            },
                            fail: (res) => {
                                console.log(res)
                                console.log(123456)
                                if (res.code == 401) {
                                    uni.redirectTo({
                                        url: '/pages/login/login'
                                    })
                                }
                            }
                        });
                    } else {
                        uni.redirectTo({
                            url: '/pages/login/login'
                        })
                    }

                })
            },

        }
    };
</script>

喜欢