Commit a47cb52bbb7bcdc8c6fa63b81c05a475211a036f

Authored by yvan.ni
1 parent 206bdff9

1、秒杀加载慢的主要原因是定时器加载慢,吃内存! 另外定义变量timer_js来显示倒计时。接口合并优化获取redis

pages/activity/seckill_list/seckill_list.js
... ... @@ -17,7 +17,9 @@ Page({
17 17 isshow: 0,
18 18 ad_data: null,
19 19 max_sw_height: 200,
20   - is_retail_price:0
  20 + is_retail_price:0,
  21 +
  22 + timer_js:[]
21 23 },
22 24  
23 25 //------初始化加载----------
... ... @@ -101,6 +103,7 @@ Page({
101 103 onHide: function () {
102 104 //--清理定时器--
103 105 clearInterval(this.data.timer);
  106 + this.data.is_timer=0;
104 107 this.setData({
105 108 isshow: 0
106 109 });
... ... @@ -110,9 +113,10 @@ Page({
110 113 return param < 10 ? '0' + param : param;
111 114 },
112 115  
113   - //----倒计时函数-----
  116 + //----倒计时函数,优化定时器只显示在可视范围内的-----
114 117 countDown() {
115 118 if (!this.data.is_timer) return false;
  119 +
116 120 var th = this;
117 121 // 获取当前时间,同时得到活动结束时间数组
118 122 let newTime = ut.gettimestamp();
... ... @@ -120,45 +124,78 @@ Page({
120 124 if (endTimeList == null) return null
121 125 // 对结束时间进行处理渲染到页面
122 126 for (var i = 0; i < endTimeList.length; i++) {
  127 +
  128 + if (!this.data.is_timer) break;
  129 +
123 130 var o = endTimeList[i];
124 131 if(!o) continue;
125 132 var endTime = o.end_time;
126 133 if (th.data.type == 0) endTime = o.start_time;
127   - let obj = null;
128   - // 如果活动未结束,对时间进行处理
129   - if (endTime - newTime > 0) {
130   - let time = (endTime - newTime);
131   - // 获取天、时、分、秒
132   - let day = parseInt(time / (60 * 60 * 24));
133   - let hou = parseInt(time % (60 * 60 * 24) / 3600);
134   - let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
135   - let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
136   - obj = {
137   - day: this.timeFormat(day),
138   - hou: this.timeFormat(hou),
139   - min: this.timeFormat(min),
140   - sec: this.timeFormat(sec),
141   - hide: 1
142   - }
143   - } else {
144   - //活动已结束,全部设置为'00'
145   - obj = {
146   - day: '00',
147   - hou: '00',
148   - min: '00',
149   - sec: '00'
  134 +
  135 + // 创建查询对象
  136 + let query = wx.createSelectorQuery();
  137 + // 获取目标元素,并获取目标元素的信息
  138 + query.select("#kill-item"+i).boundingClientRect();
  139 + // 获取视口,并获取视口信息
  140 + query.selectViewport().boundingClientRect();
  141 + //-- 执行查询,元素要在显示中的才显示定时器 --
  142 + query.exec(([target, view]) => {
  143 +
  144 + if (!th.data.is_timer) return;
  145 + if (!target) return;
  146 + if (!target.id) return;
  147 +
  148 + let idx= target.id.replace('kill-item','');
  149 + // 条件1:当目标顶部距离视口顶部距离小于视口的高度时
  150 + // 条件2:当目标底部距离视口顶部距离大于0时
  151 + // 当以上两个条件同时成立时,目标元素在视口中
  152 + if (target.top < view.height && target.bottom > 0) {
  153 + let obj = null;
  154 + // 如果活动未结束,对时间进行处理
  155 + if (endTime - newTime > 0) {
  156 + let time = (endTime - newTime);
  157 + // 获取天、时、分、秒
  158 + let day = parseInt(time / (60 * 60 * 24));
  159 + let hou = parseInt(time % (60 * 60 * 24) / 3600);
  160 + let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
  161 + let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
  162 + obj = {
  163 + day: this.timeFormat(day),
  164 + hou: this.timeFormat(hou),
  165 + min: this.timeFormat(min),
  166 + sec: this.timeFormat(sec),
  167 + hide: 1
  168 + }
  169 + } else {
  170 + //活动已结束,全部设置为'00'
  171 + obj = {
  172 + day: '00',
  173 + hou: '00',
  174 + min: '00',
  175 + sec: '00'
  176 + }
  177 + }
  178 + var txt = "timer_js[" + idx + "]";
  179 + th.setData({
  180 + [txt]: obj
  181 + });
  182 + } else {
  183 + console.log("隐藏中");
150 184 }
151   - }
152   - var txt = "goodlist[" + i + "].djs";
153   - th.setData({
154   - [txt]: obj
155 185 });
  186 +
  187 +
  188 +
156 189 }
157 190 },
158 191  
159 192 async requestSalelist() {
160 193 //--没有更多就不调用--
161 194 if (!this.data.ismore) return false;
  195 + if(this.data.lding) return false;
  196 + this.data.lding=1;
  197 +
  198 +
162 199 var e = this,
163 200 th = e,
164 201 i = "/api/ms/flash_sale/spikepage?page=" + e.data.currentPage;
... ... @@ -223,6 +260,7 @@ Page({
223 260 getApp().showWarning("没有更多数据");
224 261 }
225 262  
  263 + this.data.lding=0;
226 264 return false;
227 265 }
228 266  
... ... @@ -239,14 +277,22 @@ Page({
239 277 plist[i].price_n = price
240 278 }
241 279 var prom_id = plist[i].id;
242   - await getApp().request.promiseGet("/api/weshop/activitylist/getActLen/" +
243   - os.stoid + "/1/" + prom_id, {}
244   - ).then(res => {
245   - if (res.data.code == 0) {
246   - plist[i].status = 1;
247   - if (res.data.data <= 0) plist[i].status = 3;
248   - }
249   - })
  280 +
  281 + // await getApp().request.promiseGet("/api/weshop/activitylist/getActLen/" +
  282 + // os.stoid + "/1/" + prom_id, {}
  283 + // ).then(res => {
  284 + // if (res.data.code == 0) {
  285 + // plist[i].status = 1;
  286 + // if (res.data.data <= 0) plist[i].status = 3;
  287 + // }
  288 + // })
  289 +
  290 + plist[i].status = 1;
  291 + if (plist[i].actLen <= 0) {
  292 + plist[i].status = 3;
  293 + }
  294 +
  295 +
250 296 alllist.push(plist[i]);
251 297 }
252 298 e.data.currentPage++;
... ... @@ -258,7 +304,9 @@ Page({
258 304  
259 305 setTimeout(()=>{
260 306 wx.hideLoading();
261   - },500)
  307 + },200)
  308 +
  309 + this.data.lding=0;
262 310  
263 311  
264 312  
... ...
pages/activity/seckill_list/seckill_list.wxml
... ... @@ -32,7 +32,7 @@
32 32  
33 33  
34 34 <view class="kill-list abs seckill_list">
35   - <view class="kill-item" wx:for="{{goodlist}}" wx:if="{{item.djs.hide}}" >
  35 + <view id="kill-item{{index}}" class="kill-item" wx:for="{{goodlist}}" >
36 36 <!-- <navigator class="kill-pic rel" url="/pages/goods/goodsInfo/goodsInfo?goods_id={{item.goods_id}}&prom_id={{item.id}}&prom_type=1"> -->
37 37 <navigator url="{{item.goods_type == 0 ? ('/pages/goods/goodsInfo/goodsInfo?goods_id=' + item.goods_id + '&prom_type=1&prom_id=' + item.id):('/packageA/pages/goodsInfo/goodsInfo?goods_id=' + item.goods_id + '&prom_type=1&prom_id=' + item.id )}}" class="kill-pic rel">
38 38 <image class="abs xc-miaosha" src="{{url}}/miniapp/images/zms.png"></image>
... ... @@ -59,12 +59,12 @@
59 59  
60 60 <view class="goods-num">
61 61 <view>
62   - <view class="{{type==1?'xc-wc':'blue_c'}}">
  62 + <view wx:if="{{timer_js[index].hide}}" class="{{type==1?'xc-wc':'blue_c'}}">
63 63 <text>剩余:</text>
64   - <text class='tui-conutdown-box'> {{item.djs.day}}</text>天
65   - <text class='tui-conutdown-box'>{{item.djs.hou}}</text>时
66   - <text class='tui-conutdown-box'>{{item.djs.min}}</text>分
67   - <text class='tui-conutdown-box tui-countdown-bg'>{{item.djs.sec}}</text>秒
  64 + <text class='tui-conutdown-box'> {{timer_js[index].day}}</text>天
  65 + <text class='tui-conutdown-box'>{{timer_js[index].hou}}</text>时
  66 + <text class='tui-conutdown-box'>{{timer_js[index].min}}</text>分
  67 + <text class='tui-conutdown-box tui-countdown-bg'>{{timer_js[index].sec}}</text>秒
68 68 </view>
69 69 <view class="fs40 flex xc-buttom-money {{type==1?'xc-wc':'blue_c'}}" >
70 70 <view class="fs28" style="line-height: 16px;">¥</view>
... ...