flashSaleList.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <view class="container">
  3. <!-- 头部导航栏 (使用默认或者自定义) -->
  4. <!-- 商品列表 -->
  5. <view class="goods-list">
  6. <view class="goods-item" v-for="(item, index) in list" :key="item.id" @tap="showProduct(item)">
  7. <image class="goods-img" :src="item.productImage" mode="aspectFill"></image>
  8. <view class="goods-info">
  9. <view class="goods-name ellipsis2">{{ item.productName }}</view>
  10. <view class="countdown-box" v-if="item.activityStatus !== 'ended' && item.activityStatus !== 'sold_out'">
  11. <text class="status-text" style="margin-left: 0; margin-right: 10rpx;">{{ item.activityStatus === 'not_started' ? '即将开抢' : '抢购中' }}</text>
  12. <text class="time-text">{{ formatTime(item.countdown) }}</text>
  13. </view>
  14. <view class="goods-bottom">
  15. <view class="price-info">
  16. <view class="flash-price">¥<text class="num">{{ item.flashPrice }}</text></view>
  17. <view class="original-price">¥{{ item.originalPrice }}</view>
  18. </view>
  19. <view class="btn-box">
  20. <button
  21. class="grab-btn"
  22. :class="[
  23. item.activityStatus === 'not_started' ? 'not-started' : '',
  24. item.activityStatus === 'sold_out' ? 'sold-out' : '',
  25. item.activityStatus === 'ended' ? 'ended' : ''
  26. ]"
  27. >
  28. {{ getBtnText(item.activityStatus) }}
  29. </button>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. </view>
  35. <view class="empty-box" v-if="list.length === 0 && !loading">
  36. <text>暂无秒杀活动</text>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import { getActiveFlashSaleList, getFlashSaleServerTime } from '@/api/flashSale.js';
  42. export default {
  43. data() {
  44. return {
  45. list: [],
  46. timer: null,
  47. loading: true,
  48. serverTimestamp: 0
  49. };
  50. },
  51. onLoad() {
  52. this.fetchData();
  53. },
  54. onUnload() {
  55. this.clearTimer();
  56. },
  57. methods: {
  58. async fetchData() {
  59. this.loading = true;
  60. try {
  61. const [listRes, timeRes] = await Promise.all([
  62. getActiveFlashSaleList(),
  63. getFlashSaleServerTime()
  64. ]);
  65. this.loading = false;
  66. let currentServerTime = Date.now();
  67. if (timeRes.code === 0 || timeRes.code === 200) {
  68. currentServerTime = timeRes.serverTimestamp || timeRes.data?.serverTimestamp || Date.now();
  69. } else if (listRes.serverTimestamp) {
  70. currentServerTime = listRes.serverTimestamp;
  71. }
  72. const serverTimeSec = Math.floor(currentServerTime / 1000);
  73. if (listRes.code === 0 || listRes.code === 200) {
  74. this.list = listRes.data || [];
  75. this.list.forEach(item => {
  76. let targetTime = 0;
  77. if (item.activityStatus === 'not_started') {
  78. targetTime = Math.floor(new Date(item.startTime.replace(/-/g, '/')).getTime() / 1000);
  79. } else if (item.activityStatus === 'ongoing') {
  80. targetTime = Math.floor(new Date(item.endTime.replace(/-/g, '/')).getTime() / 1000);
  81. }
  82. if (targetTime > 0) {
  83. let diff = targetTime - serverTimeSec;
  84. item.countdown = diff > 0 ? diff : 0;
  85. }
  86. });
  87. this.startTimer();
  88. } else {
  89. uni.showToast({
  90. title: listRes.msg || '获取数据失败',
  91. icon: 'none'
  92. });
  93. }
  94. } catch (error) {
  95. this.loading = false;
  96. console.error(error);
  97. }
  98. },
  99. startTimer() {
  100. this.clearTimer();
  101. this.timer = setInterval(() => {
  102. let hasCountdown = false;
  103. this.list.forEach(item => {
  104. if (item.countdown > 0) {
  105. item.countdown--;
  106. hasCountdown = true;
  107. } else if (item.countdown === 0 && item.activityStatus === 'not_started') {
  108. item.activityStatus = 'ongoing';
  109. let endTimeSec = Math.floor(new Date(item.endTime.replace(/-/g, '/')).getTime() / 1000);
  110. let nowSec = Math.floor(Date.now() / 1000);
  111. let diff = endTimeSec - nowSec;
  112. item.countdown = diff > 0 ? diff : 0;
  113. if (item.countdown > 0) hasCountdown = true;
  114. } else if (item.countdown === 0 && item.activityStatus === 'ongoing') {
  115. item.activityStatus = 'ended';
  116. }
  117. });
  118. if (!hasCountdown) {
  119. this.clearTimer();
  120. }
  121. }, 1000);
  122. },
  123. clearTimer() {
  124. if (this.timer) {
  125. clearInterval(this.timer);
  126. this.timer = null;
  127. }
  128. },
  129. formatTime(seconds) {
  130. if (!seconds || seconds <= 0) return '00:00:00';
  131. let h = Math.floor(seconds / 3600);
  132. let m = Math.floor((seconds % 3600) / 60);
  133. let s = seconds % 60;
  134. return `${h.toString().padStart(2, '0')}:${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`;
  135. },
  136. getBtnText(status) {
  137. switch (status) {
  138. case 'not_started': return '即将开抢';
  139. case 'ongoing': return '立即抢购';
  140. case 'sold_out': return '已售罄';
  141. case 'ended': return '已结束';
  142. default: return '抢购';
  143. }
  144. },
  145. showProduct(item) {
  146. uni.navigateTo({
  147. url: '/pages_index/index/activityProductDetail?type=flash&id=' + item.id
  148. });
  149. }
  150. }
  151. };
  152. </script>
  153. <style lang="scss" scoped>
  154. .container {
  155. min-height: 100vh;
  156. background-color: #f5f5f5;
  157. padding: 20rpx;
  158. }
  159. .goods-list {
  160. .goods-item {
  161. display: flex;
  162. background-color: #fff;
  163. border-radius: 16rpx;
  164. padding: 20rpx;
  165. margin-bottom: 20rpx;
  166. .goods-img {
  167. width: 240rpx;
  168. height: 240rpx;
  169. border-radius: 12rpx;
  170. background-color: #f0f0f0;
  171. flex-shrink: 0;
  172. }
  173. .goods-info {
  174. flex: 1;
  175. margin-left: 20rpx;
  176. display: flex;
  177. flex-direction: column;
  178. justify-content: space-between;
  179. .goods-name {
  180. font-size: 28rpx;
  181. color: #333;
  182. font-weight: bold;
  183. line-height: 40rpx;
  184. }
  185. .ellipsis2 {
  186. display: -webkit-box;
  187. -webkit-box-orient: vertical;
  188. -webkit-line-clamp: 2;
  189. overflow: hidden;
  190. }
  191. .countdown-box {
  192. display: flex;
  193. align-items: center;
  194. margin-top: 10rpx;
  195. .time-text {
  196. font-size: 24rpx;
  197. color: #ff4a00;
  198. background-color: rgba(255, 74, 0, 0.1);
  199. padding: 2rpx 10rpx;
  200. border-radius: 6rpx;
  201. font-weight: bold;
  202. }
  203. .status-text {
  204. font-size: 22rpx;
  205. color: #999;
  206. margin-left: 10rpx;
  207. }
  208. }
  209. .goods-bottom {
  210. display: flex;
  211. justify-content: space-between;
  212. align-items: flex-end;
  213. margin-top: 10rpx;
  214. .price-info {
  215. .flash-price {
  216. color: #ff4a00;
  217. font-size: 24rpx;
  218. font-weight: bold;
  219. .num {
  220. font-size: 36rpx;
  221. }
  222. }
  223. .original-price {
  224. color: #999;
  225. font-size: 22rpx;
  226. text-decoration: line-through;
  227. margin-top: 4rpx;
  228. }
  229. }
  230. .btn-box {
  231. .grab-btn {
  232. margin: 0;
  233. padding: 0 24rpx;
  234. height: 56rpx;
  235. line-height: 56rpx;
  236. border-radius: 28rpx;
  237. font-size: 24rpx;
  238. color: #fff;
  239. background: linear-gradient(90deg, #ff6000, #ff4a00);
  240. border: none;
  241. &::after {
  242. display: none;
  243. }
  244. &.not-started {
  245. background: #cccccc;
  246. }
  247. &.sold-out {
  248. background: #cccccc;
  249. }
  250. &.ended {
  251. background: #cccccc;
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. }
  259. .empty-box {
  260. padding: 100rpx 0;
  261. text-align: center;
  262. color: #999;
  263. font-size: 28rpx;
  264. }
  265. </style>