item.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <template>
  2. <view class="live-box">
  3. <view v-if="liveData.liveStatus === 101" class="live-ongoing" @click="toLive">
  4. <image class="cover-img" :src="liveData.feedsImg" />
  5. <view class="status">
  6. <view class="status-state">
  7. <!-- #ifdef MP-WEIXIN -->
  8. <img class="img" src="../../../static/images/live/icon-live-num.png" mode="widthFix" />
  9. <!-- #endif -->
  10. <!-- #ifdef H5 || APP-PLUS -->
  11. <image class="img" src="../../../static/images/live/icon-live-num.png" mode="widthFix" />
  12. <!-- #endif -->
  13. 直播中
  14. </view>
  15. <!-- <view class="status-num">1000人</view> -->
  16. </view>
  17. <view class="user">
  18. <view class="user-pic">
  19. <image class="img" :src="liveData.anchorHeadImg" />
  20. </view>
  21. <view class="user-name">{{ liveData.anchorNickName }}</view>
  22. </view>
  23. <view class="products">
  24. <view class="uni-padding-wrap">
  25. <view class="page-section swiper">
  26. <view class="page-section-spacing">
  27. <swiper
  28. class="swiper" :indicator-dots="indicatorDots" :autoplay="autoplay" :interval="interval"
  29. :duration="duration" :vertical="true"
  30. >
  31. <swiper-item v-for="item in liveData.goods" :key="item.goods_id">
  32. <view class="swiper-item">{{ item.name }}</view>
  33. </swiper-item>
  34. </swiper>
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. <view v-else class="live-other" @click="toLive">
  41. <image class="cover-img" :src="liveData.feedsImg" />
  42. <div class="filter-box-warp">
  43. <div class="filter-box">
  44. <image class="cover-img" :src="liveData.feedsImg" />
  45. </div>
  46. </div>
  47. <view class="user">
  48. <view class="user-pic">
  49. <image class="img" :src="liveData.anchorHeadImg" />
  50. </view>
  51. <view class="user-name">{{ liveData.anchorNickName }}</view>
  52. </view>
  53. <view v-if="liveStatus === 102" class="count-down">
  54. <image v-if="isLate" class="img" src="@/static/images/live/live-late.png" />
  55. <view class="text">{{ liveTimeTitle }}</view>
  56. <view v-if="!isLate" class="time">
  57. <view class="time-item">{{ times[0] }}</view>
  58. <view class="dot">:</view>
  59. <view class="time-item">{{ times[1] }}</view>
  60. <view class="dot">:</view>
  61. <view class="time-item">{{ times[2] }}</view>
  62. </view>
  63. </view>
  64. <!-- #ifdef MP-WEIXIN -->
  65. <view
  66. v-if="liveStatus === 102 && !isLate" class="btn-subscribe" :class="{ subscribed: subscribeLive === '已预约' }"
  67. @click.stop="onSubscribe"
  68. >
  69. {{ subscribeLive }}
  70. </view>
  71. <!-- #endif -->
  72. <view v-if="liveStatus === 103" class="endContainer">
  73. <view class="endBox">
  74. <view></view>
  75. <view></view>
  76. <view></view>
  77. <view></view>
  78. </view>
  79. <view>直播已结束</view>
  80. </view>
  81. </view>
  82. </view>
  83. </template>
  84. <script>
  85. const NET = require('@/utils/request')
  86. const API = require('@/config/api')
  87. import { startLiveTemplate } from '@/config/subscribe.js'
  88. import { liveAppid } from '@/config/live.js'
  89. // #ifdef MP-WEIXIN
  90. const livePlayer = requirePlugin('live-player-plugin')
  91. // #endif
  92. export default {
  93. props: {
  94. liveData: {
  95. type: Object,
  96. default: () => ({
  97. roomId: 0,
  98. anchorNickName: '',
  99. feedsImg: '' // 官方收录封面
  100. })
  101. }
  102. },
  103. data() {
  104. return {
  105. background: ['color1', 'color2', 'color3'],
  106. indicatorDots: false,
  107. autoplay: true,
  108. interval: 2000, // 自动播放间隔时长
  109. duration: 500, // 幻灯片切换时长(ms)
  110. d: 0,
  111. m: 0,
  112. s: 0,
  113. times: [],
  114. liveStatus: 100,
  115. liveTimeTitle: '开播倒计时',
  116. subscribeLive: '立即预约',
  117. timer: null,
  118. isLate: false
  119. }
  120. },
  121. created() {
  122. this.liveStatus = this.liveData.liveStatus
  123. this.subscribeLive = this.liveData.subscribeStatus === 0 ? '立即预约' : '已预约'
  124. this.getStatus()
  125. this.countTime()
  126. // this.getSubscribeStatus()
  127. },
  128. destroyed() {
  129. clearTimeout(this.timer)
  130. },
  131. methods: {
  132. getStatus() {
  133. if (!this.liveData.roomId) { return }
  134. const _this = this
  135. // #ifdef MP-WEIXIN
  136. livePlayer.getLiveStatus({ room_id: this.liveData.roomId })
  137. .then((res) => {
  138. // 101: 直播中, 102: 未开始, 103: 已结束, 104: 禁播, 105: 暂停中, 106: 异常,107:已过期
  139. // _this.liveData.liveStatus = res.liveStatus
  140. _this.liveStatus = res.liveStatus
  141. })
  142. .catch((err) => {
  143. })
  144. this.timer = setInterval(() => {
  145. livePlayer.getLiveStatus({ room_id: this.liveData.roomId })
  146. .then((res) => {
  147. // 101: 直播中, 102: 未开始, 103: 已结束, 104: 禁播, 105: 暂停中, 106: 异常,107:已过期
  148. _this.liveStatus = res.liveStatus
  149. this.countTime()
  150. })
  151. .catch((err) => {
  152. throw new Error(err)
  153. })
  154. }, 60000)
  155. // #endif
  156. },
  157. changeIndicatorDots(e) {
  158. this.indicatorDots = !this.indicatorDots
  159. },
  160. changeAutoplay(e) {
  161. this.autoplay = !this.autoplay
  162. },
  163. intervalChange(e) {
  164. this.interval = e.target.value
  165. },
  166. durationChange(e) {
  167. this.duration = e.target.value
  168. },
  169. countTime() {
  170. var nowtime = new Date().getTime() // 获取当前时间
  171. const starttime = new Date(this.liveData.startTime).getTime()
  172. if (this.liveStatus === 102) {
  173. if (starttime > nowtime) {
  174. var lefttime = starttime - nowtime // 距离结束时间的毫秒数
  175. var leftd = Math.floor(lefttime / (1000 * 60 * 60)) // 计算小时数
  176. var leftm = Math.floor(lefttime / (1000 * 60) % 60) // 计算分钟数
  177. var lefts = Math.floor(lefttime / 1000 % 60) // 计算秒数
  178. this.times = [leftd < 10 ? '0' + leftd : leftd, leftm < 10 ? '0' + leftm : leftm, lefts < 10 ? '0' + lefts : lefts]
  179. this.liveTimeTitle = '开播倒计时'
  180. setTimeout(() => {
  181. this.countTime()
  182. }, 1000)
  183. } else {
  184. this.times = ['00', '00', '00']
  185. this.isLate = true
  186. this.liveTimeTitle = '正在赶来的路上...'
  187. }
  188. }
  189. },
  190. toLive() {
  191. if (!liveAppid || !this.liveData) { return }
  192. // 跳转直播间携带路由参数
  193. // let customParams = encodeURIComponent(JSON.stringify({ path: 'livePage/index', pid: 1 }))
  194. // #ifdef MP-WEIXIN
  195. wx.navigateTo({
  196. url: `plugin-private://${liveAppid}/pages/live-player-plugin?room_id=${this.liveData.roomId}`
  197. // url: `plugin-private://${liveAppid}/pages/live-player-plugin?room_id=${this.liveData.roomId}&custom_params=${customParams}`
  198. })
  199. // #endif
  200. },
  201. onSubscribe() {
  202. if (this.subscribeLive === '立即预约') {
  203. const _this = this
  204. // #ifdef MP-WEIXIN
  205. uni.requestSubscribeMessage({
  206. tmplIds: [ startLiveTemplate ],
  207. success(res) {
  208. if (res[startLiveTemplate] === 'accept') {
  209. NET.request(API.SubScribeLive, { id: _this.liveData.id }, 'post')
  210. .then((res) => {
  211. if (res.data) {
  212. _this.subscribeLive = '已预约'
  213. } else {
  214. uni.showToast({
  215. title: res.message || '订阅失败,请稍后再试!',
  216. icon: 'none'
  217. })
  218. }
  219. })
  220. .catch((err) => {
  221. uni.showToast({
  222. title: res.message || '订阅失败,请稍后再试!',
  223. icon: 'none'
  224. })
  225. })
  226. }
  227. }
  228. })
  229. // #endif
  230. }
  231. }
  232. }
  233. }
  234. </script>
  235. <style lang="scss" scoped>
  236. .live-box {
  237. position: relative;
  238. color: #fff;
  239. width: 100%;
  240. height: 100%;
  241. .cover-img {
  242. width: 100%;
  243. height: 100%;
  244. position: absolute;
  245. z-index: 0;
  246. }
  247. .user {
  248. display: flex;
  249. line-height: 60rpx;
  250. height: 64rpx;
  251. &-pic {
  252. .img {
  253. width: 60rpx;
  254. height: 60rpx;
  255. border: 2px solid rgba(255, 255, 255, 0.5019607843137255);
  256. border-radius: 50%;
  257. overflow: hidden;
  258. }
  259. }
  260. &-name {
  261. font-size: 28rpx;
  262. margin-left: 16rpx;
  263. overflow: hidden;
  264. text-overflow: ellipsis;
  265. white-space: nowrap;
  266. width: 245rpx;
  267. }
  268. }
  269. .live-ongoing {
  270. width: 100%;
  271. height: 100%;
  272. position: relative;
  273. .status {
  274. position: absolute;
  275. top: 22rpx;
  276. left: 22rpx;
  277. //width: 212upx;
  278. height: 48rpx;
  279. // background: rgba(0,0,0,0.3);
  280. // border: 2rpx solid rgba(255,255,255,0.3);
  281. border-radius: 24rpx;
  282. font-size: 20rpx;
  283. line-height: 44rpx;
  284. display: flex;
  285. // padding-right: 8rpx;
  286. &-state {
  287. width: 118rpx;
  288. height: 44rpx;
  289. background: linear-gradient(90deg, #C83732 0%, #E25C44 100%);
  290. opacity: 1;
  291. border-radius: 26rpx;
  292. display: flex;
  293. align-items: center;
  294. justify-content: center;
  295. .img {
  296. width: 20rpx;
  297. height: 20rpx;
  298. margin-right: 6rpx;
  299. }
  300. }
  301. &-num {
  302. min-width: 80rpx;
  303. padding: 0 8rpx;
  304. }
  305. }
  306. .user {
  307. position: absolute;
  308. bottom: 62rpx;
  309. left: 20rpx;
  310. }
  311. .products {
  312. position: absolute;
  313. left: 0rpx;
  314. bottom: 20rpx;
  315. width: 100%;
  316. padding: 0 20rpx;
  317. .swiper {
  318. height: 34rpx;
  319. line-height: 34rpx;
  320. font-size: 24rpx;
  321. overflow: hidden;
  322. text-overflow: ellipsis;
  323. white-space: nowrap;
  324. }
  325. }
  326. }
  327. .live-other {
  328. position: relative;
  329. height: 100%;
  330. display: flex;
  331. align-items: center;
  332. justify-content: center;
  333. .filter-box-warp {
  334. background-color: #000000;
  335. position: absolute;
  336. top: 0;
  337. left: 0;
  338. width: 100%;
  339. height: 100%;
  340. .filter-box {
  341. position: absolute;
  342. top: -30rpx;
  343. left: -30rpx;
  344. width: 348rpx;
  345. height: 464rpx;
  346. display: flex;
  347. align-items: center;
  348. justify-content: center;
  349. -webkit-filter: blur(20px);
  350. -moz-filter: blur(21px);
  351. -ms-filter: blur(20px);
  352. -o-filter: blur(20px);
  353. padding: 30rpx;
  354. box-sizing: content-box;
  355. }
  356. }
  357. .user {
  358. position: absolute;
  359. top: 20rpx;
  360. left: 20rpx;
  361. }
  362. .count-down {
  363. position: relative;
  364. .text {
  365. font-size: 26rpx;
  366. line-height: 36rpx;
  367. margin-bottom: 16rpx;
  368. opacity: 0.5;
  369. text-align: center;
  370. }
  371. .img {
  372. display: block;
  373. width: 80rpx;
  374. height: 80rpx;
  375. margin: 16rpx auto;
  376. }
  377. .time {
  378. display: flex;
  379. justify-content: space-around;
  380. align-items: center;
  381. &-item {
  382. min-width: 52rpx;
  383. padding: 0 5rpx;
  384. height: 52rpx;
  385. line-height: 52rpx;
  386. background: #FFFFFF;
  387. opacity: 1;
  388. border-radius: 6rpx;
  389. font-size: 26rpx;
  390. color: #C83732;
  391. text-align: center;
  392. .dot {
  393. line-height: 52rpx;
  394. }
  395. }
  396. }
  397. }
  398. .btn-subscribe {
  399. width: 200rpx;
  400. height: 64rpx;
  401. line-height: 64rpx;
  402. background: linear-gradient(90deg, #C83732 0%, #E25C44 100%);
  403. box-shadow: 0rpx 6rpx 12rpx rgba(233, 0, 0, 0.3);
  404. opacity: 1;
  405. border-radius: 6rpx;
  406. color: #fff;
  407. font-size: 24rpx;
  408. text-align: center;
  409. position: absolute;
  410. bottom: 60rpx;
  411. left: 50%;
  412. margin-left: -100rpx;
  413. &.subscribed {
  414. background: #FFFFFF;
  415. color: #999999;
  416. box-shadow: none;
  417. }
  418. }
  419. .endContainer {
  420. position: relative;
  421. .endBox {
  422. width: 40%;
  423. height: 60rpx;
  424. margin: 20rpx auto;
  425. display: flex;
  426. justify-content: space-between;
  427. align-items: flex-end;
  428. view {
  429. width: 0;
  430. border: 2rpx solid #FFF;
  431. }
  432. view:nth-of-type(1) {
  433. height: 20%;
  434. }
  435. view:nth-of-type(2) {
  436. height: 50%;
  437. }
  438. view:nth-of-type(3) {
  439. height: 30%;
  440. }
  441. view:nth-of-type(4) {
  442. height: 70%;
  443. }
  444. }
  445. }
  446. }
  447. }
  448. </style>