photo.js 26 KB

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