function jyAppInterface(option) { this._init(option); } jyAppInterface.prototype = { _init (option) { var that = this; this.ImgStorage = []; this.url= option.url || "/MobileProblem/Base64UploadAndSave"; this.App = option.myInterfaceName; this.LodjyAppInterface(); }, SaveUserImg(imgbase64) { var that = this; var toimgbase64="data:image/jpeg;base64," + imgbase64; var obj={size:3000}; //this.compress(toimgbase64, obj, (base64) => { // that.showImgDetail(base64, that.randomn(5)) //}) that.showImgDetail(toimgbase64, that.randomn(5)) //that.showImgDetail(that.compress("data:image/jpeg;base64," + imgbase64, "1200"), that.randomn(5)) }, LodjyAppInterface() { var that = this; document.getElementById('pickPhoto').addEventListener('tap', function () { that.TakePhoto(); }); document.getElementById('pickCamera').addEventListener('tap', function () { that.TakePhoto(); }); }, //调用家医拍照接口 TakePhoto () { this.App.takePhotoJS("1"); }, //压缩base64 compress (base64String, obj,callback) { var img = new Image(); img.src = base64String; img.onload = function () { var that = this; // 默认按比例压缩 var w = that.width, h = that.height; if (Math.max(w, h) > obj.size) { if(w>h){ w = obj.size; h = obj.size * h / w; }else{ h = obj.size; w = obj.size * w / h; } } var quality = 1; // 默认图片质量为1 //生成canvas var canvas = document.createElement('canvas'); var ctx = canvas.getContext('2d'); // 创建属性节点 var anw = document.createAttribute("width"); anw.nodeValue = w; var anh = document.createAttribute("height"); anh.nodeValue = h; canvas.setAttributeNode(anw); canvas.setAttributeNode(anh); ctx.drawImage(that, 0, 0, w, h); // 图像质量 if (obj.quality && obj.quality <= 1 && obj.quality > 0) { quality = obj.quality; } // quality值越小,所绘制出的图像越模糊 var base64 = canvas.toDataURL('image/jpeg', quality); canvas = null; // 回调函数返回base64的值 callback(base64); } }, //显示缩略图并存储 showImgDetail(imgbase64, name) { var that = this; var $list = $("#ImgList"); var src = 'src="' + imgbase64 + '"'; var $li = '
  • ' + name + '

  • '; $list.append($li); //存储到ImgStorage var im = { name: "img-" + name, dataURL: imgbase64 }; this.ImgStorage.push(im); //删除照片监听 $("#" + name).children(".destory").click(() => { that.delImg(name); }); }, ///删除照片 delImg(ImgId) { var ImgKey = "img-" + ImgId; var r = this.ImgStorage.filter(function (item) { return item.name != ImgKey; }) this.ImgStorage = r; $("#" + ImgId).remove(); }, //上传 Upload (processId,callback) { var that = this; var count = 0; for(item of this.ImgStorage) { item.ProcessId = processId; $.ajax({ type: "post", url: that.url, data: item, async: false, success: function (data) { if (data.code=200) { count++; } } }); } callback(count); }, randomn(n) { let res = ''; for (; res.length < n; res += Math.random().toString(36).substr(2).toUpperCase()) { } return res.substr(0, n); } }