app.js 19 KB

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