app.js 20 KB

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