liveDetail.vue 14 KB

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