photo_back.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. var app = new Vue({
  2. el: '#app',
  3. data: {
  4. // 用户信息
  5. memberInfo: {},
  6. // 当前编辑后图片
  7. editPic: '',
  8. // 剪切参数
  9. cropParams: {
  10. id: 0,
  11. src: '',
  12. cropSrc: '',
  13. min: 400,
  14. max: 200,
  15. boundx: 0,
  16. boundy: 0,
  17. width: 0,
  18. height: 0,
  19. },
  20. // 预览
  21. previewData: {
  22. },
  23. // 参数
  24. options: {},
  25. // 图片列表
  26. picList: [],
  27. // 预览状态
  28. cropPreviewStatus: false,
  29. // 上传状态
  30. submitting: false,
  31. // 打印方式
  32. printType: 1,
  33. colorType: 1,
  34. image: null,
  35. // 设备ID
  36. deviceId: 0,
  37. taskKey: '',
  38. // 剪切对象
  39. jc: null,
  40. id: 0,
  41. },
  42. created: function () {
  43. //this.getFileList();
  44. var printType = getParam('pt');
  45. this.id = getParam('id');
  46. this.printType = printType ? printType : 1;
  47. this.taskKey = getKey();
  48. },
  49. updated: function () {
  50. // 计时器
  51. var _this = this;
  52. var MAX = 100, MIN = 1;
  53. $('.weui-count__decrease').unbind('click').on('click', function (e) {
  54. if (_this.deviceId) {
  55. return false;
  56. }
  57. var $input = $(e.target).parent().find('.weui-count__number');
  58. var number = parseInt($input.val() || "0") - 1
  59. if (number < MIN) number = MIN;
  60. $input.val(number)
  61. })
  62. $('.weui-count__increase').unbind('click').on('click', function (e) {
  63. if (_this.deviceId) {
  64. return false;
  65. }
  66. var $input = $(e.target).parent().find('.weui-count__number');
  67. var number = parseInt($input.val() || "0") + 1
  68. if (number > MAX) number = MAX;
  69. $input.val(number)
  70. });
  71. $(".weui-count__number").blur(function () {
  72. var num = $.trim($(this).val());
  73. if (!/[1-9][0-9]{0,2}/.test(num) || num < MIN || num > MAX) {
  74. num = 1;
  75. }
  76. $(this).val(num);
  77. })
  78. },
  79. methods: {
  80. // 获取图片列表
  81. getFileList: function () {
  82. var _this = this;
  83. $.post('/weixin/print/getFileList', {}, function (res) {
  84. if (res.code == 'success') {
  85. _this.picList = res.data
  86. } else if (res.code == 'login') {
  87. login(res.data.url);
  88. }
  89. }, "json");
  90. },
  91. // 获取用户信息
  92. getMemberInfo: function () {
  93. var _this = this;
  94. $.post('/weixin/member/getMemberInfo', {}, function (res) {
  95. if (res.code == 'success') {
  96. _this.memberInfo = res.data
  97. } else if (res.code == 'login') {
  98. login(res.data.url);
  99. }
  100. }, "json");
  101. },
  102. // 提交
  103. doSubmit: function () {
  104. var _this = this;
  105. var count = 0;
  106. var formData = new FormData();
  107. if (_this.submitting) {
  108. return false;
  109. }
  110. if (_this.picList.length <= 0) {
  111. $.toast('请先选择图片', 'text');
  112. return false;
  113. }
  114. console.log('提交处理');
  115. var total = $(".print-pic").length;
  116. // $each(_this.picList,function (k, item) {
  117. $(".print-pic").each(function (k, item) {
  118. var _img = $(this).find('.pic-img');
  119. var name = $.trim(_img.attr('alt'));
  120. var num = parseInt($(this).find('.pic-num').val());
  121. var colorType = $(this).find('.color').attr('data-color');
  122. var $previewImg = document.getElementById("pic_preview_" + k);
  123. var picId = $(this).attr('data-id');
  124. var fileData = typeof(_this.picList[k].fileData) != 'undefined' ? _this.picList[k].fileData : '';
  125. // var canvas = document.createElement("canvas");
  126. var scale = 12;
  127. var opts = {
  128. scale: scale, // 添加的scale 参数
  129. width: 119,
  130. height: 169,
  131. background: '#fff',
  132. };
  133. // html2canvas($previewImg, opts).then(function (canvas) {
  134. // const context = canvas.getContext('2d');
  135. // context.translate(0.5, 0.5);
  136. // context.mozImageSmoothingEnabled = false;
  137. // context.webkitImageSmoothingEnabled = false;
  138. // context.msImageSmoothingEnabled = false;
  139. // context.imageSmoothingEnabled = false;
  140. //
  141. // var src = canvas.toDataURL('image/jpeg');
  142. // var file = _this.dataURLtoFile(src, name ? name : '图片打印.jpeg');
  143. formData.append('type', 1);
  144. formData.append('taskKey', _this.taskKey);
  145. if (fileData) {
  146. formData.append('image[]', fileData);
  147. formData.append('nums[]', num);
  148. formData.append('ids[]', picId);
  149. formData.append('colors[]', colorType);
  150. }
  151. count++;
  152. });
  153. if (count <= 0) {
  154. $.toast('请选择打印的图片');
  155. return false;
  156. }
  157. // 上传
  158. $.showLoading('文件上传处理中...');
  159. _this.submitting = true;
  160. var ajax = $.ajax({
  161. url: '/weixin/upload/image',
  162. data: formData,
  163. type: "post",
  164. dataType: 'json',
  165. cache: false,
  166. contentType: false,
  167. processData: false,
  168. timeout: 30000,
  169. success: function (res) {
  170. $.hideLoading();
  171. _this.submitting = false;
  172. if (res.code == 'success') {
  173. _this.showModal();
  174. } else if (res.code == 'login') {
  175. login(res.data.url);
  176. } else {
  177. $.toast(res.message);
  178. }
  179. },
  180. error: function (res) {
  181. _this.submitting = false;
  182. $.toast('服务器错误');
  183. },
  184. complete: function (XMLHttpRequest, status) {
  185. if (status == 'timeout') {
  186. ajax.abort();
  187. $.toast('请求超时,请重新提交');
  188. location.reload();
  189. }
  190. }
  191. })
  192. // });
  193. },
  194. // 显示填写窗口
  195. showModal: function () {
  196. var _this = this;
  197. $.closeModal();
  198. $.modal({
  199. title: "",
  200. autoClose: false,
  201. text: "<div class='scan'>填写/扫码打印设备</div><div class='tips'>设备号<input type='text' id='device_code' placeholder='请填写打印机设备号'></div>",
  202. buttons: [
  203. {
  204. text: "取消",
  205. className: "default",
  206. onClick: function () {
  207. $.closeModal();
  208. return false;
  209. }
  210. },
  211. {
  212. text: "扫码打印",
  213. className: "warning",
  214. onClick: function () {
  215. _this.scan();
  216. }
  217. },
  218. {
  219. text: "填写打印",
  220. className: "success",
  221. onClick: function () {
  222. _this.inputCode();
  223. return false;
  224. }
  225. },
  226. ]
  227. });
  228. },
  229. // 输入设备号打印
  230. inputCode: function () {
  231. var _this = this;
  232. var deviceCode = $.trim($("#device_code").val());
  233. if (deviceCode == '' || deviceCode === null) {
  234. $.toast('请填写设备号');
  235. setTimeout(function () {
  236. _this.showModal();
  237. }, 500)
  238. return false;
  239. }
  240. $.post('/weixin/print/checkDevice', {deviceCode: deviceCode}, function (res) {
  241. if (res.code == 'success') {
  242. var deviceId = res.data.id;
  243. $.closeModal();
  244. $.modal({
  245. title: "",
  246. text: "<div class='scan'>设备验证成功</div><div class='info'></div><div class='tips'>点击下方确认下单</div>",
  247. buttons: [
  248. {
  249. text: "取消",
  250. className: "default",
  251. onClick: function () {
  252. $.closeModal();
  253. setTimeout(function () {
  254. _this.showModal();
  255. }, 500)
  256. return false;
  257. }
  258. },
  259. {
  260. text: "确认下单",
  261. className: "warning",
  262. onClick: function () {
  263. var params = {
  264. clusterId: _this.id,
  265. deviceId: deviceId,
  266. printType: _this.printType,
  267. colorType: _this.colorType,
  268. taskKey: _this.taskKey,
  269. type: 1
  270. };
  271. $.showLoading('订单处理中...');
  272. $.post('/weixin/order/confirm', params, function (res) {
  273. $.hideLoading();
  274. if (res.code == 'success') {
  275. var msg = _this.id > 0 && _this.printType == 2 ? '参与拼团成功' : (_this.printType == 2 ? '发起拼团成功' : '下单成功');
  276. $.showLoading(msg + ',即将前往支付...');
  277. setTimeout(function () {
  278. location.href = '/weixin/order/pay?id=' + res.data;
  279. }, 800);
  280. } else if (res.code == 'login') {
  281. login(res.data.url);
  282. } else {
  283. $.closeModal();
  284. $.modal({
  285. title: "",
  286. text: "<div class='scan'>下单失败</div><div class='tips'>" + res.message + "</div>",
  287. buttons: [
  288. {
  289. text: "取消",
  290. onClick: function () {
  291. return false;
  292. }
  293. }
  294. ]
  295. });
  296. }
  297. }, "json")
  298. }
  299. },
  300. ]
  301. });
  302. } else {
  303. $.toast(res.message);
  304. setTimeout(function () {
  305. _this.showModal();
  306. }, 500)
  307. }
  308. }, 'json');
  309. },
  310. // 扫码设备
  311. scan: function () {
  312. var _this = this;
  313. $.post('/weixin/index/getJssdkParams', {url: location.href}, function (res) {
  314. var params = res.data;
  315. // 微信JSSDK
  316. wx.config({
  317. debug: false, // 是否调试模式
  318. appId: params.appId, // 必填,公众号的唯一标识
  319. timestamp: params.timestamp, // 必填,生成签名的时间戳
  320. nonceStr: params.nonceStr, // 必填,生成签名的随机串
  321. signature: params.signature,// 必填,签名
  322. jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'scanQRCode'] // 必填,需要使用的JS接口列表
  323. });
  324. // 初始化处理
  325. wx.ready(function () {
  326. // 微信扫一扫
  327. $.closeModal();
  328. wx.scanQRCode({
  329. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  330. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  331. success: function (res) {
  332. var deviceId = res.resultStr;
  333. _this.deviceId = deviceId;
  334. if (deviceId) {
  335. $.closeModal();
  336. $.modal({
  337. title: "",
  338. text: "<div class='scan'>扫码成功</div><div class='info'></div><div class='tips'>点击下方确认下单</div>",
  339. buttons: [
  340. {
  341. text: "取消",
  342. className: "default",
  343. onClick: function () {
  344. $.closeModal();
  345. setTimeout(function () {
  346. _this.showModal();
  347. }, 500)
  348. return false;
  349. }
  350. },
  351. {
  352. text: "确认下单",
  353. className: "warning",
  354. onClick: function () {
  355. var params = {
  356. clusterId: _this.id,
  357. deviceId: deviceId,
  358. printType: _this.printType,
  359. colorType: _this.colorType,
  360. taskKey: _this.taskKey,
  361. type: 1
  362. };
  363. $.showLoading('订单处理中...');
  364. $.post('/weixin/order/confirm', params, function (res) {
  365. $.hideLoading();
  366. if (res.code == 'success') {
  367. var msg = _this.id > 0 && _this.printType == 2 ? '参与拼团成功' : (_this.printType == 2 ? '发起拼团成功' : '下单成功');
  368. $.showLoading(msg + ',即将前往支付...');
  369. setTimeout(function () {
  370. location.href = '/weixin/order/pay?id=' + res.data;
  371. }, 800);
  372. } else if (res.code == 'login') {
  373. login(res.data.url);
  374. } else {
  375. $.closeModal();
  376. $.modal({
  377. title: "",
  378. text: "<div class='scan'>下单失败</div><div class='tips'>" + res.message + "</div>",
  379. buttons: [
  380. {
  381. text: "取消",
  382. onClick: function () {
  383. return false;
  384. }
  385. },
  386. {
  387. text: "重新扫码下单",
  388. className: "warning",
  389. onClick: function () {
  390. _this.scan();
  391. }
  392. },
  393. ]
  394. });
  395. }
  396. }, "json")
  397. }
  398. },
  399. ]
  400. });
  401. } else {
  402. $.toast('扫码信息错误');
  403. }
  404. }
  405. });
  406. });
  407. }, "JSON");
  408. },
  409. // base转文件
  410. dataURLtoFile: function (dataurl, filename) {//将base64转换为文件
  411. var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
  412. bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  413. while (n--) {
  414. u8arr[n] = bstr.charCodeAt(n);
  415. }
  416. return new File([u8arr], filename, {type: mime});
  417. },
  418. // base转文件
  419. dataBlobtoFile: function (dataurl, filename) {//将base64转换为文件
  420. return new File([dataurl], filename, {type: 'image/jpeg'});
  421. },
  422. //
  423. dataURLtoBlob: function(dataurl) {
  424. var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
  425. bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  426. while (n--) {
  427. u8arr[n] = bstr.charCodeAt(n);
  428. }
  429. return new Blob([u8arr], {type: mime});
  430. },
  431. // 删除图片
  432. picDel: function (id) {
  433. var fileId = $("#pic_" + id).attr('data-id');
  434. $("#pic_" + id).detach();
  435. this.picList[id] = {};
  436. if (fileId) {
  437. $.post("/weixin/print/fileDel", {id: fileId})
  438. }
  439. },
  440. // 彩印
  441. picColor: function (k, type) {
  442. if (type == 1) {
  443. this.colorType = 1;
  444. $(".print-pic .op .color1").addClass('active');
  445. $(".print-pic .op .color2").removeClass('active');
  446. } else {
  447. this.colorType = 2;
  448. $(".print-pic .op .color1").removeClass('active');
  449. $(".print-pic .op .color2").addClass('active');
  450. }
  451. },
  452. // 编辑图片
  453. picEdit: function (id, data, type) {
  454. var _this = this;
  455. $.closePopup();
  456. _this.cropParams = data;
  457. _this.cropParams.id = id;
  458. _this.cropParams.type = type;
  459. _this.cropParams.resize = 0;
  460. _this.cropParams.rotate = 0;
  461. if (_this.cropParams.url == '') {
  462. $.toast('请重新添加图片');
  463. return false;
  464. }
  465. $("body").css('overflowY','hidden');
  466. // $("#cropImg").remove();
  467. $(".crop-img").html('<img id="cropImg" src="" alt="" off="">');
  468. if(id>=0){
  469. $("#cropImg").attr('src', _this.cropParams.old_url);
  470. }else{
  471. $("#cropImg").attr('src', _this.cropParams.url);
  472. }
  473. var iWidth = $("body").outerWidth();
  474. var iHeight = $("body").height()-130;
  475. var cropWidth = $('body').width();
  476. $image = $("#cropImg");
  477. $("#cropBox").popup();
  478. _this.options = {
  479. // aspectRatio: iWidth/iHeight,
  480. center: true,
  481. movable: true,
  482. zoomable: true,
  483. zoomOnTouch: true,
  484. zoomOnWheel: false,
  485. scalable: true,
  486. wheelZoomRatio: 0.1,
  487. responsive: true,
  488. guides: false,
  489. strict: false,
  490. width: iWidth,
  491. dragMode: 'move',
  492. height: iHeight,
  493. autoCropArea: 1,
  494. // cropBoxMovable: false,
  495. // cropBoxResizable: false,
  496. minContainerWidth: iWidth,
  497. minContainerHeight: iHeight,
  498. minCropBoxWidth: 80,
  499. maxCropBoxWidth: iWidth,
  500. minCropBoxHeight: 80,
  501. canvasWeight: cropWidth,
  502. canvasHeight: iHeight,
  503. // minCanvasWeight: cropWidth*0.4,
  504. maxCanvasWeight: cropWidth,
  505. // minCanvasHeight: iHeight,
  506. viewMode: 1,
  507. crop: function (data) {
  508. _this.cropParams.data = data;
  509. var rate = 1;
  510. if(data.height >2000 || data.width>2000){
  511. rate = 0.8;
  512. }else if( data.height < 1000 || data.width<1000){
  513. rate = 1.5;
  514. }
  515. _this.cropParams.cropData = {
  516. // width: iWidth * rate,
  517. width: data.width * rate,
  518. // height: iHeight * rate,
  519. height: data.height * rate,
  520. rotate: data.rotate,
  521. imageSmoothingQuality: 'high',
  522. fillColor: '#fff',
  523. };
  524. }
  525. }
  526. if (_this.jc != null) {
  527. $image.cropper(_this.options);
  528. _this.jc = $image.cropper('replace', _this.cropParams.old_url);
  529. $image.cropper('setData', {
  530. width: iWidth,
  531. height: iHeight
  532. });
  533. } else {
  534. $image.cropper(_this.options);
  535. _this.jc = $image.cropper('replace', _this.cropParams.old_url);
  536. $image.cropper('setData', {
  537. width: iWidth,
  538. height: iHeight
  539. });
  540. }
  541. _this.cropRotate();
  542. },
  543. // 预览
  544. picPreview: function(id, data){
  545. this.previewData = data;
  546. $.closePopup();
  547. $("#previewBox").popup();
  548. },
  549. // 取消剪切
  550. previewCancel: function () {
  551. $.closePopup();
  552. this.previewData = {};
  553. },
  554. // 图片大小
  555. resetSizeImg: function (image, FitWidth, FitHeight) {
  556. var newImg = image;
  557. if (image.width > 0 && image.height > 0) {
  558. if (image.width / image.height >= FitWidth / FitHeight) {
  559. if (image.width > FitWidth) {
  560. image.width = FitWidth;
  561. newImg.height = (image.height * FitWidth) / image.width;
  562. } else {
  563. newImg.width = image.width;
  564. newImg.height = image.height;
  565. }
  566. } else {
  567. if (image.height > FitHeight) {
  568. newImg.height = FitHeight;
  569. newImg.width = (image.width * FitHeight) / image.height;
  570. } else {
  571. newImg.width = image.width;
  572. newImg.height = image.height;
  573. }
  574. }
  575. }
  576. return newImg;
  577. },
  578. // 取消剪切
  579. cropCancel: function () {
  580. $.closePopup();
  581. $("#previewBox").hide();
  582. if (this.jc != false) {
  583. $("body").css('overflowY','scroll');
  584. $("#cropImg").cropper('destroy');
  585. this.jc = null;
  586. this.cropParams.resize = 0;
  587. $("#cropImg").remove();
  588. }
  589. },
  590. // 缩放
  591. cropZoom: function (ele, type) {
  592. $("#cropImg").cropper("zoom", type == 1 ? 0.1 : -0.1);
  593. },
  594. // 旋转
  595. rotate: function (ele, type) {
  596. if(type == 1){
  597. $("#cropImg").cropper("rotate", -90);
  598. }else{
  599. $("#cropImg").cropper("rotate", 90);
  600. }
  601. this.cropParams.resize = 0;
  602. var data = $("#cropImg").cropper('getContainerData');
  603. var cdata = $("#cropImg").cropper('getCanvasData');
  604. $("#cropImg").cropper('setCropBoxData', {
  605. width: data.width,
  606. height: data.height,
  607. // top: data.height-cdata.height>0? (data.height-cdata.height)/2 : 0,
  608. // left: data.width-cdata.width>0? (data.width-cdata.width)/2 : 0
  609. });
  610. },
  611. // 适应
  612. cropSize: function(){
  613. var _this = this;
  614. var data = _this.cropParams.data;
  615. if(_this.cropParams.resize>=2){
  616. return false;
  617. }
  618. if(data.height<data.width){
  619. $("#cropImg").cropper("rotate", 90);
  620. _this.cropParams.cropData.rotate = 90;
  621. }
  622. var cwidth = $(".cropper-canvas").outerWidth();
  623. var size = data.height<data.width? data.height: data.width;
  624. var iWidth = $("body").outerWidth();
  625. var iHeight = $("body").height()-130;
  626. if(size<iWidth){
  627. var rate = iWidth/size;
  628. $("#cropImg").cropper("zoom", rate);
  629. }else if(cwidth<iWidth){
  630. var rate = iWidth/cwidth;
  631. $("#cropImg").cropper("zoom", rate);
  632. }
  633. _this.cropParams.resize++;
  634. var data = $("#cropImg").cropper('getContainerData');
  635. var cdata = $("#cropImg").cropper('getCanvasData');
  636. $("#cropImg").cropper('setCropBoxData', {
  637. width: data.width,
  638. height: data.height,
  639. // top: data.height-cdata.height>0? (data.height-cdata.height)/2 : 0,
  640. // left: data.width-cdata.width>0? (data.width-cdata.width)/2 : 0
  641. });
  642. },
  643. // 自适应旋转
  644. cropRotate: function(){
  645. var _this = this;
  646. var image = new Image();
  647. image.src = _this.cropParams.url;
  648. var height = $("#cropImg").outerWidth();
  649. var width = $("#cropImg").outerHeight();
  650. if(height<width){
  651. $("#cropImg").cropper("rotate", 90);
  652. _this.cropParams.crotate = 90;
  653. }
  654. _this.cropParams.rotate = 1;
  655. var data = $("#cropImg").cropper('getContainerData');
  656. var cdata = $("#cropImg").cropper('getCanvasData');
  657. $("#cropImg").cropper('setCropBoxData', {
  658. width: data.width,
  659. height: data.height,
  660. // top: data.height-cdata.height>0? (data.height-cdata.height)/2 : 0,
  661. // left: data.width-cdata.width>0? (data.width-cdata.width)/2 : 0
  662. });
  663. },
  664. // 确定剪切
  665. cropConfirm: function () {
  666. var _this = this;
  667. $("body").css('overflowY','scroll');
  668. $(".crop-img").show();
  669. this.cropPreviewStatus = false;
  670. if (this.jc != null) {
  671. $.showLoading('图片处理中...');
  672. var id = _this.cropParams.id;
  673. $image = $("#cropImg");
  674. var canvas = $image.cropper('getCroppedCanvas',_this.cropParams.cropData);
  675. var blob = canvas.toBlob(function (blob) {
  676. //生成Blob的图片格式
  677. if (blob != null) {
  678. var timestamp = Date.parse(new Date());
  679. blob.name = timestamp + ".jpg";
  680. url = URL.createObjectURL(blob);
  681. if(_this.cropParams.type && id>=0){
  682. _this.picList[id] = _this.cropParams;
  683. _this.picList[id].url = url;
  684. _this.picList[id].preview = url;
  685. _this.picList[id].fileData = _this.dataBlobtoFile(blob, blob.name);
  686. }else{
  687. var data = _this.cropParams;
  688. data.url = url;
  689. data.preview = url;
  690. data.fileData = _this.dataBlobtoFile(blob, blob.name);
  691. _this.picList.push(data);
  692. }
  693. setTimeout(function () {
  694. $.closePopup();
  695. $.hideLoading();
  696. _this.cropParams = {};
  697. _this.jc = null;
  698. $("#cropImg").cropper('destroy');
  699. $("#cropImg").remove();
  700. }, 200);
  701. }else{
  702. $.closePopup();
  703. $.hideLoading();
  704. $.showLoading('图片处理失败,请重新上传');
  705. _this.cropParams = {};
  706. setTimeout(function(){
  707. $.hideLoading();
  708. }, 1000);
  709. }
  710. }, "image/jpeg");
  711. }
  712. },
  713. // 重新剪切
  714. cropReset: function () {
  715. $image = $("#cropImg");
  716. $image.cropper(this.options);
  717. this.jc = $image.cropper('replace', this.cropParams.old_url);
  718. this.cropParams.resize = false;
  719. },
  720. // 旋转图片处理
  721. rotateImg: function(img, direction,canvas) {
  722. //alert(img);
  723. //最小与最大旋转方向,图片旋转4次后回到原方向
  724. var min_step = 0;
  725. var max_step = 3;
  726. //var img = document.getElementById(pid);
  727. if (img == null)return;
  728. //img的高度和宽度不能在img元素隐藏后获取,否则会出错
  729. var height = img.height;
  730. var width = img.width;
  731. //var step = img.getAttribute('step');
  732. var step = 2;
  733. if (step == null) {
  734. step = min_step;
  735. }
  736. if (direction == 'right') {
  737. step++;
  738. //旋转到原位置,即超过最大值
  739. step > max_step && (step = min_step);
  740. } else {
  741. step--;
  742. step < min_step && (step = max_step);
  743. }
  744. //img.setAttribute('step', step);
  745. /*var canvas = document.getElementById('pic_' + pid);
  746. if (canvas == null) {
  747. img.style.display = 'none';
  748. canvas = document.createElement('canvas');
  749. canvas.setAttribute('id', 'pic_' + pid);
  750. img.parentNode.appendChild(canvas);
  751. } */
  752. //旋转角度以弧度值为参数
  753. var degree = step * 90 * Math.PI / 180;
  754. var ctx = canvas.getContext('2d');
  755. switch (step) {
  756. case 0:
  757. canvas.width = width;
  758. canvas.height = height;
  759. ctx.drawImage(img, 0, 0);
  760. break;
  761. case 1:
  762. canvas.width = height;
  763. canvas.height = width;
  764. ctx.rotate(degree);
  765. ctx.drawImage(img, 0, -height);
  766. break;
  767. case 2:
  768. canvas.width = width;
  769. canvas.height = height;
  770. ctx.rotate(degree);
  771. ctx.drawImage(img, -width, -height);
  772. break;
  773. case 3:
  774. canvas.width = height;
  775. canvas.height = width;
  776. ctx.rotate(degree);
  777. ctx.drawImage(img, -width, 0);
  778. break;
  779. }
  780. },
  781. // 选择图片
  782. selectPic: function (ele) {
  783. var _this = this;
  784. var files = ele.target.files;
  785. var id = _this.picList.length;
  786. if (files.length > 0) {
  787. $.each(files, function (k, file) {
  788. if (file) {
  789. var Orientation = null;
  790. //获取照片方向角属性,用户旋转控制
  791. EXIF.getData(file, function() {
  792. // alert(EXIF.pretty(this));
  793. EXIF.getAllTags(this);
  794. //alert(EXIF.getTag(this, 'Orientation'));
  795. Orientation = EXIF.getTag(this, 'Orientation');
  796. //return;
  797. });
  798. var reader = new FileReader();
  799. reader.readAsDataURL(file);
  800. var blobUrl = URL.createObjectURL(file);
  801. reader.onloadend = function (even) {
  802. var image = new Image();
  803. image.src = even.currentTarget.result;
  804. image.onload = function(){
  805. var expectWidth = this.naturalWidth;
  806. var expectHeight = this.naturalHeight;
  807. if (this.naturalWidth > this.naturalHeight && this.naturalWidth > 800) {
  808. expectWidth = 800;
  809. expectHeight = expectWidth * this.naturalHeight / this.naturalWidth;
  810. } else if (this.naturalHeight > this.naturalWidth && this.naturalHeight > 1200) {
  811. expectHeight = 1200;
  812. expectWidth = expectHeight * this.naturalWidth / this.naturalHeight;
  813. }
  814. var canvas = document.createElement("canvas");
  815. var ctx = canvas.getContext("2d");
  816. canvas.width = expectWidth;
  817. canvas.height = expectHeight;
  818. ctx.drawImage(this, 0, 0, expectWidth, expectHeight);
  819. var base64 = null;
  820. //修复ios
  821. if (navigator.userAgent.match(/iphone/i)) {
  822. //alert(expectWidth + ',' + expectHeight);
  823. //如果方向角不为1,都需要进行旋转 added by lzk
  824. if(Orientation != "" && Orientation != 1){
  825. //alert('旋转处理');
  826. switch(Orientation){
  827. case 6://需要顺时针(向左)90度旋转
  828. //alert('需要顺时针(向左)90度旋转');
  829. _this.rotateImg(this,'left',canvas);
  830. break;
  831. case 8://需要逆时针(向右)90度旋转
  832. //alert('需要顺时针(向右)90度旋转');
  833. _this.rotateImg(this,'right',canvas);
  834. break;
  835. case 3://需要180度旋转
  836. //alert('需要180度旋转');
  837. _this.rotateImg(this,'right',canvas);//转两次
  838. _this.rotateImg(this,'right',canvas);
  839. break;
  840. }
  841. }
  842. base64 = canvas.toDataURL("image/jpeg", 0.8);
  843. }else{
  844. //alert(Orientation);
  845. if(Orientation != "" && Orientation != 1){
  846. //alert('旋转处理');
  847. switch(Orientation){
  848. case 6://需要顺时针(向左)90度旋转
  849. //alert('需要顺时针(向左)90度旋转');
  850. _this.rotateImg(this,'left',canvas);
  851. break;
  852. case 8://需要逆时针(向右)90度旋转
  853. //alert('需要顺时针(向右)90度旋转');
  854. _this.rotateImg(this,'right',canvas);
  855. break;
  856. case 3://需要180度旋转
  857. //alert('需要180度旋转');
  858. _this.rotateImg(this,'right',canvas);//转两次
  859. _this.rotateImg(this,'right',canvas);
  860. break;
  861. }
  862. }
  863. base64 = canvas.toDataURL("image/jpeg", 0.8);
  864. }
  865. //uploadImage(base64);
  866. //$("#myImage").attr("src", base64);
  867. var img = {
  868. id: 0,
  869. url: base64,
  870. old_url: base64,
  871. // url: base64,
  872. preview: base64,
  873. name: file.name,
  874. blobUrl: blobUrl,
  875. file_size: file.size,
  876. file_type: file.type
  877. };
  878. $("#upload").val("");
  879. // _this.picList.push(img);
  880. // console.log(_this.picList)
  881. _this.picEdit(0, img, 0);
  882. }
  883. }
  884. }
  885. })
  886. /*if (count > 0) {
  887. _this.getFileList(0);
  888. }*/
  889. }
  890. }
  891. }
  892. });