seckill.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <template>
  2. <view>
  3. <view>
  4. <scroll-view scroll-x class="scroll-view">
  5. <view class="scroll-view-item-wrapper" v-for="item in tabs" :key="item" @click="onTabClick(item)">
  6. <view class="scroll-view-item" :class="{active:currentTab===item}">
  7. <view class="time">
  8. {{item}}
  9. </view>
  10. <view class="status">
  11. {{tabStatus[item]}}
  12. </view>
  13. </view>
  14. </view>
  15. </scroll-view>
  16. </view>
  17. <view class="product-list">
  18. <view class="product-item" v-for="item in currentList" :key="item.id">
  19. <view class="image">
  20. <image :src="JSON.parse(item.image)[0].url" mode="aspectFit" style="max-width: 100%;max-height: 100%;">
  21. </image>
  22. </view>
  23. <view class="content">
  24. <view class="title">
  25. {{item.title}}
  26. </view>
  27. <view class="progress">
  28. <view class="piece">
  29. 仅剩{{item.stock}}{{item.unitName}}
  30. </view>
  31. </view>
  32. <view class="tag">
  33. <view class="text">
  34. 限时价
  35. </view>
  36. </view>
  37. <view class="box">
  38. <view class="price-box">
  39. <view class="price">
  40. <view class="price-small">
  41. </view>
  42. <view class="price-large">
  43. {{item.price}}
  44. </view>
  45. </view>
  46. </view>
  47. <view>
  48. <view class="btn" @click="onSeckill(item)">
  49. {{idStatusMap[item.id]===SECKILL_STATUS[1]?'马上抢':idStatusMap[item.id]}}
  50. </view>
  51. </view>
  52. </view>
  53. </view>
  54. </view>
  55. </view>
  56. </view>
  57. </template>
  58. <script>
  59. import dayjs from 'dayjs'
  60. import {
  61. getTime
  62. } from '../../util'
  63. const SECKILL_STATUS = {
  64. 0: '已结束',
  65. 1: '抢购中',
  66. 2: '即将开始'
  67. }
  68. export default {
  69. data() {
  70. return {
  71. seckillStartTimeMap: {},
  72. idStatusMap: {},
  73. currentTab: null,
  74. SECKILL_STATUS
  75. }
  76. },
  77. computed: {
  78. tabs() {
  79. return Object.keys(this.seckillStartTimeMap)
  80. },
  81. currentList() {
  82. return this.seckillStartTimeMap[this.currentTab] || []
  83. },
  84. tabStatus() {
  85. const {
  86. idStatusMap,
  87. seckillStartTimeMap
  88. } = this
  89. const statusMap = {}
  90. for (const k in seckillStartTimeMap) {
  91. statusMap[k] = []
  92. const list = seckillStartTimeMap[k]
  93. let status = SECKILL_STATUS[2]
  94. list.forEach(({
  95. id
  96. }) => {
  97. const _s = idStatusMap[id]
  98. if (_s === SECKILL_STATUS[0] && status === SECKILL_STATUS[1]) {
  99. return
  100. }
  101. status = _s
  102. })
  103. statusMap[k] = status
  104. }
  105. if (this.currentTab === null) {
  106. Object.keys(statusMap).forEach(el => {
  107. const v = statusMap[el]
  108. if (v === SECKILL_STATUS[1] && this.currentTab === null) {
  109. this.currentTab = el
  110. }
  111. })
  112. }
  113. return statusMap
  114. }
  115. },
  116. mounted() {
  117. this.listCurrent()
  118. },
  119. methods: {
  120. listCurrent() {
  121. this.$api.request('get', 'seckill/app/listCurrent', {
  122. storageId: this.$store.state.storageId,
  123. sort: '0'
  124. }).then(({
  125. data = []
  126. }) => {
  127. this.getList(data)
  128. this.getProductStatus(data)
  129. })
  130. },
  131. getList(data) {
  132. this.seckillStartTimeMap = {}
  133. this.idStatusMap = {}
  134. for (let i = 0; i < data.length; i++) {
  135. const {
  136. seckillStartTime
  137. } = data[i]
  138. if (!(seckillStartTime in this.seckillStartTimeMap)) {
  139. this.seckillStartTimeMap[seckillStartTime] = []
  140. }
  141. this.seckillStartTimeMap[seckillStartTime].push(data[i])
  142. }
  143. },
  144. getProductStatus(data) {
  145. this.idStatusMap = {}
  146. const now = dayjs()
  147. for (let i = 0; i < data.length; i++) {
  148. const {
  149. seckillStartTime,
  150. id,
  151. seckillStopTime
  152. } = data[i]
  153. if (getTime(seckillStopTime).isBefore(now)) {
  154. this.idStatusMap[id] = SECKILL_STATUS[0]
  155. } else if (getTime(seckillStartTime).isAfter(now)) {
  156. this.idStatusMap[id] = SECKILL_STATUS[2]
  157. } else {
  158. this.idStatusMap[id] = SECKILL_STATUS[1]
  159. }
  160. }
  161. if (this.$_timer) {
  162. clearTimeout(this.$_timer)
  163. this.$_timer = null
  164. }
  165. this.$_timer = setTimeout(() => {
  166. this.getProductStatus(data)
  167. }, 1000)
  168. },
  169. onTabClick(v) {
  170. this.currentTab = v
  171. },
  172. onSeckill(item) {
  173. const {
  174. idStatusMap
  175. } = this
  176. const {
  177. id,
  178. productId
  179. } = item
  180. if (idStatusMap[id] !== SECKILL_STATUS[1]) {
  181. return
  182. }
  183. uni.navigateTo({
  184. url: `/pages/product/detail?id=${productId}`
  185. })
  186. }
  187. }
  188. }
  189. </script>
  190. <style lang="scss" scoped>
  191. .scroll-view {
  192. white-space: nowrap;
  193. width: 100%;
  194. display: flex;
  195. align-items: center;
  196. height: 128rpx;
  197. background-image: url('../../static/product/bg_seckill.png');
  198. }
  199. .scroll-view-item-wrapper {
  200. display: inline-block;
  201. height: 100%;
  202. }
  203. .scroll-view-item {
  204. flex-shrink: 0;
  205. display: flex;
  206. align-items: center;
  207. flex-direction: column;
  208. justify-content: center;
  209. color: #ffb2b2;
  210. min-width: 160rpx;
  211. height: 100%;
  212. .time {
  213. font-size: 32rpx;
  214. line-height: 32rpx;
  215. font-weight: 700;
  216. }
  217. .status {
  218. font-size: 24rpx;
  219. line-height: 24rpx;
  220. font-weight: 500;
  221. padding-top: 16rpx;
  222. }
  223. &.active {
  224. .time {
  225. color: #fff;
  226. font-size: 36rpx;
  227. line-height: 36rpx;
  228. }
  229. .status {
  230. color: #fff;
  231. font-size: 28rpx;
  232. line-height: 28rpx;
  233. font-weight: 700;
  234. }
  235. }
  236. }
  237. .product-list {
  238. width: 100%;
  239. display: flex;
  240. justify-content: space-between;
  241. flex-wrap: wrap;
  242. margin-top: 20rpx;
  243. padding-bottom: 32rpx;
  244. }
  245. .product-item {
  246. width: 49%;
  247. padding: 20rpx 20rpx 38rpx;
  248. border-radius: 12rpx;
  249. background-color: #fff;
  250. margin-bottom: 4%;
  251. position: relative;
  252. .image {
  253. width: 100%;
  254. height: 320rpx;
  255. }
  256. .content {
  257. width: 100%;
  258. padding-top: 16rpx;
  259. }
  260. .title {
  261. font-size: 26rpx;
  262. font-weight: 400;
  263. color: #333;
  264. word-break: break-all;
  265. overflow: hidden;
  266. text-overflow: ellipsis;
  267. margin-bottom: 20rpx;
  268. }
  269. .progress {
  270. overflow: hidden;
  271. background-color: #fff;
  272. width: 100%;
  273. border-radius: 20rpx;
  274. height: 36rpx;
  275. position: relative;
  276. border: 1px solid #eb3729;
  277. .piece {
  278. position: absolute;
  279. left: 0;
  280. right: 0;
  281. transform: translateY(-50%);
  282. top: 49%;
  283. font-size: 22rpx;
  284. word-wrap: normal;
  285. text-align: center;
  286. color: #eb3729;
  287. }
  288. }
  289. .tag {
  290. display: flex;
  291. padding-top: 26rpx;
  292. padding-bottom: 26rpx;
  293. color: #ff201f;
  294. font-size: 24rpx;
  295. .text {
  296. border: 1px solid #ff201f;
  297. padding: 8rpx 12rpx;
  298. border-radius: 12rpx;
  299. }
  300. }
  301. .box {
  302. width: 100%;
  303. display: flex;
  304. align-items: center;
  305. justify-content: space-between;
  306. .price-box {
  307. display: flex;
  308. flex-direction: column;
  309. align-items: center;
  310. justify-content: center;
  311. .price {
  312. display: flex;
  313. align-items: flex-end;
  314. color: #eb0909;
  315. }
  316. .price-small {
  317. font-size: 24rpx;
  318. }
  319. .price-large {
  320. font-size: 36rpx;
  321. font-weight: 600;
  322. }
  323. }
  324. .btn {
  325. font-size: 24rpx;
  326. color: #fff;
  327. width: 148rpx;
  328. height: 56rpx;
  329. border-radius: 4rpx;
  330. text-align: center;
  331. line-height: 56rpx;
  332. position: absolute;
  333. right: 32rpx;
  334. bottom: 32rpx;
  335. background-color: #eb3729;
  336. }
  337. }
  338. }
  339. </style>