chat-video.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <view class="page chat-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/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. },
  82. },
  83. onBackPress() {
  84. this.sendMessageToWebView("NAV_BACK",{})
  85. },
  86. onLoad(options) {
  87. uni.$on('WS_RTC', msg => {
  88. // 推送给web-view处理
  89. this.sendMessageToWebView("RTC_MESSAGE", msg);
  90. })
  91. // #ifdef H5
  92. window.onmessage = (e) => {
  93. this.onWebviewMessage(e.data.data.arg);
  94. }
  95. // #endif
  96. // 模式:视频呼叫/语音呼叫
  97. this.mode = options.mode;
  98. // 是否呼叫方
  99. this.isHost = JSON.parse(options.isHost);
  100. // 解析页面跳转时带过来的好友信息
  101. this.friend = JSON.parse(decodeURIComponent(options.friend));
  102. // 构建url
  103. this.initUrl();
  104. },
  105. onUnload() {
  106. uni.$off('WS_RTC')
  107. }
  108. }
  109. </script>
  110. <style lang="scss" scoped>
  111. </style>