chat-group-video.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. this.sendMessageToWebView("NAV_BACK", {})
  78. },
  79. onLoad(options) {
  80. uni.$on('WS_RTC_GROUP', msg => {
  81. // 推送给web-view处理
  82. this.sendMessageToWebView("RTC_MESSAGE", msg);
  83. })
  84. // #ifdef H5
  85. window.onmessage = (e) => {
  86. this.onWebviewMessage(e.data.data.arg);
  87. }
  88. // #endif
  89. // 是否发起人
  90. this.isHost = JSON.parse(options.isHost);
  91. // 发起者的用户
  92. this.inviterId = options.inviterId;
  93. // 解析页面跳转时带过来的好友信息
  94. this.groupId = options.groupId;
  95. // 邀请的用户信息
  96. this.userInfos = JSON.parse(decodeURIComponent(options.userInfos));
  97. // 构建url
  98. this.initUrl();
  99. },
  100. onUnload() {
  101. uni.$off('WS_RTC_GROUP')
  102. }
  103. }
  104. </script>
  105. <style lang="scss" scoped>
  106. .chat-group-video {
  107. .header {
  108. display: flex;
  109. justify-content: center;
  110. align-items: center;
  111. height: 60rpx;
  112. padding: 5px;
  113. background-color: white;
  114. line-height: 50px;
  115. font-size: 40rpx;
  116. font-weight: 600;
  117. .btn-side {
  118. position: absolute;
  119. line-height: 60rpx;
  120. font-size: 28rpx;
  121. cursor: pointer;
  122. &.left {
  123. left: 30rpx;
  124. }
  125. &.right {
  126. right: 30rpx;
  127. }
  128. }
  129. }
  130. }
  131. </style>