websocket.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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('t'),
  33. /**
  34. * 切换风格id
  35. */
  36. switchId: 1,
  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. $('.photo').on("click",function () {
  70. app.checkOperationTimeout()
  71. if (!app.sendCheck()) {
  72. return;
  73. }
  74. self.send('readyphoto 1')
  75. })
  76. $('.app-alert .btn-use').on('click', function () {
  77. var $alert = $('.app-alert')
  78. app.hideMask()
  79. $alert.removeClass($alert.data('class') + ' animated zoomIn')
  80. var text = $(this).text();
  81. if (text == '尝试使用' || text == '再次使用') {
  82. app.send('request')
  83. }
  84. })
  85. $('.app-alert .btn-link').on('click', function () {
  86. if ($(this).text() == '重新拍照') {
  87. var $alert = $('.app-alert')
  88. app.hideMask()
  89. $alert.removeClass($alert.data('class') + ' animated zoomIn')
  90. $('.btn-pic-take').click()
  91. }
  92. })
  93. },
  94. /**
  95. * 检验时间戳
  96. */
  97. checkTimestamp(){
  98. let del = (new Date()).getTime() - this.timestamp
  99. console.log(del>ONEDAY);
  100. if (del>ONEDAY) {
  101. return false
  102. }
  103. return true
  104. },
  105. showMask: function (clazz) {
  106. var $elem = $('.app-mask');
  107. if (clazz) {
  108. $elem.addClass(this.showMask._clazz = clazz)
  109. }
  110. $elem.css('display', 'block')
  111. },
  112. hideMask: function () {
  113. var $elem = $('.app-mask');
  114. if (this.showMask._clazz) {
  115. $elem.removeClass(this.showMask._clazz)
  116. this.showMask._clazz = null
  117. }
  118. $elem.css('display', 'none')
  119. },
  120. showAlert: function (msg, icon) {
  121. this.showMask();
  122. var $alert = $('.app-alert');
  123. var $btns = $alert.find('.btns a');
  124. $alert.data('class', icon).addClass(icon).animateCss('zoomIn')
  125. $alert.find('.tips').html(msg)
  126. if (/error|used/.test(icon)) {
  127. $btns.eq(0).text('尝试使用')
  128. } else if (/timeout|stop/.test(icon)) {
  129. $btns.eq(0).text('再次使用')
  130. } else if (/fail/.test(icon)) {
  131. $btns.eq(0).text('重选画风')
  132. }
  133. if (/used|timeout|stop|error/.test(icon)) {
  134. $btns.eq(1).text('试试在线版').attr('href', $btns.eq(1).data('href'))
  135. } else if (/fail/.test(icon)) {
  136. $btns.eq(1).text('重新拍照').attr('href', 'javascript:;')
  137. }
  138. },
  139. showActionTips: function (delay, done) {
  140. var $elem = $('.app-action-tips');
  141. var flag = flag || 1;
  142. delay = delay || 1000;
  143. app.showMask('white')
  144. $elem.find('.content').hide()
  145. $elem.find('.content.face-' + flag).show()
  146. $elem.animateCss('zoomIn', function () {
  147. var timer = setInterval(() => {
  148. if (++flag > 4) {
  149. $elem.removeClass('animated zoomIn');
  150. clearInterval(timer)
  151. if (!app.is_action) {
  152. app.hideMask('white')
  153. done && done()
  154. }
  155. return;
  156. }
  157. $elem.find('.content').hide()
  158. $elem.find('.content.face-' + flag).show()
  159. }, delay);
  160. })
  161. },
  162. showLoading: function (msg) {
  163. msg = msg || '相片正在生成中,请耐心等候'
  164. this.showMask();
  165. $('.app-action-tips .content').css('display', 'none');
  166. $('.app-action-tips .content.loading').css('display', 'block').find('.tips').html(msg);
  167. $('.app-action-tips').addClass('animated zoomIn') //.animateCss('zoomIn')
  168. },
  169. hideLoading: function () {
  170. this.hideMask();
  171. $('.app-action-tips .content.loading').css('display', 'none');
  172. $('.app-action-tips').removeClass('animated zoomIn')
  173. },
  174. showLoadingSwitch: function (done) {
  175. this.showMask();
  176. $('.app-action-tips .content').css('display', 'none');
  177. $('.app-action-tips .content.loading-switch').css('display', 'block')
  178. $('.app-action-tips').animateCss('zoomIn', function ($elem, aname) {
  179. setTimeout(function () {
  180. app.hideMask();
  181. $elem.removeClass('animated ' + aname);
  182. done && done()
  183. }, 1000);
  184. })
  185. },
  186. renderPictures() {
  187. var html = [];
  188. this.imgList.forEach(function (item) {
  189. html.push('<div class="swiper-slide">');
  190. html.push(' <div class="img'+(item.vertical?' vertical':'')+'">');
  191. html.push(' <img src="' + item.url + '?v=' + Date.now() + '">');
  192. html.push(' </div>');
  193. html.push(' <div class="info">');
  194. html.push(' <div class="time">');
  195. html.push(' ' + item.datetime);
  196. html.push(' </div>');
  197. html.push(' <div class="style style-' + item.id + '"></div>');
  198. html.push(' </div>');
  199. html.push('</div>');
  200. })
  201. if (!html.length) {
  202. html.push('<div class="swiper-slide" style="color:#999;border-bottom:none">暂无相片</div>');
  203. }
  204. $('.pic-swiper .swiper-wrapper').html(html.join(''));
  205. $('.pic-swiper .swiper-wrapper img').click(function () {
  206. wx.previewImage({
  207. current: this.src,
  208. urls: app.imgList.map(function (item) {
  209. return item.url
  210. })
  211. });
  212. });
  213. this.picSwiper();
  214. },
  215. showView: function (json) {
  216. this.hideLoading()
  217. setTimeout(function () {
  218. // app.showMask();
  219. // $('.app-view').animateCss('zoomIn', function () {
  220. // app.send('close')
  221. // }).find('.img').html('<img src="' + json.url + '?v=' + Date.now() + '">')
  222. var img = new Image()
  223. img.src = json.url + '?v=' + Date.now()
  224. img.onload = img.onerror = function(){
  225. app.showMask();
  226. var $view = $('.app-view')
  227. if(img.width<img.height){
  228. json.vertical = true
  229. $view.addClass('vertical')
  230. }
  231. app.imgList.unshift(json);
  232. $view.animateCss('zoomIn', function () {
  233. app.send('close')
  234. }).find('.img').html('<img src="' + json.url + '?v=' + Date.now() + '">')
  235. }
  236. }, 300);
  237. },
  238. hideView: function () {
  239. this.hideMask();
  240. $('.app-view').removeClass('animated zoomIn')
  241. },
  242. connection: function () {
  243. if (!this.checkTimestamp()) {
  244. return app.showAlert('二维码超时不可用', 'fail')
  245. }
  246. try {
  247. var timer
  248. this.websocket = new WebSocket(__config.ws);
  249. this.websocket.onclose = function (e) {
  250. app.is_connect = false
  251. app.checkTimeout(true)
  252. app.checkOperationTimeout(true)
  253. console.error('连接失败', e)
  254. setTimeout(function () {
  255. app.connection()
  256. }, 5000)
  257. }.bind(this);
  258. this.websocket.onopen = function (msg) {
  259. this.is_connect = true
  260. this.is_connect_ok = null
  261. this.send('request')
  262. }.bind(this);
  263. this.websocket.onmessage = function (msg) {
  264. console.log(msg.data)
  265. if (typeof msg.data == "string") {
  266. switch (msg.data) {
  267. case 'error 101':
  268. console.log('发送的消息有误');
  269. break;
  270. case 'error 102':
  271. //pop_tip('连接打印机失败<br/>请重新刷新页面');
  272. break;
  273. case 'error 302':
  274. app.hideLoading()
  275. app.showAlert('网络异常,生成失败', 'fail')
  276. break;
  277. case 'error 404':
  278. app.switchId = 0;
  279. app.showAlert('切换图片失败', 'error');
  280. break;
  281. case 'error 500':
  282. app.is_connect_ok = false
  283. if (app.is_send_msg) {
  284. app.hideLoading()
  285. app.showAlert('网络异常,请稍后再试', 'error');
  286. }
  287. break;
  288. case 'error 600':
  289. //console.log('未定义的消息');
  290. break;
  291. case 'error 707':
  292. app.showAlert('二维码不正确', 'error')
  293. break;
  294. case 'error 808':
  295. app.is_lose_used = true
  296. if (app.is_send_msg) {
  297. app.showAlert('当前设备正在使用中,请稍后', 'used');
  298. }
  299. break;
  300. case 'error 202':
  301. break;
  302. case 'error 909':
  303. app.is_other_used = true;
  304. if (app.is_send_msg) {
  305. app.showAlert('当前设备正在使用中,请稍后', 'used');
  306. }
  307. break;
  308. case 'ok 200':
  309. console.log('切换图片成功')
  310. break;
  311. case 'ok 201':
  312. console.log('操作成功')
  313. app.is_connect_ok = null
  314. app.checkTimeout(true)
  315. app.checkOperationTimeout(true)
  316. break;
  317. case 'ok 300':
  318. app.hideLoading();
  319. app.is_lose_used = false;
  320. app.is_other_used = false;
  321. app.is_connect_ok = true;
  322. app.checkTimeout()
  323. app.checkOperationTimeout()
  324. break;
  325. case 'ok 301':
  326. console.log('拍照成功,开始等待照片')
  327. app.is_action = false;
  328. app.checkTimeout(true)
  329. app.checkOperationTimeout(true)
  330. app.showActionTips(1000, function () {
  331. app.showLoading()
  332. });
  333. timer = setTimeout(function () {
  334. app.send('close')
  335. setTimeout(() => {
  336. app.hideLoading()
  337. app.showAlert('网络异常,生成失败', 'fail')
  338. }, 500);
  339. }, 20000);
  340. break;
  341. case 'ok 1000':
  342. app.is_lose_used = false;
  343. app.is_other_used = false;
  344. break;
  345. default:
  346. try {
  347. app.is_action = true;
  348. timer && clearTimeout(timer)
  349. var json = JSON.parse(msg.data)
  350. if (json.url) {
  351. app.showView(json)
  352. }
  353. } catch (error) {
  354. console.error(error)
  355. }
  356. break;
  357. }
  358. app.is_send_msg = false
  359. } else {
  360. console.log("error 600");
  361. }
  362. }.bind(this);
  363. } catch (error) {
  364. console.error(error)
  365. setTimeout(function () {
  366. app.connection()
  367. }, 5000)
  368. }
  369. },
  370. send: function (msg) {
  371. if (msg === 'request') {
  372. this.websocket.send(msg + ' ' + this.id)
  373. } else if (msg === 'close') {
  374. this.websocket.send(msg + ' 0')
  375. } else {
  376. app.websocket.send(msg)
  377. }
  378. },
  379. sendCheck() {
  380. if (!this.is_connect) {
  381. app.showAlert('网络异常,请稍后再试', 'error');
  382. return false;
  383. } else if (this.is_other_used) {
  384. app.showAlert('当前设备正在使用中,请稍后', 'used');
  385. return false;
  386. } else if (this.is_lose_used) {
  387. app.showAlert('当前设备正在使用中,请稍后', 'used');
  388. return false;
  389. } else if (this.is_connect_ok === false) {
  390. app.showAlert('网络异常,请稍后再试', 'error');
  391. return false;
  392. }
  393. this.is_send_msg = true;
  394. return true;
  395. },
  396. checkOperationTimeout(stop) {
  397. this.operationTimer && clearTimeout(this.operationTimer)
  398. if (stop) {
  399. // 暂停检测
  400. return
  401. }
  402. this.operationTimer = setTimeout(function () {
  403. if (app.is_connect_ok) {
  404. app.send('close')
  405. app.showAlert('因您长时间未使用<br>已无法操作艺术照一体机', 'timeout')
  406. }
  407. }, (__config.noOperationTimeout || 30) * 1000);
  408. },
  409. checkTimeout(stop) {
  410. this.timeoutTimer && clearTimeout(this.timeoutTimer)
  411. if (stop) {
  412. // 暂停检测
  413. return
  414. }
  415. // 强制超时结束控制 180s
  416. this.timeoutTimer = setTimeout(function () {
  417. if (app.is_connect_ok) {
  418. app.send('close')
  419. app.showAlert('抱歉,因您长时间使用<br>让下一位小伙伴体验吧', 'timeout')
  420. }
  421. }, (__config.timeout || 180) * 1000);
  422. },
  423. render: function () {
  424. this.connection()
  425. this.events()
  426. // this.imgList.push({
  427. // "id": 7,
  428. // "url": "http://ost.4dage.com/4dart/img/31913592.jpg",
  429. // "datetime": "2018年10月29日 16:27:26"
  430. // })
  431. }
  432. }
  433. app.render()
  434. window.onbeforeunload = function () {
  435. try {
  436. app.websocket.close();
  437. app.websocket = null;
  438. } catch (ex) {}
  439. };