chat-private-video.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <web-view class="page chat-private-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. mode: "video",
  12. isHost: true,
  13. friend: {}
  14. }
  15. },
  16. methods: {
  17. onMessage(e) {
  18. this.onWebviewMessage(e.detail.data[0]);
  19. },
  20. onWebviewMessage(event) {
  21. console.log("来自webview的消息:" + JSON.stringify(event))
  22. switch (event.key) {
  23. case "WV_READY":
  24. this.initWebView();
  25. break;
  26. case "WV_CLOSE":
  27. uni.navigateBack();
  28. break;
  29. }
  30. },
  31. sendMessageToWebView(key, message) {
  32. // 如果webview还没初始化好,则延迟100ms再推送
  33. if (!this.wv) {
  34. setTimeout(() => this.sendMessageToWebView(key, message), 100)
  35. return;
  36. }
  37. let event = {
  38. key: key,
  39. data: message
  40. }
  41. // #ifdef APP-PLUS
  42. this.wv.evalJS(`onEvent('${encodeURIComponent(JSON.stringify(event))}')`)
  43. // #endif
  44. // #ifdef H5
  45. this.wv.postMessage(event, '*');
  46. // #endif
  47. },
  48. initWebView() {
  49. // #ifdef APP-PLUS
  50. // APP的webview
  51. this.wv = this.$scope.$getAppWebview().children()[0]
  52. // #endif
  53. // #ifdef H5
  54. // H5的webview就是iframe
  55. this.wv = document.getElementById('chat-video-wv').contentWindow
  56. // #endif
  57. },
  58. initUrl() {
  59. this.url = "/hybrid/html/rtc-private/index.html";
  60. this.url += "?mode=" + this.mode;
  61. this.url += "&isHost=" + this.isHost;
  62. this.url += "&baseUrl=" + UNI_APP.BASE_URL;
  63. this.url += "&loginInfo=" + JSON.stringify(uni.getStorageSync("loginInfo"));
  64. this.url += "&userInfo=" + JSON.stringify(this.userStore.userInfo);
  65. this.url += "&friend=" + JSON.stringify(this.friend);
  66. this.url += "&config=" + JSON.stringify(this.configStore.webrtc);
  67. },
  68. },
  69. onBackPress() {
  70. this.sendMessageToWebView("NAV_BACK", {})
  71. },
  72. onLoad(options) {
  73. uni.$on('WS_RTC_PRIVATE', msg => {
  74. // 推送给web-view处理
  75. this.sendMessageToWebView("RTC_MESSAGE", msg);
  76. })
  77. // #ifdef H5
  78. window.onmessage = (e) => {
  79. this.onWebviewMessage(e.data.data.arg);
  80. }
  81. // #endif
  82. // 模式:视频呼叫/语音呼叫
  83. this.mode = options.mode;
  84. // 是否呼叫方
  85. this.isHost = JSON.parse(options.isHost);
  86. // 解析页面跳转时带过来的好友信息
  87. this.friend = JSON.parse(decodeURIComponent(options.friend));
  88. // 构建url
  89. this.initUrl();
  90. },
  91. onUnload() {
  92. uni.$off('WS_RTC_PRIVATE')
  93. }
  94. }
  95. </script>
  96. <style lang="scss" scoped>
  97. .chat-web-view {}
  98. </style>