files.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 {
  251. $.closeModal();
  252. setTimeout(function(){
  253. $.modal({
  254. title: "",
  255. text: "<div class='scan'>下单失败</div><div class='tips'>"+res.message+"</div>",
  256. buttons: [
  257. {
  258. text: "取消",
  259. onClick: function () {
  260. return false;
  261. }
  262. }
  263. ]
  264. });
  265. },200)
  266. }
  267. }, "json")
  268. }
  269. },
  270. ]
  271. });
  272. }, 200);
  273. }else{
  274. $.toast(res.message);
  275. setTimeout(function(){
  276. _this.showModal();
  277. }, 500)
  278. }
  279. },'json');
  280. },
  281. // 扫码设备
  282. scan: function () {
  283. var _this = this;
  284. $.post('/weixin/index/getJssdkParams', {url: location.href}, function (res) {
  285. var params = res.data;
  286. // 微信JSSDK
  287. wx.config({
  288. debug: false, // 是否调试模式
  289. appId: params.appId, // 必填,公众号的唯一标识
  290. timestamp: params.timestamp, // 必填,生成签名的时间戳
  291. nonceStr: params.nonceStr, // 必填,生成签名的随机串
  292. signature: params.signature,// 必填,签名
  293. jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'scanQRCode'] // 必填,需要使用的JS接口列表
  294. });
  295. // 初始化处理
  296. wx.ready(function () {
  297. // 微信扫一扫
  298. wx.scanQRCode({
  299. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  300. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  301. success: function (res) {
  302. var deviceId = res.resultStr;
  303. _this.deviceId = deviceId;
  304. if (deviceId) {
  305. $.closeModal();
  306. setTimeout(function(){
  307. $.modal({
  308. title: "",
  309. text: "<div class='scan'>扫码成功</div><div class='info'></div><div class='tips'>点击下方确认下单</div>",
  310. buttons: [
  311. {
  312. text: "取消",
  313. className: "default",
  314. onClick: function () {
  315. $.closeModal();
  316. setTimeout(function () {
  317. _this.showModal();
  318. }, 500)
  319. return false;
  320. }
  321. },
  322. {
  323. text: "确认下单",
  324. className: "warning",
  325. onClick: function () {
  326. $.showLoading('订单处理中...');
  327. $.post('/weixin/order/confirm', {
  328. clusterId: _this.id,
  329. deviceId: deviceId,
  330. printType: _this.printType,
  331. colorType: _this.colorType,
  332. pageType: _this.pageType,
  333. files: _this.files,
  334. type: 2
  335. }, function (res) {
  336. $.hideLoading();
  337. if (res.code == 'success') {
  338. var msg = _this.id > 0 && _this.printType == 2 ? '参与拼团成功' : (_this.printType == 2 ? '发起拼团成功' : '下单成功');
  339. $.showLoading(msg + ',即将前往支付...');
  340. setTimeout(function () {
  341. location.href = '/weixin/order/pay?id=' + res.data;
  342. }, 800);
  343. } else if (res.code == 'login') {
  344. login(res.data.url);
  345. } else {
  346. $.toast(res.message);
  347. $.closeModal();
  348. setTimeout(function(){
  349. $.modal({
  350. title: "",
  351. text: "<div class='scan'>请扫一扫设备上的二维码</div><div class='tips'>获取设备信息</div>",
  352. buttons: [
  353. {
  354. text: "扫一扫",
  355. className: "warning",
  356. onClick: function () {
  357. _this.scan();
  358. }
  359. },
  360. ]
  361. });
  362. })
  363. }
  364. }, "json")
  365. }
  366. },
  367. ]
  368. });
  369. }, 200)
  370. } else {
  371. webToast('扫码信息错误', "middle", 1000)
  372. }
  373. }
  374. });
  375. });
  376. }, "JSON");
  377. },
  378. // 删除图片
  379. picDel: function (id) {
  380. var fileId = $("#pic_" + id).attr('data-id');
  381. $("#pic_" + id).detach();
  382. $("#weui-actions input").val("");
  383. if (fileId) {
  384. $.post("/weixin/print/fileDel", {id: fileId})
  385. }
  386. },
  387. // 切换预览
  388. hidePreview: function () {
  389. $.closePopup();
  390. },
  391. // 预览
  392. preview: function (file) {
  393. // this.fileData = file;
  394. // var url = 'http://'+document.domain+'/weixin/index/preview?t='+file.file_type+'&url=';
  395. // $("#preview").attr('src', url+file.preview);
  396. // location.href = url;
  397. // window.open(url);
  398. /*alert(url+file.preview);
  399. $("#previewBox").popup();
  400. console.log(file)*/
  401. },
  402. // 选择图片
  403. selectFile: function (ele) {
  404. var _this = this;
  405. var files = ele.target.files;
  406. if (files.length > 0) {
  407. var _this = this;
  408. var count = 0;
  409. var formData = new FormData();
  410. var error = false;
  411. $.each(files, function (k, file) {
  412. var fileType = (file.name).substring((file.name).lastIndexOf(".")).toLowerCase();
  413. if(!new RegExp("(xls|xlsx|doc|docx|pdf|ppt|pptx)+","gi").test(fileType)){
  414. $.toast("文件类型不支持,必须是xls|xlsx|doc|docx|pdf|ppt|pptx格式文件",'text');
  415. $(ele.target).val("");
  416. error = true;
  417. return false;
  418. }
  419. if (file.size / 1024 / 1024 > 20) {
  420. $.toast('上传文件大小不能超过20M','text');
  421. $(ele.target).val("");
  422. error = true;
  423. return false;
  424. }
  425. if (file) {
  426. count++;
  427. formData.append('file', file);
  428. $(ele.target).val("");
  429. }
  430. });
  431. if(error){
  432. return false;
  433. }
  434. if (count <= 0) {
  435. $.toast('请选择打印的文件');
  436. return false;
  437. }
  438. // 上传
  439. $.showLoading('文件上传处理中...');
  440. var ajax = $.ajax({
  441. url: '/weixin/upload/file',
  442. data: formData,
  443. type: "post",
  444. dataType: 'json',
  445. cache: false,
  446. contentType: false,
  447. processData: false,
  448. timeout: 30000,
  449. // timeout: 30000,
  450. success: function (res) {
  451. $.hideLoading();
  452. if (res.code == 'success') {
  453. $.toast('文件上传完成');
  454. if (res.data) {
  455. _this.picList.push(res.data);
  456. }
  457. } else if (res.code == 'login') {
  458. login(res.data.url);
  459. } else {
  460. $.toast(res.message);
  461. }
  462. },
  463. error: function(res){
  464. $.toast('服务器错误');
  465. },
  466. complete: function (XMLHttpRequest, status) {
  467. if(status == 'timeout'){
  468. ajax.abort();
  469. $.toast('请求超时,请重新上传');
  470. location.reload();
  471. }
  472. }
  473. })
  474. }
  475. },
  476. }
  477. });