liveDetail.vue 13 KB

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