liveDetail.vue 14 KB

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