liveDetail.vue 13 KB

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