pickVideo.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <template>
  2. <view class="container">
  3. <trtc-room ref="trtc-component" :config="rtcConfig"> </trtc-room>
  4. <view class="tip-toast" v-if="showTipToast">
  5. <view>当前房间为1v1双人通话房间</view>
  6. <view>不希望其他人打扰</view>
  7. </view>
  8. <u-top-tips ref="uTips"></u-top-tips>
  9. <view class="top_box" :style="{ top: btn_top, left: btn_left}">
  10. <image class="top_box_img" :src="head_img"></image>
  11. <view class="top_box_text">
  12. <text class="text_box_top">{{title}}</text><text class="text_box_bottom" style="display:none">{{user_name}}</text>
  13. </view>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. const NET = require('@/utils/request')
  19. const API = require('@/config/api')
  20. import {
  21. genTestUserSig,
  22. setData
  23. } from "@/pagesGood/debug/GenerateTestUserSig";
  24. import trtcRoom from "@/pagesGood/trtc-room/trtc-room";
  25. export default {
  26. components: {
  27. trtcRoom
  28. },
  29. data() {
  30. return {
  31. liveId: '',
  32. roomId: '',
  33. rtcConfig: {
  34. sdkAppID: '',
  35. // 必要参数 开通实时音视频服务创建应用后分配的 sdkAppID
  36. userID: '',
  37. // 必要参数 用户 ID 可以由您的帐号系统指定
  38. userSig: '',
  39. // 必要参数 身份签名,相当于登录密码的作用
  40. template: '' // 必要参数 组件模版,支持的值 1v1 grid custom ,注意:不支持动态修改, iOS 不支持 pusher 动态渲染
  41. },
  42. showTipToast: false,
  43. btn_left:0,
  44. btn_top:0,
  45. title:"",
  46. user_name:'1',
  47. head_img:"../static/images/loginLogo.png"
  48. }
  49. },
  50. onLoad(options) {
  51. let menuButtonObject = wx.getMenuButtonBoundingClientRect();
  52. wx.getSystemInfo({
  53. success: res => {
  54. this.setData({
  55. btn_left: res.windowWidth - menuButtonObject.right+'rpx',
  56. btn_top:menuButtonObject.top+menuButtonObject.height+14+'rpx'
  57. })
  58. },
  59. })
  60. NET.request(API.linkPickVideo, {
  61. tenantCode : options.tenantCode
  62. }, 'GET').then(res => {
  63. this.liveId = res.data.liveId
  64. this.roomId = res.data.roomId
  65. wx.setKeepScreenOn({
  66. keepScreenOn: true
  67. }); // 获取 rtcroom 实例
  68. this.trtcComponent = this.$refs['trtc-component']; // 监听TRTC Room 关键事件
  69. this.bindTRTCRoomEvent(); // 将String 类型的 true false 转换成 boolean
  70. options.template = '1v1';
  71. this.options = options; // querystring 只支持传递 String 类型, 注意类型转换
  72. this.enterRoom({
  73. roomID: this.roomId,
  74. userID: uni.getStorageSync("userData").userId, //设置为用户id
  75. template: '1v1',
  76. debugMode: false,
  77. cloudenv: 'PRO',
  78. });
  79. }).catch(error => {
  80. this.$refs.uTips.show({
  81. title: error.data.msg,
  82. type: 'warning',
  83. })
  84. })
  85. },
  86. methods: {
  87. setData,
  88. enterRoom: function(params) {
  89. params.template = params.template || '1v1';
  90. params.roomID = params.roomID || 2333;
  91. params.userID = params.userID || new Date().getTime().toString(16);
  92. console.log('* room enterRoom', params);
  93. const Signature = genTestUserSig(params.userID);
  94. params.sdkAppID = Signature.sdkAppID;
  95. params.userSig = Signature.userSig;
  96. this.template = params.template;
  97. this.rtcConfig = {
  98. sdkAppID: params.sdkAppID,
  99. // 您的实时音视频服务创建应用后分配的 sdkAppID
  100. userID: params.userID,
  101. userSig: params.userSig,
  102. template: params.template,
  103. // 1v1 grid custom
  104. debugMode: params.debugMode,
  105. // 非必要参数,打开组件的调试模式,开发调试时建议设置为 true
  106. beautyLevel: 9 // 默认开启美颜
  107. // cloudenv: params.cloudenv, // 非必要参数
  108. };
  109. this.setData({
  110. rtcConfig: this.rtcConfig
  111. }, () => {
  112. // roomID 取值范围 1 ~ 4294967295
  113. this.trtcComponent.enterRoom({
  114. roomID: params.roomID
  115. }).then(() => {
  116. }).catch(res => {
  117. console.error('* room joinRoom 进房失败:', res);
  118. });
  119. });
  120. },
  121. bindTRTCRoomEvent: function() {
  122. const TRTC_EVENT = this.trtcComponent.EVENT;
  123. this.timestamp = []; // 初始化事件订阅
  124. this.trtcComponent.on(TRTC_EVENT.LOCAL_JOIN, event => {
  125. var userId=this.trtcComponent.getRemoteUserList()[0].userID;
  126. NET.request(API.getMerchantsMainInfo + userId, {}, 'GET').then(res => {
  127. this.setData({
  128. head_img:res.data.merchantImg,
  129. title:res.data.merchantNickname
  130. })
  131. }).catch(error => {
  132. this.$refs.uTips.show({
  133. title: '获取个人信息失败',
  134. type: 'warning',
  135. })
  136. })
  137. console.log('* room LOCAL_JOIN', event); // 进房成功,触发该事件后可以对本地视频和音频进行设置
  138. if (this.options.localVideo === true || this.options.template === '1v1') {
  139. this.trtcComponent.publishLocalVideo();
  140. }
  141. if (this.options.localAudio === true || this.options.template === '1v1') {
  142. this.trtcComponent.publishLocalAudio();
  143. }
  144. });
  145. this.trtcComponent.on(TRTC_EVENT.LOCAL_LEAVE, event => {
  146. console.log('* room LOCAL_LEAVE', event);
  147. });
  148. this.trtcComponent.on(TRTC_EVENT.ERROR, event => {
  149. console.log('* room ERROR', event);
  150. }); // 远端用户进房
  151. this.trtcComponent.on(TRTC_EVENT.REMOTE_USER_JOIN, event => {
  152. var userId=this.trtcComponent.getRemoteUserList()[0].userID;
  153. NET.request(API.getMerchantsMainInfo + userId, {}, 'GET').then(res => {
  154. this.setData({
  155. head_img:res.data.merchantImg,
  156. title:res.data.merchantNickname
  157. })
  158. }).catch(error => {
  159. this.$refs.uTips.show({
  160. title: '获取个人信息失败',
  161. type: 'warning',
  162. })
  163. })
  164. console.log('* room REMOTE_USER_JOIN --- room.vue', event, this.trtcComponent.getRemoteUserList(), this.template);
  165. this.timestamp.push(new Date()); // 1v1视频通话时限制人数为两人的简易逻辑,建议通过后端实现房间人数管理
  166. // 2人以上同时进行通话请选择网格布局
  167. if (this.template === '1v1' && this.timestamp.length > 1) {
  168. const interval = this.timestamp[1] - this.timestamp[0];
  169. if (interval < 1000) {
  170. // 房间里已经有两个人
  171. this.setData({
  172. showTipToast: true
  173. }, () => {
  174. setTimeout(() => {
  175. this.setData({
  176. showTipToast: false
  177. });
  178. wx.navigateBack({
  179. delta: 1
  180. });
  181. }, 4000);
  182. });
  183. }
  184. }
  185. }); // 远端用户退出
  186. this.trtcComponent.on(TRTC_EVENT.REMOTE_USER_LEAVE, event => {
  187. this.setData({
  188. head_img:"../static/images/loginLogo.png",
  189. title:''
  190. })
  191. console.log('* room REMOTE_USER_LEAVE', event, this.trtcComponent.getRemoteUserList());
  192. if (this.template === '1v1') {
  193. this.timestamp = [];
  194. }
  195. if (this.template === '1v1' && this.remoteUser === event.data.userID) {
  196. this.remoteUser = null;
  197. }
  198. }); // 远端用户推送视频
  199. this.trtcComponent.on(TRTC_EVENT.REMOTE_VIDEO_ADD, event => {
  200. console.log('* room REMOTE_VIDEO_ADD', event, this.trtcComponent.getRemoteUserList()); // 订阅视频
  201. const userList = this.trtcComponent.getRemoteUserList();
  202. const data = event.data;
  203. if (this.template === '1v1' && (!this.remoteUser || this.remoteUser === data.userID)) {
  204. // 1v1 只订阅第一个远端流
  205. this.remoteUser = data.userID;
  206. this.trtcComponent.subscribeRemoteVideo({
  207. userID: data.userID,
  208. streamType: data.streamType
  209. });
  210. } else if (this.template === 'grid') {
  211. this.trtcComponent.subscribeRemoteVideo({
  212. userID: data.userID,
  213. streamType: data.streamType
  214. });
  215. }
  216. if (this.template === 'custom' && data.userID && data.streamType) {
  217. let index = userList.findIndex(item => {
  218. return item.userID === data.userID;
  219. });
  220. index++;
  221. const y = 320 * index + 160; // 设置远端视图坐标和尺寸
  222. this.trtcComponent.setViewRect({
  223. userID: data.userID,
  224. streamType: data.streamType,
  225. xAxis: '480rpx',
  226. yAxis: y + 'rpx',
  227. width: '240rpx',
  228. height: '320rpx'
  229. });
  230. }
  231. }); // 远端用户取消推送视频
  232. this.trtcComponent.on(TRTC_EVENT.REMOTE_VIDEO_REMOVE, event => {
  233. console.log('* room REMOTE_VIDEO_REMOVE', event, this.trtcComponent.getRemoteUserList());
  234. }); // 远端用户推送音频
  235. this.trtcComponent.on(TRTC_EVENT.REMOTE_AUDIO_ADD, event => {
  236. console.log('* room REMOTE_AUDIO_ADD', event, this.trtcComponent.getRemoteUserList()); // 订阅音频
  237. const data = event.data;
  238. if (this.template === '1v1' && (!this.remoteUser || this.remoteUser === data.userID)) {
  239. this.remoteUser = data.userID;
  240. this.trtcComponent.subscribeRemoteAudio({
  241. userID: data.userID
  242. });
  243. } else if (this.template === 'grid' || this.template === 'custom') {
  244. this.trtcComponent.subscribeRemoteAudio({
  245. userID: data.userID
  246. });
  247. } // 如果不订阅就不会自动播放音频
  248. // this.trtcComponent.subscribeRemoteAudio({ userID: data.userID })
  249. }); // 远端用户取消推送音频
  250. this.trtcComponent.on(TRTC_EVENT.REMOTE_AUDIO_REMOVE, event => {
  251. console.log('* room REMOTE_AUDIO_REMOVE', event, this.trtcComponent.getRemoteUserList());
  252. });
  253. }
  254. }
  255. }
  256. </script>
  257. <style lang="less" scoped>
  258. page {
  259. width: 100%;
  260. height: 100%;
  261. }
  262. .container {
  263. width: 100%;
  264. height: 100%;
  265. float: left;
  266. position: relative;
  267. .tip-toast {
  268. position: absolute;
  269. top: 40vh;
  270. width: 70vw;
  271. left: 15vw;
  272. border-radius: 12rpx;
  273. height: 20vh;
  274. background: rgba(0, 0, 0, 0.8);
  275. color: white;
  276. display: flex;
  277. flex-direction: column;
  278. align-items: center;
  279. justify-content: center;
  280. }
  281. .tip-toast view {
  282. padding: 20rpx 0;
  283. font-size: 32rpx;
  284. }
  285. .top_box{
  286. width: 304rpx;
  287. height: 86rpx;
  288. position: absolute;
  289. z-index: 100;
  290. background:rgba(0, 0, 0, 0.4);
  291. border-radius: 100px;
  292. top: 182rpx;
  293. left: 30rpx;
  294. }
  295. .top_box_img{
  296. width: 86rpx;
  297. height: 86rpx;
  298. border-radius: 100rpx;
  299. float: left;
  300. }
  301. .top_box_text{
  302. float: left;
  303. color: #fff;
  304. margin-left: 30rpx;
  305. width: 180rpx;
  306. padding-top: 2rpx;
  307. }
  308. .text_box_top{
  309. font-size: 34rpx;
  310. width: 100%;
  311. display: block;
  312. height: 30rpx;
  313. float: left;
  314. margin-top: 19rpx;
  315. }
  316. .text_box_bottom{
  317. font-size: 22rpx;
  318. float: left;
  319. margin-top: 10rpx;
  320. }
  321. }
  322. </style>