美文网首页前端开发笔记
MUI+Hbuilder之图片上传(六)

MUI+Hbuilder之图片上传(六)

作者: 喵呜Yuri | 来源:发表于2018-12-19 09:23 被阅读0次
    document.getElementById("uploadImg").addEventListener("tap", function() {
                    var editButtons = new Array();
                    if(vm.uploadImgArr.length < 9) {
                        editButtons.push({
                            title: "拍照上传",
                            style: "default"
                        });
                        editButtons.push({
                            title: "从相册选择",
                            style: "default"
                        });
                        plus.nativeUI.actionSheet({
                            cancel: "取消",
                            buttons: editButtons
                        }, function(e) {
                            var index = e.index;
                            switch(index) {
                                case 1:
                                    captureImage(); //拍照
                                    break;
                                case 2:
                                    selectImage(); //相册选择
                                    break;
                            }
                        });
    
                    } else {
                        mui.toast("最大允许上传九张图片");
                    }
    
                });
    
                //选择图片
                function selectImage() {
                    plus.gallery.pick(function(path) {
                        //设置最大只能上传9张图片
                        var canUploadLength=9-vm.uploadImgUrlArr.length;
                        if(canUploadLength < path.files.length){
                            mui.toast("最大允许上传九张图片");
                            return false;
                        } 
                        loadImage(path);
                    }, function(e) {
                        mui.toast("没有选择图片");
                    }, {
                        filter: 'image',
                        multiple: true,
                        system: false
                    });
                }
                //确定选择图片
                function loadImage(path) {
                    if(typeof path == 'string') {
                        vm.uploadImgArr.push({
                            "status": 0,
                            "url": path
                        });
                        uploadPicture(path);
                    } else {
                        path.files.forEach(function(v, k) {
                            vm.uploadImgArr.push({
                                "status": 0,
                                "url": v
                            });
                        });
                        uploadPicture();
                    }
                }
    
                //上传图片 
                function uploadPicture() {
                    vm.uploadImgArr.forEach(function(v, k) {
                        if(v.status == '0') {
                            vm.uploadImgArr[k].status=2;
                            var image = new Image();
                            image.src = v.url;
                            image.onload = function() {
                                var imgData = getBase64Image(image);
                                var sendData = {
                                    'image': imgData
                                }
                                var AUTH_TOKEN = myStorage.getItem('AUTH_TOKEN');
                                var USER_KEY = myStorage.getItem('USER_KEY');
                                showLoading();
                                mui.ajax(BASE_URL + 'XXXXXXXXX/XXXXXX', {
                                    type: 'POST',
                                    dataType: 'json',
                                    data: sendData,
                                    async: true,
                                    timeout: 100000, //超时时间设置为10秒;
                                    success: function(res) {
                                        if(typeof res == 'string') {
                                            var res = JSON.parse(res);
                                        }
    
                                        //如果登陆失效
                                        if(res.code == '-1' || res.code == '-2') {
                                            mui.toast(res.msg);
                                            clearUserInfoCache(myStorage)
                                            mui.openWindow({
                                                url: '/view/login.html'
                                            })
                                        } else if(res.code == '-3') {
                                            mui.toast(res.msg);
                                            hideLoading();
                                            return false;
                                        }
                                        if(res.code != '1') {
                                            mui.toast(res.msg);
                                        }
                                        vm.uploadImgArr[k].status=1;
                                        vm.uploadImgUrlArr.push(res.param);//上传成功
                                        hideLoading();
                                    },
                                    error: function(xhr, type, errorThrown) {
                                        vm.uploadImgArr[k].status=-1;
                                        hideLoading();
                                    },
                                    headers: {
                                        'access_token': AUTH_TOKEN,
                                        'user_key': USER_KEY,
                                    }
                                });
                            }
                        }
                    });
                }
    
                //将图片压缩转成base64 
                function getBase64Image(img) {
                    var canvas = document.createElement("canvas");
                    var width = img.width;
                    var height = img.height;
                    // calculate the width and height, constraining the proportions 
                    if(width > 1000){
                        height=height/1.5;
                        width=width/1.5;
                    }
                    
                    if(height > 1000){
                        height=height/1.5;
                        width=width/1.5;
                    }
                    
                    canvas.width = width; /*设置新的图片的宽度*/
                    canvas.height = height; /*设置新的图片的长度*/
                    var ctx = canvas.getContext("2d");
                    ctx.drawImage(img, 0, 0, width, height); /*绘图*/
                    var dataURL = canvas.toDataURL("image/png", 1);
                    return dataURL.replace("data:image/png;base64,", "");
                }
    
                //拍照
                function captureImage() {
                    var cmr = plus.camera.getCamera(2);
                    cmr.captureImage(
                        function(path) {
                            //将图片地址转换
                            plus.io.resolveLocalFileSystemURL(path, function(entry) {
                                var newPath = entry.toLocalURL() + "?version=" + Math.random();
                                loadImage(newPath);
                            });
                        },
                        function(error) {
                            mui.toast(error.message);
                        }, {
                            filename: "_documents/"
                        }
                    );
    
                }
    

    相关文章

      网友评论

        本文标题:MUI+Hbuilder之图片上传(六)

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