chat-group-video.vue 3.1 KB

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