chat-private-video.vue 2.9 KB

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