liveDetail.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. <image class="goods-img" :src="item.imgPath"></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.popupShow = false
  152. this.$refs.uTips.show({
  153. title: '绑定商品成功',
  154. type: 'success',
  155. })
  156. }).catch(res => {
  157. this.$refs.uTips.show({
  158. title: '绑定商品失败',
  159. type: 'warning',
  160. })
  161. })
  162. },
  163. setData,
  164. enterRoom: function(params) {
  165. params.template = params.template || '1v1';
  166. params.roomID = params.roomID || 2333;
  167. params.userID = params.userID || new Date().getTime().toString(16);
  168. console.log('* room enterRoom', params);
  169. const Signature = genTestUserSig(params.userID);
  170. params.sdkAppID = Signature.sdkAppID;
  171. params.userSig = Signature.userSig;
  172. this.template = params.template;
  173. this.rtcConfig = {
  174. sdkAppID: params.sdkAppID,
  175. // 您的实时音视频服务创建应用后分配的 sdkAppID
  176. userID: params.userID,
  177. userSig: params.userSig,
  178. template: params.template,
  179. // 1v1 grid custom
  180. debugMode: false,
  181. // 非必要参数,打开组件的调试模式,开发调试时建议设置为 true
  182. // cloudenv: params.cloudenv, // 非必要参数
  183. frontCamera: "front",
  184. enableEarMonitor: false,
  185. enableAutoFocus: true,
  186. localMirror: 'auto',
  187. enableAgc: true,
  188. enableAns: true,
  189. encsmall: false ? 1 : 0,
  190. videoWidth: 1280,
  191. videoHeight: 720,
  192. scene: "live",
  193. maxBitrate: 2000,
  194. minBitrate: 1500,
  195. beautyLevel: 9 // 默认开启美颜
  196. };
  197. this.setData({
  198. rtcConfig: this.rtcConfig
  199. }, () => {
  200. // roomID 取值范围 1 ~ 4294967295
  201. this.trtcComponent.enterRoom({
  202. roomID: params.roomID
  203. }).then(() => {
  204. if (this.template === 'custom') {
  205. // 设置推流端视窗的坐标和尺寸
  206. this.trtcComponent.setViewRect({
  207. userID: params.userID,
  208. xAxis: '480rpx',
  209. yAxis: '160rpx',
  210. width: '240rpx',
  211. height: '320rpx'
  212. });
  213. }
  214. }).catch(res => {
  215. console.error('* room joinRoom 进房失败:', res);
  216. });
  217. });
  218. },
  219. bindTRTCRoomEvent: function() {
  220. const TRTC_EVENT = this.trtcComponent.EVENT;
  221. this.timestamp = []; // 初始化事件订阅
  222. this.trtcComponent.on(TRTC_EVENT.LOCAL_JOIN, event => {
  223. console.log('* room LOCAL_JOIN', event); // 进房成功,触发该事件后可以对本地视频和音频进行设置
  224. if (this.options.localVideo === true || this.options.template === '1v1') {
  225. this.trtcComponent.publishLocalVideo();
  226. }
  227. if (this.options.localAudio === true || this.options.template === '1v1') {
  228. this.trtcComponent.publishLocalAudio();
  229. }
  230. });
  231. this.trtcComponent.on(TRTC_EVENT.LOCAL_LEAVE, event => {
  232. console.log('* room LOCAL_LEAVE', event);
  233. });
  234. this.trtcComponent.on(TRTC_EVENT.ERROR, event => {
  235. console.log('* room ERROR', event);
  236. }); // 远端用户进房
  237. this.trtcComponent.on(TRTC_EVENT.REMOTE_USER_JOIN, event => {
  238. console.log('* room REMOTE_USER_JOIN --- room.vue', event, this.trtcComponent.getRemoteUserList(), this.template);
  239. this.timestamp.push(new Date()); // 1v1视频通话时限制人数为两人的简易逻辑,建议通过后端实现房间人数管理
  240. // 2人以上同时进行通话请选择网格布局
  241. if (this.template === '1v1' && this.timestamp.length > 1) {
  242. const interval = this.timestamp[1] - this.timestamp[0];
  243. if (interval < 1000) {
  244. // 房间里已经有两个人
  245. this.setData({
  246. showTipToast: true
  247. }, () => {
  248. setTimeout(() => {
  249. this.setData({
  250. showTipToast: false
  251. });
  252. wx.navigateBack({
  253. delta: 1
  254. });
  255. }, 4000);
  256. });
  257. }
  258. }
  259. }); // 远端用户退出
  260. this.trtcComponent.on(TRTC_EVENT.REMOTE_USER_LEAVE, event => {
  261. console.log('* room REMOTE_USER_LEAVE', event, this.trtcComponent.getRemoteUserList());
  262. if (this.template === '1v1') {
  263. this.timestamp = [];
  264. }
  265. if (this.template === '1v1' && this.remoteUser === event.data.userID) {
  266. this.remoteUser = null;
  267. }
  268. }); // 远端用户推送视频
  269. this.trtcComponent.on(TRTC_EVENT.REMOTE_VIDEO_ADD, event => {
  270. console.log('* room REMOTE_VIDEO_ADD', event, this.trtcComponent.getRemoteUserList()); // 订阅视频
  271. const userList = this.trtcComponent.getRemoteUserList();
  272. const data = event.data;
  273. if (this.template === '1v1' && (!this.remoteUser || this.remoteUser === data.userID)) {
  274. // 1v1 只订阅第一个远端流
  275. this.remoteUser = data.userID;
  276. this.trtcComponent.subscribeRemoteVideo({
  277. userID: data.userID,
  278. streamType: data.streamType
  279. });
  280. } else if (this.template === 'grid') {
  281. this.trtcComponent.subscribeRemoteVideo({
  282. userID: data.userID,
  283. streamType: data.streamType
  284. });
  285. }
  286. if (this.template === 'custom' && data.userID && data.streamType) {
  287. let index = userList.findIndex(item => {
  288. return item.userID === data.userID;
  289. });
  290. index++;
  291. const y = 320 * index + 160; // 设置远端视图坐标和尺寸
  292. this.trtcComponent.setViewRect({
  293. userID: data.userID,
  294. streamType: data.streamType,
  295. xAxis: '480rpx',
  296. yAxis: y + 'rpx',
  297. width: '240rpx',
  298. height: '320rpx'
  299. });
  300. }
  301. }); // 远端用户取消推送视频
  302. this.trtcComponent.on(TRTC_EVENT.REMOTE_VIDEO_REMOVE, event => {
  303. console.log('* room REMOTE_VIDEO_REMOVE', event, this.trtcComponent.getRemoteUserList());
  304. }); // 远端用户推送音频
  305. this.trtcComponent.on(TRTC_EVENT.REMOTE_AUDIO_ADD, event => {
  306. console.log('* room REMOTE_AUDIO_ADD', event, this.trtcComponent.getRemoteUserList()); // 订阅音频
  307. const data = event.data;
  308. if (this.template === '1v1' && (!this.remoteUser || this.remoteUser === data.userID)) {
  309. this.remoteUser = data.userID;
  310. this.trtcComponent.subscribeRemoteAudio({
  311. userID: data.userID
  312. });
  313. } else if (this.template === 'grid' || this.template === 'custom') {
  314. this.trtcComponent.subscribeRemoteAudio({
  315. userID: data.userID
  316. });
  317. } // 如果不订阅就不会自动播放音频
  318. // this.trtcComponent.subscribeRemoteAudio({ userID: data.userID })
  319. }); // 远端用户取消推送音频
  320. this.trtcComponent.on(TRTC_EVENT.REMOTE_AUDIO_REMOVE, event => {
  321. console.log('* room REMOTE_AUDIO_REMOVE', event, this.trtcComponent.getRemoteUserList());
  322. });
  323. },
  324. changeCamera: function() {
  325. this.trtcComponent.switchCamera();
  326. }
  327. },
  328. }
  329. </script>
  330. <style lang="less" scoped>
  331. page {
  332. width: 100%;
  333. height: 100%;
  334. }
  335. .container {
  336. width: 100%;
  337. height: 100%;
  338. float: left;
  339. position: relative;
  340. .popup-flip {
  341. width: 50px;
  342. height: 50px;
  343. position: fixed;
  344. top: 15px;
  345. right: 15px;
  346. background: #52A63A;
  347. border-radius: 50%;
  348. text-align: center;
  349. line-height: 50px;
  350. z-index: 10000;
  351. .iconFill {
  352. color: #FFFFFF;
  353. font-size: 34px;
  354. }
  355. }
  356. .popup-open {
  357. width: 50px;
  358. height: 50px;
  359. position: fixed;
  360. bottom: 15px;
  361. right: 15px;
  362. background: #52A63A;
  363. border-radius: 50%;
  364. text-align: center;
  365. line-height: 50px;
  366. z-index: 10000;
  367. .iconzhibo-shangpin {
  368. color: #FFFFFF;
  369. font-size: 34px;
  370. }
  371. }
  372. .popup-box {
  373. width: 100%;
  374. height: 400px;
  375. float: left;
  376. background-color: #FFFFFF;
  377. border-radius: 15px 15px 0px 0px;
  378. box-sizing: border-box;
  379. padding: 16px 0 0 0;
  380. position: relative;
  381. .popup-close {
  382. width: 50%;
  383. height: 26px;
  384. float: left;
  385. box-sizing: border-box;
  386. padding: 10px 15px 0 15px;
  387. font-size: 15px;
  388. font-family: PingFang SC;
  389. color: #52A63A;
  390. line-height: 16px;
  391. margin-bottom: 10px;
  392. }
  393. .popup-edit {
  394. width: 50%;
  395. height: 26px;
  396. float: right;
  397. box-sizing: border-box;
  398. padding: 10px 15px 0 15px;
  399. font-size: 15px;
  400. font-family: PingFang SC;
  401. color: #52A63A;
  402. line-height: 16px;
  403. margin-bottom: 10px;
  404. text-align: right;
  405. }
  406. .good-select-box {
  407. width: 100%;
  408. height: 400px;
  409. float: left;
  410. overflow-y: auto;
  411. padding: 40px 0 10px 0;
  412. }
  413. .select-good-row {
  414. width: 100%;
  415. height: 114px;
  416. float: left;
  417. }
  418. .good-check {
  419. width: 30px;
  420. height: 104px;
  421. float: left;
  422. line-height: 104px;
  423. margin-right: 10px;
  424. .iconfont {
  425. font-size: 40px;
  426. text-align: right;
  427. color: #51A539;
  428. }
  429. }
  430. .good-card {
  431. width: 100%;
  432. float: left;
  433. margin: 5px;
  434. background: #FFFFFF;
  435. border-radius: 15px;
  436. box-shadow: 0px 1px 5px 0px rgba(102, 102, 102, 0.43);
  437. padding: 12px;
  438. box-sizing: border-box;
  439. .goods-img {
  440. width: 80px;
  441. height: 80px;
  442. float: left;
  443. border-radius: 10px;
  444. object-fit: cover;
  445. }
  446. .good-info {
  447. width: calc(100% - 86px);
  448. height: 80px;
  449. float: right;
  450. .good-name {
  451. width: 100%;
  452. height: 40px;
  453. float: left;
  454. font-size: 15px;
  455. font-family: PingFang SC;
  456. color: #333333;
  457. line-height: 20px;
  458. overflow: hidden;
  459. text-overflow: ellipsis;
  460. display: -webkit-box;
  461. -webkit-line-clamp: 2;
  462. -webkit-box-orient: vertical;
  463. word-wrap: break-word;
  464. }
  465. .good-price {
  466. width: 100%;
  467. height: 20px;
  468. float: left;
  469. line-height: 20px;
  470. font-family: PingFang SC;
  471. white-space: nowrap;
  472. margin: 3px 0 3px 0;
  473. .goods-spec {
  474. font-size: 15px;
  475. color: #52A63A;
  476. margin-right: 6px;
  477. }
  478. .goods-original {
  479. font-size: 12px;
  480. text-decoration: line-through;
  481. color: #A67954;
  482. }
  483. }
  484. }
  485. }
  486. }
  487. }
  488. </style>