app.js 22 KB

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