chat-private-video.vue 2.5 KB

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