pickVideo1.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. <view class="popup-open" @click="showGoods()">
  9. <view class="iconfont iconzhibo-shangpin"></view>
  10. </view>
  11. <view class="popup-flip" @click="changeCamera()">
  12. <view class="iconfont iconFill"></view>
  13. </view>
  14. <uni-popup ref="popup" type="bottom">
  15. <view class="popup-box">
  16. <view class="popup-close" @click="closeGoods()">收起</view>
  17. <scroll-view scroll-y="true" class="good-box">
  18. <view class="goods-row" v-for="(item, index) in goodsList" :key="index">
  19. <image class="goods-img" :src="item.imgPath" mode="aspectFill"></image>
  20. <view class="goods-info">
  21. <view class="goods-name">{{item.productName}}</view>
  22. <view class="goods-number">
  23. <text class="goods-icon">¥</text>
  24. <text class="goods-spec">{{item.bizPrice}}</text>
  25. <text class="price">原价:{{item.originalPrice}}</text>
  26. </view>
  27. </view>
  28. </view>
  29. </scroll-view>
  30. </view>
  31. </uni-popup>
  32. <u-top-tips ref="uTips"></u-top-tips>
  33. <view class="top_box" :style="{ top: btn_top, left: btn_left}">
  34. <image class="top_box_img" :src="head_img"></image>
  35. <view class="top_box_text">
  36. <text class="text_box_top">{{title}}</text><text style="display:none" class="text_box_bottom">{{user_name}}</text>
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. const NET = require('@/utils/request')
  43. const API = require('@/config/api')
  44. import {
  45. genTestUserSig,
  46. setData
  47. } from "@/pagesMedia/debug/GenerateTestUserSig";
  48. import trtcRoom from "@/pagesMedia/trtc-room/trtc-room";
  49. export default {
  50. components: {
  51. trtcRoom
  52. },
  53. data() {
  54. return {
  55. liveId: '',
  56. roomId: '',
  57. rtcConfig: {
  58. sdkAppID: '',
  59. // 必要参数 开通实时音视频服务创建应用后分配的 sdkAppID
  60. userID: '',
  61. // 必要参数 用户 ID 可以由您的帐号系统指定
  62. userSig: '',
  63. // 必要参数 身份签名,相当于登录密码的作用
  64. template: '' // 必要参数 组件模版,支持的值 1v1 grid custom ,注意:不支持动态修改, iOS 不支持 pusher 动态渲染
  65. },
  66. showTipToast: false,
  67. options: null,
  68. btn_left:0,
  69. btn_top:0,
  70. title:"测试",
  71. user_name:'123',
  72. head_img:"../static/images/loginLogo.png",
  73. orderId: '',
  74. goodsList: []
  75. }
  76. },
  77. onLoad(options) {
  78. let menuButtonObject = wx.getMenuButtonBoundingClientRect();
  79. wx.getSystemInfo({
  80. success: res => {
  81. this.setData({
  82. btn_left: res.windowWidth - menuButtonObject.right+'rpx',
  83. btn_top:menuButtonObject.top+menuButtonObject.height+14+'rpx'
  84. })
  85. },
  86. })
  87. wx.setKeepScreenOn({
  88. keepScreenOn: true
  89. }); // 获取 rtcroom 实例
  90. this.options = {
  91. template: '1v1'
  92. }
  93. this.orderId = options.orderId
  94. NET.request(API.creatPickVideo, {orderId: this.orderId}, 'GET').then(res => {
  95. this.liveId = res.data.liveId
  96. this.roomId = Number(res.data.roomId)
  97. this.trtcComponent = this.$refs['trtc-component'] // 监听TRTC Room 关键事件
  98. this.goodsList = res.data.liveProducResVO
  99. this.enterRoom({
  100. roomID: this.roomId,
  101. userID: uni.getStorageSync("userData").userId, //设置为用户id
  102. template: '1v1',
  103. debugMode: false,
  104. cloudenv: 'PRO'
  105. });
  106. this.bindTRTCRoomEvent(); // 将String 类型的 true false 转换成 boolean
  107. }).catch(error => {
  108. this.$refs.uTips.show({
  109. title: error.data.msg,
  110. type: 'warning',
  111. })
  112. })
  113. },
  114. methods: {
  115. changeCamera: function() {
  116. this.trtcComponent.switchCamera();
  117. },
  118. // 打开弹窗
  119. showGoods() {
  120. this.$refs.popup.open()
  121. },
  122. // 关闭弹窗
  123. closeGoods() {
  124. this.$refs.popup.close()
  125. },
  126. setData,
  127. enterRoom: function(params) {
  128. params.template = params.template || '1v1';
  129. params.roomID = params.roomID || 2333;
  130. params.userID = params.userID || new Date().getTime().toString(16);
  131. console.log('* room enterRoom', params);
  132. const Signature = genTestUserSig(params.userID);
  133. params.sdkAppID = Signature.sdkAppID;
  134. params.userSig = Signature.userSig;
  135. this.template = params.template;
  136. this.rtcConfig = {
  137. sdkAppID: params.sdkAppID,
  138. // 您的实时音视频服务创建应用后分配的 sdkAppID
  139. userID: params.userID,
  140. userSig: params.userSig,
  141. template: params.template,
  142. // 1v1 grid custom
  143. debugMode: params.debugMode,
  144. // 非必要参数,打开组件的调试模式,开发调试时建议设置为 true
  145. beautyLevel: 9 // 默认开启美颜
  146. // cloudenv: params.cloudenv, // 非必要参数
  147. };
  148. this.setData({
  149. rtcConfig: this.rtcConfig
  150. }, () => {
  151. // roomID 取值范围 1 ~ 4294967295
  152. this.trtcComponent.enterRoom({
  153. roomID: params.roomID
  154. }).then(() => {
  155. }).catch(res => {
  156. console.error('* room joinRoom 进房失败:', res);
  157. });
  158. });
  159. },
  160. bindTRTCRoomEvent: function() {
  161. const TRTC_EVENT = this.trtcComponent.EVENT;
  162. this.timestamp = []; // 初始化事件订阅
  163. this.trtcComponent.on(TRTC_EVENT.LOCAL_JOIN, event => {
  164. if(this.trtcComponent.getRemoteUserList().lenth>0){
  165. var userId=this.trtcComponent.getRemoteUserList()[0].userID;
  166. NET.request(API.getMainInfo +"?userId="+ userId, {}, 'GET').then(res => {
  167. this.setData({
  168. head_img:res.data.headimg,
  169. title:res.data.nickname
  170. })
  171. }).catch(error => {
  172. this.$refs.uTips.show({
  173. title: '获取个人信息失败',
  174. type: 'warning',
  175. })
  176. })
  177. }
  178. console.log('* room LOCAL_JOIN', event); // 进房成功,触发该事件后可以对本地视频和音频进行设置
  179. if (this.options.localVideo === true || this.options.template === '1v1') {
  180. this.trtcComponent.publishLocalVideo();
  181. }
  182. if (this.options.localAudio === true || this.options.template === '1v1') {
  183. this.trtcComponent.publishLocalAudio();
  184. }
  185. });
  186. this.trtcComponent.on(TRTC_EVENT.LOCAL_LEAVE, event => {
  187. console.log('* room LOCAL_LEAVE', event);
  188. });
  189. this.trtcComponent.on(TRTC_EVENT.ERROR, event => {
  190. console.log('* room ERROR', event);
  191. }); // 远端用户进房
  192. this.trtcComponent.on(TRTC_EVENT.REMOTE_USER_JOIN, event => {
  193. var userId=this.trtcComponent.getRemoteUserList()[0].userID;
  194. NET.request(API.getMainInfo +"?userId="+ userId, {}, 'GET').then(res => {
  195. this.setData({
  196. head_img:res.data.headimg,
  197. title:res.data.nickname
  198. })
  199. }).catch(error => {
  200. this.$refs.uTips.show({
  201. title: '获取个人信息失败',
  202. type: 'warning',
  203. })
  204. })
  205. console.log('* room REMOTE_USER_JOIN --- room.vue', event, this.trtcComponent.getRemoteUserList(), this.template);
  206. this.timestamp.push(new Date()); // 1v1视频通话时限制人数为两人的简易逻辑,建议通过后端实现房间人数管理
  207. // 2人以上同时进行通话请选择网格布局
  208. if (this.template === '1v1' && this.timestamp.length > 1) {
  209. const interval = this.timestamp[1] - this.timestamp[0];
  210. if (interval < 1000) {
  211. // 房间里已经有两个人
  212. this.setData({
  213. showTipToast: true
  214. }, () => {
  215. setTimeout(() => {
  216. this.setData({
  217. showTipToast: false
  218. });
  219. wx.navigateBack({
  220. delta: 1
  221. });
  222. }, 4000);
  223. });
  224. }
  225. }
  226. }); // 远端用户退出
  227. this.trtcComponent.on(TRTC_EVENT.REMOTE_USER_LEAVE, event => {
  228. this.setData({
  229. head_img:"../static/images/loginLogo.png",
  230. title:''
  231. })
  232. console.log('* room REMOTE_USER_LEAVE', event, this.trtcComponent.getRemoteUserList());
  233. if (this.template === '1v1') {
  234. this.timestamp = [];
  235. }
  236. if (this.template === '1v1' && this.remoteUser === event.data.userID) {
  237. this.remoteUser = null;
  238. }
  239. }); // 远端用户推送视频
  240. this.trtcComponent.on(TRTC_EVENT.REMOTE_VIDEO_ADD, event => {
  241. console.log('* room REMOTE_VIDEO_ADD', event, this.trtcComponent.getRemoteUserList()); // 订阅视频
  242. const userList = this.trtcComponent.getRemoteUserList();
  243. const data = event.data;
  244. if (this.template === '1v1' && (!this.remoteUser || this.remoteUser === data.userID)) {
  245. // 1v1 只订阅第一个远端流
  246. this.remoteUser = data.userID;
  247. this.trtcComponent.subscribeRemoteVideo({
  248. userID: data.userID,
  249. streamType: data.streamType
  250. });
  251. } else if (this.template === 'grid') {
  252. this.trtcComponent.subscribeRemoteVideo({
  253. userID: data.userID,
  254. streamType: data.streamType
  255. });
  256. }
  257. if (this.template === 'custom' && data.userID && data.streamType) {
  258. let index = userList.findIndex(item => {
  259. return item.userID === data.userID;
  260. });
  261. index++;
  262. const y = 320 * index + 160; // 设置远端视图坐标和尺寸
  263. this.trtcComponent.setViewRect({
  264. userID: data.userID,
  265. streamType: data.streamType,
  266. xAxis: '480rpx',
  267. yAxis: y + 'rpx',
  268. width: '240rpx',
  269. height: '320rpx'
  270. });
  271. }
  272. }); // 远端用户取消推送视频
  273. this.trtcComponent.on(TRTC_EVENT.REMOTE_VIDEO_REMOVE, event => {
  274. console.log('* room REMOTE_VIDEO_REMOVE', event, this.trtcComponent.getRemoteUserList());
  275. }); // 远端用户推送音频
  276. this.trtcComponent.on(TRTC_EVENT.REMOTE_AUDIO_ADD, event => {
  277. console.log('* room REMOTE_AUDIO_ADD', event, this.trtcComponent.getRemoteUserList()); // 订阅音频
  278. const data = event.data;
  279. if (this.template === '1v1' && (!this.remoteUser || this.remoteUser === data.userID)) {
  280. this.remoteUser = data.userID;
  281. this.trtcComponent.subscribeRemoteAudio({
  282. userID: data.userID
  283. });
  284. } else if (this.template === 'grid' || this.template === 'custom') {
  285. this.trtcComponent.subscribeRemoteAudio({
  286. userID: data.userID
  287. });
  288. } // 如果不订阅就不会自动播放音频
  289. // this.trtcComponent.subscribeRemoteAudio({ userID: data.userID })
  290. }); // 远端用户取消推送音频
  291. this.trtcComponent.on(TRTC_EVENT.REMOTE_AUDIO_REMOVE, event => {
  292. console.log('* room REMOTE_AUDIO_REMOVE', event, this.trtcComponent.getRemoteUserList());
  293. });
  294. }
  295. }
  296. }
  297. </script>
  298. <style lang="less" scoped>
  299. page {
  300. width: 100%;
  301. height: 100%;
  302. }
  303. .container {
  304. width: 100%;
  305. height: 100%;
  306. float: left;
  307. position: relative;
  308. .popup-flip {
  309. width: 50px;
  310. height: 50px;
  311. position: fixed;
  312. top: 15px;
  313. right: 15px;
  314. background: #52A63A;
  315. border-radius: 50%;
  316. text-align: center;
  317. line-height: 50px;
  318. z-index: 10000;
  319. .iconFill {
  320. color: #FFFFFF;
  321. font-size: 34px;
  322. }
  323. }
  324. .tip-toast {
  325. position: absolute;
  326. top: 40vh;
  327. width: 70vw;
  328. left: 15vw;
  329. border-radius: 12rpx;
  330. height: 20vh;
  331. background: rgba(0, 0, 0, 0.8);
  332. color: white;
  333. display: flex;
  334. flex-direction: column;
  335. align-items: center;
  336. justify-content: center;
  337. }
  338. .tip-toast view {
  339. padding: 20rpx 0;
  340. font-size: 32rpx;
  341. }
  342. .top_box{
  343. width: 304rpx;
  344. height: 86rpx;
  345. position: absolute;
  346. z-index: 100;
  347. background:rgba(0, 0, 0, 0.4);
  348. border-radius: 100px;
  349. top: 182rpx;
  350. left: 30rpx;
  351. }
  352. .top_box_img{
  353. width: 86rpx;
  354. height: 86rpx;
  355. border-radius: 100rpx;
  356. float: left;
  357. }
  358. .top_box_text{
  359. float: left;
  360. color: #fff;
  361. margin-left: 30rpx;
  362. width: 180rpx;
  363. padding-top: 2rpx;
  364. }
  365. .text_box_top{
  366. font-size: 34rpx;
  367. width: 100%;
  368. display: block;
  369. height: 30rpx;
  370. float: left;
  371. margin-top: 19rpx;
  372. }
  373. .text_box_bottom{
  374. font-size: 22rpx;
  375. float: left;
  376. margin-top: 10rpx;
  377. }
  378. .popup-open {
  379. width: 50px;
  380. height: 50px;
  381. position: fixed;
  382. bottom: 15px;
  383. right: 15px;
  384. background: #52A63A;
  385. border-radius: 50%;
  386. text-align: center;
  387. line-height: 50px;
  388. z-index: 20000;
  389. .iconzhibo-shangpin {
  390. color: #FFFFFF;
  391. font-size: 34px;
  392. }
  393. }
  394. .popup-box {
  395. width: 100%;
  396. height: 265px;
  397. float: left;
  398. background-color: #FFFFFF;
  399. border-radius: 15px 15px 0px 0px;
  400. box-sizing: border-box;
  401. padding: 16px 0 0 0;
  402. position: relative;
  403. .popup-close {
  404. width: 100%;
  405. height: 26px;
  406. float: left;
  407. box-sizing: border-box;
  408. padding: 10px 15px 0 15px;
  409. font-size: 15px;
  410. font-family: PingFang SC;
  411. color: #52A63A;
  412. line-height: 16px;
  413. }
  414. .good-box {
  415. width: 100%;
  416. height: calc(100% - 26px);
  417. box-sizing: border-box;
  418. padding: 0 0 10px 0;
  419. overflow: auto;
  420. position: relative;
  421. .goods-row:first-child {
  422. margin-top: 12px;
  423. }
  424. .goods-row {
  425. width: calc(100% - 30px);
  426. height: 104px;
  427. float: left;
  428. background: #FFFFFF;
  429. box-shadow: 0px 2px 10px 0px rgba(0, 0, 0, 0.1);
  430. border-radius: 5px;
  431. margin: 0 15px 10px 15px;
  432. position: relative;
  433. .goods-img {
  434. width: 104px;
  435. height: 104px;
  436. object-fit: cover;
  437. float: left;
  438. margin-right: 15px;
  439. }
  440. .goods-info {
  441. width: calc(100% - 120px);
  442. float: left;
  443. padding-top: 10px;
  444. .goods-name {
  445. width: 100%;
  446. height: 30px;
  447. float: left;
  448. font-size: 15px;
  449. font-family: PingFang SC;
  450. color: #333333;
  451. line-height: 15px;
  452. overflow: hidden;
  453. text-overflow: ellipsis;
  454. display: -webkit-box;
  455. -webkit-line-clamp: 2;
  456. -webkit-box-orient: vertical;
  457. word-wrap: break-word;
  458. }
  459. .goods-sales {
  460. width: 100%;
  461. float: left;
  462. font-size: 12px;
  463. font-family: PingFang SC;
  464. color: #666666;
  465. line-height: 15px;
  466. margin: 4px 0 8px 0;
  467. }
  468. .goods-number {
  469. width: 100%;
  470. height: 24px;
  471. float: left;
  472. white-space: nowrap;
  473. font-family: PingFang SC;
  474. color: #52A63A;
  475. line-height: 24px;
  476. .goods-icon {
  477. font-size: 14px;
  478. }
  479. .goods-spec {
  480. font-size: 24px;
  481. }
  482. .price {
  483. font-size: 12px;
  484. text-decoration: line-through;
  485. color: #A67954;
  486. margin-left: 6px;
  487. }
  488. }
  489. }
  490. .more-button {
  491. width: 24px;
  492. height: 24px;
  493. position: absolute;
  494. right: 16px;
  495. bottom: 16px;
  496. .iconfont {
  497. font-size: 36px;
  498. color: #999999;
  499. }
  500. }
  501. }
  502. }
  503. }
  504. }
  505. </style>