H5、uniapp等上传图片

做的H5公众号开发,上传图片非得包一层微信的方法。。。。。无语。。微信也太严谨了。

是先把图片上传到七牛云,拿到地址,再传给后端的~同时把七牛云上传成功返回回来的地址赋值给了页面

methods:{
    async uniUpload() {
                let tokenRes = await this.$api.myRequest({
                    url: '/api/get_token',
                    method: 'POST'
                })
                console.log(tokenRes, '我是')

                let that = this

                let u = window.location.href
                console.log(u)
                let request = await this.$api.myRequest({
                    url: '/api/getwxconfig',
                    data: {
                        url: u
                    }
                })
                console.log(request, 'res')
                const {
                    appId,
                    nonceStr,
                    signature,
                    timestamp,
                    jsApiList,
                    debug
                } = request.data
                // 微信分享授权
                wx.config({
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出
                    appId, // 必填,公众号的唯一标识,填自己的!
                    timestamp, // 必填,生成签名的时间戳,刚才接口拿到的数据
                    nonceStr, // 必填,生成签名的随机串
                    signature, // 必填,签名,见附录1
                    jsApiList
                })
                // wx.ready(function() {
                wx.ready(() => {
                    uni.chooseImage({
                        count: 9, // 最多可以选择的图片张数,默认9
                        sizeType: ['compressed'], // original 原图,compressed 压缩图,默认二者都有
                        sourceType: ['album', 'camera'], // album 从相册选图,camera 使用相机,默认二者都有
                        success: function(res) {

                            let img = res.tempFilePaths[0];
                            console.log(img);
                            that.imageUrl = img;
                            let formData = {
                                'token': tokenRes.token,
                                'key': new Date().valueOf() + '_' + Math.ceil(Math.random() *
                                    8000 +
                                    1000) + '.jpg'
                            };
                            console.log(formData);
                            uni.uploadFile({
                                url: 'https://upload-z2.qiniup.com',
                                filePath: img,
                                name: 'file',
                                formData: formData,
                                success: (uploadFileRes) => {
                                    console.log(uploadFileRes);
                                    // let key = JSON.parse(uploadFileRes.data).key;
                                    // let partImgUrl = uploadMsg.prefix + key;
                                    // this.partImgList.push({
                                    //     imgUrl: partImgUrl
                                    // })
                                    console.log(typeof(uploadFileRes));
                                    let dataStr = uploadFileRes.data;
                                    console.log(typeof(dataStr));
                                    let dataObj = JSON.parse(dataStr);
                                    console.log(typeof(dataObj));
                                    if (uploadFileRes.errMsg == 'uploadFile:ok') {
                                        console.log(uploadFileRes.errMsg)
                                        console.log(uploadFileRes.data)
                                        let imageUrl = 'https://img.jialebi.net/' +
                                            dataObj.key;
                                        console.log(imageUrl);
                                        // that.img = imageUrl
                                        let imgMap = {
                                            'url': imageUrl
                                        }
                                        that.imgArr.push(imgMap);
                                        console.log(JSON.stringify(that.imgArr));
                                    }
                                },
                                fail: (err) => {
                                    console.log('fail', err);
                                }
                            });
                        }
                    })
                })

                // }
            }
}

data:

data() {
            return {
                noClick:true,
                array: ['甩卖', '打折', '优惠'],
                index: 0,
                imageUrl: '', //本地图片
                type: '甩卖', //类型
                title: '', //标题
                info: '', //详情
                img: '', //线上图片
                imgArr: [],
                id: '',
                isshow:0,
                aid:0,
            }
        },

template:

<view class="jishi-li2">
                    <view class="li-title li-rtext">活动图片二维码等</view>
                        <view>
                            <view class="upimg-box2" v-if="imageUrl!=''">
                                <image :src="imageUrl"></image>
                            </view>
                            <view class="upimg-box2" @click="uniUpload">
                                <image src="../../static/img/img3.png"></image>
                            </view>
                        </view>
 </view>

猜你喜欢

转载自blog.csdn.net/keaicll/article/details/129671915