Jacob
1 天以前 f7034f535b85264e0c5b18e82fc295dab2052304
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>
....@@ -70,8 +71,10 @@
7071 </view>
7172 </view>
7273 </view>
73
- <view @click="goLottery" class="lottery_banner" v-if="info && info.isLottery && info.lotteryBanner">
74
- <image class="lottery_bannerImg" :src="info.lotteryBanner" mode="widthFix">
74
+ <view @click="goLottery" class="lottery_banner"
75
+ v-if="info && info.isLottery && info.lotteryBanner && lotteryBannerReady">
76
+ <image class="lottery_bannerImg" :src="info.lotteryBanner" mode="widthFix"
77
+ :style="{ height: lotteryBannerHeight }">
7578 </image>
7679 </view>
7780 <view class="tag-img-container" v-if="info.zoneType == 7">
....@@ -588,7 +591,7 @@
588591 showMenu: false,
589592 selectedMenuArr: [],
590593 expireTime: [],
591
- expireTimeUnit: [],
594
+ countdownToken: 0,
592595 isPageShow: false,
593596 loginBackFn: null,
594597 errText: '',
....@@ -621,6 +624,8 @@
621624 stockEmpty: true,
622625 urlOption: {},
623626 homeTop: '200',
627
+ lotteryBannerHeight: '', //抽奖banner预计算高度(rpx),空字符串表示未取到,按原逻辑自适应
628
+ lotteryBannerReady: false, //是否已拿到banner尺寸(或已确认拿不到),未就绪前不渲染,避免首帧撑高
624629 hasNewPop: false,
625630 leftCoupon: {},
626631 rightCoupon: {},
....@@ -1295,6 +1300,9 @@
12951300 })
12961301 },
12971302 validAlert(data) {
1303
+ // 每次重新计算前作废旧的倒计时链,避免多条定时器同时刷新导致数字跳动
1304
+ this.countdownToken++
1305
+ const token = this.countdownToken
12981306 const {
12991307 startTime,
13001308 expireTime
....@@ -1313,17 +1321,17 @@
13131321 let minutes = startTimetamp.getMinutes()
13141322 minutes = minutes < 10 ? '0' + minutes : minutes;
13151323
1316
- this.countdownTimer(startTime.split('-').join('/'), 'start')
1324
+ this.countdownTimer(startTime.split('-').join('/'), 'start', token)
13171325 this.startOrEnd = "后开启"
13181326 return
1319
- }
1320
- }
1321
- if (expireTime) {
1322
- const expireTimetamp = new Date(expireTime.split('-').join('/')).getTime();
1323
- if (expireTimetamp < nowTimestamp) {
1327
+ }
1328
+ }
1329
+ if (expireTime) {
1330
+ const expireTimetamp = new Date(expireTime.split('-').join('/')).getTime();
1331
+ if (expireTimetamp < nowTimestamp) {
13241332 return
1325
- }
1326
- this.countdownTimer(expireTime.split('-').join('/'), 'end')
1333
+ }
1334
+ this.countdownTimer(expireTime.split('-').join('/'), 'end', token)
13271335 this.startOrEnd = "后结束"
13281336 }
13291337 },
....@@ -1445,6 +1453,29 @@
14451453 this.$refs.popNewuserRef.show()
14461454 }
14471455 },
1456
+ // 预取抽奖banner真实宽高,计算出高度后再渲染,避免加载前被组件默认高度(225px)撑大
1457
+ setLotteryBannerSize(info) {
1458
+ let that = this
1459
+ that.lotteryBannerHeight = ''
1460
+ that.lotteryBannerReady = false
1461
+ if (!info || !info.isLottery || !info.lotteryBanner) {
1462
+ that.lotteryBannerReady = true
1463
+ return
1464
+ }
1465
+ uni.getImageInfo({
1466
+ src: info.lotteryBanner,
1467
+ success: (img) => {
1468
+ if (img.width > 0 && img.height > 0) {
1469
+ that.lotteryBannerHeight = (690 * img.height / img.width).toFixed(2) + 'rpx'
1470
+ }
1471
+ that.lotteryBannerReady = true
1472
+ },
1473
+ fail: (err) => {
1474
+ console.warn('抽奖banner尺寸预取失败,回退为自适应', err)
1475
+ that.lotteryBannerReady = true
1476
+ }
1477
+ })
1478
+ },
14481479 getZoneDetail: function () {
14491480 let that = this;
14501481 let belong = this.$store.getters.belong;
....@@ -1452,6 +1483,8 @@
14521483 console.log('专区详情入参::', params)
14531484 getZoneDetail(that.id, params).then(res => {
14541485 that.info = res.data;
1486
+ //that.info.zoneType=8;
1487
+ that.setLotteryBannerSize(res.data);
14551488 that.updateZoneShareMenu();
14561489 if (res.data.zoneType == 6) {
14571490 that.setYueyangZone()
....@@ -1461,7 +1494,7 @@
14611494 that.homeTop = that.getHomeTop(0.4)
14621495 }
14631496 that.expireTime = [];
1464
- that.expireTimeUnit = [];
1497
+ that.countdownToken++;
14651498 that.where.page = 1;
14661499 that.getGoodList()
14671500 that.validAlert(res.data)
....@@ -1487,8 +1520,9 @@
14871520 }
14881521 });
14891522 },
1490
- countdownTimer(expireTime, type) {
1523
+ countdownTimer(expireTime, type, token) {
14911524 if (!expireTime) return
1525
+ if (token !== undefined && token !== this.countdownToken) return
14921526 let that = this;
14931527 // 结束时间的时间戳(需传入一个有效的日期对象或时间戳)
14941528 const endTimestamp = new Date(expireTime).getTime();
....@@ -1500,56 +1534,56 @@
15001534 if (type == 'end') {
15011535 if (diff <= 0) {
15021536 this.expireTime = []
1503
- this.expireTimeUnit = []
15041537 return
15051538 } else { }
15061539 } else if (type == 'start') {
15071540 if (diff <= 0) {
15081541 this.expireTime = []
1509
- this.expireTimeUnit = []
15101542 const expireTime2 = that.info.expireTime
15111543 if (expireTime2) {
15121544 const expireTimetamp = new Date(expireTime2.split('-').join('/')).getTime();
15131545 if (expireTimetamp < nowTimestamp) {
15141546 return
15151547 }
1516
- this.countdownTimer(expireTime2.split('-').join('/'), 'end')
1548
+ this.countdownTimer(expireTime2.split('-').join('/'), 'end', token)
15171549 this.startOrEnd = "后结束"
15181550 }
15191551 return
15201552 }
15211553 }
1554
+ const pad = num => (num < 10 ? '0' + num : String(num))
15221555 const res = []
1523
- const unit = []
15241556 const days = Math.floor(diff / (1000 * 60 * 60 * 24))
15251557 diff %= (1000 * 60 * 60 * 24)
1526
- if (days > 0) {
1527
- res.push(days);
1528
- unit.push('日')
1529
- }
15301558 // 转换为小时、分钟、秒
15311559 const hours = Math.floor(diff / (1000 * 60 * 60));
15321560 diff %= (1000 * 60 * 60); // 剩余毫秒数
1533
- if (hours > 0) {
1534
- res.push(hours < 10 ? '0' + hours : hours);
1535
- unit.push('时')
1536
- }
15371561 const minutes = Math.floor(diff / (1000 * 60));
1538
- if (minutes > 0) {
1539
- res.push(minutes < 10 ? '0' + minutes : minutes);
1540
- unit.push('分')
1541
- }
15421562 diff %= (1000 * 60); // 剩余毫秒数
15431563 const seconds = Math.floor(diff / 1000);
1544
- if (seconds > 0) {
1545
- res.push(seconds < 10 ? '0' + seconds : seconds);
1546
- unit.push('秒')
1564
+ // 天数为 0 时不展示,时/分/秒始终展示(补 0),保证倒计时长度稳定
1565
+ if (days > 0) {
1566
+ res.push({
1567
+ digits: pad(days).split(''),
1568
+ unit: '日'
1569
+ })
15471570 }
1571
+ res.push({
1572
+ digits: pad(hours).split(''),
1573
+ unit: '时'
1574
+ })
1575
+ res.push({
1576
+ digits: pad(minutes).split(''),
1577
+ unit: '分'
1578
+ })
1579
+ res.push({
1580
+ digits: pad(seconds).split(''),
1581
+ unit: '秒'
1582
+ })
15481583 this.expireTime = res
1549
- this.expireTimeUnit = unit
15501584 if (this.isPageShow) {
15511585 setTimeout(() => {
1552
- that.countdownTimer(expireTime, type)
1586
+ that.countdownTimer(expireTime, type, token)
15531587 }, 1000)
15541588 }
15551589 },
....@@ -2082,11 +2116,22 @@
20822116 padding: 15rpx;
20832117 padding-left: 30rpx;
20842118 color: #979797;
2119
+ font-size: 26rpx;
20852120
20862121 .num {
2122
+ display: inline-block;
20872123 color: #000000;
20882124 background: #FFE20E;
2089
- padding: 0 10rpx;
2125
+ padding: 0 6rpx;
2126
+ margin: 0 4rpx;
2127
+ vertical-align: middle;
2128
+ }
2129
+
2130
+ /* 每个数字固定宽度,避免倒计时跳动时宽度忽长忽短 */
2131
+ .digit {
2132
+ display: inline-block;
2133
+ width: 19rpx;
2134
+ text-align: center;
20902135 }
20912136 }
20922137 </style>