liveDetail.vue 12 KB

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