websocket.js 20 KB

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