Commit 5b9b00ab0ebdbc2db3f2f3e23eb6e67db382dbeb
1 parent
062942df
购好物,首页“折扣专区”和“积分商城”模块,定时切换商品的逻辑健壮性处理~
Showing
1 changed file
with
47 additions
and
27 deletions
src/pages/optimization2/optimization2.vue
| ... | ... | @@ -658,48 +658,68 @@ |
| 658 | 658 | // 判断是否有积分商城 |
| 659 | 659 | if (this.showIntegral && datas.integral_goods && datas.integral_goods.length) { |
| 660 | 660 | |
| 661 | - // 当前展示的折扣专区数据 | |
| 662 | - this.$set(this.discountData, 'now', [this.discountLists[0], this.discountLists[1]]); | |
| 661 | + // 判断折扣专区是否有数据 | |
| 662 | + if (this.discountLists.length) { | |
| 663 | + | |
| 664 | + // 当前展示的折扣专区数据(兼容数据不足 2 个的情况) | |
| 665 | + if (this.discountLists.length >= 2) { | |
| 666 | + this.$set(this.discountData, 'now', [this.discountLists[0], this.discountLists[1]]); | |
| 667 | + } else { | |
| 668 | + this.$set(this.discountData, 'now', [this.discountLists[0]]); | |
| 669 | + } | |
| 663 | 670 | |
| 664 | - // 设置折扣专区下一条数据数组下标 | |
| 665 | - this.$set(this.discountData, 'next', this.discountData.now.length); | |
| 671 | + // 设置折扣专区下一条数据数组下标(根据数组长度,兼容数据 <= 2 的情况) | |
| 672 | + this.$set(this.discountData, 'next', this.discountData.now.length < this.discountLists.length ? this.discountData.now.length : 0); | |
| 666 | 673 | |
| 667 | - // 设置折扣专区计时器,每3秒切换一次数据 | |
| 668 | - this.$nextTick(() => { | |
| 674 | + // 设置折扣专区计时器,每3秒切换一次数据 | |
| 675 | + this.$nextTick(() => { | |
| 669 | 676 | |
| 670 | - // 轮训计时器 | |
| 671 | - setInterval(() => { | |
| 672 | - | |
| 673 | - // 切换折扣专区数据 | |
| 674 | - this.fade(this.discountData, this.discountLists); | |
| 675 | - }, 3000); | |
| 676 | - }); | |
| 677 | + // 判断数据数量是否 > 2 ,如果是,执行切换轮训 | |
| 678 | + if (this.discountData.now.length < this.discountLists.length) { | |
| 679 | + | |
| 680 | + // 轮训计时器 | |
| 681 | + setInterval(() => { | |
| 682 | + | |
| 683 | + // 切换折扣专区数据 | |
| 684 | + this.fade(this.discountData, this.discountLists); | |
| 685 | + }, 3000); | |
| 686 | + } | |
| 687 | + }); | |
| 688 | + } | |
| 677 | 689 | |
| 678 | 690 | // 积分商城数据 |
| 679 | 691 | this.$set(this, 'pointsLists', datas.integral_goods); |
| 680 | 692 | |
| 681 | - // 当前展示的积分商城数据 | |
| 682 | - this.$set(this.pointsData, 'now', [this.pointsLists[0], this.pointsLists[1]]); | |
| 693 | + // 当前展示的积分商城数据(兼容数据不足 2 个的情况) | |
| 694 | + if (this.pointsLists.length >= 2) { | |
| 695 | + this.$set(this.pointsData, 'now', [this.pointsLists[0], this.pointsLists[1]]); | |
| 696 | + } else { | |
| 697 | + this.$set(this.pointsData, 'now', [this.pointsLists[0]]); | |
| 698 | + } | |
| 683 | 699 | |
| 684 | 700 | // 设置积分商城下一条数据数组下标 |
| 685 | - this.$set(this.pointsData, 'next', this.pointsData.now.length); | |
| 701 | + this.$set(this.pointsData, 'next', this.pointsData.now.length < this.pointsLists.length ? this.pointsData.now.length : 0); | |
| 686 | 702 | |
| 687 | 703 | // 设置积分商城计时器,每3秒切换一次数据 |
| 688 | 704 | this.$nextTick(() => { |
| 689 | 705 | |
| 690 | - // 和折扣专区岔开,延时1秒执行 | |
| 691 | - setTimeout(() => { | |
| 692 | - | |
| 693 | - // 随机生成轮训时间 | |
| 694 | - let timer = (parseInt(Math.random() * (500 + 1) + 3000, 10) || 3000); | |
| 706 | + // 判断数据数量是否 > 2 ,如果是,执行切换轮训 | |
| 707 | + if (this.pointsData.now.length < this.pointsLists.length) { | |
| 695 | 708 | |
| 696 | - // 轮训计时器 | |
| 697 | - setInterval(() => { | |
| 709 | + // 和折扣专区岔开,延时1秒执行 | |
| 710 | + setTimeout(() => { | |
| 698 | 711 | |
| 699 | - // 切换折扣专区数据 | |
| 700 | - this.fade(this.pointsData, this.pointsLists); | |
| 701 | - }, timer || 3000); | |
| 702 | - }, 1000); | |
| 712 | + // 随机生成轮训时间 | |
| 713 | + let timer = (parseInt(Math.random() * (500 + 1) + 3000, 10) || 3000); | |
| 714 | + | |
| 715 | + // 轮训计时器 | |
| 716 | + setInterval(() => { | |
| 717 | + | |
| 718 | + // 切换折扣专区数据 | |
| 719 | + this.fade(this.pointsData, this.pointsLists); | |
| 720 | + }, timer || 3000); | |
| 721 | + }, 1000); | |
| 722 | + } | |
| 703 | 723 | }); |
| 704 | 724 | } else { |
| 705 | 725 | ... | ... |