photo.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. picList: [],
  22. // 预览状态
  23. cropPreviewStatus: false,
  24. // 上传状态
  25. submitting: false,
  26. // 打印方式
  27. printType: 1,
  28. colorType: 1,
  29. image: null,
  30. // 设备ID
  31. deviceId: 0,
  32. taskKey: '',
  33. // 剪切对象
  34. jc: null,
  35. id: 0,
  36. },
  37. created: function () {
  38. //this.getFileList();
  39. var printType = getParam('pt');
  40. this.id = getParam('id');
  41. this.printType = printType ? printType : 1;
  42. this.taskKey = getKey();
  43. },
  44. updated: function () {
  45. // 计时器
  46. var _this = this;
  47. var MAX = 100, MIN = 1;
  48. $('.weui-count__decrease').unbind('click').on('click', function (e) {
  49. if (_this.deviceId) {
  50. return false;
  51. }
  52. var $input = $(e.target).parent().find('.weui-count__number');
  53. var number = parseInt($input.val() || "0") - 1
  54. if (number < MIN) number = MIN;
  55. $input.val(number)
  56. })
  57. $('.weui-count__increase').unbind('click').on('click', function (e) {
  58. if (_this.deviceId) {
  59. return false;
  60. }
  61. var $input = $(e.target).parent().find('.weui-count__number');
  62. var number = parseInt($input.val() || "0") + 1
  63. if (number > MAX) number = MAX;
  64. $input.val(number)
  65. });
  66. $(".weui-count__number").blur(function () {
  67. var num = $.trim($(this).val());
  68. if (!/[1-9][0-9]{0,2}/.test(num) || num < MIN || num > MAX) {
  69. num = 1;
  70. }
  71. $(this).val(num);
  72. })
  73. },
  74. methods: {
  75. // 获取图片列表
  76. getFileList: function () {
  77. var _this = this;
  78. $.post('/weixin/print/getFileList', {}, function (res) {
  79. if (res.code == 'success') {
  80. _this.picList = res.data
  81. } else if (res.code == 'login') {
  82. login(res.data.url);
  83. }
  84. }, "json");
  85. },
  86. // 获取用户信息
  87. getMemberInfo: function () {
  88. var _this = this;
  89. $.post('/weixin/member/getMemberInfo', {}, function (res) {
  90. if (res.code == 'success') {
  91. _this.memberInfo = res.data
  92. } else if (res.code == 'login') {
  93. login(res.data.url);
  94. }
  95. }, "json");
  96. },
  97. // 提交
  98. doSubmit: function () {
  99. var _this = this;
  100. var count = 0;
  101. var formData = new FormData();
  102. if (_this.submitting) {
  103. return false;
  104. }
  105. if (_this.picList.length <= 0) {
  106. $.toast('请先选择图片', 'text');
  107. return false;
  108. }
  109. console.log('提交处理');
  110. var total = $(".print-pic").length;
  111. // $each(_this.picList,function (k, item) {
  112. $(".print-pic").each(function (k, item) {
  113. var _img = $(this).find('.pic-img');
  114. var name = $.trim(_img.attr('alt'));
  115. var num = parseInt($(this).find('.pic-num').val());
  116. var colorType = $(this).find('.color').attr('data-color');
  117. var $previewImg = document.getElementById("pic_preview_" + k);
  118. var picId = $(this).attr('data-id');
  119. var fileData = typeof(_this.picList[k]) != 'undefined' ? _this.picList[k] : '';
  120. // var canvas = document.createElement("canvas");
  121. var scale = 12;
  122. var opts = {
  123. scale: scale, // 添加的scale 参数
  124. width: 119,
  125. height: 169,
  126. background: '#fff',
  127. };
  128. // html2canvas($previewImg, opts).then(function (canvas) {
  129. // const context = canvas.getContext('2d');
  130. // context.translate(0.5, 0.5);
  131. // context.mozImageSmoothingEnabled = false;
  132. // context.webkitImageSmoothingEnabled = false;
  133. // context.msImageSmoothingEnabled = false;
  134. // context.imageSmoothingEnabled = false;
  135. //
  136. // var src = canvas.toDataURL('image/jpeg');
  137. // var file = _this.dataURLtoFile(src, name ? name : '图片打印.jpeg');
  138. formData.append('type', 1);
  139. formData.append('taskKey', _this.taskKey);
  140. if (fileData) {
  141. formData.append('image[]', fileData);
  142. formData.append('nums[]', num);
  143. formData.append('ids[]', id);
  144. formData.append('colors[]', colorType);
  145. }
  146. count++;
  147. });
  148. if (count <= 0) {
  149. $.toast('请选择打印的图片');
  150. return false;
  151. }
  152. // 上传
  153. $.showLoading('文件上传处理中...');
  154. _this.submitting = true;
  155. var ajax = $.ajax({
  156. url: '/weixin/upload/image',
  157. data: formData,
  158. type: "post",
  159. dataType: 'json',
  160. cache: false,
  161. contentType: false,
  162. processData: false,
  163. timeout: 30000,
  164. success: function (res) {
  165. $.hideLoading();
  166. _this.submitting = false;
  167. if (res.code == 'success') {
  168. _this.showModal();
  169. } else if (res.code == 'login') {
  170. login(res.data.url);
  171. } else {
  172. $.toast(res.message);
  173. }
  174. },
  175. error: function (res) {
  176. _this.submitting = false;
  177. $.toast('服务器错误');
  178. },
  179. complete: function (XMLHttpRequest, status) {
  180. if (status == 'timeout') {
  181. ajax.abort();
  182. $.toast('请求超时,请重新提交');
  183. location.reload();
  184. }
  185. }
  186. })
  187. // });
  188. },
  189. // 显示填写窗口
  190. showModal: function () {
  191. var _this = this;
  192. $.closeModal();
  193. $.modal({
  194. title: "",
  195. autoClose: false,
  196. text: "<div class='scan'>填写/扫码打印设备</div><div class='tips'>设备号<input type='text' id='device_code' placeholder='请填写打印机设备号'></div>",
  197. buttons: [
  198. {
  199. text: "取消",
  200. className: "default",
  201. onClick: function () {
  202. $.closeModal();
  203. return false;
  204. }
  205. },
  206. {
  207. text: "扫码打印",
  208. className: "warning",
  209. onClick: function () {
  210. _this.scan();
  211. }
  212. },
  213. {
  214. text: "填写打印",
  215. className: "success",
  216. onClick: function () {
  217. _this.inputCode();
  218. return false;
  219. }
  220. },
  221. ]
  222. });
  223. },
  224. // 输入设备号打印
  225. inputCode: function () {
  226. var _this = this;
  227. var deviceCode = $.trim($("#device_code").val());
  228. if (deviceCode == '' || deviceCode === null) {
  229. $.toast('请填写设备号');
  230. setTimeout(function () {
  231. _this.showModal();
  232. }, 500)
  233. return false;
  234. }
  235. $.post('/weixin/print/checkDevice', {deviceCode: deviceCode}, function (res) {
  236. if (res.code == 'success') {
  237. var deviceId = res.data.id;
  238. $.closeModal();
  239. $.modal({
  240. title: "",
  241. text: "<div class='scan'>设备验证成功</div><div class='info'></div><div class='tips'>点击下方确认下单</div>",
  242. buttons: [
  243. {
  244. text: "取消",
  245. className: "default",
  246. onClick: function () {
  247. $.closeModal();
  248. setTimeout(function () {
  249. _this.showModal();
  250. }, 500)
  251. return false;
  252. }
  253. },
  254. {
  255. text: "确认下单",
  256. className: "warning",
  257. onClick: function () {
  258. var params = {
  259. clusterId: _this.id,
  260. deviceId: deviceId,
  261. printType: _this.printType,
  262. colorType: _this.colorType,
  263. taskKey: _this.taskKey,
  264. type: 1
  265. };
  266. $.showLoading('订单处理中...');
  267. $.post('/weixin/order/confirm', params, function (res) {
  268. $.hideLoading();
  269. if (res.code == 'success') {
  270. var msg = _this.id > 0 && _this.printType == 2 ? '参与拼团成功' : (_this.printType == 2 ? '发起拼团成功' : '下单成功');
  271. $.showLoading(msg + ',即将前往支付...');
  272. setTimeout(function () {
  273. location.href = '/weixin/order/pay?id=' + res.data;
  274. }, 800);
  275. } else if (res.code == 'login') {
  276. login(res.data.url);
  277. } else {
  278. $.closeModal();
  279. $.modal({
  280. title: "",
  281. text: "<div class='scan'>下单失败</div><div class='tips'>" + res.message + "</div>",
  282. buttons: [
  283. {
  284. text: "取消",
  285. onClick: function () {
  286. return false;
  287. }
  288. }
  289. ]
  290. });
  291. }
  292. }, "json")
  293. }
  294. },
  295. ]
  296. });
  297. } else {
  298. $.toast(res.message);
  299. setTimeout(function () {
  300. _this.showModal();
  301. }, 500)
  302. }
  303. }, 'json');
  304. },
  305. // 扫码设备
  306. scan: function () {
  307. var _this = this;
  308. $.post('/weixin/index/getJssdkParams', {url: location.href}, function (res) {
  309. var params = res.data;
  310. // 微信JSSDK
  311. wx.config({
  312. debug: false, // 是否调试模式
  313. appId: params.appId, // 必填,公众号的唯一标识
  314. timestamp: params.timestamp, // 必填,生成签名的时间戳
  315. nonceStr: params.nonceStr, // 必填,生成签名的随机串
  316. signature: params.signature,// 必填,签名
  317. jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'scanQRCode'] // 必填,需要使用的JS接口列表
  318. });
  319. // 初始化处理
  320. wx.ready(function () {
  321. // 微信扫一扫
  322. $.closeModal();
  323. wx.scanQRCode({
  324. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  325. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  326. success: function (res) {
  327. var deviceId = res.resultStr;
  328. _this.deviceId = deviceId;
  329. if (deviceId) {
  330. $.closeModal();
  331. $.modal({
  332. title: "",
  333. text: "<div class='scan'>扫码成功</div><div class='info'></div><div class='tips'>点击下方确认下单</div>",
  334. buttons: [
  335. {
  336. text: "取消",
  337. className: "default",
  338. onClick: function () {
  339. $.closeModal();
  340. setTimeout(function () {
  341. _this.showModal();
  342. }, 500)
  343. return false;
  344. }
  345. },
  346. {
  347. text: "确认下单",
  348. className: "warning",
  349. onClick: function () {
  350. var params = {
  351. clusterId: _this.id,
  352. deviceId: deviceId,
  353. printType: _this.printType,
  354. colorType: _this.colorType,
  355. taskKey: _this.taskKey,
  356. type: 1
  357. };
  358. $.showLoading('订单处理中...');
  359. $.post('/weixin/order/confirm', params, function (res) {
  360. $.hideLoading();
  361. if (res.code == 'success') {
  362. var msg = _this.id > 0 && _this.printType == 2 ? '参与拼团成功' : (_this.printType == 2 ? '发起拼团成功' : '下单成功');
  363. $.showLoading(msg + ',即将前往支付...');
  364. setTimeout(function () {
  365. location.href = '/weixin/order/pay?id=' + res.data;
  366. }, 800);
  367. } else if (res.code == 'login') {
  368. login(res.data.url);
  369. } else {
  370. $.closeModal();
  371. $.modal({
  372. title: "",
  373. text: "<div class='scan'>下单失败</div><div class='tips'>" + res.message + "</div>",
  374. buttons: [
  375. {
  376. text: "取消",
  377. onClick: function () {
  378. return false;
  379. }
  380. },
  381. {
  382. text: "重新扫码下单",
  383. className: "warning",
  384. onClick: function () {
  385. _this.scan();
  386. }
  387. },
  388. ]
  389. });
  390. }
  391. }, "json")
  392. }
  393. },
  394. ]
  395. });
  396. } else {
  397. $.toast('扫码信息错误');
  398. }
  399. }
  400. });
  401. });
  402. }, "JSON");
  403. },
  404. // base转文件
  405. dataURLtoFile: function (dataurl, filename) {//将base64转换为文件
  406. var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
  407. bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  408. while (n--) {
  409. u8arr[n] = bstr.charCodeAt(n);
  410. }
  411. return new File([u8arr], filename, {type: mime});
  412. },
  413. // base转文件
  414. dataBlobtoFile: function (dataurl, filename) {//将base64转换为文件
  415. return new File([u8arr], filename, {type: 'image/jpeg'});
  416. },
  417. // 删除图片
  418. picDel: function (id) {
  419. var fileId = $("#pic_" + id).attr('data-id');
  420. $("#pic_" + id).detach();
  421. this.picList[id] = {};
  422. if (fileId) {
  423. $.post("/weixin/print/fileDel", {id: fileId})
  424. }
  425. },
  426. // 彩印
  427. picColor: function (k) {
  428. if ($("#color_" + k).hasClass('active')) {
  429. this.colorType = 1;
  430. $(".print-pic .op .color").attr('data-color', 1);
  431. $(".print-pic .op .color").text('黑白');
  432. $(".print-pic .pic-preview img").addClass('gray');
  433. } else {
  434. this.colorType = 2;
  435. $(".print-pic .op .color").attr('data-color', 2);
  436. $(".print-pic .op .color").text('彩印');
  437. $(".print-pic .pic-preview img").removeClass('gray');
  438. }
  439. $(".print-pic .op .color").toggleClass('active');
  440. },
  441. // 编辑图片
  442. picEdit: function (id) {
  443. var _this = this;
  444. $.closePopup();
  445. var src = $("#pic_img_" + id).attr('data-pic');
  446. $(".crop-img").html('<img id="cropImg" src="" alt="" off="">');
  447. _this.cropParams.id = id;
  448. _this.cropParams.url = src;
  449. _this.cropParams.preview = src;
  450. if (_this.cropParams.url == '') {
  451. $.toast('请重新添加图片');
  452. return false;
  453. }
  454. _this.image = new Image();
  455. _this.image.src = src;
  456. var iWidth = _this.image.width;
  457. var iHeight = _this.height;
  458. var rate = iWidth / iHeight;
  459. $("#cropImg").attr('src', _this.cropParams.url);
  460. $image = $("#cropImg");
  461. var imgWidth = $('body').width() * 0.6;
  462. $("#cropBox").popup();
  463. var options = {
  464. aspectRatio: 0,
  465. center: true,
  466. movable: true,
  467. zoomable: true,
  468. zoomOnTouch: true,
  469. zoomOnWheel: false,
  470. scalable: true,
  471. // cropBoxMovable: false,
  472. wheelZoomRatio: 0.1,
  473. background: false,
  474. responsive: true,
  475. guides: false,
  476. strict: false,
  477. width: width,
  478. dragMode: 'move',
  479. height: height,
  480. autoCropArea: 0,
  481. cropBoxMovable: false,
  482. cropBoxResizable: false,
  483. minContainerWidth: iWidth,
  484. minContainerHeight: iHeight,
  485. minCropBoxWidth: iWidth,
  486. maxCropBoxWidth: iWidth,
  487. minCropBoxHeight: iHeight,
  488. maxCropBoxHeight: iHeight,
  489. minCanvasWeight: iWidth,
  490. maxCanvasWeight: iWidth,
  491. minCanvasHeight: iHeight,
  492. maxCanvasHeight: iHeight,
  493. // preview: '.preview-img',
  494. viewMode: 1,
  495. crop: function (data) {
  496. var cropData = {
  497. x: Math.round(data.x),
  498. y: Math.round(data.y),
  499. height: Math.round(data.height),
  500. width: Math.round(data.width),
  501. rotate: Math.round(data.rotate),
  502. };
  503. /* _this.picList[id].preview = canvas.toDataURL("image/jpeg", 1);
  504. _this.cropParams.preview = canvas.toDataURL("image/jpeg", 1);*/
  505. _this.cropParams.cropData = cropData;
  506. }
  507. }
  508. if (_this.jc != null) {
  509. $image.cropper(options);
  510. $image.cropper('replace', _this.cropParams.blobUrl);
  511. } else {
  512. $image.cropper(options);
  513. _this.jc = $image.cropper('replace', _this.cropParams.blobUrl);
  514. }
  515. },
  516. // 图片大小
  517. resetSizeImg: function (image, FitWidth, FitHeight) {
  518. var newImg = image;
  519. if (image.width > 0 && image.height > 0) {
  520. if (image.width / image.height >= FitWidth / FitHeight) {
  521. if (image.width > FitWidth) {
  522. image.width = FitWidth;
  523. newImg.height = (image.height * FitWidth) / image.width;
  524. } else {
  525. newImg.width = image.width;
  526. newImg.height = image.height;
  527. }
  528. } else {
  529. if (image.height > FitHeight) {
  530. newImg.height = FitHeight;
  531. newImg.width = (image.width * FitHeight) / image.height;
  532. } else {
  533. newImg.width = image.width;
  534. newImg.height = image.height;
  535. }
  536. }
  537. }
  538. return newImg;
  539. },
  540. // 取消剪切
  541. cropCancel: function () {
  542. $.closePopup();
  543. $("#previewBox").hide();
  544. if (this.jc != false) {
  545. $("#cropImg").cropper('destroy');
  546. this.jc = null;
  547. $("#cropImg").remove();
  548. }
  549. },
  550. // 预览剪切
  551. cropPreview: function () {
  552. this.cropPreviewStatus = true;
  553. $("#cropBox .crop-img").hide();
  554. // $("#cropImgPreview").attr('src', this.cropParams.cropSrc);
  555. $("#cropBox .crop-preview").show();
  556. },
  557. // 缩放
  558. cropZoom: function (ele, type) {
  559. $("#cropImg").cropper("zoom", type == 1 ? 0.1 : -0.1);
  560. },
  561. // 旋转
  562. rotateConfirm: function () {
  563. $("#cropImg").cropper("rotate", 45);
  564. },
  565. // 确定剪切
  566. cropConfirm: function () {
  567. var _this = this;
  568. $.closePopup();
  569. $("#previewBox").hide();
  570. $(".crop-img").show();
  571. $(".crop-preview").hide();
  572. this.cropPreviewStatus = false;
  573. if (this.jc != null) {
  574. $.showLoading('图片处理中...');
  575. var canvas = $image.cropper('getCroppedCanvas', {
  576. width: data.height * 15,
  577. height: data.width * 15,
  578. rotate: data.rotate
  579. });
  580. var url = canvas.toBlob(function (e) {
  581. console.log(e); //生成Blob的图片格式
  582. if (e != null) {
  583. var timestamp = Date.parse(new Date());
  584. e.name = timestamp + ".png";
  585. url = URL.createObjectURL(e);
  586. _this.picList[id].url = url;
  587. _this.picList[id].preview = url;
  588. _this.picList[id].fileData = _this.dataBlobtoFile(e, e.name);
  589. _this.cropParams.preview = url;
  590. setTimeout(function () {
  591. $.hideLoading();
  592. $("#cropImg").cropper('destroy');
  593. this.jc = null;
  594. $("#cropImg").remove();
  595. }, 200);
  596. }
  597. }, "image/jpeg");
  598. }
  599. },
  600. // 重新剪切
  601. cropReset: function () {
  602. this.cropPreviewStatus = false;
  603. $("#cropBox .crop-img").show();
  604. $("#cropBox .crop-preview").hide();
  605. },
  606. // 选择图片
  607. selectPic: function (ele) {
  608. var _this = this;
  609. var files = ele.target.files;
  610. if (files.length > 0) {
  611. var count = _this.picList.length;
  612. $.each(files, function (k, file) {
  613. if (file) {
  614. var reader = new FileReader();
  615. reader.readAsDataURL(file);
  616. var blobUrl = URL.createObjectURL(file);
  617. reader.onloadend = function (even) {
  618. var img = {
  619. id: 0,
  620. url: even.currentTarget.result,
  621. // url: even.currentTarget.result,
  622. preview: even.currentTarget.result,
  623. name: file.name,
  624. blobUrl: blobUrl,
  625. file_size: file.size,
  626. file_type: file.type
  627. };
  628. _this.picList.push(img);
  629. _this.picEdit(id);
  630. }
  631. }
  632. })
  633. if (count > 0) {
  634. _this.getFileList(0);
  635. }
  636. }
  637. }
  638. }
  639. });