liveDetail.vue 16 KB

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