app.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. $.fn.extend({
  2. animateCss: function (animationName, callback) {
  3. var animationEnd = (function (el) {
  4. var animations = {
  5. animation: 'animationend',
  6. OAnimation: 'oAnimationEnd',
  7. MozAnimation: 'mozAnimationEnd',
  8. WebkitAnimation: 'webkitAnimationEnd',
  9. };
  10. for (var t in animations) {
  11. if (el.style[t] !== undefined) {
  12. return animations[t];
  13. }
  14. }
  15. })(document.createElement('div'));
  16. this.addClass('animated ' + animationName).one(animationEnd, function () {
  17. //$(this).removeClass('animated ' + animationName);
  18. callback && callback.call(this, $(this), animationName);
  19. });
  20. return this;
  21. },
  22. });
  23. var app = {
  24. /**
  25. * 请求id
  26. */
  27. id: location.search.substring(1).replace(/ID=|\&pic=1/gi, ''),
  28. /**
  29. * 切换画风id
  30. */
  31. switchId: 0,
  32. /**
  33. * 切换风景id
  34. */
  35. bgId: 0,
  36. /**
  37. * 切换风格类型
  38. */
  39. type: 1,
  40. /**
  41. * 我的相片列表
  42. */
  43. imgList: [],
  44. /**
  45. * 广告切换
  46. */
  47. adsSwiper: function () {
  48. var html = [];
  49. var $elem = $('.ads-swiper')
  50. __config.adsPath && __config.adsPath.forEach(function (item) {
  51. html.push('<div class="swiper-slide"><a href="' + (item.link || '#') + '"><img src="' + (item.image || 'static/images/adds/4dart.jpg') + '"><a></div>')
  52. })
  53. if (!html.length) {
  54. html.push('<div class="swiper-slide"><a href="#"><img src="static/images/adds/4dart.jpg"><a></div>')
  55. }
  56. $elem.find('.swiper-wrapper').html(html.join(''))
  57. new Swiper($elem[0], {
  58. autoplay: {
  59. delay: 3000,
  60. stopOnLastSlide: false,
  61. disableOnInteraction: true,
  62. },
  63. pagination: {
  64. el: $elem.find('.swiper-pagination')[0],
  65. bulletClass: 'my-bullet'
  66. }
  67. });
  68. },
  69. /**
  70. * 画风切换
  71. */
  72. stlSwiper: function () {
  73. var $elem = $('.stl-swiper')
  74. if (this.stlSwiper.swiper) {
  75. this.stlSwiper.swiper.destroy()
  76. }
  77. let html = ''
  78. if (this.type==2) {
  79. html = `<div class="swiper-wrapper">
  80. <div data-msg="1" class="swiper-slide style-1"></div>
  81. <div data-msg="2" class="swiper-slide style-2"></div>
  82. <div data-msg="3" class="swiper-slide style-3"></div>
  83. <div data-msg="4" class="swiper-slide style-4"></div>
  84. <div data-msg="5" class="swiper-slide style-5"></div>
  85. </div>`
  86. } else{
  87. html = `<div class="swiper-wrapper">
  88. <div data-msg="1" class="swiper-slide style-1"></div>
  89. <div data-msg="2" class="swiper-slide style-2"></div>
  90. <div data-msg="3" class="swiper-slide style-3"></div>
  91. <div data-msg="4" class="swiper-slide style-4"></div>
  92. <div data-msg="5" class="swiper-slide style-5"></div>
  93. </div>`
  94. }
  95. $elem.html(html)
  96. this.stlSwiper.swiper = new Swiper($elem[0], {
  97. //speed: 300,
  98. freeMode: true,
  99. slidesPerView: 3.5,
  100. });
  101. var time;
  102. $elem.find('.swiper-slide').on('click', function () {
  103. var $this = $(this);
  104. var msg = $this.data('msg')
  105. app.checkOperationTimeout()
  106. let tmp = ''
  107. if (app.type == 2) {
  108. if (app.bgId == msg) {
  109. // 已选择的不再执行
  110. return;
  111. }
  112. tmp = 'bg '
  113. app.bgId = msg
  114. }else{
  115. if (app.switchId == msg) {
  116. // 已选择的不再执行
  117. return;
  118. }
  119. tmp = 'switch '
  120. app.switchId = msg
  121. }
  122. if (!app.sendCheck()) {
  123. return;
  124. }
  125. if (time && (Date.now() - time < __config.switchInterval * 1000)) {
  126. app.showLoadingSwitch(function () {
  127. $elem.find('.swiper-slide').removeClass('active');
  128. $this.addClass('active');
  129. })
  130. } else {
  131. $elem.find('.swiper-slide').removeClass('active')
  132. $this.addClass('active')
  133. }
  134. time = Date.now();
  135. app.send(tmp + msg)
  136. });
  137. $elem.find('.swiper-slide').each(function () {
  138. var style = $(this).data('msg');
  139. if (style) {
  140. var img = new Image()
  141. img.src = (__config.staticUrl || 'static') + `/images/styles${app.type==2?'1':''}/` + style + '-active.png';
  142. }
  143. })
  144. },
  145. /**
  146. * 我的相片滚动
  147. */
  148. picSwiper: function () {
  149. if (this.picSwiper.swiper) {
  150. this.picSwiper.swiper.destroy()
  151. }
  152. this.picSwiper.swiper = new Swiper($('.pic-swiper')[0], {
  153. speed: 300,
  154. slidesPerView: 'auto',
  155. freeMode: true,
  156. direction: 'vertical',
  157. setWrapperSize: true,
  158. scrollbar: {
  159. el: '.swiper-scrollbar',
  160. },
  161. });
  162. },
  163. events: function () {
  164. var self = this,
  165. $picList = $('.my-pic-list');
  166. if (__config.link) {
  167. $('.logo').attr('href', __config.link)
  168. }
  169. $picList.find('header').on('touchstart', function (event) {
  170. event.preventDefault();
  171. });
  172. $picList.find('.btn-back').on('touchend', function (event) {
  173. event.preventDefault();
  174. $picList.hide()
  175. });
  176. $('.swt-style').on('click','a', function (event) {
  177. self.type = $(event.currentTarget).data('id')
  178. $(event.currentTarget).addClass("active").siblings().removeClass("active");
  179. self.stlSwiper()
  180. if (self.type==2) {
  181. $('.stl-swiper').addClass('stl-swiper1')
  182. }else {
  183. $('.stl-swiper').removeClass('stl-swiper1')
  184. }
  185. $('.swiper-wrapper').find('.swiper-slide').removeClass('active');
  186. $(`.swiper-slide[data-msg=${self.type==2?self.bgId:self.switchId}]`).addClass('active');
  187. });
  188. $('.btn-pic-list').on('click', function (event) {
  189. app.send('close')
  190. $picList.show()
  191. self.renderPictures()
  192. });
  193. $('.btn-pic-take').on('click', function (event) {
  194. app.checkOperationTimeout()
  195. if (!app.sendCheck()) {
  196. return;
  197. }
  198. self.send('photo empty')
  199. });
  200. $('.app-view .close').on('click', function (event) {
  201. self.hideView()
  202. });
  203. $('.app-alert .btn-use').on('click', function () {
  204. var $alert = $('.app-alert')
  205. app.hideMask()
  206. $alert.removeClass($alert.data('class') + ' animated zoomIn')
  207. var text = $(this).text();
  208. if (text == '尝试使用' || text == '再次使用') {
  209. app.send('request')
  210. }
  211. })
  212. $('.app-alert .btn-link').on('click', function () {
  213. if ($(this).text() == '重新拍照') {
  214. var $alert = $('.app-alert')
  215. app.hideMask()
  216. $alert.removeClass($alert.data('class') + ' animated zoomIn')
  217. $('.btn-pic-take').click()
  218. }
  219. })
  220. },
  221. showMask: function (clazz) {
  222. var $elem = $('.app-mask');
  223. if (clazz) {
  224. $elem.addClass(this.showMask._clazz = clazz)
  225. }
  226. $elem.css('display', 'block')
  227. },
  228. hideMask: function () {
  229. var $elem = $('.app-mask');
  230. if (this.showMask._clazz) {
  231. $elem.removeClass(this.showMask._clazz)
  232. this.showMask._clazz = null
  233. }
  234. $elem.css('display', 'none')
  235. },
  236. showAlert: function (msg, icon) {
  237. this.showMask();
  238. var $alert = $('.app-alert');
  239. var $btns = $alert.find('.btns a');
  240. $alert.data('class', icon).addClass(icon).animateCss('zoomIn')
  241. $alert.find('.tips').html(msg)
  242. if (/error|used/.test(icon)) {
  243. $btns.eq(0).text('尝试使用')
  244. } else if (/timeout|stop/.test(icon)) {
  245. $btns.eq(0).text('再次使用')
  246. } else if (/fail/.test(icon)) {
  247. $btns.eq(0).text('重选画风')
  248. }
  249. if (/used|timeout|stop|error/.test(icon)) {
  250. $btns.eq(1).text('刷新页面').attr('href', $btns.eq(1).data('href'))
  251. } else if (/fail/.test(icon)) {
  252. $btns.eq(1).text('重新拍照').attr('href', 'javascript:;')
  253. }
  254. },
  255. showActionTips: function (delay, done) {
  256. var $elem = $('.app-action-tips');
  257. var flag = flag || 1;
  258. delay = delay || 1000;
  259. app.showMask('white')
  260. $elem.find('.content').hide()
  261. $elem.find('.content.face-' + flag).show()
  262. $elem.animateCss('zoomIn', function () {
  263. var timer = setInterval(() => {
  264. if (++flag > 4) {
  265. $elem.removeClass('animated zoomIn');
  266. clearInterval(timer)
  267. if (!app.is_action) {
  268. app.hideMask('white')
  269. done && done()
  270. }
  271. return;
  272. }
  273. $elem.find('.content').hide()
  274. $elem.find('.content.face-' + flag).show()
  275. }, delay);
  276. })
  277. },
  278. showLoading: function (msg) {
  279. msg = msg || '相片正在生成中,请耐心等候'
  280. this.showMask();
  281. $('.app-action-tips .content').css('display', 'none');
  282. $('.app-action-tips .content.loading').css('display', 'block').find('.tips').html(msg);
  283. $('.app-action-tips').addClass('animated zoomIn') //.animateCss('zoomIn')
  284. },
  285. hideLoading: function () {
  286. this.hideMask();
  287. $('.app-action-tips .content.loading').css('display', 'none');
  288. $('.app-action-tips').removeClass('animated zoomIn')
  289. },
  290. showLoadingSwitch: function (done) {
  291. this.showMask();
  292. $('.app-action-tips .content').css('display', 'none');
  293. $('.app-action-tips .content.loading-switch').css('display', 'block')
  294. $('.app-action-tips .content.loading-switch .tips').html('正在切换'+(app.type==2?'风景':'画风'))
  295. $('.app-action-tips').animateCss('zoomIn', function ($elem, aname) {
  296. setTimeout(function () {
  297. app.hideMask();
  298. $elem.removeClass('animated ' + aname);
  299. done && done()
  300. }, 1000);
  301. })
  302. },
  303. renderPictures() {
  304. var html = [];
  305. console.log('====================================');
  306. console.log(this.imgList);
  307. console.log('====================================');
  308. this.imgList.forEach(function (item) {
  309. html.push('<div class="swiper-slide">');
  310. html.push(' <div class="img'+(item.vertical?' vertical':'')+'">');
  311. html.push(' <img src="' + item.url + '?v=' + Date.now() + '">');
  312. html.push(' </div>');
  313. html.push(' <div class="info">');
  314. html.push(' <div class="time">');
  315. html.push(' ' + item.datetime);
  316. html.push(' </div>');
  317. html.push(' <div class="style style-' + item.id + '"></div>');
  318. html.push(' </div>');
  319. html.push('</div>');
  320. })
  321. if (!html.length) {
  322. html.push('<div class="swiper-slide" style="color:#999;border-bottom:none">暂无相片</div>');
  323. }
  324. $('.pic-swiper .swiper-wrapper').html(html.join(''));
  325. $('.pic-swiper .swiper-wrapper img').click(function (e) {
  326. wx.previewImage({
  327. current: e.currentTarget.src.split('?v=')[0],
  328. urls: app.imgList.map(function (item) {
  329. return item.url
  330. })
  331. });
  332. });
  333. this.picSwiper();
  334. },
  335. showView: function (json) {
  336. this.hideLoading()
  337. setTimeout(function () {
  338. // app.showMask();
  339. // $('.app-view').animateCss('zoomIn', function () {
  340. // app.send('close')
  341. // }).find('.img').html('<img src="' + json.url + '?v=' + Date.now() + '">')
  342. var img = new Image()
  343. img.src = json.url + '?v=' + Date.now()
  344. img.onload = img.onerror = function(){
  345. app.showMask();
  346. var $view = $('.app-view')
  347. if(img.width<img.height){
  348. json.vertical = true
  349. $view.addClass('vertical')
  350. }
  351. app.imgList.unshift(json);
  352. $view.animateCss('zoomIn', function () {
  353. app.send('close')
  354. }).find('.img').html('<img src="' + json.url + '?v=' + Date.now() + '">')
  355. }
  356. }, 300);
  357. },
  358. hideView: function () {
  359. this.hideMask();
  360. $('.app-view').removeClass('animated zoomIn')
  361. },
  362. connection: function () {
  363. console.log('正在尝试连接')
  364. try {
  365. var timer
  366. this.websocket = new WebSocket(__config.ws);
  367. this.websocket.onclose = function (e) {
  368. app.is_connect = false
  369. app.checkTimeout(true)
  370. app.checkOperationTimeout(true)
  371. console.error('连接失败', e)
  372. setTimeout(function () {
  373. app.connection()
  374. }, 5000)
  375. }.bind(this);
  376. this.websocket.onopen = function (msg) {
  377. this.is_connect = true
  378. this.is_connect_ok = null
  379. this.send('request')
  380. }.bind(this);
  381. this.websocket.onmessage = function (msg) {
  382. console.log(msg.data)
  383. if (typeof msg.data == "string") {
  384. switch (msg.data) {
  385. case 'error 101':
  386. console.log('发送的消息有误');
  387. break;
  388. case 'error 102':
  389. //pop_tip('连接打印机失败<br/>请重新刷新页面');
  390. break;
  391. case 'error 302':
  392. app.hideLoading()
  393. app.showAlert('网络异常,生成失败', 'fail')
  394. break;
  395. case 'error 404':
  396. app.switchId = 0;
  397. app.showAlert('切换图片失败', 'error');
  398. break;
  399. case 'error 500':
  400. app.is_connect_ok = false
  401. if (app.is_send_msg) {
  402. app.hideLoading()
  403. app.showAlert('网络异常,请稍后再试', 'error');
  404. }
  405. break;
  406. case 'error 600':
  407. //console.log('未定义的消息');
  408. break;
  409. case 'error 707':
  410. app.showAlert('二维码不正确', 'error')
  411. break;
  412. case 'error 808':
  413. app.is_lose_used = true
  414. if (app.is_send_msg) {
  415. app.showAlert('当前画屏正在被使用,请稍后', 'used');
  416. }
  417. break;
  418. case 'error 202':
  419. break;
  420. case 'error 909':
  421. app.is_other_used = true;
  422. if (app.is_send_msg) {
  423. app.showAlert('当前画屏正在被使用,请稍后', 'used');
  424. }
  425. break;
  426. case 'ok 200':
  427. console.log('切换图片成功')
  428. break;
  429. case 'ok 201':
  430. console.log('操作成功')
  431. app.is_connect_ok = null
  432. app.checkTimeout(true)
  433. app.checkOperationTimeout(true)
  434. break;
  435. case 'ok 300':
  436. app.hideLoading();
  437. app.is_lose_used = false;
  438. app.is_other_used = false;
  439. app.is_connect_ok = true;
  440. app.checkTimeout()
  441. app.checkOperationTimeout()
  442. break;
  443. case 'ok 301':
  444. console.log('拍照成功,开始等待照片')
  445. app.is_action = false;
  446. app.checkTimeout(true)
  447. app.checkOperationTimeout(true)
  448. app.showActionTips(1000, function () {
  449. app.showLoading()
  450. });
  451. timer = setTimeout(function () {
  452. app.send('close')
  453. setTimeout(() => {
  454. app.hideLoading()
  455. app.showAlert('网络异常,生成失败', 'fail')
  456. }, 500);
  457. }, 20000);
  458. break;
  459. case 'ok 1000':
  460. app.is_lose_used = false;
  461. app.is_other_used = false;
  462. break;
  463. default:
  464. try {
  465. app.is_action = true;
  466. timer && clearTimeout(timer)
  467. var json = JSON.parse(msg.data)
  468. if (json.url) {
  469. app.showView(json)
  470. }
  471. } catch (error) {
  472. console.error(error)
  473. }
  474. break;
  475. }
  476. app.is_send_msg = false
  477. } else {
  478. console.log("error 600");
  479. }
  480. }.bind(this);
  481. } catch (error) {
  482. console.error(error)
  483. setTimeout(function () {
  484. app.connection()
  485. }, 5000)
  486. }
  487. },
  488. send: function (msg) {
  489. if (msg === 'request') {
  490. this.websocket.send(msg + ' ' + this.id)
  491. } else if (msg === 'close') {
  492. this.websocket.send(msg + ' 0')
  493. } else {
  494. if (app.is_connect_ok == null && !app.is_other_used) {
  495. app.websocket.send('request ' + this.id)
  496. }
  497. setTimeout(function () {
  498. app.websocket.send(msg)
  499. }, 300);
  500. }
  501. },
  502. sendCheck() {
  503. if (!this.is_connect) {
  504. app.showAlert('网络异常,请稍后再试', 'error');
  505. return false;
  506. } else if (this.is_other_used) {
  507. app.showAlert('当前画屏正在被使用,请稍后', 'used');
  508. return false;
  509. } else if (this.is_lose_used) {
  510. app.showAlert('当前画屏正在被使用,请稍后', 'used');
  511. return false;
  512. } else if (this.is_connect_ok === false) {
  513. app.showAlert('网络异常,请稍后再试', 'error');
  514. return false;
  515. }
  516. this.is_send_msg = true;
  517. return true;
  518. },
  519. checkOperationTimeout(stop) {
  520. this.operationTimer && clearTimeout(this.operationTimer)
  521. if (stop) {
  522. // 暂停检测
  523. return
  524. }
  525. this.operationTimer = setTimeout(function () {
  526. if (app.is_connect_ok) {
  527. app.send('close')
  528. app.showAlert('因您长时间未使用<br>已无法操作艺术照一体机', 'timeout')
  529. }
  530. }, (__config.noOperationTimeout || 30) * 1000);
  531. },
  532. checkTimeout(stop) {
  533. this.timeoutTimer && clearTimeout(this.timeoutTimer)
  534. if (stop) {
  535. // 暂停检测
  536. return
  537. }
  538. // 强制超时结束控制 180s
  539. this.timeoutTimer = setTimeout(function () {
  540. if (app.is_connect_ok) {
  541. app.send('close')
  542. app.showAlert('抱歉,因您长时间使用<br>让下一位小伙伴体验吧', 'timeout')
  543. }
  544. }, (__config.timeout || 180) * 1000);
  545. },
  546. render: function () {
  547. this.connection()
  548. this.events()
  549. this.adsSwiper()
  550. this.stlSwiper()
  551. this.picSwiper()
  552. // this.imgList.push({
  553. // "id": 7,
  554. // "url": "http://ost.4dage.com/4dart/img/31913592.jpg",
  555. // "datetime": "2018年10月29日 16:27:26"
  556. // })
  557. }
  558. }
  559. app.render()
  560. window.onbeforeunload = function () {
  561. try {
  562. app.websocket.close();
  563. app.websocket = null;
  564. } catch (ex) {}
  565. };
  566. //app.showAlert('抱歉,因您长时间使用<br>让下一位小伙伴体验吧', 'timeout')
  567. //app.showAlert('因您长时间未使用<br>已无法操作艺术照一体机', 'stop')
  568. //app.showAlert('当前画屏正在被使用,请稍后', 'used')
  569. //app.showAlert('网络异常,请稍后再试', 'error')
  570. //app.showAlert('网络异常,生成失败', 'fail')
  571. //app.showAlert('您已丢失使用权', 'used');