files_back.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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: 2,
  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. $.modal({
  144. title: "",
  145. text: "<div class='scan'>请扫一扫设备上的二维码</div><div class='tips'>获取设备信息</div>",
  146. buttons: [
  147. {
  148. text: "扫一扫",
  149. className: "warning",
  150. onClick: function () {
  151. _this.scan();
  152. }
  153. },
  154. ]
  155. });
  156. },
  157. // 扫码设备
  158. scan: function () {
  159. var _this = this;
  160. $.post('/weixin/index/getJssdkParams', {url: location.href}, function (res) {
  161. var params = res.data;
  162. // 微信JSSDK
  163. wx.config({
  164. debug: false, // 是否调试模式
  165. appId: params.appId, // 必填,公众号的唯一标识
  166. timestamp: params.timestamp, // 必填,生成签名的时间戳
  167. nonceStr: params.nonceStr, // 必填,生成签名的随机串
  168. signature: params.signature,// 必填,签名
  169. jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'scanQRCode'] // 必填,需要使用的JS接口列表
  170. });
  171. // 初始化处理
  172. wx.ready(function () {
  173. // 微信扫一扫
  174. wx.scanQRCode({
  175. needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
  176. scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
  177. success: function (res) {
  178. var deviceId = res.resultStr;
  179. _this.deviceId = deviceId;
  180. if (deviceId) {
  181. $.closeModal();
  182. $.modal({
  183. title: "",
  184. text: "<div class='scan'>扫码成功</div><div class='info'></div><div class='tips'>点击下方确认下单</div>",
  185. buttons: [
  186. {
  187. text: "确认下单",
  188. className: "warning",
  189. onClick: function () {
  190. $.showLoading('订单处理中...');
  191. $.post('/weixin/order/confirm', {
  192. clusterId: _this.id,
  193. deviceId: deviceId,
  194. printType: _this.printType,
  195. colorType: _this.colorType,
  196. pageType: _this.pageType,
  197. files: _this.files,
  198. type: 2
  199. }, function (res) {
  200. $.hideLoading();
  201. if (res.code == 'success') {
  202. var msg = _this.id > 0 && _this.printType == 2 ? '参与拼团成功' : (_this.printType == 2 ? '发起拼团成功' : '下单成功');
  203. $.showLoading(msg + ',即将前往支付...');
  204. setTimeout(function () {
  205. location.href = '/weixin/order/pay?id=' + res.data;
  206. }, 800);
  207. } else if (res.code == 'login') {
  208. login(res.data.url);
  209. } else {
  210. $.toast(res.message);
  211. $.closeModal();
  212. $.modal({
  213. title: "",
  214. text: "<div class='scan'>请扫一扫设备上的二维码</div><div class='tips'>获取设备信息</div>",
  215. buttons: [
  216. {
  217. text: "扫一扫",
  218. className: "warning",
  219. onClick: function () {
  220. _this.scan();
  221. }
  222. },
  223. ]
  224. });
  225. }
  226. }, "json")
  227. }
  228. },
  229. ]
  230. });
  231. } else {
  232. webToast('扫码信息错误', "middle", 1000)
  233. }
  234. }
  235. });
  236. });
  237. }, "JSON");
  238. },
  239. // 删除图片
  240. picDel: function (id) {
  241. var fileId = $("#pic_" + id).attr('data-id');
  242. $("#pic_" + id).detach();
  243. if (fileId) {
  244. $.post("/weixin/print/fileDel", {id: fileId})
  245. }
  246. },
  247. // 切换预览
  248. hidePreview: function () {
  249. $.closePopup();
  250. },
  251. // 预览
  252. preview: function (file) {
  253. // this.fileData = file;
  254. // var url = 'http://'+document.domain+'/weixin/index/preview?t='+file.file_type+'&url=';
  255. // $("#preview").attr('src', url+file.preview);
  256. // location.href = url;
  257. // window.open(url);
  258. /*alert(url+file.preview);
  259. $("#previewBox").popup();
  260. console.log(file)*/
  261. },
  262. // 选择图片
  263. selectFile: function (ele) {
  264. var _this = this;
  265. var files = ele.target.files;
  266. if (files.length > 0) {
  267. var _this = this;
  268. var count = 0;
  269. var formData = new FormData();
  270. var error = false;
  271. $.each(files, function (k, file) {
  272. var fileType = (file.name).substring((file.name).lastIndexOf(".")).toLowerCase();
  273. if(!new RegExp("(xls|xlsx|doc|docx|pdf|ppt|pptx)+","gi").test(fileType)){
  274. $.toast("文件类型不支持,必须是xls|xlsx|doc|docx|pdf|ppt|pptx格式文件",'text');
  275. $(ele.target).val("");
  276. error = true;
  277. return false;
  278. }
  279. if (file.size / 1024 / 1024 > 20) {
  280. $.toast('上传文件大小不能超过20M','text');
  281. $(ele.target).val("");
  282. error = true;
  283. return false;
  284. }
  285. if (file) {
  286. count++;
  287. formData.append('file', file);
  288. }
  289. });
  290. if(error){
  291. return false;
  292. }
  293. if (count <= 0) {
  294. $.toast('请选择打印的文件');
  295. return false;
  296. }
  297. // 上传
  298. $.showLoading('文件上传处理中...');
  299. var ajax = $.ajax({
  300. url: '/weixin/upload/file',
  301. data: formData,
  302. type: "post",
  303. dataType: 'json',
  304. cache: false,
  305. contentType: false,
  306. processData: false,
  307. timeout: 30000,
  308. // timeout: 30000,
  309. success: function (res) {
  310. $.hideLoading();
  311. if (res.code == 'success') {
  312. $.toast('文件上传完成');
  313. if (res.data) {
  314. _this.picList.push(res.data);
  315. }
  316. } else if (res.code == 'login') {
  317. login(res.data.url);
  318. } else {
  319. $.toast(res.message);
  320. }
  321. },
  322. error: function(res){
  323. $.toast('服务器错误');
  324. },
  325. complete: function (XMLHttpRequest, status) {
  326. if(status == 'timeout'){
  327. ajax.abort();
  328. $.toast('请求超时,请重新上传');
  329. location.reload();
  330. }
  331. }
  332. })
  333. }
  334. },
  335. }
  336. });