touch.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <div
  3. class="disk-touch"
  4. @touchstart="ontouchstart"
  5. @touchmove="ontouchmove"
  6. @touchend="ontouchend"
  7. >
  8. </div>
  9. </template>
  10. <script>
  11. import qs from 'qs';
  12. export default {
  13. name: 'DiskTouch',
  14. props: {
  15. info: {
  16. type: Object,
  17. default: null,
  18. },
  19. },
  20. async fetch() {
  21. this.info && (await this.initBusinessChannelWebSocket());
  22. },
  23. watch: {
  24. info: '$fetch',
  25. },
  26. methods: {
  27. genSendDataList(event, action) {
  28. const width = 720;
  29. const height = 1280;
  30. return Array.from(event.changedTouches).map((item) => ({
  31. data: {
  32. action,
  33. count: event.touches.length,
  34. pointerId: item.identifier,
  35. x: (
  36. item.clientX *
  37. (width / this.$el.getBoundingClientRect().width)
  38. ).toFixed(2),
  39. y: (
  40. item.clientY *
  41. (height / this.$el.getBoundingClientRect().height)
  42. ).toFixed(2),
  43. },
  44. type: 'event',
  45. }));
  46. },
  47. initBusinessChannelWebSocket() {
  48. this?._wx?.close();
  49. const { internetHttps, localIp, sn, cardToken } = this.info;
  50. const url = `wss://${internetHttps}/businessChannel${qs.stringify(
  51. {
  52. cardIp: localIp,
  53. token: cardToken,
  54. type: 'directives',
  55. },
  56. { addQueryPrefix: true, encode: false },
  57. )}`;
  58. const ws = new WebSocket(url);
  59. ws.addEventListener('open', (e) => {
  60. ws.send(JSON.stringify({ type: 'getVsStatus' }));
  61. ws.send(
  62. JSON.stringify({ type: 'bitRate', data: { bitRate: 1243000 } }),
  63. );
  64. ws.send(
  65. JSON.stringify({
  66. type: 'forwardMsg',
  67. data: { code: '3000', desc: '询问是否有在控制' },
  68. }),
  69. );
  70. ws.send(
  71. JSON.stringify({
  72. type: 'getPhoneSize',
  73. }),
  74. );
  75. });
  76. ws.addEventListener('error', (e) => {});
  77. ws.addEventListener('message', (event) => {
  78. var result =
  79. typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
  80. console.log(result);
  81. if (result.type === 'getPhoneSize') {
  82. this.$emit('sizeChange', result.data);
  83. }
  84. });
  85. ws.addEventListener('close', (e) => {});
  86. this.$once('hook:beforeDestroy', () => {
  87. ws.close();
  88. });
  89. this._ws = ws;
  90. },
  91. ontouchstart(event) {
  92. this.genSendDataList(event, 0).forEach((item) =>
  93. this._ws.send(JSON.stringify(item)),
  94. );
  95. },
  96. ontouchmove(event) {
  97. this.genSendDataList(event, 2).forEach((item) =>
  98. this._ws.send(JSON.stringify(item)),
  99. );
  100. },
  101. ontouchend(event) {
  102. this.genSendDataList(event, 1).forEach((item) =>
  103. this._ws.send(JSON.stringify(item)),
  104. );
  105. },
  106. },
  107. };
  108. </script>
  109. <style lang="scss" scoped>
  110. .disk-touch {
  111. position: absolute;
  112. top: 0;
  113. left: 0;
  114. right: 0;
  115. bottom: 0;
  116. z-index: 1;
  117. }
  118. </style>