chat-group-video.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <view class="page chat-group-video">
  3. <view>
  4. <web-view id="chat-video-wv" @message="onMessage" :src="url"></web-view>
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. import UNI_APP from '@/.env.js'
  10. export default {
  11. data() {
  12. return {
  13. url: "",
  14. wv: '',
  15. isHost: false,
  16. groupId: null,
  17. inviterId: null,
  18. userInfos: []
  19. }
  20. },
  21. methods: {
  22. onMessage(e) {
  23. this.onWebviewMessage(e.detail.data[0]);
  24. },
  25. onInsertMessage(msgInfo) {
  26. },
  27. onWebviewMessage(event) {
  28. console.log("来自webview的消息:" + JSON.stringify(event))
  29. switch (event.key) {
  30. case "WV_READY":
  31. this.initWebView();
  32. break;
  33. case "WV_CLOSE":
  34. uni.navigateBack();
  35. break;
  36. case "INSERT_MESSAGE":
  37. this.onInsertMessage(event.data);
  38. break;
  39. }
  40. },
  41. sendMessageToWebView(key, message) {
  42. // 如果webview还没初始化好,则延迟100ms再推送
  43. if (!this.wv) {
  44. setTimeout(() => this.sendMessageToWebView(key, message), 100)
  45. return;
  46. }
  47. let event = {
  48. key: key,
  49. data: message
  50. }
  51. // #ifdef APP-PLUS
  52. this.wv.evalJS(`onEvent('${encodeURIComponent(JSON.stringify(event))}')`)
  53. // #endif
  54. // #ifdef H5
  55. this.wv.postMessage(event, '*');
  56. // #endif
  57. },
  58. initWebView() {
  59. // #ifdef APP-PLUS
  60. // APP的webview
  61. this.wv = this.$scope.$getAppWebview().children()[0]
  62. // #endif
  63. // #ifdef H5
  64. // H5的webview就是iframe
  65. this.wv = document.getElementById('chat-video-wv').contentWindow
  66. // #endif
  67. },
  68. initUrl() {
  69. this.url = "/hybrid/html/rtc-group/index.html?";
  70. this.url += "baseUrl=" + UNI_APP.BASE_URL;
  71. this.url += "&groupId=" + this.groupId;
  72. this.url += "&userId=" + this.$store.state.userStore.userInfo.id;
  73. this.url += "&inviterId=" + this.inviterId;
  74. this.url += "&isHost=" + this.isHost;
  75. this.url += "&loginInfo=" + JSON.stringify(uni.getStorageSync("loginInfo"));
  76. this.url += "&userInfos=" + JSON.stringify(this.userInfos);
  77. this.url += "&config=" + JSON.stringify(this.$store.state.configStore.webrtc);
  78. },
  79. },
  80. onBackPress() {
  81. console.log("onBackPress")
  82. this.sendMessageToWebView("NAV_BACK", {})
  83. },
  84. onLoad(options) {
  85. uni.$on('WS_RTC_GROUP', msg => {
  86. // 推送给web-view处理
  87. this.sendMessageToWebView("RTC_MESSAGE", msg);
  88. })
  89. // #ifdef H5
  90. window.onmessage = (e) => {
  91. this.onWebviewMessage(e.data.data.arg);
  92. }
  93. // #endif
  94. // 是否发起人
  95. this.isHost = JSON.parse(options.isHost);
  96. // 发起者的用户
  97. this.inviterId = options.inviterId;
  98. // 解析页面跳转时带过来的好友信息
  99. this.groupId = options.groupId;
  100. // 邀请的用户信息
  101. this.userInfos = JSON.parse(decodeURIComponent(options.userInfos));
  102. // 构建url
  103. this.initUrl();
  104. },
  105. onUnload() {
  106. uni.$off('WS_RTC_GROUP')
  107. }
  108. }
  109. </script>
  110. <style lang="scss" scoped>
  111. .chat-group-video {
  112. .header {
  113. display: flex;
  114. justify-content: center;
  115. align-items: center;
  116. height: 60rpx;
  117. padding: 5px;
  118. background-color: white;
  119. line-height: 50px;
  120. font-size: 40rpx;
  121. font-weight: 600;
  122. border: #dddddd solid 1px;
  123. .btn-side {
  124. position: absolute;
  125. line-height: 60rpx;
  126. font-size: 28rpx;
  127. cursor: pointer;
  128. &.left {
  129. left: 30rpx;
  130. }
  131. &.right {
  132. right: 30rpx;
  133. }
  134. }
  135. }
  136. }
  137. </style>