FlashSale.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <view class="group-goods" v-if="flashSaleList.length > 0">
  3. <view class="title-box x-bc">
  4. <view class="left-title">
  5. <text class="title-icon">⚡</text>
  6. <text class="title">限时秒杀</text>
  7. <view class="home-countdown" v-if="globalCountdown > 0">
  8. <text class="time-block">{{ formatTimeObj(globalCountdown).h }}</text>
  9. <text class="time-colon">:</text>
  10. <text class="time-block">{{ formatTimeObj(globalCountdown).m }}</text>
  11. <text class="time-colon">:</text>
  12. <text class="time-block">{{ formatTimeObj(globalCountdown).s }}</text>
  13. </view>
  14. </view>
  15. <view class="group-people x-f" @tap="navTo('/pages_index/index/flashSaleList')">
  16. <text class="tip">更多</text>
  17. <text class="cuIcon-right">></text>
  18. </view>
  19. </view>
  20. <view class="goods-box">
  21. <view class="min-goods" v-for="(item, index) in flashSaleList" :key="item.id" @tap="showProduct(item)">
  22. <view class="img-box">
  23. <image class="img" :src="item.productImage" mode="aspectFill"></image>
  24. </view>
  25. <view class="title ellipsis">{{ item.productName }}</view>
  26. <view class="price-box">
  27. <text class="price">¥{{ item.flashPrice }}</text>
  28. <view :class="['btn-grab', item.activityStatus === 'not_started' ? 'disabled' : '']">抢</view>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. import { getActiveFlashSaleList, getFlashSaleServerTime } from '@/api/flashSale.js';
  36. export default {
  37. name: "FlashSale",
  38. data() {
  39. return {
  40. flashSaleList: [],
  41. timer: null,
  42. globalCountdown: 0,
  43. serverTimestamp: 0
  44. };
  45. },
  46. mounted() {
  47. this.getFlashSaleData();
  48. },
  49. beforeDestroy() {
  50. if (this.timer) {
  51. clearInterval(this.timer);
  52. }
  53. },
  54. methods: {
  55. async getFlashSaleData() {
  56. try {
  57. // 并发请求列表和服务器时间
  58. const [listRes, timeRes] = await Promise.all([
  59. getActiveFlashSaleList(),
  60. getFlashSaleServerTime()
  61. ]);
  62. let currentServerTime = Date.now();
  63. if (timeRes.code === 0 || timeRes.code === 200) {
  64. currentServerTime = timeRes.serverTimestamp || timeRes.data?.serverTimestamp || Date.now();
  65. } else if (listRes.serverTimestamp) {
  66. currentServerTime = listRes.serverTimestamp;
  67. }
  68. // 将服务器时间转为秒级用于校准倒计时
  69. const serverTimeSec = Math.floor(currentServerTime / 1000);
  70. if (listRes.code === 0 || listRes.code === 200) {
  71. let list = listRes.data || [];
  72. this.flashSaleList = list.slice(0, 6);
  73. // 基于服务器时间校准倒计时
  74. this.flashSaleList.forEach(item => {
  75. let targetTime = 0;
  76. if (item.activityStatus === 'not_started') {
  77. targetTime = Math.floor(new Date(item.startTime.replace(/-/g, '/')).getTime() / 1000);
  78. } else if (item.activityStatus === 'ongoing') {
  79. targetTime = Math.floor(new Date(item.endTime.replace(/-/g, '/')).getTime() / 1000);
  80. }
  81. if (targetTime > 0) {
  82. let diff = targetTime - serverTimeSec;
  83. item.countdown = diff > 0 ? diff : 0;
  84. }
  85. });
  86. if (this.flashSaleList.length > 0) {
  87. this.globalCountdown = this.flashSaleList[0].countdown;
  88. }
  89. this.startCountdown();
  90. }
  91. } catch (error) {
  92. console.error('获取秒杀数据失败', error);
  93. }
  94. },
  95. startCountdown() {
  96. if (this.timer) {
  97. clearInterval(this.timer);
  98. }
  99. this.timer = setInterval(() => {
  100. let hasCountdown = false;
  101. if (this.globalCountdown > 0) {
  102. this.globalCountdown--;
  103. }
  104. this.flashSaleList.forEach(item => {
  105. if (item.countdown > 0) {
  106. item.countdown--;
  107. hasCountdown = true;
  108. } else if (item.countdown === 0 && item.activityStatus === 'not_started') {
  109. item.activityStatus = 'ongoing';
  110. // 到达开始时间后,将倒计时切换为距离结束时间的倒计时
  111. let endTimeSec = Math.floor(new Date(item.endTime.replace(/-/g, '/')).getTime() / 1000);
  112. let nowSec = Math.floor(Date.now() / 1000); // 粗略计算,可结合初始偏差
  113. let diff = endTimeSec - nowSec;
  114. item.countdown = diff > 0 ? diff : 0;
  115. if (item.countdown > 0) hasCountdown = true;
  116. } else if (item.countdown === 0 && item.activityStatus === 'ongoing') {
  117. item.activityStatus = 'ended';
  118. }
  119. });
  120. if (!hasCountdown && this.globalCountdown <= 0) {
  121. clearInterval(this.timer);
  122. }
  123. }, 1000);
  124. },
  125. formatTimeObj(seconds) {
  126. if (!seconds || seconds <= 0) return { h: '00', m: '00', s: '00' };
  127. let h = Math.floor(seconds / 3600);
  128. let m = Math.floor((seconds % 3600) / 60);
  129. let s = seconds % 60;
  130. return {
  131. h: h.toString().padStart(2, '0'),
  132. m: m.toString().padStart(2, '0'),
  133. s: s.toString().padStart(2, '0')
  134. };
  135. },
  136. navTo(url) {
  137. uni.navigateTo({
  138. url: url
  139. });
  140. },
  141. showProduct(item) {
  142. uni.navigateTo({
  143. url: '/pages_index/index/activityProductDetail?type=flash&id=' + item.id
  144. });
  145. }
  146. }
  147. }
  148. </script>
  149. <style lang="scss" scoped>
  150. .group-goods {
  151. position: relative;
  152. z-index: 1;
  153. background: linear-gradient(180deg, #fff0f0 0%, #FFFFFF 20%);
  154. border-radius: 16upx;
  155. margin-bottom: 20upx;
  156. margin-top: 20upx;
  157. padding: 20upx;
  158. }
  159. .x-bc {
  160. display: flex;
  161. justify-content: space-between;
  162. align-items: center;
  163. }
  164. .x-f {
  165. display: flex;
  166. align-items: center;
  167. }
  168. .title-box {
  169. padding-bottom: 20rpx;
  170. .left-title {
  171. display: flex;
  172. align-items: center;
  173. .title-icon {
  174. font-size: 36rpx;
  175. color: #ff6a00;
  176. margin-right: 10rpx;
  177. }
  178. .title {
  179. font-size: 32rpx;
  180. font-weight: bold;
  181. color: #333;
  182. margin-right: 16rpx;
  183. }
  184. .home-countdown {
  185. display: flex;
  186. align-items: center;
  187. .time-block {
  188. background: #ff6a00;
  189. color: #fff;
  190. font-size: 20rpx;
  191. padding: 2rpx 6rpx;
  192. border-radius: 6rpx;
  193. line-height: 28rpx;
  194. }
  195. .time-colon {
  196. color: #ff6a00;
  197. font-size: 24rpx;
  198. margin: 0 4rpx;
  199. font-weight: bold;
  200. }
  201. }
  202. }
  203. .group-people {
  204. .tip {
  205. font-size: 24rpx;
  206. color: #999999;
  207. }
  208. .cuIcon-right {
  209. font-size: 24rpx;
  210. color: #999;
  211. margin-left: 4rpx;
  212. }
  213. }
  214. }
  215. .goods-box {
  216. display: flex;
  217. flex-wrap: wrap;
  218. justify-content: start;
  219. .min-goods {
  220. width: 210rpx;
  221. background: #fff;
  222. margin-bottom: 20rpx;
  223. margin: 0 14rpx;
  224. border-radius: 12rpx;
  225. overflow: hidden;
  226. .img-box {
  227. width: 210rpx;
  228. height: 210rpx;
  229. overflow: hidden;
  230. .img {
  231. width: 100%;
  232. height: 100%;
  233. background-color: #f5f5f5;
  234. }
  235. }
  236. .title {
  237. font-size: 24rpx;
  238. color: #333;
  239. margin: 10rpx 8rpx;
  240. line-height: 34rpx;
  241. height: 34rpx;
  242. }
  243. .ellipsis {
  244. white-space: nowrap;
  245. overflow: hidden;
  246. text-overflow: ellipsis;
  247. }
  248. .price-box {
  249. display: flex;
  250. justify-content: space-between;
  251. align-items: center;
  252. margin: 0 8rpx 10rpx;
  253. border: 1rpx solid #ff6a00;
  254. border-radius: 6rpx;
  255. overflow: hidden;
  256. .price {
  257. font-size: 24rpx;
  258. font-weight: bold;
  259. color: #ff6a00;
  260. padding: 0 6rpx;
  261. flex: 1;
  262. }
  263. .btn-grab {
  264. background: #ff6a00;
  265. color: #fff;
  266. font-size: 22rpx;
  267. padding: 4rpx 12rpx;
  268. &.disabled {
  269. background: #ccc;
  270. color: #fff;
  271. }
  272. }
  273. }
  274. }
  275. }
  276. </style>