Jacob
1 天以前 106fc09e267f67bb35a279ed1bf71068a61e73c4
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>
....@@ -88,8 +89,10 @@
8889 </view>
8990 </view>
9091 </view>
91
- <view @click="goLottery" class="lottery_banner" v-if="info && info.isLottery && info.lotteryBanner">
92
- <image class="lottery_bannerImg" :src="info.lotteryBanner" mode="widthFix">
92
+ <view @click="goLottery" class="lottery_banner"
93
+ v-if="info && info.isLottery && info.lotteryBanner && lotteryBannerReady">
94
+ <image class="lottery_bannerImg" :src="info.lotteryBanner" mode="widthFix"
95
+ :style="{ height: lotteryBannerHeight }">
9396 </image>
9497 </view>
9598 <view class="tag-img-container" v-if="info.zoneType == 7">
....@@ -606,7 +609,7 @@
606609 showMenu: false,
607610 selectedMenuArr: [],
608611 expireTime: [],
609
- expireTimeUnit: [],
612
+ countdownToken: 0,
610613 isPageShow: false,
611614 loginBackFn: null,
612615 errText: '',
....@@ -641,6 +644,8 @@
641644 stockEmpty: true,
642645 urlOption: {},
643646 homeTop: '200',
647
+ lotteryBannerHeight: '', //抽奖banner预计算高度(rpx),空字符串表示未取到,按原逻辑自适应
648
+ lotteryBannerReady: false, //是否已拿到banner尺寸(或已确认拿不到),未就绪前不渲染,避免首帧撑高
644649 hasNewPop: false,
645650 leftCoupon: {},
646651 rightCoupon: {},
....@@ -1326,6 +1331,9 @@
13261331 })
13271332 },
13281333 validAlert(data) {
1334
+ // 每次重新计算前作废旧的倒计时链,避免多条定时器同时刷新导致数字跳动
1335
+ this.countdownToken++
1336
+ const token = this.countdownToken
13291337 const {
13301338 startTime,
13311339 expireTime
....@@ -1344,17 +1352,17 @@
13441352 let minutes = startTimetamp.getMinutes()
13451353 minutes = minutes < 10 ? '0' + minutes : minutes;
13461354
1347
- this.countdownTimer(startTime.split('-').join('/'), 'start')
1355
+ this.countdownTimer(startTime.split('-').join('/'), 'start', token)
13481356 this.startOrEnd = "后开启"
13491357 return
1350
- }
1351
- }
1352
- if (expireTime) {
1353
- const expireTimetamp = new Date(expireTime.split('-').join('/')).getTime();
1354
- if (expireTimetamp < nowTimestamp) {
1358
+ }
1359
+ }
1360
+ if (expireTime) {
1361
+ const expireTimetamp = new Date(expireTime.split('-').join('/')).getTime();
1362
+ if (expireTimetamp < nowTimestamp) {
13551363 return
1356
- }
1357
- this.countdownTimer(expireTime.split('-').join('/'), 'end')
1364
+ }
1365
+ this.countdownTimer(expireTime.split('-').join('/'), 'end', token)
13581366 this.startOrEnd = "后结束"
13591367 }
13601368 },
....@@ -1476,6 +1484,29 @@
14761484 this.$refs.popNewuserRef.show()
14771485 }
14781486 },
1487
+ // 预取抽奖banner真实宽高,计算出高度后再渲染,避免加载前被组件默认高度(225px)撑大
1488
+ setLotteryBannerSize(info) {
1489
+ let that = this
1490
+ that.lotteryBannerHeight = ''
1491
+ that.lotteryBannerReady = false
1492
+ if (!info || !info.isLottery || !info.lotteryBanner) {
1493
+ that.lotteryBannerReady = true
1494
+ return
1495
+ }
1496
+ uni.getImageInfo({
1497
+ src: info.lotteryBanner,
1498
+ success: (img) => {
1499
+ if (img.width > 0 && img.height > 0) {
1500
+ that.lotteryBannerHeight = (690 * img.height / img.width).toFixed(2) + 'rpx'
1501
+ }
1502
+ that.lotteryBannerReady = true
1503
+ },
1504
+ fail: (err) => {
1505
+ console.warn('抽奖banner尺寸预取失败,回退为自适应', err)
1506
+ that.lotteryBannerReady = true
1507
+ }
1508
+ })
1509
+ },
14791510 getZoneDetail: function () {
14801511 let that = this;
14811512 let belong = this.$store.getters.belong;
....@@ -1483,7 +1514,7 @@
14831514 console.log('专区详情入参::', params)
14841515 getZoneDetail(that.id, params).then(res => {
14851516 that.info = res.data;
1486
- //that.info.zoneType=8;
1517
+ that.setLotteryBannerSize(res.data);
14871518 that.updateZoneShareMenu();
14881519 if (res.data.zoneType == 6) {
14891520 that.setYueyangZone()
....@@ -1493,7 +1524,7 @@
14931524 that.homeTop = that.getHomeTop(0.4)
14941525 }
14951526 that.expireTime = [];
1496
- that.expireTimeUnit = [];
1527
+ that.countdownToken++;
14971528 that.where.page = 1;
14981529 that.getGoodList()
14991530 that.validAlert(res.data)
....@@ -1519,8 +1550,9 @@
15191550 }
15201551 });
15211552 },
1522
- countdownTimer(expireTime, type) {
1553
+ countdownTimer(expireTime, type, token) {
15231554 if (!expireTime) return
1555
+ if (token !== undefined && token !== this.countdownToken) return
15241556 let that = this;
15251557 // 结束时间的时间戳(需传入一个有效的日期对象或时间戳)
15261558 const endTimestamp = new Date(expireTime).getTime();
....@@ -1532,56 +1564,56 @@
15321564 if (type == 'end') {
15331565 if (diff <= 0) {
15341566 this.expireTime = []
1535
- this.expireTimeUnit = []
15361567 return
15371568 } else { }
15381569 } else if (type == 'start') {
15391570 if (diff <= 0) {
15401571 this.expireTime = []
1541
- this.expireTimeUnit = []
15421572 const expireTime2 = that.info.expireTime
15431573 if (expireTime2) {
15441574 const expireTimetamp = new Date(expireTime2.split('-').join('/')).getTime();
15451575 if (expireTimetamp < nowTimestamp) {
15461576 return
15471577 }
1548
- this.countdownTimer(expireTime2.split('-').join('/'), 'end')
1578
+ this.countdownTimer(expireTime2.split('-').join('/'), 'end', token)
15491579 this.startOrEnd = "后结束"
15501580 }
15511581 return
15521582 }
15531583 }
1584
+ const pad = num => (num < 10 ? '0' + num : String(num))
15541585 const res = []
1555
- const unit = []
15561586 const days = Math.floor(diff / (1000 * 60 * 60 * 24))
15571587 diff %= (1000 * 60 * 60 * 24)
1558
- if (days > 0) {
1559
- res.push(days);
1560
- unit.push('日')
1561
- }
15621588 // 转换为小时、分钟、秒
15631589 const hours = Math.floor(diff / (1000 * 60 * 60));
15641590 diff %= (1000 * 60 * 60); // 剩余毫秒数
1565
- if (hours > 0) {
1566
- res.push(hours < 10 ? '0' + hours : hours);
1567
- unit.push('时')
1568
- }
15691591 const minutes = Math.floor(diff / (1000 * 60));
1570
- if (minutes > 0) {
1571
- res.push(minutes < 10 ? '0' + minutes : minutes);
1572
- unit.push('分')
1573
- }
15741592 diff %= (1000 * 60); // 剩余毫秒数
15751593 const seconds = Math.floor(diff / 1000);
1576
- if (seconds > 0) {
1577
- res.push(seconds < 10 ? '0' + seconds : seconds);
1578
- unit.push('秒')
1594
+ // 天数为 0 时不展示,时/分/秒始终展示(补 0),保证倒计时长度稳定
1595
+ if (days > 0) {
1596
+ res.push({
1597
+ digits: pad(days).split(''),
1598
+ unit: '日'
1599
+ })
15791600 }
1601
+ res.push({
1602
+ digits: pad(hours).split(''),
1603
+ unit: '时'
1604
+ })
1605
+ res.push({
1606
+ digits: pad(minutes).split(''),
1607
+ unit: '分'
1608
+ })
1609
+ res.push({
1610
+ digits: pad(seconds).split(''),
1611
+ unit: '秒'
1612
+ })
15801613 this.expireTime = res
1581
- this.expireTimeUnit = unit
15821614 if (this.isPageShow) {
15831615 setTimeout(() => {
1584
- that.countdownTimer(expireTime, type)
1616
+ that.countdownTimer(expireTime, type, token)
15851617 }, 1000)
15861618 }
15871619 },
....@@ -2156,11 +2188,22 @@
21562188 padding: 15rpx;
21572189 padding-left: 30rpx;
21582190 color: #979797;
2191
+ font-size: 26rpx;
21592192
21602193 .num {
2194
+ display: inline-block;
21612195 color: #000000;
21622196 background: #FFE20E;
2163
- padding: 0 10rpx;
2197
+ padding: 0 6rpx;
2198
+ margin: 0 4rpx;
2199
+ vertical-align: middle;
2200
+ }
2201
+
2202
+ /* 每个数字固定宽度,避免倒计时跳动时宽度忽长忽短 */
2203
+ .digit {
2204
+ display: inline-block;
2205
+ width: 19rpx;
2206
+ text-align: center;
21642207 }
21652208 }
21662209 </style>