websocket.js 19 KB

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