call.nvue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <view class="call-view">
  3. <view class="nav-bar">
  4. <text class="duration">{{duration}}</text>
  5. </view>
  6. <view class="user-list">
  7. <view v-for="(member,index) in members" :key="index">
  8. <view v-if="member.status !== 'Quit'" class="user-item">
  9. <!-- #ifdef APP -->
  10. <grtc-video v-if="currentCall.mediaType === 1 && member.status === 'InCall' && !member.cameraMuted" :userId="member.id" class="callee-view"></grtc-video>
  11. <image v-else class="user-item-avatar" mode="widthFix" :src="member.data.avatar"></image>
  12. <!-- #endif -->
  13. <!-- #ifdef H5 -->
  14. <web-grtc-video v-if="currentCall.mediaType === 1 && member.status === 'InCall' && !member.cameraMuted" :userId="member.id" class="callee-view"></web-grtc-video>
  15. <image v-else class="user-item-avatar" mode="widthFix" :src="member.data.avatar"></image>
  16. <!-- #endif -->
  17. <view v-if="member.micMuted" class="user-muted">
  18. <image class="user-muted-icon" mode="widthFix" src="/static/images/microphone-red.png" />
  19. </view>
  20. </view>
  21. </view>
  22. </view>
  23. <view class="action-box">
  24. <view class="action-top">
  25. <view class="action-item" @click="toggleMic()">
  26. <view v-if="self.micMuted">
  27. <view class="item-background-disable">
  28. <image class="action-bar-icon" src="/static/images/microphone-disable.png"></image>
  29. </view>
  30. <text class="action-text">麦克风已关</text>
  31. </view>
  32. <view v-else>
  33. <view class="item-background">
  34. <image class="action-bar-icon" src="/static/images/microphone.png"></image>
  35. </view>
  36. <text class="action-text">麦克风已开</text>
  37. </view>
  38. </view>
  39. <view class="action-item" @click="toggleSpeaker()">
  40. <view v-if="currentCall.speakerOn">
  41. <view class="item-background">
  42. <image class="action-bar-icon" src="/static/images/speaker.png"></image>
  43. </view>
  44. <text class="action-text">扬声器已开</text>
  45. </view>
  46. <view v-else>
  47. <view class="item-background-disable">
  48. <image class="action-bar-icon" src="/static/images/speaker-disable.png"></image>
  49. </view>
  50. <text class="action-text">扬声器已关</text>
  51. </view>
  52. </view>
  53. <view class="action-item" @click="toggleCamera()" v-if="currentCall.mediaType === 1">
  54. <view v-if="self.cameraMuted">
  55. <view class="item-background-disable">
  56. <image class="action-bar-icon" src="/static/images/camera-disable.png"></image>
  57. </view>
  58. <text class="action-text">摄像头已关</text>
  59. </view>
  60. <view v-else>
  61. <view class="item-background">
  62. <image class="action-bar-icon" src="/static/images/camera.png"></image>
  63. </view>
  64. <text class="action-text">摄像头已开</text>
  65. </view>
  66. </view>
  67. </view>
  68. <view class="action-item" @click="end()">
  69. <view class="end-background">
  70. <image class="action-icon" src="/static/images/phoneEnd.png"></image>
  71. </view>
  72. </view>
  73. <view class="switch-camera" @click="switchCamera()" v-if="currentCall.mediaType === 1">
  74. <image class="switch-camera-icon" src="/static/images/switch-camera.png"></image>
  75. </view>
  76. </view>
  77. </view>
  78. </template>
  79. <script>
  80. import webGrtcVideo from '../components/web-grtc-video/web-grtc-video.vue';
  81. const GRTC = uni.$GRTC;
  82. let interval = null;
  83. export default {
  84. data() {
  85. return {
  86. currentCall: GRTC.currentCall(),
  87. self: null,
  88. duration: null
  89. }
  90. },
  91. components: {
  92. webGrtcVideo,
  93. },
  94. computed: {
  95. members() {
  96. const { callees, caller } = this.currentCall;
  97. const allMembers = [...callees, caller];
  98. const sortedMembers = allMembers.sort((a, b) => {
  99. if (a.id === uni.$currentUser.id) return 1;
  100. if (b.id === uni.$currentUser.id) return -1;
  101. return 0;
  102. });
  103. return sortedMembers;
  104. },
  105. },
  106. onLoad() {
  107. this.self = this.getSelf();
  108. interval = setInterval(() => {
  109. this.duration = this.formattedDuration();
  110. }, 1000);
  111. this.initListener();
  112. },
  113. onUnload() {
  114. GRTC.off(GRTC.EVENT.USER_RANG, this.onUserRang);
  115. GRTC.off(GRTC.EVENT.USER_ACCEPTED, this.onUserAccepted);
  116. GRTC.off(GRTC.EVENT.USER_QUIT, this.onUserQuit);
  117. GRTC.off(GRTC.EVENT.CALL_ENDED, this.onCallEnded);
  118. GRTC.off(GRTC.EVENT.USER_MIC_CHANGED, this.onUserMicChanged);
  119. GRTC.off(GRTC.EVENT.USER_CAMERA_CHANGED, this.onUserCameraChanged);
  120. },
  121. onBackPress(event) {
  122. return event.from === 'backbutton'; // 禁止安卓侧滑返回
  123. },
  124. methods: {
  125. getSelf() {
  126. if (uni.$currentUser.id === this.currentCall.caller.id) {
  127. return this.currentCall.caller
  128. } else {
  129. const index = this.currentCall.callees.findIndex(member => member.id === uni.$currentUser.id);
  130. return this.currentCall.callees[index]
  131. }
  132. },
  133. initListener() {
  134. GRTC.on(GRTC.EVENT.USER_RANG, this.onUserRang);
  135. GRTC.on(GRTC.EVENT.USER_ACCEPTED, this.onUserAccepted);
  136. GRTC.on(GRTC.EVENT.USER_QUIT, this.onUserQuit);
  137. GRTC.on(GRTC.EVENT.CALL_ENDED, this.onCallEnded);
  138. GRTC.on(GRTC.EVENT.USER_MIC_CHANGED, this.onUserMicChanged);
  139. GRTC.on(GRTC.EVENT.USER_CAMERA_CHANGED, this.onUserCameraChanged);
  140. },
  141. onUserRang(event) {
  142. uni.showToast({
  143. title: event.user.data.name+"已响铃",
  144. icon: "none"
  145. });
  146. },
  147. onUserAccepted(event) {
  148. uni.showToast({
  149. title: event.user.data.name+"已接听",
  150. icon: "none"
  151. });
  152. },
  153. onUserQuit(event) {
  154. const username = event.user.id === uni.$currentUser.id ? '' : event.user.data.name;
  155. let message = '';
  156. switch (event.reason) {
  157. case 'GOEASY_DISCONNECTED':
  158. message = `${username}网络异常`;
  159. break;
  160. case 'HUNG_UP':
  161. message = `${username}已挂断`;
  162. break;
  163. case 'REJECTED':
  164. message = `${username}已拒绝`;
  165. break;
  166. }
  167. uni.showToast({title: message,icon: "none"});
  168. },
  169. onCallEnded() {
  170. clearInterval(interval);
  171. uni.navigateBack();
  172. },
  173. onUserMicChanged(event) {
  174. console.log("onUserMicChange",event);
  175. },
  176. onUserCameraChanged(event) {
  177. console.log("onUserCameraChanged",event);
  178. },
  179. switchCamera() {
  180. GRTC.switchCamera();
  181. },
  182. toggleSpeaker() {
  183. GRTC.toggleSpeaker(!this.currentCall.speakerOn);
  184. },
  185. toggleMic() {
  186. GRTC.muteMic(!this.self.micMuted);
  187. },
  188. toggleCamera() {
  189. GRTC.muteCamera(!this.self.cameraMuted);
  190. },
  191. end() {
  192. clearInterval(interval);
  193. GRTC.end();
  194. },
  195. formattedDuration() {
  196. const seconds = this.currentCall.getDuration();
  197. const minutes = Math.floor(seconds / 60);
  198. const formattedSeconds = String(seconds % 60).padStart(2, '0');
  199. const formattedMinutes = String(minutes).padStart(2, '0');
  200. return `${formattedMinutes}:${formattedSeconds}`; // mm:ss
  201. }
  202. }
  203. }
  204. </script>
  205. <style scoped>
  206. @import url('/static/style/rtcCallStyle.css');
  207. </style>