pickVideo.vue 14 KB

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