liveDetail.vue 16 KB

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