export default {
    //获取图片
    imgPreview(file, callback) {
        let self = this;
        //判断支不支持FileReader
        if (!file || !window.FileReader) return;
        if (/^image/.test(file.type)) {
            //创建一个reader
            let reader = new FileReader();

            //将图片转成base64格式
            reader.readAsDataURL(file);
            //读取成功后的回调
            reader.onloadend = function() {
                let result = this.result;
                let img = new Image();
                img.src = result;
                // self.imgSrc = result;
                img.onload = function() {
                    let data = self.compress(img);
                    let blob = self.dataURItoBlob(data);
                    console.log(blob);
                    var formData = new FormData();
                    formData.append("file", blob);
                    let config = {
                        headers: { "Content-Type": "multipart/form-data" }
                    };
                    //图片预览,配置文件,formData数据;
                    callback && callback(data,config,formData);
                    // console.log(formData.file);
                    // console.log(formData);

                  

                    // 数据结构请求
                };
            };
        }
    },
    // 压缩图片
    compress(img) {
        let canvas = document.createElement("canvas");
        let ctx = canvas.getContext("2d");
        // let initSize = img.src.length;
        let width = img.width;
        let height = img.height;
        canvas.width = width;
        canvas.height = height;
        // 铺底色
        ctx.fillStyle = "#fff";
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(img, 0, 0, width, height);

        //进行最小压缩
        let ndata = canvas.toDataURL("image/jpeg", 0.4);
        // console.log("*******压缩后的图片大小*******");
        // console.log(ndata)
        // console.log(ndata.length);
        return ndata;
    },

    // base64转成bolb对象
    dataURItoBlob(base64Data) {
        var byteString;
        if (base64Data.split(",")[0].indexOf("base64") >= 0)
            byteString = atob(base64Data.split(",")[1]);
        else byteString = unescape(base64Data.split(",")[1]);
        var mimeString = base64Data
            .split(",")[0]
            .split(":")[1]
            .split(";")[0];
        var ia = new Uint8Array(byteString.length);
        for (var i = 0; i < byteString.length; i++) {
            ia[i] = byteString.charCodeAt(i);
        }
        return new Blob([ia], { type: mimeString });
    },
    //获取url参数
    getQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    } 
}