redEnvelope.js
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import {login, loginData} from '../loginPop/login.js';
import {API_HOST, SERVICE_HOST} from '../../../../libs/config';
import {GET, POST} from '../../../../libs/request';
let app = getApp();
// pages/redEnvelope/redEnvelope.js
Page(Object.assign({}, login, {
/**
* 页面的初始数据
*/
data: {
isAlreadyReceived: false,
isLogin: false,
showLoginBox: false,
redBagType: '',
notOpen: false,
shareCode: '',
recieveMessage: '分享码已被别的用户领取',
isWechatService: false,
userInfo: {},
rewardType: 1,
rewardUrl: '',
rewardName: '',
account: '',
screenHeight: 0,
isStop: true,
lightStop: true,
rewardList: [],
imageLoadTotal: 0,
rewardIndex: -1, // 最终结果所在的index
scrollIndex: -1 // 滚动时的index
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
let self = this;
if (app.isLogin()) {
this.setData({
isLogin: true
});
this.checkIsWechatCs({});
} else {
this.setData({
isLogin: false
});
}
let screenHeight = 0;
let query = tt.createSelectorQuery(); // 让蒙层铺满整个页面长度
query.select('#rewardContainer').boundingClientRect(function(res) {
console.log(res.height);
screenHeight = res.height;
self.setData({
screenHeight: screenHeight + 'px'
})
});
query.exec();
this.getRewardsUrlList(); // 获取奖品图片列表
this.setData({
...loginData,
shareCode: options.shareCode || '',
});
new app.WeToast();
if (tt.hideShareMenu) {
tt.hideShareMenu();
}
},
/**
* 领取红包
*/
openRedBag: function() {
if (!this.data.isLogin) {
this.setData({
showLoginBox: true
});
this.fetchImageCheck_SwitchOn();
return;
} else {
if (this.data.notOpen) {
this.wetoast.toast({
title: this.data.recieveMessage,
titleClassName: 'wetoast-title',
duration: 2000
});
return false;
}
// if (tt.showLoading) {
// tt.showLoading({
// title: '加载中',
// });
// }
this.receiveCouponForWxCS({});
}
},
// 获取奖品列表
getRewardsUrlList: function() {
let self = this;
tt.showLoading({
title: '奖品加载中',
mask: true
})
GET(API_HOST, {
method: 'app.passport.getRewardsUrlList'
}).then(ret => {
console.log(ret);
for (let i = 0; i < ret.data.length; i++) {
let imageURL = ret.data[i].rewardImageUrl;
ret.data[i].rewardImageUrl = imageURL.split('?')[0].replace('http', 'https') + '?imageView2/2/w/186/h/170/q/60';
}
self.setData({
rewardList: ret.data || []
})
});
},
receiveCouponForWxCS: function(option) {
let self = this;
// 领取优惠券
if (this.data.lightStop && this.data.isStop) {
GET(API_HOST, {
method: 'app.passport.receiveCouponForWxCS',
uid: app.getUid(),
shareCode: this.data.shareCode
}).then(ret => {
console.log(ret);
if (ret && ret.code && ret.code == 200) {
// this.getIsCanReceive(option);
self.setData({
lightStop: false,
isStop: false
});
self.roller(0, 200); // 开始转动奖品
setTimeout(function () {
let rewardIndex = -1;
for (let i = 0; i < self.data.rewardList.length; i++) {
if (ret.data.rewardId === self.data.rewardList[i].rewardId) {
rewardIndex = i;
}
}
self.setData({
isStop: true,
rewardIndex: rewardIndex,
rewardType: ret.data.rewardType,
rewardUrl: ret.data.rewardUrl,
rewardName: ret.data.rewardName
})
}, 2000);
} else {
if (tt.hideLoading) {
tt.hideLoading();
}
this.wetoast.toast({
title: ret.message,
titleClassName: 'wetoast-title',
duration: 2000
});
}
});
// const result = {
// alg: 'SALT_MD5',
// code: 200,
// data: {
// rewardUrl: 'https://flv01.static.yhbimg.com/activity/2018/08/02/10/03d2c97327a48c0467fe64e5bdeff442bb.png',
// rewardName: '20元优惠券1234567980-2031203189',
// rewardId: 34,
// rewardType: 2
// },
// md5: 'e8bcca09966627bea87a974dc5413133',
// message: 'success.'
// };
}
},
stopRoller:function (index, times, spd) {
this.data.isStop = true;
this.setData({
isStop: true,
scrollIndex: index
});
if (spd < 500) {
spd += 20;
}
if (times > 0) {
times -= 1;
if (index < 7) {
index += 1;
} else {
index = 0;
}
setTimeout(this.stopRoller, spd, index, times, spd);
} else {
let self = this;
setTimeout(function() {
self.setData({
lightStop: true,
isAlreadyReceived: true // 显示中奖结果
});
}, 1000);
}
},
roller: function (index, spd) {
if (!this.data.isStop) {
this.setData({
scrollIndex: index
});
if (index < 7) {
index += 1;
} else {
index = 0;
}
if (spd > 50) {
spd -= 10;
}
setTimeout(this.roller, spd, index, spd);
} else {
this.stopRoller(index, this.data.rewardIndex + 8 * 2 - index, spd);
}
},
goToUse: function() {
tt.switchTab({
url: '../../../../pages/index/index'
});
},
/**
* 获取用户是否可以领取优惠券[200(可以领取) 201(当前用户已领取,返回couponName) 202(分享码已被被别的用户领取)]
*/
getIsCanReceive: function(option) {
let params = {
method: 'app.passport.getIsCanReceive',
uid: app.getUid(),
shareCode: this.data.shareCode
};
GET(API_HOST, params).then(ret => {
if (ret && ret.code && ret.code == 200) {
// 可以领取
if (ret.data.recieveCode == 200) {
if (option.get) {
this.receiveCouponForWxCS({
get: false
});
} else {
this.setData({
isAlreadyReceived: false
});
if (tt.hideLoading) {
tt.hideLoading();
}
}
}
// 当前用户已领取
if (ret.data.recieveCode == 201) {
this.setData({
isAlreadyReceived: true,
rewardType: ret.data.rewardType || ''
});
if (this.data.rewardType == 1) {
this.setData({
redBagType: ret.data.couponAmount || ''
});
}
if (this.data.rewardType == 2) {
this.setData({
rewardUrl: ret.data.rewardUrl || '',
rewardName: ret.data.rewardName || ''
});
}
}
// 分享码被其他用户领取
if (ret.data.recieveCode == 202) {
this.setData({
isAlreadyReceived: false,
notOpen: true,
recieveMessage: ret.data.recieveMessage
});
if (tt.hideLoading) {
tt.hideLoading();
}
this.wetoast.toast({
title: ret.data.recieveMessage,
titleClassName: 'wetoast-title',
duration: 2000
});
}
setTimeout(function () {
tt.showToast({
title: ret.data.recieveMessage,
icon: 'none',
duration: 2000
});
}, 1000)
} else {
this.setData({
isAlreadyReceived: false,
notOpen: true,
recieveMessage: ret.message
});
if (tt.hideLoading) {
tt.hideLoading();
}
}
});
},
/**
* 判断是否是微信客服
*/
checkIsWechatCs: function(option) {
GET(API_HOST, {
method: 'app.passport.checkIsWechatCs',
uid: app.getUid()
}).then(ret => {
if (ret && ret.code && ret.code == 200) {
this.setData({
isWechatService: ret.data.isWechatService == 'Y' ? true : false
});
if (ret.data.isWechatService == 'Y') {
this.getUserInfoByShareCode();
} else {
this.getIsCanReceive(option);
}
} else {
if (tt.hideLoading) {
tt.hideLoading();
}
}
});
},
/**
* 根据分享码,客服uid获取领取用户的信息
*/
getUserInfoByShareCode: function() {
GET(API_HOST, {
method: 'app.passport.getUserInfoByShareCode',
uid: app.getUid(),
shareCode: this.data.shareCode
}).then(ret => {
if (tt.hideLoading) {
tt.hideLoading();
}
if (ret && ret.code && ret.code == 200) {
this.setData({
userInfo: ret.data,
isAlreadyReceived: true
});
}
});
},
copy: function() {
tt.setClipboardData({
data: `${this.data.userInfo.vipLevel} ${this.data.userInfo.uid}`
})
},
imageLoad: function() {
if (tt.hideLoading) {
tt.hideLoading();
}
},
rewardImageLoadComplete: function() {
let total = this.data.imageLoadTotal + 1;
this.setData({
imageLoadTotal: total
});
if (total >= 7) {
if (tt.hideLoading) {
tt.hideLoading();
}
}
},
bindAccount: function(e) {
this.setData({
account: e.detail.value
})
},
// 提交微信客服账号
submitWxAccount: function() {
if (!this.data.account) {
this.wetoast.toast({
title: '请输入用户微信号',
titleClassName: 'wetoast-title',
duration: 2000
});
return;
}
if (tt.showLoading) {
tt.showLoading({
title: '正在提交',
});
}
GET(API_HOST, {
method: 'app.passport.submitUserWechatCode',
uid: app.getUid(),
wechat_uid: this.data.userInfo && this.data.userInfo.uid,
userCode: this.data.account
}).then(ret => {
if (ret && ret.code && ret.code == 200) {
this.wetoast.toast({
title: '微信号提交成功',
titleClassName: 'wetoast-title',
duration: 2000
});
this.setData({
'userInfo.submitWechat': true,
'userInfo.userWechatCode': this.data.account
});
this.setData({
account: ''
});
if (tt.hideLoading) {
tt.hideLoading();
}
} else {
this.wetoast.toast({
title: ret.message,
titleClassName: 'wetoast-title',
duration: 2000
});
if (tt.hideLoading) {
tt.hideLoading();
}
}
});
}
}))