| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925 |
- var app = new Vue({
- el: '#app',
- data: {
- // 用户信息
- memberInfo: {},
- // 当前编辑后图片
- editPic: '',
- // 剪切参数
- cropParams: {
- id: 0,
- src: '',
- cropSrc: '',
- min: 400,
- max: 200,
- boundx: 0,
- boundy: 0,
- width: 0,
- height: 0,
- },
- // 预览
- previewData: {
- },
- // 参数
- options: {},
- // 图片列表
- picList: [],
- // 预览状态
- cropPreviewStatus: false,
- // 上传状态
- submitting: false,
- // 打印方式
- printType: 1,
- colorType: 1,
- image: null,
- // 设备ID
- deviceId: 0,
- taskKey: '',
- // 剪切对象
- jc: null,
- id: 0,
- },
- created: function () {
- //this.getFileList();
- var printType = getParam('pt');
- this.id = getParam('id');
- this.printType = printType ? printType : 1;
- this.taskKey = getKey();
- },
- updated: function () {
- // 计时器
- var _this = this;
- var MAX = 100, MIN = 1;
- $('.weui-count__decrease').unbind('click').on('click', function (e) {
- if (_this.deviceId) {
- return false;
- }
- var $input = $(e.target).parent().find('.weui-count__number');
- var number = parseInt($input.val() || "0") - 1
- if (number < MIN) number = MIN;
- $input.val(number)
- })
- $('.weui-count__increase').unbind('click').on('click', function (e) {
- if (_this.deviceId) {
- return false;
- }
- var $input = $(e.target).parent().find('.weui-count__number');
- var number = parseInt($input.val() || "0") + 1
- if (number > MAX) number = MAX;
- $input.val(number)
- });
- $(".weui-count__number").blur(function () {
- var num = $.trim($(this).val());
- if (!/[1-9][0-9]{0,2}/.test(num) || num < MIN || num > MAX) {
- num = 1;
- }
- $(this).val(num);
- })
- },
- methods: {
- // 获取图片列表
- getFileList: function () {
- var _this = this;
- $.post('/weixin/print/getFileList', {}, function (res) {
- if (res.code == 'success') {
- _this.picList = res.data
- } else if (res.code == 'login') {
- login(res.data.url);
- }
- }, "json");
- },
- // 获取用户信息
- getMemberInfo: function () {
- var _this = this;
- $.post('/weixin/member/getMemberInfo', {}, function (res) {
- if (res.code == 'success') {
- _this.memberInfo = res.data
- } else if (res.code == 'login') {
- login(res.data.url);
- }
- }, "json");
- },
- // 提交
- doSubmit: function () {
- var _this = this;
- var count = 0;
- var formData = new FormData();
- if (_this.submitting) {
- return false;
- }
- if (_this.picList.length <= 0) {
- $.toast('请先选择图片', 'text');
- return false;
- }
- console.log('提交处理');
- var total = $(".print-pic").length;
- // $each(_this.picList,function (k, item) {
- $(".print-pic").each(function (k, item) {
- var _img = $(this).find('.pic-img');
- var name = $.trim(_img.attr('alt'));
- var num = parseInt($(this).find('.pic-num').val());
- var colorType = $(this).find('.color').attr('data-color');
- var $previewImg = document.getElementById("pic_preview_" + k);
- var picId = $(this).attr('data-id');
- var fileData = typeof(_this.picList[k].fileData) != 'undefined' ? _this.picList[k].fileData : '';
- // var canvas = document.createElement("canvas");
- var scale = 12;
- var opts = {
- scale: scale, // 添加的scale 参数
- width: 119,
- height: 169,
- background: '#fff',
- };
- // html2canvas($previewImg, opts).then(function (canvas) {
- // const context = canvas.getContext('2d');
- // context.translate(0.5, 0.5);
- // context.mozImageSmoothingEnabled = false;
- // context.webkitImageSmoothingEnabled = false;
- // context.msImageSmoothingEnabled = false;
- // context.imageSmoothingEnabled = false;
- //
- // var src = canvas.toDataURL('image/jpeg');
- // var file = _this.dataURLtoFile(src, name ? name : '图片打印.jpeg');
- formData.append('type', 1);
- formData.append('taskKey', _this.taskKey);
- if (fileData) {
- formData.append('image[]', fileData);
- formData.append('nums[]', num);
- formData.append('ids[]', picId);
- formData.append('colors[]', colorType);
- }
- count++;
- });
- if (count <= 0) {
- $.toast('请选择打印的图片');
- return false;
- }
- // 上传
- $.showLoading('文件上传处理中...');
- _this.submitting = true;
- var ajax = $.ajax({
- url: '/weixin/upload/image',
- data: formData,
- type: "post",
- dataType: 'json',
- cache: false,
- contentType: false,
- processData: false,
- timeout: 30000,
- success: function (res) {
- $.hideLoading();
- _this.submitting = false;
- if (res.code == 'success') {
- _this.showModal();
- } else if (res.code == 'login') {
- login(res.data.url);
- } else {
- $.toast(res.message);
- }
- },
- error: function (res) {
- _this.submitting = false;
- $.toast('服务器错误');
- },
- complete: function (XMLHttpRequest, status) {
- if (status == 'timeout') {
- ajax.abort();
- $.toast('请求超时,请重新提交');
- location.reload();
- }
- }
- })
- // });
- },
- // 显示填写窗口
- showModal: function () {
- var _this = this;
- $.closeModal();
- $.modal({
- title: "",
- autoClose: false,
- text: "<div class='scan'>填写/扫码打印设备</div><div class='tips'>设备号<input type='text' id='device_code' placeholder='请填写打印机设备号'></div>",
- buttons: [
- {
- text: "取消",
- className: "default",
- onClick: function () {
- $.closeModal();
- return false;
- }
- },
- {
- text: "扫码打印",
- className: "warning",
- onClick: function () {
- _this.scan();
- }
- },
- {
- text: "填写打印",
- className: "success",
- onClick: function () {
- _this.inputCode();
- return false;
- }
- },
- ]
- });
- },
- // 输入设备号打印
- inputCode: function () {
- var _this = this;
- var deviceCode = $.trim($("#device_code").val());
- if (deviceCode == '' || deviceCode === null) {
- $.toast('请填写设备号');
- setTimeout(function () {
- _this.showModal();
- }, 500)
- return false;
- }
- $.post('/weixin/print/checkDevice', {deviceCode: deviceCode}, function (res) {
- if (res.code == 'success') {
- var deviceId = res.data.id;
- $.closeModal();
- $.modal({
- title: "",
- text: "<div class='scan'>设备验证成功</div><div class='info'></div><div class='tips'>点击下方确认下单</div>",
- buttons: [
- {
- text: "取消",
- className: "default",
- onClick: function () {
- $.closeModal();
- setTimeout(function () {
- _this.showModal();
- }, 500)
- return false;
- }
- },
- {
- text: "确认下单",
- className: "warning",
- onClick: function () {
- var params = {
- clusterId: _this.id,
- deviceId: deviceId,
- printType: _this.printType,
- colorType: _this.colorType,
- taskKey: _this.taskKey,
- type: 1
- };
- $.showLoading('订单处理中...');
- $.post('/weixin/order/confirm', params, function (res) {
- $.hideLoading();
- if (res.code == 'success') {
- var msg = _this.id > 0 && _this.printType == 2 ? '参与拼团成功' : (_this.printType == 2 ? '发起拼团成功' : '下单成功');
- $.showLoading(msg + ',即将前往支付...');
- setTimeout(function () {
- location.href = '/weixin/order/pay?id=' + res.data;
- }, 800);
- } else if (res.code == 'login') {
- login(res.data.url);
- } else {
- $.closeModal();
- $.modal({
- title: "",
- text: "<div class='scan'>下单失败</div><div class='tips'>" + res.message + "</div>",
- buttons: [
- {
- text: "取消",
- onClick: function () {
- return false;
- }
- }
- ]
- });
- }
- }, "json")
- }
- },
- ]
- });
- } else {
- $.toast(res.message);
- setTimeout(function () {
- _this.showModal();
- }, 500)
- }
- }, 'json');
- },
- // 扫码设备
- scan: function () {
- var _this = this;
- $.post('/weixin/index/getJssdkParams', {url: location.href}, function (res) {
- var params = res.data;
- // 微信JSSDK
- wx.config({
- debug: false, // 是否调试模式
- appId: params.appId, // 必填,公众号的唯一标识
- timestamp: params.timestamp, // 必填,生成签名的时间戳
- nonceStr: params.nonceStr, // 必填,生成签名的随机串
- signature: params.signature,// 必填,签名
- jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'scanQRCode'] // 必填,需要使用的JS接口列表
- });
- // 初始化处理
- wx.ready(function () {
- // 微信扫一扫
- $.closeModal();
- wx.scanQRCode({
- needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
- scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
- success: function (res) {
- var deviceId = res.resultStr;
- _this.deviceId = deviceId;
- if (deviceId) {
- $.closeModal();
- $.modal({
- title: "",
- text: "<div class='scan'>扫码成功</div><div class='info'></div><div class='tips'>点击下方确认下单</div>",
- buttons: [
- {
- text: "取消",
- className: "default",
- onClick: function () {
- $.closeModal();
- setTimeout(function () {
- _this.showModal();
- }, 500)
- return false;
- }
- },
- {
- text: "确认下单",
- className: "warning",
- onClick: function () {
- var params = {
- clusterId: _this.id,
- deviceId: deviceId,
- printType: _this.printType,
- colorType: _this.colorType,
- taskKey: _this.taskKey,
- type: 1
- };
- $.showLoading('订单处理中...');
- $.post('/weixin/order/confirm', params, function (res) {
- $.hideLoading();
- if (res.code == 'success') {
- var msg = _this.id > 0 && _this.printType == 2 ? '参与拼团成功' : (_this.printType == 2 ? '发起拼团成功' : '下单成功');
- $.showLoading(msg + ',即将前往支付...');
- setTimeout(function () {
- location.href = '/weixin/order/pay?id=' + res.data;
- }, 800);
- } else if (res.code == 'login') {
- login(res.data.url);
- } else {
- $.closeModal();
- $.modal({
- title: "",
- text: "<div class='scan'>下单失败</div><div class='tips'>" + res.message + "</div>",
- buttons: [
- {
- text: "取消",
- onClick: function () {
- return false;
- }
- },
- {
- text: "重新扫码下单",
- className: "warning",
- onClick: function () {
- _this.scan();
- }
- },
- ]
- });
- }
- }, "json")
- }
- },
- ]
- });
- } else {
- $.toast('扫码信息错误');
- }
- }
- });
- });
- }, "JSON");
- },
- // base转文件
- dataURLtoFile: function (dataurl, filename) {//将base64转换为文件
- var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
- bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
- while (n--) {
- u8arr[n] = bstr.charCodeAt(n);
- }
- return new File([u8arr], filename, {type: mime});
- },
- // base转文件
- dataBlobtoFile: function (dataurl, filename) {//将base64转换为文件
- return new File([dataurl], filename, {type: 'image/jpeg'});
- },
- //
- dataURLtoBlob: function(dataurl) {
- var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
- bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
- while (n--) {
- u8arr[n] = bstr.charCodeAt(n);
- }
- return new Blob([u8arr], {type: mime});
- },
- // 删除图片
- picDel: function (id) {
- var fileId = $("#pic_" + id).attr('data-id');
- $("#pic_" + id).detach();
- this.picList[id] = {};
- if (fileId) {
- $.post("/weixin/print/fileDel", {id: fileId})
- }
- },
- // 彩印
- picColor: function (k, type) {
- if (type == 1) {
- this.colorType = 1;
- $(".print-pic .op .color1").addClass('active');
- $(".print-pic .op .color2").removeClass('active');
- } else {
- this.colorType = 2;
- $(".print-pic .op .color1").removeClass('active');
- $(".print-pic .op .color2").addClass('active');
- }
- },
- // 编辑图片
- picEdit: function (id, data, type) {
- var _this = this;
- $.closePopup();
- _this.cropParams = data;
- _this.cropParams.id = id;
- _this.cropParams.type = type;
- _this.cropParams.resize = 0;
- _this.cropParams.rotate = 0;
- if (_this.cropParams.url == '') {
- $.toast('请重新添加图片');
- return false;
- }
- $("body").css('overflowY','hidden');
- // $("#cropImg").remove();
- $(".crop-img").html('<img id="cropImg" src="" alt="" off="">');
- if(id>=0){
- $("#cropImg").attr('src', _this.cropParams.old_url);
- }else{
- $("#cropImg").attr('src', _this.cropParams.url);
- }
- var iWidth = $("body").outerWidth();
- var iHeight = $("body").height()-130;
- var cropWidth = $('body').width();
- $image = $("#cropImg");
- $("#cropBox").popup();
- _this.options = {
- // aspectRatio: iWidth/iHeight,
- center: true,
- movable: true,
- zoomable: true,
- zoomOnTouch: true,
- zoomOnWheel: false,
- scalable: true,
- wheelZoomRatio: 0.1,
- responsive: true,
- guides: false,
- strict: false,
- width: iWidth,
- dragMode: 'move',
- height: iHeight,
- autoCropArea: 1,
- // cropBoxMovable: false,
- // cropBoxResizable: false,
- minContainerWidth: iWidth,
- minContainerHeight: iHeight,
- minCropBoxWidth: 80,
- maxCropBoxWidth: iWidth,
- minCropBoxHeight: 80,
- canvasWeight: cropWidth,
- canvasHeight: iHeight,
- // minCanvasWeight: cropWidth*0.4,
- maxCanvasWeight: cropWidth,
- // minCanvasHeight: iHeight,
- viewMode: 1,
- crop: function (data) {
- _this.cropParams.data = data;
- var rate = 1;
- if(data.height >2000 || data.width>2000){
- rate = 0.8;
- }else if( data.height < 1000 || data.width<1000){
- rate = 1.5;
- }
- _this.cropParams.cropData = {
- // width: iWidth * rate,
- width: data.width * rate,
- // height: iHeight * rate,
- height: data.height * rate,
- rotate: data.rotate,
- imageSmoothingQuality: 'high',
- fillColor: '#fff',
- };
- }
- }
- if (_this.jc != null) {
- $image.cropper(_this.options);
- _this.jc = $image.cropper('replace', _this.cropParams.old_url);
- $image.cropper('setData', {
- width: iWidth,
- height: iHeight
- });
- } else {
- $image.cropper(_this.options);
- _this.jc = $image.cropper('replace', _this.cropParams.old_url);
- $image.cropper('setData', {
- width: iWidth,
- height: iHeight
- });
- }
- _this.cropRotate();
- },
- // 预览
- picPreview: function(id, data){
- this.previewData = data;
- $.closePopup();
- $("#previewBox").popup();
- },
- // 取消剪切
- previewCancel: function () {
- $.closePopup();
- this.previewData = {};
- },
- // 图片大小
- resetSizeImg: function (image, FitWidth, FitHeight) {
- var newImg = image;
- if (image.width > 0 && image.height > 0) {
- if (image.width / image.height >= FitWidth / FitHeight) {
- if (image.width > FitWidth) {
- image.width = FitWidth;
- newImg.height = (image.height * FitWidth) / image.width;
- } else {
- newImg.width = image.width;
- newImg.height = image.height;
- }
- } else {
- if (image.height > FitHeight) {
- newImg.height = FitHeight;
- newImg.width = (image.width * FitHeight) / image.height;
- } else {
- newImg.width = image.width;
- newImg.height = image.height;
- }
- }
- }
- return newImg;
- },
- // 取消剪切
- cropCancel: function () {
- $.closePopup();
- $("#previewBox").hide();
- if (this.jc != false) {
- $("body").css('overflowY','scroll');
- $("#cropImg").cropper('destroy');
- this.jc = null;
- this.cropParams.resize = 0;
- $("#cropImg").remove();
- }
- },
- // 缩放
- cropZoom: function (ele, type) {
- $("#cropImg").cropper("zoom", type == 1 ? 0.1 : -0.1);
- },
- // 旋转
- rotate: function (ele, type) {
- if(type == 1){
- $("#cropImg").cropper("rotate", -90);
- }else{
- $("#cropImg").cropper("rotate", 90);
- }
- this.cropParams.resize = 0;
- var data = $("#cropImg").cropper('getContainerData');
- var cdata = $("#cropImg").cropper('getCanvasData');
- $("#cropImg").cropper('setCropBoxData', {
- width: data.width,
- height: data.height,
- // top: data.height-cdata.height>0? (data.height-cdata.height)/2 : 0,
- // left: data.width-cdata.width>0? (data.width-cdata.width)/2 : 0
- });
- },
- // 适应
- cropSize: function(){
- var _this = this;
- var data = _this.cropParams.data;
- if(_this.cropParams.resize>=2){
- return false;
- }
- if(data.height<data.width){
- $("#cropImg").cropper("rotate", 90);
- _this.cropParams.cropData.rotate = 90;
- }
- var cwidth = $(".cropper-canvas").outerWidth();
- var size = data.height<data.width? data.height: data.width;
- var iWidth = $("body").outerWidth();
- var iHeight = $("body").height()-130;
- if(size<iWidth){
- var rate = iWidth/size;
- $("#cropImg").cropper("zoom", rate);
- }else if(cwidth<iWidth){
- var rate = iWidth/cwidth;
- $("#cropImg").cropper("zoom", rate);
- }
- _this.cropParams.resize++;
- var data = $("#cropImg").cropper('getContainerData');
- var cdata = $("#cropImg").cropper('getCanvasData');
- $("#cropImg").cropper('setCropBoxData', {
- width: data.width,
- height: data.height,
- // top: data.height-cdata.height>0? (data.height-cdata.height)/2 : 0,
- // left: data.width-cdata.width>0? (data.width-cdata.width)/2 : 0
- });
- },
- // 自适应旋转
- cropRotate: function(){
- var _this = this;
- var image = new Image();
- image.src = _this.cropParams.url;
- var height = $("#cropImg").outerWidth();
- var width = $("#cropImg").outerHeight();
- if(height<width){
- $("#cropImg").cropper("rotate", 90);
- _this.cropParams.crotate = 90;
- }
- _this.cropParams.rotate = 1;
- var data = $("#cropImg").cropper('getContainerData');
- var cdata = $("#cropImg").cropper('getCanvasData');
- $("#cropImg").cropper('setCropBoxData', {
- width: data.width,
- height: data.height,
- // top: data.height-cdata.height>0? (data.height-cdata.height)/2 : 0,
- // left: data.width-cdata.width>0? (data.width-cdata.width)/2 : 0
- });
- },
- // 确定剪切
- cropConfirm: function () {
- var _this = this;
- $("body").css('overflowY','scroll');
- $(".crop-img").show();
- this.cropPreviewStatus = false;
- if (this.jc != null) {
- $.showLoading('图片处理中...');
- var id = _this.cropParams.id;
- $image = $("#cropImg");
- var canvas = $image.cropper('getCroppedCanvas',_this.cropParams.cropData);
- var blob = canvas.toBlob(function (blob) {
- //生成Blob的图片格式
- if (blob != null) {
- var timestamp = Date.parse(new Date());
- blob.name = timestamp + ".jpg";
- url = URL.createObjectURL(blob);
- if(_this.cropParams.type && id>=0){
- _this.picList[id] = _this.cropParams;
- _this.picList[id].url = url;
- _this.picList[id].preview = url;
- _this.picList[id].fileData = _this.dataBlobtoFile(blob, blob.name);
- }else{
- var data = _this.cropParams;
- data.url = url;
- data.preview = url;
- data.fileData = _this.dataBlobtoFile(blob, blob.name);
- _this.picList.push(data);
- }
- setTimeout(function () {
- $.closePopup();
- $.hideLoading();
- _this.cropParams = {};
- _this.jc = null;
- $("#cropImg").cropper('destroy');
- $("#cropImg").remove();
- }, 200);
- }else{
- $.closePopup();
- $.hideLoading();
- $.showLoading('图片处理失败,请重新上传');
- _this.cropParams = {};
- setTimeout(function(){
- $.hideLoading();
- }, 1000);
- }
- }, "image/jpeg");
- }
- },
- // 重新剪切
- cropReset: function () {
- $image = $("#cropImg");
- $image.cropper(this.options);
- this.jc = $image.cropper('replace', this.cropParams.old_url);
- this.cropParams.resize = false;
- },
- // 旋转图片处理
- rotateImg: function(img, direction,canvas) {
- //alert(img);
- //最小与最大旋转方向,图片旋转4次后回到原方向
- var min_step = 0;
- var max_step = 3;
- //var img = document.getElementById(pid);
- if (img == null)return;
- //img的高度和宽度不能在img元素隐藏后获取,否则会出错
- var height = img.height;
- var width = img.width;
- //var step = img.getAttribute('step');
- var step = 2;
- if (step == null) {
- step = min_step;
- }
- if (direction == 'right') {
- step++;
- //旋转到原位置,即超过最大值
- step > max_step && (step = min_step);
- } else {
- step--;
- step < min_step && (step = max_step);
- }
- //img.setAttribute('step', step);
- /*var canvas = document.getElementById('pic_' + pid);
- if (canvas == null) {
- img.style.display = 'none';
- canvas = document.createElement('canvas');
- canvas.setAttribute('id', 'pic_' + pid);
- img.parentNode.appendChild(canvas);
- } */
- //旋转角度以弧度值为参数
- var degree = step * 90 * Math.PI / 180;
- var ctx = canvas.getContext('2d');
- switch (step) {
- case 0:
- canvas.width = width;
- canvas.height = height;
- ctx.drawImage(img, 0, 0);
- break;
- case 1:
- canvas.width = height;
- canvas.height = width;
- ctx.rotate(degree);
- ctx.drawImage(img, 0, -height);
- break;
- case 2:
- canvas.width = width;
- canvas.height = height;
- ctx.rotate(degree);
- ctx.drawImage(img, -width, -height);
- break;
- case 3:
- canvas.width = height;
- canvas.height = width;
- ctx.rotate(degree);
- ctx.drawImage(img, -width, 0);
- break;
- }
- },
- // 选择图片
- selectPic: function (ele) {
- var _this = this;
- var files = ele.target.files;
- var id = _this.picList.length;
- if (files.length > 0) {
- $.each(files, function (k, file) {
- if (file) {
- var Orientation = null;
- //获取照片方向角属性,用户旋转控制
- EXIF.getData(file, function() {
- // alert(EXIF.pretty(this));
- EXIF.getAllTags(this);
- //alert(EXIF.getTag(this, 'Orientation'));
- Orientation = EXIF.getTag(this, 'Orientation');
- //return;
- });
- var reader = new FileReader();
- reader.readAsDataURL(file);
- var blobUrl = URL.createObjectURL(file);
- reader.onloadend = function (even) {
- var image = new Image();
- image.src = even.currentTarget.result;
- image.onload = function(){
- var expectWidth = this.naturalWidth;
- var expectHeight = this.naturalHeight;
- if (this.naturalWidth > this.naturalHeight && this.naturalWidth > 800) {
- expectWidth = 800;
- expectHeight = expectWidth * this.naturalHeight / this.naturalWidth;
- } else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) {
- expectHeight = 1200;
- expectWidth = expectHeight * this.naturalWidth / this.naturalHeight;
- }
- var canvas = document.createElement("canvas");
- var ctx = canvas.getContext("2d");
- canvas.width = expectWidth;
- canvas.height = expectHeight;
- ctx.drawImage(this, 0, 0, expectWidth, expectHeight);
- var base64 = null;
- //修复ios
- if (navigator.userAgent.match(/iphone/i)) {
- //alert(expectWidth + ',' + expectHeight);
- //如果方向角不为1,都需要进行旋转 added by lzk
- if(Orientation != "" && Orientation != 1){
- //alert('旋转处理');
- switch(Orientation){
- case 6://需要顺时针(向左)90度旋转
- //alert('需要顺时针(向左)90度旋转');
- _this.rotateImg(this,'left',canvas);
- break;
- case 8://需要逆时针(向右)90度旋转
- //alert('需要顺时针(向右)90度旋转');
- _this.rotateImg(this,'right',canvas);
- break;
- case 3://需要180度旋转
- //alert('需要180度旋转');
- _this.rotateImg(this,'right',canvas);//转两次
- _this.rotateImg(this,'right',canvas);
- break;
- }
- }
- base64 = canvas.toDataURL("image/jpeg", 0.8);
- }else{
- //alert(Orientation);
- if(Orientation != "" && Orientation != 1){
- //alert('旋转处理');
- switch(Orientation){
- case 6://需要顺时针(向左)90度旋转
- //alert('需要顺时针(向左)90度旋转');
- _this.rotateImg(this,'left',canvas);
- break;
- case 8://需要逆时针(向右)90度旋转
- //alert('需要顺时针(向右)90度旋转');
- _this.rotateImg(this,'right',canvas);
- break;
- case 3://需要180度旋转
- //alert('需要180度旋转');
- _this.rotateImg(this,'right',canvas);//转两次
- _this.rotateImg(this,'right',canvas);
- break;
- }
- }
- base64 = canvas.toDataURL("image/jpeg", 0.8);
- }
- //uploadImage(base64);
- //$("#myImage").attr("src", base64);
- var img = {
- id: 0,
- url: base64,
- old_url: base64,
- // url: base64,
- preview: base64,
- name: file.name,
- blobUrl: blobUrl,
- file_size: file.size,
- file_type: file.type
- };
- $("#upload").val("");
- // _this.picList.push(img);
- // console.log(_this.picList)
- _this.picEdit(0, img, 0);
- }
- }
- }
- })
- /*if (count > 0) {
- _this.getFileList(0);
- }*/
- }
- }
- }
- });
|