photo_back.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. image: null,
  29. // 设备ID
  30. deviceId: 0,
  31. // 剪切对象
  32. jc: null,
  33. id: 0,
  34. },
  35. created: function () {
  36. this.getFileList();
  37. var printType = getParam('pt');
  38. this.id = getParam('id');
  39. this.printType = printType ? printType : 1;
  40. },
  41. updated: function () {
  42. // 计时器
  43. var _this = this;
  44. var MAX = 100, MIN = 1;
  45. $('.weui-count__decrease').unbind('click').on('click',function (e) {
  46. if (_this.deviceId) {
  47. return false;
  48. }
  49. var $input = $(e.target).parent().find('.weui-count__number');
  50. var number = parseInt($input.val() || "0") - 1
  51. if (number < MIN) number = MIN;
  52. $input.val(number)
  53. })
  54. $('.weui-count__increase').unbind('click').on('click',function (e) {
  55. if (_this.deviceId) {
  56. return false;
  57. }
  58. var $input = $(e.target).parent().find('.weui-count__number');
  59. var number = parseInt($input.val() || "0") + 1
  60. if (number > MAX) number = MAX;
  61. $input.val(number)
  62. });
  63. $(".weui-count__number").blur(function () {
  64. var num = $.trim($(this).val());
  65. if (!/[1-9][0-9]{0,2}/.test(num) || num < MIN || num > MAX) {
  66. num = 1;
  67. }
  68. $(this).val(num);
  69. })
  70. },
  71. methods: {
  72. // 获取图片列表
  73. getFileList: function () {
  74. var _this = this;
  75. $.post('/weixin/print/getFileList', {}, function (res) {
  76. if (res.code == 'success') {
  77. _this.picList = res.data
  78. } else if (res.code == 'login') {
  79. login(res.data.url);
  80. }
  81. }, "json");
  82. },
  83. // 获取用户信息
  84. getMemberInfo: function () {
  85. var _this = this;
  86. $.post('/weixin/member/getMemberInfo', {}, function (res) {
  87. if (res.code == 'success') {
  88. _this.memberInfo = res.data
  89. } else if (res.code == 'login') {
  90. login(res.data.url);
  91. }
  92. }, "json");
  93. },
  94. // 提交
  95. doSubmit: function () {
  96. var _this = this;
  97. var count = 0;
  98. var formData = new FormData();
  99. if (_this.submitting) {
  100. return false;
  101. }
  102. $(".print-pic").each(function (k, item) {
  103. var _img = $(this).find('.pic-img');
  104. var src = $.trim(_img.attr('src'));
  105. var pic = $.trim(_img.attr('data-pic'));
  106. var id = $.trim($(this).attr('data-id'));
  107. var name = $.trim(_img.attr('alt'));
  108. var num = parseInt($(this).find('.pic-num').val());
  109. if (src && (id == 0 || pic != src)) {
  110. var file = _this.dataURLtoFile(src, name);
  111. if (file) {
  112. count++;
  113. formData.append('image[]', file);
  114. formData.append('nums[]', num);
  115. }
  116. } else if (id > 0) {
  117. count++;
  118. }
  119. });
  120. if (count <= 0) {
  121. $.toast('请选择打印的图片');
  122. return false;
  123. }
  124. // 上传
  125. $.showLoading('文件上传处理中...');
  126. var ajax = $.ajax({
  127. url: '/weixin/upload/image',
  128. data: formData,
  129. type: "post",
  130. dataType: 'json',
  131. cache: false,
  132. contentType: false,
  133. processData: false,
  134. timeout: 30000,
  135. success: function (res) {
  136. $.hideLoading();
  137. if (res.code == 'success') {
  138. $.modal({
  139. title: "",
  140. text: "<div class='scan'>请扫一扫设备上的二维码</div><div class='tips'>获取设备信息</div>",
  141. buttons: [
  142. {
  143. text: "扫一扫",
  144. className: "warning",
  145. onClick: function () {
  146. _this.scan();
  147. }
  148. },
  149. ]
  150. });
  151. } else if (res.code == 'login') {
  152. login(res.data.url);
  153. } else {
  154. $.toast(res.message);
  155. }
  156. },
  157. error: function(res){
  158. $.toast('服务器错误');
  159. },
  160. complete: function (XMLHttpRequest, status) {
  161. if(status == 'timeout'){
  162. ajax.abort();
  163. $.toast('请求超时,请重新提交');
  164. location.reload();
  165. }
  166. }
  167. })
  168. },
  169. // 扫码设备
  170. scan: function () {
  171. var _this = this;
  172. $.post('/weixin/index/getJssdkParams', {url: location.href}, function (res) {
  173. var params = res.data;
  174. // 微信JSSDK
  175. wx.config({
  176. debug: false, // 是否调试模式
  177. appId: params.appId, // 必填,公众号的唯一标识
  178. timestamp: params.timestamp, // 必填,生成签名的时间戳
  179. nonceStr: params.nonceStr, // 必填,生成签名的随机串
  180. signature: params.signature,// 必填,签名
  181. jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'scanQRCode'] // 必填,需要使用的JS接口列表
  182. });
  183. // 初始化处理
  184. wx.ready(function () {
  185. // 微信扫一扫
  186. wx.scanQRCode({
  187. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  188. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  189. success: function (res) {
  190. var deviceId = res.resultStr;
  191. _this.deviceId = deviceId;
  192. if (deviceId) {
  193. $.closeModal();
  194. $.modal({
  195. title: "",
  196. text: "<div class='scan'>扫码成功</div><div class='info'></div><div class='tips'>点击下方确认下单</div>",
  197. buttons: [
  198. {
  199. text: "确认下单",
  200. className: "warning",
  201. onClick: function () {
  202. var params = {
  203. clusterId: _this.id,
  204. deviceId: deviceId,
  205. printType: _this.printType,
  206. type: 1
  207. };
  208. $.showLoading('订单处理中...');
  209. $.post('/weixin/order/confirm', params, function (res) {
  210. $.hideLoading();
  211. if (res.code == 'success') {
  212. var msg = _this.id > 0 && _this.printType == 2 ? '参与拼团成功' : (_this.printType == 2 ? '发起拼团成功' : '下单成功');
  213. $.showLoading(msg + ',即将前往支付...');
  214. setTimeout(function () {
  215. location.href = '/weixin/order/pay?id=' + res.data;
  216. }, 800);
  217. } else if (res.code == 'login') {
  218. login(res.data.url);
  219. } else {
  220. $.closeModal();
  221. $.modal({
  222. title: "",
  223. text: "<div class='scan'>下单失败</div><div class='tips'>"+res.message+"</div>",
  224. buttons: [
  225. {
  226. text: "取消",
  227. onClick: function () {
  228. return false;
  229. }
  230. },
  231. {
  232. text: "重新扫码下单",
  233. className: "warning",
  234. onClick: function () {
  235. _this.scan();
  236. }
  237. },
  238. ]
  239. });
  240. }
  241. }, "json")
  242. }
  243. },
  244. ]
  245. });
  246. } else {
  247. webToast('扫码信息错误', "middle", 1000)
  248. }
  249. }
  250. });
  251. });
  252. }, "JSON");
  253. },
  254. // base转文件
  255. dataURLtoFile: function (dataurl, filename) {//将base64转换为文件
  256. var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
  257. bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  258. while (n--) {
  259. u8arr[n] = bstr.charCodeAt(n);
  260. }
  261. return new File([u8arr], filename, {type: mime});
  262. },
  263. // 删除图片
  264. picDel: function (id) {
  265. var fileId = $("#pic_" + id).attr('data-id');
  266. $("#pic_" + id).detach();
  267. if (fileId) {
  268. $.post("/weixin/print/fileDel", {id: fileId})
  269. }
  270. },
  271. // 编辑图片
  272. picEdit: function (id) {
  273. var _this = this;
  274. $.closePopup();
  275. _this.cropCancel();
  276. _this.cropCancel();
  277. var imgWidth = $('body').width();
  278. $("#cropBox .crop-img").html(" ");
  279. var src = $("#pic_img_" + id).attr('data-pic');
  280. _this.cropParams = {id: id, src: src, cropSrc: src};
  281. _this.image = new Image();
  282. _this.image.src = src;
  283. _this.image.id = 'cropImg_' + id;
  284. _this.image.setAttribute('class', 'img');
  285. // image.setAttribute('data-pic', src);
  286. _this.image.style.width = imgWidth + 'px !important';
  287. _this.image.style.display = 'block !important';
  288. var canvas = document.createElement('canvas');
  289. canvas.id = 'cut';
  290. canvas.style.display = 'none';
  291. canvas.style.width = '100%';
  292. canvas.style.height = '100%';
  293. $("#cropBox .crop-img").append(_this.image);
  294. $("#cropBox .crop-img").append(canvas);
  295. $("#cropBox .crop-preview").html('<div id="preview" class="preview"><img id="cropImgPreview" src="" alt=""></div>');
  296. //_this.loadCrop(id, _this.image);
  297. $("#cropBox").popup();
  298. _this.image.onload = function () {
  299. if (_this.jc != null) {
  300. _this.jc.destroy();
  301. }
  302. _this.loadCrop(id, _this.image);
  303. }
  304. },
  305. // 加载剪切处理
  306. loadCrop: function (id, image) {
  307. var _this = this;
  308. _this.cropPreviewStatus = false;
  309. if (_this.jc != null) {
  310. _this.jc.setImage(image.src)
  311. _this.jc.destroy();
  312. }
  313. var imgWidth = $(".crop-img").outerWidth();
  314. var imgHeight = $(".crop-img").outerHeight();
  315. var iWidth = image.width;
  316. var iHeight = image.height;
  317. _this.cropParams.width = iWidth;
  318. _this.cropParams.height = iHeight;
  319. $("#cropBox .crop-img").show();
  320. $("#cropBox .crop-preview").hide()
  321. $("#cropImg_" + id).Jcrop({
  322. aspectRatio: 0,
  323. allowSelect: false,
  324. allowResize: true,
  325. // boxWidth: 640,
  326. // boxHeight: imgHeight,
  327. setSelect: [(imgWidth) / 4, 0, imgWidth * (3 / 4), imgHeight],
  328. maxSize: [imgWidth, imgHeight],
  329. minSize: [imgWidth / 3, imgHeight / 2],
  330. onChange: function (c) {
  331. if (parseInt(c.w) > 0 && c.x <= (imgWidth - c.w)) {
  332. var width = c.w > imgWidth ? imgWidth : c.w;
  333. $("#cropImgPreview").attr('src', image.src);
  334. $("#preview").css({
  335. width: width + 'px',
  336. height: c.h + 'px',
  337. margin: '0 auto',
  338. overflow: 'hidden'
  339. });
  340. //$.toptip('当前数据:W:' + imgWidth + 'H:' + imgHeight + '+' + JSON.stringify(c), 10000, 'error');
  341. $("#cropImgPreview").css({
  342. width: imgWidth + 'px',
  343. height: 'px',
  344. marginLeft: '-' + c.x + 'px',
  345. marginTop: '-' + c.y + 'px'
  346. });
  347. console.log(c.x, c.y, c.w, c.h);
  348. }
  349. },
  350. }, function () {
  351. _this.jc = this;
  352. });
  353. },
  354. // 图片大小
  355. resetSizeImg: function (image, FitWidth, FitHeight) {
  356. var newImg = image;
  357. if (image.width > 0 && image.height > 0) {
  358. if (image.width / image.height >= FitWidth / FitHeight) {
  359. if (image.width > FitWidth) {
  360. image.width = FitWidth;
  361. newImg.height = (image.height * FitWidth) / image.width;
  362. } else {
  363. newImg.width = image.width;
  364. newImg.height = image.height;
  365. }
  366. } else {
  367. if (image.height > FitHeight) {
  368. newImg.height = FitHeight;
  369. newImg.width = (image.width * FitHeight) / image.height;
  370. } else {
  371. newImg.width = image.width;
  372. newImg.height = image.height;
  373. }
  374. }
  375. }
  376. return newImg;
  377. },
  378. // 取消剪切
  379. cropCancel: function () {
  380. if (this.jc != null) {
  381. this.jc.destroy();
  382. }
  383. this.cropParams = {id: 0, src: '', cropSrc: ''};
  384. $("#cropBox .crop-preview").hide();
  385. $("#cropBox .crop-preview").html("");
  386. $(".crop-img").html("");
  387. $.closePopup();
  388. },
  389. // 预览剪切
  390. cropPreview: function () {
  391. this.cropPreviewStatus = true;
  392. $("#cropBox .crop-img").hide();
  393. // $("#cropImgPreview").attr('src', this.cropParams.cropSrc);
  394. $("#cropBox .crop-preview").show();
  395. },
  396. // 确定剪切
  397. cropConfirm: function () {
  398. var _this = this;
  399. _this.jc.destroy();
  400. $(".crop-img").html("");
  401. $.showLoading('图片剪切处理中...');
  402. var $previewImg = document.getElementById("preview");
  403. html2canvas($previewImg).then(function (canvas) {
  404. var src = canvas.toDataURL('image/jpeg');
  405. $("#cropBox .crop-preview").hide();
  406. $("#cropBox .crop-preview").html("");
  407. $("#pic_img_" + _this.cropParams.id).attr('src', src);
  408. _this.cropParams = {id: 0, src: '', cropSrc: ''};
  409. $.closePopup();
  410. $.hideLoading();
  411. });
  412. },
  413. // 重新剪切
  414. cropReset: function () {
  415. this.cropPreviewStatus = false;
  416. $("#cropBox .crop-img").show();
  417. $("#cropBox .crop-preview").hide();
  418. },
  419. // 选择图片
  420. selectPic: function (ele) {
  421. var _this = this;
  422. var files = ele.target.files;
  423. if (files.length > 0) {
  424. $.each(files, function (k, file) {
  425. if (file) {
  426. var reader = new FileReader();
  427. reader.readAsDataURL(file);
  428. reader.onloadend = function (even) {
  429. var img = {
  430. id: 0,
  431. url: even.currentTarget.result,
  432. name: file.name,
  433. file_size: file.size,
  434. file_type: file.type
  435. };
  436. _this.picList.push(img);
  437. }
  438. }
  439. })
  440. }
  441. }
  442. }
  443. });