Jacob
1 天以前 24b8f2afa92ce7bbe490185ecbab869c460b798b
pages/zone/index.vue
....@@ -20,8 +20,9 @@
2020 <view v-else>
2121 <view class="expireTime" v-if="expireTime && expireTime.length">
2222 <text>专区将在</text>
23
- <text v-for="(val, index) in expireTime" :key="index">
24
- <text class="num">{{ val }}</text>{{ expireTimeUnit[index] }}
23
+ <text v-for="(item, index) in expireTime" :key="index">
24
+ <text class="num"><text v-for="(char, ci) in item.digits" :key="ci"
25
+ class="digit">{{ char }}</text></text>{{ item.unit }}
2526 </text>
2627 <text>{{ startOrEnd }}</text>
2728 </view>
....@@ -89,7 +90,8 @@
8990 </view>
9091 </view>
9192 <view @click="goLottery" class="lottery_banner" v-if="info && info.isLottery && info.lotteryBanner">
92
- <image class="lottery_bannerImg" :src="info.lotteryBanner" mode="widthFix">
93
+ <image class="lottery_bannerImg" :src="info.lotteryBanner" mode="widthFix"
94
+ :style="{ height: lotteryBannerHeight }">
9395 </image>
9496 </view>
9597 <view class="tag-img-container" v-if="info.zoneType == 7">
....@@ -606,7 +608,7 @@
606608 showMenu: false,
607609 selectedMenuArr: [],
608610 expireTime: [],
609
- expireTimeUnit: [],
611
+ countdownToken: 0,
610612 isPageShow: false,
611613 loginBackFn: null,
612614 errText: '',
....@@ -641,6 +643,7 @@
641643 stockEmpty: true,
642644 urlOption: {},
643645 homeTop: '200',
646
+ lotteryBannerHeight: '', //抽奖banner预计算高度(rpx),空字符串表示未取到,按原逻辑自适应
644647 hasNewPop: false,
645648 leftCoupon: {},
646649 rightCoupon: {},
....@@ -1326,6 +1329,9 @@
13261329 })
13271330 },
13281331 validAlert(data) {
1332
+ // 每次重新计算前作废旧的倒计时链,避免多条定时器同时刷新导致数字跳动
1333
+ this.countdownToken++
1334
+ const token = this.countdownToken
13291335 const {
13301336 startTime,
13311337 expireTime
....@@ -1344,17 +1350,17 @@
13441350 let minutes = startTimetamp.getMinutes()
13451351 minutes = minutes < 10 ? '0' + minutes : minutes;
13461352
1347
- this.countdownTimer(startTime.split('-').join('/'), 'start')
1353
+ this.countdownTimer(startTime.split('-').join('/'), 'start', token)
13481354 this.startOrEnd = "后开启"
13491355 return
1350
- }
1351
- }
1352
- if (expireTime) {
1353
- const expireTimetamp = new Date(expireTime.split('-').join('/')).getTime();
1354
- if (expireTimetamp < nowTimestamp) {
1356
+ }
1357
+ }
1358
+ if (expireTime) {
1359
+ const expireTimetamp = new Date(expireTime.split('-').join('/')).getTime();
1360
+ if (expireTimetamp < nowTimestamp) {
13551361 return
1356
- }
1357
- this.countdownTimer(expireTime.split('-').join('/'), 'end')
1362
+ }
1363
+ this.countdownTimer(expireTime.split('-').join('/'), 'end', token)
13581364 this.startOrEnd = "后结束"
13591365 }
13601366 },
....@@ -1476,6 +1482,21 @@
14761482 this.$refs.popNewuserRef.show()
14771483 }
14781484 },
1485
+ // 预取抽奖banner真实宽高,计算出高度后再渲染,避免加载前被组件默认高度(225px)撑大
1486
+ setLotteryBannerSize(info) {
1487
+ let that = this
1488
+ that.lotteryBannerHeight = ''
1489
+ if (!info || !info.isLottery || !info.lotteryBanner) return
1490
+ uni.getImageInfo({
1491
+ src: info.lotteryBanner,
1492
+ success: (img) => {
1493
+ if (img.width > 0 && img.height > 0) {
1494
+ that.lotteryBannerHeight = (690 * img.height / img.width).toFixed(2) + 'rpx'
1495
+ }
1496
+ },
1497
+ fail: () => { }
1498
+ })
1499
+ },
14791500 getZoneDetail: function () {
14801501 let that = this;
14811502 let belong = this.$store.getters.belong;
....@@ -1483,7 +1504,7 @@
14831504 console.log('专区详情入参::', params)
14841505 getZoneDetail(that.id, params).then(res => {
14851506 that.info = res.data;
1486
- //that.info.zoneType=8;
1507
+ that.setLotteryBannerSize(res.data);
14871508 that.updateZoneShareMenu();
14881509 if (res.data.zoneType == 6) {
14891510 that.setYueyangZone()
....@@ -1493,7 +1514,7 @@
14931514 that.homeTop = that.getHomeTop(0.4)
14941515 }
14951516 that.expireTime = [];
1496
- that.expireTimeUnit = [];
1517
+ that.countdownToken++;
14971518 that.where.page = 1;
14981519 that.getGoodList()
14991520 that.validAlert(res.data)
....@@ -1519,8 +1540,9 @@
15191540 }
15201541 });
15211542 },
1522
- countdownTimer(expireTime, type) {
1543
+ countdownTimer(expireTime, type, token) {
15231544 if (!expireTime) return
1545
+ if (token !== undefined && token !== this.countdownToken) return
15241546 let that = this;
15251547 // 结束时间的时间戳(需传入一个有效的日期对象或时间戳)
15261548 const endTimestamp = new Date(expireTime).getTime();
....@@ -1532,56 +1554,56 @@
15321554 if (type == 'end') {
15331555 if (diff <= 0) {
15341556 this.expireTime = []
1535
- this.expireTimeUnit = []
15361557 return
15371558 } else { }
15381559 } else if (type == 'start') {
15391560 if (diff <= 0) {
15401561 this.expireTime = []
1541
- this.expireTimeUnit = []
15421562 const expireTime2 = that.info.expireTime
15431563 if (expireTime2) {
15441564 const expireTimetamp = new Date(expireTime2.split('-').join('/')).getTime();
15451565 if (expireTimetamp < nowTimestamp) {
15461566 return
15471567 }
1548
- this.countdownTimer(expireTime2.split('-').join('/'), 'end')
1568
+ this.countdownTimer(expireTime2.split('-').join('/'), 'end', token)
15491569 this.startOrEnd = "后结束"
15501570 }
15511571 return
15521572 }
15531573 }
1574
+ const pad = num => (num < 10 ? '0' + num : String(num))
15541575 const res = []
1555
- const unit = []
15561576 const days = Math.floor(diff / (1000 * 60 * 60 * 24))
15571577 diff %= (1000 * 60 * 60 * 24)
1558
- if (days > 0) {
1559
- res.push(days);
1560
- unit.push('日')
1561
- }
15621578 // 转换为小时、分钟、秒
15631579 const hours = Math.floor(diff / (1000 * 60 * 60));
15641580 diff %= (1000 * 60 * 60); // 剩余毫秒数
1565
- if (hours > 0) {
1566
- res.push(hours < 10 ? '0' + hours : hours);
1567
- unit.push('时')
1568
- }
15691581 const minutes = Math.floor(diff / (1000 * 60));
1570
- if (minutes > 0) {
1571
- res.push(minutes < 10 ? '0' + minutes : minutes);
1572
- unit.push('分')
1573
- }
15741582 diff %= (1000 * 60); // 剩余毫秒数
15751583 const seconds = Math.floor(diff / 1000);
1576
- if (seconds > 0) {
1577
- res.push(seconds < 10 ? '0' + seconds : seconds);
1578
- unit.push('秒')
1584
+ // 天数为 0 时不展示,时/分/秒始终展示(补 0),保证倒计时长度稳定
1585
+ if (days > 0) {
1586
+ res.push({
1587
+ digits: pad(days).split(''),
1588
+ unit: '日'
1589
+ })
15791590 }
1591
+ res.push({
1592
+ digits: pad(hours).split(''),
1593
+ unit: '时'
1594
+ })
1595
+ res.push({
1596
+ digits: pad(minutes).split(''),
1597
+ unit: '分'
1598
+ })
1599
+ res.push({
1600
+ digits: pad(seconds).split(''),
1601
+ unit: '秒'
1602
+ })
15801603 this.expireTime = res
1581
- this.expireTimeUnit = unit
15821604 if (this.isPageShow) {
15831605 setTimeout(() => {
1584
- that.countdownTimer(expireTime, type)
1606
+ that.countdownTimer(expireTime, type, token)
15851607 }, 1000)
15861608 }
15871609 },
....@@ -2156,11 +2178,22 @@
21562178 padding: 15rpx;
21572179 padding-left: 30rpx;
21582180 color: #979797;
2181
+ font-size: 26rpx;
21592182
21602183 .num {
2184
+ display: inline-block;
21612185 color: #000000;
21622186 background: #FFE20E;
2163
- padding: 0 10rpx;
2187
+ padding: 0 6rpx;
2188
+ margin: 0 4rpx;
2189
+ vertical-align: middle;
2190
+ }
2191
+
2192
+ /* 每个数字固定宽度,避免倒计时跳动时宽度忽长忽短 */
2193
+ .digit {
2194
+ display: inline-block;
2195
+ width: 19rpx;
2196
+ text-align: center;
21642197 }
21652198 }
21662199 </style>