lives.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <template>
  2. <div class="box">
  3. <u-navbar class="navbar">
  4. <view class="slot-wrap">
  5. <u-search placeholder="搜索直播间" @custom="searchLive" @clear="clear" @search="searchLive" v-model="keyword"></u-search>
  6. </view>
  7. </u-navbar>
  8. <!-- 轮播图 -->
  9. <u-swiper @click="clickSwiper" class="swiper" :effect3d="true" :list="swiperImg">
  10. </u-swiper>
  11. <u-tabs :is-scroll="false" @change="changeTabs" :current="current" :active-color="activeColor" inactive-color="#606266" ref="tabs" :list="tabs"></u-tabs>
  12. <div class="wrapper">
  13. <!-- 直播中 全部 直播回放 -->
  14. <div class="live-item" :class="{'invalid':item.status == 'END'}" v-for="(item,index) in liveList" :key="index" @click="handleLivePlayer(item)">
  15. <div class="live-cover-img">
  16. <div class="tips">
  17. <div class="live-box">
  18. <image class="live-gif" src="./static/live.gif"></image>
  19. </div>
  20. <span>{{item.status == 'END' ? '已结束' : item.status =='NEW' ? '未开始' : '直播中'}}</span>
  21. </div>
  22. <div class="bg"></div>
  23. <u-image width="326" height="354" :src="item.shareImg" />
  24. </div>
  25. <div class="live-goods">
  26. <div class="live-goods-name">
  27. {{item.name}}
  28. </div>
  29. <div class="live-store">
  30. <span class="wes">南粤商城</span>
  31. </div>
  32. <div class="live-goods-list">
  33. <div class="live-goods-item">
  34. <u-image border-radius="20" :src="item.roomGoodsList ? item.roomGoodsList[0] : ''" height="140"></u-image>
  35. </div>
  36. <div class="live-goods-item">
  37. <u-image border-radius="20" :src="item.roomGoodsList ? item.roomGoodsList[1] : ''" height="140"></u-image>
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. <u-loadmore v-if="liveList.length > 10" bg-color="#f8f8f8" :status="status" />
  43. </div>
  44. </div>
  45. </template>
  46. <script>
  47. import { getLiveList } from "@/api/promotions.js";
  48. export default {
  49. data() {
  50. return {
  51. status: "loadmore",
  52. activeColor: this.$lightColor,
  53. current: 0, // 当前tabs索引
  54. keyword: "", //搜索直播间
  55. // 标签栏
  56. tabs: [
  57. {
  58. name: "直播中",
  59. },
  60. {
  61. name: "全部",
  62. },
  63. ],
  64. // 导航栏的配置
  65. background: {
  66. background: "#ff9f28",
  67. },
  68. // 直播间params
  69. params: [
  70. {
  71. pageNumber: 1,
  72. pageSize: 10,
  73. status: "START",
  74. },
  75. {
  76. pageNumber: 1,
  77. pageSize: 4,
  78. },
  79. ],
  80. // 推荐直播间Params
  81. recommendParams: {
  82. pageNumber: 1,
  83. pageSize: 3,
  84. recommend: 0,
  85. },
  86. // 直播间列表
  87. liveList: [],
  88. // 推荐直播间列表
  89. recommendLiveList: [],
  90. //轮播图滚动的图片
  91. swiperImg: [
  92. {
  93. image:
  94. "https://lilishop-oss.oss-cn-beijing.aliyuncs.com/48d789cb9c864b7b87c1c0f70996c3e8.jpeg",
  95. },
  96. ],
  97. };
  98. },
  99. onShow() {
  100. this.params[this.current].pageNumber = 1;
  101. this.liveList = [];
  102. this.getLives();
  103. this.getRecommendLives();
  104. },
  105. onReachBottom() {
  106. this.params[this.current].pageNumber++;
  107. this.getLives();
  108. },
  109. methods: {
  110. /**
  111. * 点击标签栏切换
  112. */
  113. changeTabs(index) {
  114. this.current = index;
  115. this.init();
  116. },
  117. /**
  118. * 初始化直播间
  119. */
  120. init() {
  121. this.liveList = [];
  122. this.status = "loadmore";
  123. this.getLives();
  124. },
  125. /**
  126. * 清除搜索内容
  127. */
  128. clear() {
  129. delete this.params[this.current].name;
  130. this.init();
  131. },
  132. /**
  133. * 点击顶部推荐直播间
  134. */
  135. clickSwiper(val) {
  136. console.log(this.swiperImg[val]);
  137. this.handleLivePlayer(this.swiperImg[val]);
  138. },
  139. /**
  140. * 搜索直播间
  141. */
  142. searchLive(val) {
  143. this.params[this.current].pageNumber = 1;
  144. this.params[this.current].name = val;
  145. this.init();
  146. },
  147. /**
  148. * 获取推荐直播间
  149. */
  150. async getRecommendLives() {
  151. this.status = "loading";
  152. let recommendLives = await getLiveList(this.recommendParams);
  153. if (recommendLives.data.success) {
  154. // 推荐直播间
  155. if (recommendLives.data.result.records.length != 0) {
  156. this.status = "loadmore";
  157. this.recommendLives = recommendLives.data.result.records;
  158. } else {
  159. this.status = "noMore";
  160. }
  161. /**
  162. * 如果推荐直播间没有的情况下
  163. * 1.获取直播间第一个图片
  164. * 2.如果没有直播间设置一个默认图片
  165. */
  166. if (this.recommendLives.length == 0) {
  167. if (this.liveList[0].shareImg) {
  168. this.$set(this, "swiperImg", [
  169. {
  170. image: this.liveList[0].shareImg,
  171. roomId: this.liveList[0].roomId,
  172. },
  173. ]);
  174. }
  175. } else {
  176. this.recommendLives.forEach((item) => {
  177. this.$set(this, "swiperImg", [
  178. { image: item.shareImg, roomId: item.roomId },
  179. ]);
  180. });
  181. }
  182. }
  183. },
  184. /**
  185. * 获取直播间
  186. */
  187. async getLives() {
  188. this.status = "loading";
  189. let res = await getLiveList(this.params[this.current]);
  190. // 直播间
  191. if (res.data.success) {
  192. if (res.data.result.records.length != 0) {
  193. this.status = "loadmore";
  194. this.liveList.push(...res.data.result.records);
  195. } else {
  196. this.status = "noMore";
  197. }
  198. res.data.result.total >
  199. this.params[this.current].pageNumber *
  200. this.params[this.current].pageSize
  201. ? (this.status = "loadmore")
  202. : (this.status = "noMore");
  203. console.log(this.status);
  204. this.liveList.forEach((item) => {
  205. if (item.roomGoodsList) {
  206. item.roomGoodsList = JSON.parse(item.roomGoodsList);
  207. }
  208. });
  209. }
  210. },
  211. /**
  212. * 进入直播间
  213. */
  214. handleLivePlayer(val) {
  215. // #ifdef MP-WEIXIN
  216. let roomId = val.roomId; // 填写具体的房间号,可通过下面【获取直播房间列表】 API 获取
  217. let customParams = encodeURIComponent(
  218. JSON.stringify({ path: "pages/index/index", pid: 1 })
  219. ); // 开发者在直播间页面路径上携带自定义参数,后续可以在分享卡片链接和跳转至商详页时获取,详见【获取自定义参数】、【直播间到商详页面携带参数】章节(上限600个字符,超过部分会被截断)
  220. uni.navigateTo({
  221. url:
  222. "plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=" +
  223. roomId +
  224. "&custom_params=" +
  225. customParams,
  226. });
  227. // #endif
  228. // #ifndef MP-WEIXIN
  229. uni.showToast({
  230. title: "请从微信小程序中预览直播功能",
  231. duration: 2000,
  232. icon: "none",
  233. });
  234. // #endif
  235. },
  236. },
  237. };
  238. </script>
  239. <style lang="scss" scoped>
  240. .slot-wrap {
  241. display: flex;
  242. align-items: center;
  243. /* 如果您想让slot内容占满整个导航栏的宽度 */
  244. flex: 1;
  245. /* 如果您想让slot内容与导航栏左右有空隙 */
  246. /* padding: 0 30rpx; */
  247. }
  248. .invalid {
  249. filter: grayscale(1);
  250. }
  251. .wrapper {
  252. padding: 0 24rpx;
  253. }
  254. .live-item {
  255. display: flex;
  256. overflow: hidden;
  257. border-radius: 20rpx;
  258. flex-wrap: wrap;
  259. background: #fff;
  260. margin: 20rpx 0;
  261. }
  262. .live-cover-img {
  263. position: relative;
  264. }
  265. .swiper {
  266. margin: 20rpx 0;
  267. }
  268. .live-goods {
  269. position: relative;
  270. flex: 1;
  271. padding: 16rpx 24rpx 24rpx;
  272. }
  273. .live-goods-name {
  274. height: 84rpx;
  275. font-weight: bold;
  276. font-size: 30rpx;
  277. display: -webkit-box;
  278. -webkit-box-orient: vertical;
  279. -webkit-line-clamp: 2;
  280. overflow: hidden;
  281. }
  282. .live-store {
  283. display: flex;
  284. align-items: center;
  285. margin: 20rpx 0;
  286. overflow: hidden;
  287. width: calc(100% - 50rpx);
  288. }
  289. .live-gif {
  290. width: 20rpx;
  291. height: 20rpx;
  292. }
  293. .live-box {
  294. display: flex;
  295. align-items: center;
  296. justify-content: center;
  297. border-radius: 50%;
  298. width: 40rpx;
  299. margin-right: 10rpx;
  300. height: 40rpx;
  301. background: linear-gradient(90deg, #ff6b35, #ff9f28, #ffcc03);
  302. }
  303. .live-goods-list {
  304. display: flex;
  305. align-items: center;
  306. justify-content: space-between;
  307. > .live-goods-item {
  308. flex: 1;
  309. }
  310. > .live-goods-item:nth-of-type(1) {
  311. padding-right: 38rpx;
  312. }
  313. }
  314. .live-icon,
  315. .zan {
  316. position: absolute;
  317. width: 80rpx;
  318. height: 80rpx;
  319. z-index: 9;
  320. }
  321. .tips {
  322. display: flex;
  323. position: absolute;
  324. z-index: 9;
  325. align-items: center;
  326. top: 20rpx;
  327. right: 0;
  328. padding: 4rpx 12rpx 4rpx 0;
  329. font-size: 24rpx;
  330. border-radius: 100px;
  331. color: #fff;
  332. background: rgba(0, 0, 0, 0.46);
  333. }
  334. .live-icon {
  335. right: 0;
  336. top: 104rpx;
  337. }
  338. .zan {
  339. bottom: 0;
  340. right: 0;
  341. width: 100rpx;
  342. height: 100rpx;
  343. }
  344. .bg {
  345. position: absolute;
  346. bottom: 4rpx;
  347. width: 100%;
  348. height: 100rpx;
  349. z-index: 8;
  350. background-image: -webkit-gradient(
  351. linear,
  352. left bottom,
  353. left top,
  354. from(rgba(0, 0, 0, 0.25)),
  355. color-stop(82%, transparent)
  356. );
  357. background-image: linear-gradient(0deg, rgba(0, 0, 0, 0.25), transparent 82%);
  358. border-bottom-left-radius: 20rpx;
  359. }
  360. </style>