export default {
    //获取图片
    imgPreview(file, callback) {
        let self = this;
        let name = file.name.split('.')[0]
        //判断支不支持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();
                let formData = new FormData();
                img.src = result;
                img.onload = function() {
                    let data = self.compress(img,name);
                    let blob = self.dataURItoBlob(data);
                    blob.name = name+'.jpeg'
                    console.log(blob,blob.name);
                    // blob.name = blob.name+'jpeg';
                    formData.append("file", blob);
                    //图片预览,配置文件,formData数据;
                    callback && callback(data,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)
        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);
        }
        console.log([ia]);
        return new Blob([ia], { type: mimeString },);
    },
    getDate(){
        let myDate = new Date();
        myDate.getFullYear(); //获取完整的年份(4位,1970-????)
        myDate.getMonth(); //获取当前月份(0-11,0代表1月)
        myDate.getDate(); //获取当前日(1-31)
        myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
        myDate.getHours(); //获取当前小时数(0-23)
        myDate.getMinutes(); //获取当前分钟数(0-59)
        myDate.getSeconds(); //获取当前秒数(0-59)
    },

    //检查字符串是否JSON格式
    isJSON(str) {
        if (typeof str == 'string') {
            try {
                JSON.parse(str);
                return true;
            } catch(e) {
                //console.log(e);
                return false;
            }
        } 
    },

    //获取url参数
    getQueryString(name){
        var reg = new RegExp('(^|&)'+name+'=([^&]*)(&|$)','i');
        var res = window.location.search.substr(1).match(reg);
        if(res != null){
            return unescape(res[2]);
        }
        return null;
    }

}