files.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. var app = new Vue({
  2. el: '#app',
  3. data: {
  4. // 用户信息
  5. memberInfo: {},
  6. // 当前文件
  7. fileData: {},
  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. files: [],
  22. // 图片列表
  23. picList: [],
  24. // 打印方式
  25. printType: 1,
  26. // 提交状态
  27. submitting: false,
  28. // 预览状态
  29. cropPreviewStatus: false,
  30. // 剪切对象
  31. jc: null,
  32. // 设备ID
  33. deviceId: 0,
  34. // 色彩
  35. colorType: 1,
  36. pageType: 1,
  37. id: 0,
  38. },
  39. created: function () {
  40. var printType = getParam('pt');
  41. this.printType = printType ? printType : 1;
  42. this.id = getParam('id');
  43. this.getFileList();
  44. },
  45. updated: function () {
  46. // 计时器
  47. var _this = this;
  48. var MAX = 100, MIN = 1;
  49. $('.weui-count__decrease').unbind('click').on('click',function (e) {
  50. if (_this.deviceId) {
  51. return false;
  52. }
  53. var $input = $(e.target).parent().find('.weui-count__number');
  54. var number = parseInt($input.val() || "0") - 1
  55. if (number < MIN) number = MIN;
  56. $input.val(number)
  57. })
  58. $('.weui-count__increase').unbind('click').on('click',function (e) {
  59. if (_this.deviceId) {
  60. return false;
  61. }
  62. var $input = $(e.target).parent().find('.weui-count__number');
  63. var number = parseInt($input.val() || "0") + 1
  64. if (number > MAX) number = MAX;
  65. $input.val(number)
  66. });
  67. $(".weui-count__number").blur(function () {
  68. var num = $.trim($(this).val());
  69. if (!/[1-9][0-9]{0,2}/.test(num) || num < MIN || num > MAX) {
  70. num = 1;
  71. }
  72. $(this).val(num);
  73. })
  74. $(".page-type .btn").unbind('click').on('click', function(){
  75. var index = $(this).index();
  76. _this.pageType = index+1;
  77. $(".page-type").each(function(){
  78. $(this).find('.btn').eq(index).addClass('active').siblings().removeClass('active');
  79. })
  80. });
  81. $(".print-type .btn").unbind('click').on('click', function(){
  82. var index = $(this).index();
  83. _this.colorType = index+1;
  84. $(".print-type").each(function(){
  85. $(this).find('.btn').eq(index).addClass('active').siblings().removeClass('active');
  86. })
  87. });
  88. $(".item-box .page").change(function(){
  89. var pageStart = $("#page-start").val();
  90. var pageEnd = $("#page-end").val();
  91. var pageMax = $(this).parent().attr('data-max');
  92. pageMax = pageMax? pageMax : 1;
  93. if(pageStart>pageEnd){
  94. $.toast('起始页不能大于结束页','text');
  95. $("#page-start").val(1);
  96. $("#page-end").val(pageMax);
  97. return false;
  98. }
  99. })
  100. },
  101. methods: {
  102. // 获取图片列表
  103. getFileList: function(){
  104. var _this = this;
  105. $.post('/weixin/print/getFileList', {type: 2}, function (res) {
  106. if (res.code == 'success') {
  107. _this.picList = res.data
  108. } else if (res.code == 'login') {
  109. login(res.data.url);
  110. }
  111. }, "json");
  112. },
  113. // 获取用户信息
  114. getMemberInfo: function () {
  115. var _this = this;
  116. $.post('/weixin/member/getMemberInfo', {}, function (res) {
  117. if (res.code == 'success') {
  118. _this.memberInfo = res.data
  119. } else if (res.code == 'login') {
  120. login(res.data.url);
  121. }
  122. }, "json");
  123. },
  124. // 提交
  125. doSubmit: function () {
  126. var _this = this;
  127. if (_this.submitting) {
  128. return false;
  129. }
  130. $(".file-box").each(function (k, item) {
  131. var name = $.trim($(this).find('.file-name').text());
  132. var nums = parseInt($(this).find('.file-num').val());
  133. var page_type = parseInt($(this).find('.page-type .btn.active').index() + 1);
  134. var print_type = parseInt($(this).find('.print-type .btn.active').index() + 1);
  135. var page_start = parseInt($(this).find('.page-start').val());
  136. var page_end = parseInt($(this).find('.page-end').val());
  137. var id = $(this).attr('data-id');
  138. if (id) {
  139. var file = {
  140. id: id,
  141. name: name,
  142. nums: nums,
  143. page_type: page_type,
  144. print_type: print_type,
  145. page_start: page_start,
  146. page_end: page_end
  147. };
  148. _this.files.push(file);
  149. }
  150. });
  151. if (_this.files.length <= 0) {
  152. $.toast('请选择打印的文件');
  153. return false;
  154. }
  155. _this.showModal();
  156. },
  157. // 显示填写窗口
  158. showModal: function(){
  159. var _this = this;
  160. $.closeModal();
  161. setTimeout(function(){
  162. $.modal({
  163. title: "",
  164. autoClose: false,
  165. text: "<div class='scan'>填写/扫码打印设备</div><div class='tips'>设备号<input type='text' id='device_code' placeholder='请填写打印机设备号'></div>",
  166. buttons: [
  167. {
  168. text: "取消",
  169. className: "default",
  170. onClick: function () {
  171. $.closeModal();
  172. $.closePopup();
  173. return false;
  174. }
  175. },
  176. {
  177. text: "扫码打印",
  178. className: "warning",
  179. onClick: function () {
  180. _this.scan();
  181. }
  182. },
  183. {
  184. text: "填写打印",
  185. className: "success",
  186. onClick: function () {
  187. _this.inputCode();
  188. return false;
  189. }
  190. },
  191. ]
  192. });
  193. }, 500)
  194. },
  195. // 输入设备号打印
  196. inputCode: function(){
  197. var _this = this;
  198. var deviceCode = $.trim($("#device_code").val());
  199. if(deviceCode == '' || deviceCode == null){
  200. $.toast('请填写设备号');
  201. setTimeout(function(){
  202. _this.showModal();
  203. }, 500)
  204. return false;
  205. }
  206. $.post('/weixin/print/checkDevice', {deviceCode: deviceCode}, function (res) {
  207. if(res.code == 'success'){
  208. var deviceId = res.data.id;
  209. $.closeModal();
  210. setTimeout(function(){
  211. $.modal({
  212. title: "",
  213. text: "<div class='scan'>设备验证成功</div><div class='info'></div><div class='tips'>点击下方确认下单</div>",
  214. buttons: [
  215. {
  216. text: "取消",
  217. className: "default",
  218. onClick: function () {
  219. $.closeModal();
  220. setTimeout(function () {
  221. _this.showModal();
  222. }, 500)
  223. return false;
  224. }
  225. },
  226. {
  227. text: "确认下单",
  228. className: "warning",
  229. onClick: function () {
  230. var params = {
  231. clusterId: _this.id,
  232. deviceId: deviceId,
  233. printType: _this.printType,
  234. colorType: _this.colorType,
  235. pageType: _this.pageType,
  236. files: _this.files,
  237. type: 2
  238. };
  239. $.showLoading('订单处理中...');
  240. $.post('/weixin/order/confirm', params, function (res) {
  241. $.hideLoading();
  242. if (res.code == 'success') {
  243. var msg = _this.id > 0 && _this.printType == 2 ? '参与拼团成功' : (_this.printType == 2 ? '发起拼团成功' : '下单成功');
  244. $.showLoading(msg + ',即将前往支付...');
  245. setTimeout(function () {
  246. location.href = '/weixin/order/pay?id=' + res.data;
  247. }, 800);
  248. } else if (res.code == 'login') {
  249. login(res.data.url);
  250. } else if(res.code == 'excepition'){
  251. showAlert(res.message);
  252. } else {
  253. $.closeModal();
  254. setTimeout(function(){
  255. $.modal({
  256. title: "",
  257. text: "<div class='scan'>下单失败</div><div class='tips'>"+res.message+"</div>",
  258. buttons: [
  259. {
  260. text: "取消",
  261. onClick: function () {
  262. return false;
  263. }
  264. }
  265. ]
  266. });
  267. },200)
  268. }
  269. }, "json")
  270. }
  271. },
  272. ]
  273. });
  274. }, 200);
  275. }else{
  276. $.toast(res.message);
  277. setTimeout(function(){
  278. _this.showModal();
  279. }, 500)
  280. }
  281. },'json');
  282. },
  283. // 扫码设备
  284. scan: function () {
  285. var _this = this;
  286. $.post('/weixin/index/getJssdkParams', {url: location.href}, function (res) {
  287. var params = res.data;
  288. // 微信JSSDK
  289. wx.config({
  290. debug: false, // 是否调试模式
  291. appId: params.appId, // 必填,公众号的唯一标识
  292. timestamp: params.timestamp, // 必填,生成签名的时间戳
  293. nonceStr: params.nonceStr, // 必填,生成签名的随机串
  294. signature: params.signature,// 必填,签名
  295. jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'scanQRCode'] // 必填,需要使用的JS接口列表
  296. });
  297. // 初始化处理
  298. wx.ready(function () {
  299. // 微信扫一扫
  300. wx.scanQRCode({
  301. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  302. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  303. success: function (res) {
  304. var deviceId = res.resultStr;
  305. _this.deviceId = deviceId;
  306. if (deviceId) {
  307. $.closeModal();
  308. setTimeout(function(){
  309. $.modal({
  310. title: "",
  311. text: "<div class='scan'>扫码成功</div><div class='info'></div><div class='tips'>点击下方确认下单</div>",
  312. buttons: [
  313. {
  314. text: "取消",
  315. className: "default",
  316. onClick: function () {
  317. $.closeModal();
  318. setTimeout(function () {
  319. _this.showModal();
  320. }, 500)
  321. return false;
  322. }
  323. },
  324. {
  325. text: "确认下单",
  326. className: "warning",
  327. onClick: function () {
  328. $.showLoading('订单处理中...');
  329. $.post('/weixin/order/confirm', {
  330. clusterId: _this.id,
  331. deviceId: deviceId,
  332. printType: _this.printType,
  333. colorType: _this.colorType,
  334. pageType: _this.pageType,
  335. files: _this.files,
  336. type: 2
  337. }, function (res) {
  338. $.hideLoading();
  339. if (res.code == 'success') {
  340. var msg = _this.id > 0 && _this.printType == 2 ? '参与拼团成功' : (_this.printType == 2 ? '发起拼团成功' : '下单成功');
  341. $.showLoading(msg + ',即将前往支付...');
  342. setTimeout(function () {
  343. location.href = '/weixin/order/pay?id=' + res.data;
  344. }, 800);
  345. } else if (res.code == 'login') {
  346. login(res.data.url);
  347. } else {
  348. $.toast(res.message);
  349. $.closeModal();
  350. setTimeout(function(){
  351. $.modal({
  352. title: "",
  353. text: "<div class='scan'>请扫一扫设备上的二维码</div><div class='tips'>获取设备信息</div>",
  354. buttons: [
  355. {
  356. text: "扫一扫",
  357. className: "warning",
  358. onClick: function () {
  359. _this.scan();
  360. }
  361. },
  362. ]
  363. });
  364. })
  365. }
  366. }, "json")
  367. }
  368. },
  369. ]
  370. });
  371. }, 200)
  372. } else {
  373. webToast('扫码信息错误', "middle", 1000)
  374. }
  375. }
  376. });
  377. });
  378. }, "JSON");
  379. },
  380. // 删除图片
  381. picDel: function (id) {
  382. var fileId = $("#pic_" + id).attr('data-id');
  383. $("#pic_" + id).detach();
  384. $("#weui-actions input").val("");
  385. if (fileId) {
  386. $.post("/weixin/print/fileDel", {id: fileId})
  387. }
  388. },
  389. // 切换预览
  390. hidePreview: function () {
  391. $.closePopup();
  392. },
  393. // 预览
  394. preview: function (file) {
  395. // this.fileData = file;
  396. // var url = 'http://'+document.domain+'/weixin/index/preview?t='+file.file_type+'&url=';
  397. // $("#preview").attr('src', url+file.preview);
  398. // location.href = url;
  399. // window.open(url);
  400. /*alert(url+file.preview);
  401. $("#previewBox").popup();
  402. console.log(file)*/
  403. },
  404. // 选择图片
  405. selectFile: function (ele) {
  406. var _this = this;
  407. var files = ele.target.files;
  408. if (files.length > 0) {
  409. var _this = this;
  410. var count = 0;
  411. var formData = new FormData();
  412. var error = false;
  413. $.each(files, function (k, file) {
  414. var fileType = (file.name).substring((file.name).lastIndexOf(".")).toLowerCase();
  415. if(!new RegExp("(xls|xlsx|doc|docx|pdf|ppt|pptx)+","gi").test(fileType)){
  416. $.toast("文件类型不支持,必须是xls|xlsx|doc|docx|pdf|ppt|pptx格式文件",'text');
  417. $(ele.target).val("");
  418. error = true;
  419. return false;
  420. }
  421. if (file.size / 1024 / 1024 > 20) {
  422. $.toast('上传文件大小不能超过20M','text');
  423. $(ele.target).val("");
  424. error = true;
  425. return false;
  426. }
  427. if (file) {
  428. count++;
  429. formData.append('file', file);
  430. $(ele.target).val("");
  431. }
  432. });
  433. if(error){
  434. return false;
  435. }
  436. if (count <= 0) {
  437. $.toast('请选择打印的文件', 'text');
  438. return false;
  439. }
  440. // 上传
  441. $.showLoading('文件上传处理中...');
  442. var ajax = $.ajax({
  443. url: '/weixin/upload/file',
  444. data: formData,
  445. type: "post",
  446. dataType: 'json',
  447. cache: false,
  448. contentType: false,
  449. processData: false,
  450. timeout: 300000,
  451. // timeout: 30000,
  452. success: function (res) {
  453. $.hideLoading();
  454. if (res.code == 'success') {
  455. $.toast('文件上传完成');
  456. if (res.data) {
  457. _this.picList.push(res.data);
  458. }
  459. } else if (res.code == 'login') {
  460. login(res.data.url);
  461. } else {
  462. $.toast(res.message);
  463. }
  464. },
  465. error: function(res){
  466. $.toast('服务器错误');
  467. },
  468. complete: function (XMLHttpRequest, status) {
  469. if(status == 'timeout'){
  470. ajax.abort();
  471. $.toast('请求超时,请重新上传');
  472. location.reload();
  473. }
  474. }
  475. })
  476. }
  477. },
  478. }
  479. });