files.js 21 KB

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