liveDetail.vue 14 KB

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