liveDetail.vue 12 KB

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