WXdraw.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. let noOperationSetTimeoutTime = null
  2. let noOperationSetIntervalTime = null
  3. let isFiringNoOperationSetTimeout = false
  4. // 没有任何触碰时会触发该定时器
  5. function noOperationSetTimeout() {
  6. clearTimeout(noOperationSetTimeoutTime)
  7. clearTimeout(noOperationSetIntervalTime)
  8. noOperationSetTimeoutTime = setTimeout(() => {
  9. let index = 10
  10. $.modal({
  11. text: "由于您长时间未操作,将自动断开视频链接(不影响云手机内应用运行)",
  12. buttons: [
  13. { text: "继续操作", className: "default", onClick: function () { noOperationSetTimeout() } },
  14. { text: "退出(10秒)", className: 'operate-time-exit', onClick: function () { quit() } },
  15. ]
  16. });
  17. noOperationSetIntervalTime = setInterval(() => {
  18. if (index <= 0) {
  19. $('.operate-time-exit').text(`退出`)
  20. clearInterval(noOperationSetIntervalTime)
  21. quit()
  22. $.closeModal();
  23. return
  24. }
  25. index--
  26. $('.operate-time-exit').text(`退出(${index}秒)`)
  27. }, 1000)
  28. }, 300000)
  29. }
  30. // 蒙版
  31. var canvas_bak = document.getElementById('box');
  32. var winHeight = window.screen.height - window.innerHeight;
  33. var vowidth = window.screen.width;
  34. var topwinHeightDraw = window.screen.height - window.innerHeight + 30; //计算title top 头部
  35. var numse = window.screen.height; // -winHeight
  36. //计算title top 头部
  37. if (numse <= 70) {
  38. var voheight = window.screen.height - winHeight - 34 - 20;
  39. } else {
  40. var voheight = window.screen.height - topwinHeightDraw - 20;
  41. }
  42. //画笔大小
  43. var resolving = 1; // 1: 竖屏;2:横屏;
  44. var url = window.location.href;
  45. url = url.split('/');
  46. var parameters = GetRequest();
  47. var videoWidth, videoHeight;
  48. var isControl = false; // 是否是观看模式
  49. changIsControl(true);
  50. var isAuth = parameters['authPhone']; // 是否是获取的云手机
  51. var wsss;
  52. var errorTime = 0;
  53. var first = true;
  54. let intervalerPing = null
  55. function changIsControl(value) {
  56. isControl = value;
  57. // if(){}
  58. $('#open-set-phone-size-dialog-btn').attr('hidden', !value);
  59. }
  60. let directivesTimeInterval = 1
  61. // function throttle(fn, delay) {
  62. // var flag = true;
  63. // return () => {
  64. // if (!flag) return;
  65. // flag = false;
  66. // errorTime += delay;
  67. // timer = setTimeout(() => {
  68. // fn();
  69. // flag = true;
  70. // }, delay);
  71. // };
  72. // }
  73. // const throttleDoConnectDirectives = throttle(() => {
  74. // doConnectDirectives();
  75. // }, 100);
  76. function doConnectDirectives() {
  77. videoWidth = Number(resolvingPower) ? Number(resolvingPower) : 720;
  78. videoHeight = videoWidth === 720 ? 1280 : 1920;
  79. wsss = new WebSocket(cUrl);
  80. wsss.binaryType = 'arraybuffer';
  81. wsss.onopen = function () {
  82. $.ajax({
  83. url: baseUrl + '/api/public/v5/pushflow/popup',
  84. headers: {
  85. Authorization: token,
  86. versionName: '5.9.0'
  87. },
  88. type: 'get',
  89. dataType: 'json',
  90. success: function (res) {
  91. let { data } = res
  92. if (data) {
  93. isFiringNoOperationSetTimeout = data
  94. noOperationSetTimeout()
  95. }
  96. },
  97. });
  98. clearInterval(intervalerPing)
  99. // 获取虚拟场景状态
  100. intervalerPing = setInterval(() => {
  101. if (wsss.readyState === 1) {
  102. wsss.send(JSON.stringify({type: 'ping'}));
  103. } else {
  104. clearInterval(intervalerPing);
  105. }
  106. }, 3000)
  107. errorTime = 0;
  108. var pings = { type: 'getVsStatus' };
  109. wsss.send(JSON.stringify(pings));
  110. var bitRate = {
  111. data: {
  112. bitRate: 1243000,
  113. },
  114. type: 'bitRate',
  115. };
  116. wsss.send(JSON.stringify(bitRate));
  117. // 键盘透传 2 是用云机内置的
  118. wsss.send(JSON.stringify({ data: { type: 2 }, type: 'InputMethod' }));
  119. // // 进入发起询问
  120. // var pings2 = {
  121. // type: 'forwardMsg',
  122. // data: {
  123. // code: '3000',
  124. // userName: username,
  125. // desc: '询问是否有在控制', // 可选
  126. // },
  127. // };
  128. // wsss.send(JSON.stringify(pings2));
  129. wsss.send(
  130. JSON.stringify({
  131. type: 'getPhoneSize',
  132. }),
  133. );
  134. };
  135. wsss.onerror = function (e) {
  136. clearInterval(intervalerPing)
  137. if (!quitBool) return
  138. if (directivesTimeInterval > 7) {
  139. quit();
  140. return
  141. }
  142. // console.log('🚀 ~ file: WXdraw.js ~ line 82 ~ onerror ~ e', e);
  143. // wsss.close(1006);
  144. // throttle(doConnectDirectives, 100);
  145. // if (errorTime > 1000) {
  146. // quit();
  147. // }
  148. directivesTimeInterval++
  149. // 异常关闭,重连
  150. doConnectDirectives();
  151. // doConnectDirectives();
  152. // throttle(doConnectDirectives, 100);
  153. };
  154. wsss.onclose = function (e) {
  155. // console.log('🚀 ~ file: WXdraw.js ~ line 93 ~ onclose ~ e', e);
  156. // new WebSocket(e.)
  157. // doConnectDirectives();
  158. // if (e.code === 1006) {
  159. // // 异常关闭,重连
  160. // throttleDoConnectDirectives();
  161. // // doConnectDirectives();
  162. // // throttle(doConnectDirectives, 100);
  163. // if (errorTime > 1000) {
  164. // quit();
  165. // }
  166. // }
  167. };
  168. wsss.onmessage = function (res) {
  169. var result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
  170. console.log(
  171. '🚀 ~ file: WXdraw.js ~ line 78 ~ doConnectDirectives ~ result',
  172. result,
  173. );
  174. // 接受到这个消息就自动退出云机
  175. if(result.type === 'exitPhone') {
  176. quit()
  177. return
  178. }
  179. if (result.type === 'cutting') {
  180. if (result.data.status === 0) {
  181. $.toast('复制成功', 'text');
  182. } else {
  183. $.toast(result.msg, 'text');
  184. }
  185. return;
  186. }
  187. // if (result.type === 'forwardMsgRep') {
  188. // // 当前云机无其他终端在线,获得控制权
  189. // changIsControl(true);
  190. // }
  191. if (result.type === 'downAdnInstallRep') {
  192. $.toast(result.data.msg, 'text', 4000);
  193. }
  194. // if (result.type === 'forwardMsg') {
  195. // /**
  196. // * @type {boolean} isControl 当前是否拥有控制权,初始化时为false
  197. // * @type {string} isAuth 当前云机类型 - huo: 获取的云机,none: 自己的云机
  198. // * @type {string} username 当前登录的双子星账号username
  199. // */
  200. // if (result.data.userName !== username) {
  201. // switch (String(result.data.code)) {
  202. // case '3000': {
  203. // if (isControl) {
  204. // // 回复有控制
  205. // wsss.send(
  206. // JSON.stringify({
  207. // type: 'forwardMsg',
  208. // data: {
  209. // code: '4000',
  210. // userName: username,
  211. // desc: '有控制', // 可选
  212. // },
  213. // }),
  214. // );
  215. // return;
  216. // }
  217. // // 回复有观看
  218. // wsss.send(
  219. // JSON.stringify({
  220. // type: 'forwardMsg',
  221. // data: {
  222. // code: '4100',
  223. // userName: username,
  224. // desc: '有观看', // 可选
  225. // },
  226. // }),
  227. // );
  228. // return;
  229. // }
  230. // case '4000': {
  231. // // console.log(
  232. // // '🚀 ~ file: WXdraw.js ~ line 184 ~ doConnectDirectives ~ isAuth',
  233. // // isAuth,
  234. // // );
  235. // // 当前是获取方
  236. // if (isAuth === 'huo' && isControl) {
  237. // $.confirm(
  238. // '授权方已收回控制权,您进入观看屏幕模式',
  239. // function () {
  240. // //点击确认后的回调函数
  241. // changIsControl(false);
  242. // },
  243. // function () {
  244. // changIsControl(false);
  245. // //点击取消后的回调函数
  246. // quit();
  247. // },
  248. // );
  249. // return;
  250. // }
  251. // if (isAuth === 'shou') {
  252. // // 当前是授权方切没有控制权
  253. // $.confirm(
  254. // '当前云手机正在授控,是否请求获取云手机控制权?',
  255. // function () {
  256. // //点击确认后的回调函数
  257. // wsss.send(
  258. // JSON.stringify({
  259. // type: 'forwardMsg',
  260. // data: {
  261. // code: '5000',
  262. // userName: username,
  263. // desc: '控制权限收回', // 可选
  264. // },
  265. // }),
  266. // );
  267. // changIsControl(true);
  268. // },
  269. // function () {
  270. // //点击取消后的回调函数
  271. // changIsControl(false);
  272. // },
  273. // );
  274. // return;
  275. // }
  276. // }
  277. // case '5000': {
  278. // // if (result.data.username === username) {
  279. // // changIsControl(true);
  280. // // return;
  281. // // }
  282. // if (isAuth === 'huo' && isControl) {
  283. // $.confirm(
  284. // '授权方已收回控制权,您进入观看屏幕模式',
  285. // function () {
  286. // //点击确认后的回调函数
  287. // changIsControl(false);
  288. // },
  289. // function () {
  290. // changIsControl(false);
  291. // //点击取消后的回调函数
  292. // quit();
  293. // },
  294. // );
  295. // return;
  296. // }
  297. // return;
  298. // }
  299. // default: {
  300. // return;
  301. // }
  302. // }
  303. // }
  304. // return;
  305. // }
  306. if (result.type === 'payInitiateEvent') {
  307. // var url = window.location.href;
  308. // url = url.split('/');
  309. // var baseUrl = url[0] + '//' + url[2];
  310. $.ajax({
  311. url: baseUrl + '/api/pay/third/order',
  312. data: JSON.stringify({
  313. orderNum: result.data.orderNum,
  314. orderAmount: result.data.orderAmount,
  315. appKey: result.data.appKey,
  316. }),
  317. type: 'POST',
  318. dataType: 'json',
  319. contentType: 'application/json;charset=UTF-8',
  320. success: function (res) {
  321. if (result.data.payType === 1) {
  322. // 微信
  323. if (window.__wxjs_environment === 'miniprogram') {
  324. // 小程序
  325. // copyUrl(result.data.payUrl);
  326. } else {
  327. window.location.href = result.data.payUrl;
  328. }
  329. } else {
  330. window.location.href = result.data.payUrl;
  331. }
  332. },
  333. });
  334. return;
  335. }
  336. if (result.type === 'getPhoneSize' || result.type === 'setPhoneSize') {
  337. // console.log(result);
  338. if (
  339. window.currentPhoneSize &&
  340. (window.currentPhoneSize.width !==
  341. Math.min(result.data.width, result.data.height) ||
  342. window.currentPhoneSize.height !==
  343. Math.max(result.data.width, result.data.height) ||
  344. window.currentPhoneSize.dpi !== result.data.dpi)
  345. ) {
  346. // 获取到的分辨率与当前分辨率不符
  347. const data = window.phoneSizeList.find(function (v) {
  348. return (
  349. v.width === Math.min(result.data.width, result.data.height) &&
  350. v.height === Math.max(result.data.width, result.data.height) &&
  351. v.dpi === result.data.dpi
  352. );
  353. });
  354. window.currentPhoneSize = data || {
  355. width: Math.min(result.data.width, result.data.height),
  356. height: Math.max(result.data.width, result.data.height),
  357. dpi: result.data.dpi,
  358. };
  359. if (result.type === 'setPhoneSize') {
  360. lastSetPhone = Date.now();
  361. }
  362. // if (result.type === 'getPhoneSize') {
  363. // 上报给后端
  364. $.ajax({
  365. url:
  366. baseUrl +
  367. '/api/resources/v5/machine/resolution/operationResolvingPower',
  368. headers: {
  369. Authorization: token,
  370. },
  371. type: 'post',
  372. dataType: 'json',
  373. contentType: 'application/json; charset=UTF-8',
  374. data: JSON.stringify({
  375. userCardId: window.userCardId,
  376. resolvingPowerId: window.currentPhoneSize.id,
  377. }),
  378. });
  379. }
  380. // }
  381. updateDB(db, storeName, {
  382. id: userCardId,
  383. socketURL: socketURL,
  384. cUrl: cUrl,
  385. cardToken: cardToken,
  386. resolvingPower: resolvingPower,
  387. width: window.currentPhoneSize.width,
  388. height: window.currentPhoneSize.height,
  389. dpi: window.currentPhoneSize.dpi,
  390. });
  391. return;
  392. }
  393. if (result.type === 'reProduceText') {
  394. // 接收到云机剪贴板复制事件
  395. // window.copyToClipboard(result.data.text);
  396. if (navigator.clipboard) {
  397. navigator.clipboard.writeText(result.data.text);
  398. }
  399. }
  400. };
  401. }
  402. draw_graph('pencil', $('body'));
  403. //剪切板
  404. $('.upload').on('click', function () {
  405. var texts = $(this).attr('data-text');
  406. if (texts == 'uploads') {
  407. $('.mainbox').css({
  408. display: 'block',
  409. });
  410. $('.sbox').css({
  411. display: 'none',
  412. });
  413. }
  414. });
  415. //home 控制home
  416. $('.botmat1img').on('click', function () {
  417. var codes = $(this).attr('data-text');
  418. if (codes == 'home' && isControl) {
  419. wsss.send(ExexuteKeyBoard(3));
  420. } else if (codes == 'return' && isControl) {
  421. wsss.send(ExexuteKeyBoard(4));
  422. } else if (codes == 'gengduo' && isControl) {
  423. wsss.send(ExexuteKeyBoard(187));
  424. }
  425. });
  426. // 高清控制
  427. $('.PictureQuality').on('click', function () {
  428. if (!isControl) {
  429. return;
  430. }
  431. $(this).addClass('avit').siblings().removeClass('avit');
  432. var id = $(this).attr('data-id');
  433. var cmd = {
  434. type: 'switchSharpness',
  435. };
  436. decodeWoker.postMessage(cmd); //通知解码器worker切换分辨率
  437. var buffer = makeSharpness(Number(id));
  438. webSocketWorker.postMessage(buffer);
  439. $('.control-right-img').attr({
  440. 'data-id': '1',
  441. });
  442. $('.leftmains').css({
  443. right: '-4rem',
  444. });
  445. });
  446. var canDraw = false;
  447. //画图形
  448. function draw_graph(graphType) {
  449. //把蒙版放于画板上面
  450. $('#container').css('z-index', 30);
  451. $('#dedit').css('z-index', 20);
  452. // 先画在蒙版上 再复制到画布上
  453. //鼠标按下获取 开始xy开始画图
  454. // var ongoingTouches = [];
  455. var touchstart = function (e) {
  456. if (isFiringNoOperationSetTimeout) noOperationSetTimeout()
  457. // console.log('🚀 ~ file: WXdraw.js ~ line 244 ~ touchstart ~ e', e);
  458. if (!isControl) {
  459. return;
  460. }
  461. const action = 0;
  462. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  463. const x = item.clientX - item.target.getBoundingClientRect().x;
  464. const y = item.clientY - item.target.getBoundingClientRect().y;
  465. return wsss.send(
  466. JSON.stringify({
  467. type: 'event',
  468. data: {
  469. action,
  470. count: e.originalEvent.touches.length,
  471. pointerId: item.identifier,
  472. x: (function () {
  473. return (
  474. resolving
  475. ? x * (window.currentPhoneSize.width / vowidth)
  476. : y * (window.currentPhoneSize.height / voheight)
  477. ).toFixed(2);
  478. })(),
  479. y: (function () {
  480. return (
  481. resolving
  482. ? y * (window.currentPhoneSize.height / voheight)
  483. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  484. ).toFixed(2);
  485. })(),
  486. },
  487. }),
  488. );
  489. });
  490. canDraw = true;
  491. };
  492. //鼠标离开 把蒙版canvas的图片生成到canvas中
  493. var touchend = function (e) {
  494. if (!isControl) {
  495. return;
  496. }
  497. if (isFiringNoOperationSetTimeout) noOperationSetTimeout()
  498. const action = 1;
  499. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  500. const x = item.clientX - item.target.getBoundingClientRect().x;
  501. const y = item.clientY - item.target.getBoundingClientRect().y;
  502. return wsss.send(
  503. JSON.stringify({
  504. type: 'event',
  505. data: {
  506. action,
  507. count: e.originalEvent.touches.length,
  508. pointerId: item.identifier,
  509. x: (function () {
  510. return (
  511. resolving
  512. ? x * (window.currentPhoneSize.width / vowidth)
  513. : y * (window.currentPhoneSize.height / voheight)
  514. ).toFixed(2);
  515. })(),
  516. y: (function () {
  517. return (
  518. resolving
  519. ? y * (window.currentPhoneSize.height / voheight)
  520. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  521. ).toFixed(2);
  522. })(),
  523. },
  524. }),
  525. );
  526. });
  527. // var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  528. // //是否横屏
  529. // for (var i = 0; i < touchfor.length; i++) {
  530. // // var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  531. // // var acrossHeightY =
  532. // // videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  533. // // if (resolving) {
  534. // // var verticalWidthX =
  535. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  536. // // var verticalHeightY =
  537. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  538. // // } else {
  539. // // var verticalWidthX =
  540. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  541. // // var verticalHeightY =
  542. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  543. // // }
  544. // var ping =
  545. // // resolving == 0 ?
  546. // // { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  547. // {
  548. // data: {
  549. // action: 1,
  550. // count: ongoingTouches.length,
  551. // pointerId: touchfor[i].identifier,
  552. // x: (() =>
  553. // (resolving
  554. // ? touchfor[i].pageX * (window.currentPhoneSize.width / vowidth)
  555. // : touchfor[i].pageY * (window.currentPhoneSize.width / voheight)
  556. // ).toFixed(2))(),
  557. // y: (() =>
  558. // (resolving
  559. // ? touchfor[i].pageY *
  560. // (window.currentPhoneSize.height / voheight)
  561. // : (vowidth - touchfor[i].pageX) *
  562. // (window.currentPhoneSize.height / vowidth)
  563. // ).toFixed(2))(),
  564. // },
  565. // type: 'event',
  566. // };
  567. // wsss.send(JSON.stringify(ping));
  568. // ongoingTouches.forEach(function (item, index) {
  569. // if (item.identifier === touchfor[i].identifier) {
  570. // ongoingTouches.splice(index, 1);
  571. // }
  572. // });
  573. // }
  574. canDraw = false;
  575. };
  576. //清空层 云手机超出屏幕的开关
  577. var clearContext = function () {
  578. canDraw = false;
  579. };
  580. // 鼠标移动
  581. var touchmove = function (e) {
  582. e.preventDefault();
  583. if (isFiringNoOperationSetTimeout) noOperationSetTimeout()
  584. if (!isControl) {
  585. return;
  586. }
  587. const action = 2;
  588. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  589. const x = item.clientX - item.target.getBoundingClientRect().x;
  590. const y = item.clientY - item.target.getBoundingClientRect().y;
  591. return wsss.send(
  592. JSON.stringify({
  593. type: 'event',
  594. data: {
  595. action,
  596. count: e.originalEvent.touches.length,
  597. pointerId: item.identifier,
  598. x: (function () {
  599. return (
  600. resolving
  601. ? x * (window.currentPhoneSize.width / vowidth)
  602. : y * (window.currentPhoneSize.height / voheight)
  603. ).toFixed(2);
  604. })(),
  605. y: (function () {
  606. return (
  607. resolving
  608. ? y * (window.currentPhoneSize.height / voheight)
  609. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  610. ).toFixed(2);
  611. })(),
  612. },
  613. }),
  614. );
  615. });
  616. // var touchfor = e.originalEvent.targetTouches; //for 的手指数组
  617. // for (var i = 0; i < touchfor.length; i++) {
  618. // // var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  619. // // var acrossHeightY =
  620. // // videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  621. // // let verticalWidthX = 0;
  622. // // let verticalHeightY = 0;
  623. // // if (resolving) {
  624. // // verticalWidthX =
  625. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  626. // // verticalHeightY =
  627. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  628. // // } else {
  629. // // verticalWidthX =
  630. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  631. // // verticalHeightY =
  632. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  633. // // }
  634. // var ping =
  635. // // resolving == 0 ?
  636. // // { "data": { "action": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  637. // {
  638. // data: {
  639. // action: 2,
  640. // count: touchfor.length,
  641. // pointerId: touchfor[i].identifier,
  642. // x: (() =>
  643. // (resolving
  644. // ? touchfor[i].pageX * (window.currentPhoneSize.width / vowidth)
  645. // : touchfor[i].pageY * (window.currentPhoneSize.width / voheight)
  646. // ).toFixed(2))(),
  647. // y: (() =>
  648. // (resolving
  649. // ? touchfor[i].pageY *
  650. // (window.currentPhoneSize.height / voheight)
  651. // : (vowidth - touchfor[i].pageX) *
  652. // (window.currentPhoneSize.height / vowidth)
  653. // ).toFixed(2))(),
  654. // },
  655. // type: 'event',
  656. // };
  657. // wsss.send(JSON.stringify(ping));
  658. // }
  659. };
  660. //鼠标离开区域以外 除了涂鸦 都清空
  661. var mouseout = function () {
  662. if (isFiringNoOperationSetTimeout) noOperationSetTimeout()
  663. if (graphType != 'handwriting') {
  664. clearContext();
  665. }
  666. };
  667. $(canvas_bak).off();
  668. $(canvas_bak).on('touchstart', touchstart);
  669. $(canvas_bak).on('touchmove', touchmove);
  670. $(canvas_bak).on('touchend', touchend);
  671. $(canvas_bak).on('mouseout', mouseout);
  672. };
  673. function GetRequest() {
  674. var url = location.search; // 获取url中"?"符后的字串
  675. var obj = new Object();
  676. if (url.indexOf('?') != -1) {
  677. var str = url.substr(1);
  678. strs = str.split('&');
  679. for (var i = 0; i < strs.length; i++) {
  680. obj[strs[i].split('=')[0]] = strs[i].split('=')[1];
  681. }
  682. }
  683. return obj;
  684. }
  685. // 添加监听 页面显示或隐藏 事件
  686. document.addEventListener('visibilitychange', visibilitychanged);
  687. // 监听 页面显示或隐藏 执行的函数
  688. function visibilitychanged() {
  689. // 获取当前环境
  690. const env = isBrowserEnvironment();
  691. // 获取当前页面的可见性状态
  692. const visibilityState = document.visibilityState;
  693. if (visibilityState === 'visible') {
  694. // 页面显示时的逻辑
  695. // 网页重载
  696. if (env.isBrowser && env.isTopWindow && env.isIPhone) {
  697. location.reload();
  698. }
  699. } else if (visibilityState === 'hidden') {
  700. // 页面隐藏时的逻辑
  701. // video.pause();
  702. } else if (visibilityState === 'prerender') {
  703. // 页面预渲染时的逻辑
  704. console.log('页面处于预渲染状态');
  705. } else if (visibilityState === 'unloaded') {
  706. // 页面即将卸载时的逻辑 移除监听
  707. document.removeEventListener('visibilitychange', visibilitychanged);
  708. }
  709. }
  710. /**
  711. * @description: 判断当前页面运行环境
  712. * @return {Object} 返回当前页面运行环境
  713. */
  714. function isBrowserEnvironment() {
  715. // 判断是否在浏览器环境中
  716. const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined' && typeof navigator !== 'undefined';
  717. // 判断是否在微信环境中
  718. const isWechat = /MicroMessenger/i.test(navigator.userAgent);
  719. // 判断是否在微信小程序的 web-view 中
  720. const isMiniProgramWebview = isWechat && /miniProgram/i.test(navigator.userAgent);
  721. // 判断是否是 iPhone 设备
  722. const isIPhone = /iPhone/i.test(navigator.userAgent);
  723. // 判断是否是顶级窗口(不是嵌套在 iframe 中)目前只有ios的浏览器中才会是顶级窗口
  724. const isTopWindow = window.parent === window.self;
  725. return {
  726. isBrowser, // 是否在浏览器环境中
  727. isWechat, // 是否在微信环境中
  728. isMiniProgramWebview, // 是否在微信小程序的 web-view 中
  729. isIPhone, // 是否是 iPhone 设备
  730. isTopWindow, // 是否是顶级窗口
  731. };
  732. }