app.js 22 KB

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