groupBuyList.vue 7.3 KB

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