receiveCoupon.js
20.5 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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// pack-activity/pack/receiveCoupon.js
import util from '../../utils/util';
import App from '../../utils/api';
Page({
/**
* 页面的初始数据
*/
data: {
opt: {},
dataStr: Date.now() * 1 / 1000,
detail: {},
exp_date: {},
couponType:'',//1代金券 2折扣券 3菜品券
resultCoupon: '', //0未领取 1已领取
seller: {},
distance: '',
num: 1,
totalize: '',
},
myModal: '',
myToast:'',
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
console.log('options', options);
var obj = {};
if (options.scene) {
let scene = decodeURIComponent(options.scene);
console.log('scene', scene);
for (var i = 0; i < scene.split('&').length; i++) {
var arr = scene.split('&')[i].split('=');
obj[arr[0]] = arr[1];
}
}
console.log('obj', {
...obj
});
this.setData({
opt: {
id: options.id || obj.id,
sid: options.sid || obj.sid,
oid: options.oid,
types: options.types || obj.types, //1是邀请函过来的(邀请函的券或者点击意见反馈买券) 2是提前点餐 3是商家APP分享的或者短信的链接 4好友赠送的券 5充值赠送的券,不显示按钮
rid: options.rid ? options.rid : '',
activity_id:options.activity_id || '',
}
})
console.log('opt', {
...this.data.opt
});
this.myModal = this.selectComponent("#myModal");
},
// 购买完成券之后关注公众号
wananchi() {
wx.navigateTo({
url: '/pages/me/wananchi'
})
},
// 提交订单(免费券)
submitOrder: util.throttle(function () {
let eTime = this.data.detail.useEtime ? new Date(this.data.detail.useEtime).getTime()/1000 : '0';
if (this.data.detail.aexpiration && this.data.detail.aexpiration.valid_forever != '1' && (this.data.dataStr > eTime )) {
return wx.showToast({
title: '优惠券已过期,不可购买!',
duration: 2000,
mask: true,
icon: 'none'
})
}
// is_type 1店铺优惠券 需要判断优惠券剩余数量 2个人优惠券不需要判断数量 3免费券 4付费券
if (this.data.detail.grant_last <= '0' && this.data.detail.is_type == '3') {
return wx.showToast({
title: '您来晚了,当前优惠券已经售罄,请与商家联系!',
duration: 2000,
mask: true,
icon: 'none'
})
}
// 好友分享的券
if(this.data.opt.types=='4'){
App.Activity.GetStatusByActivityId({
activity_id: this.data.opt.activity_id
}).then(res => {
// status 0未核销 1已核销
if (res.ret === 200 && res.data.status == '0') {
this.getActivityCouponFun('1')
}else{
this.myModal.showModal()
}
})
}else{
this.getActivityCouponFun()
}
}, 3000),
// 关闭领券失败弹框
closeCouponModal() {
this.myModal.closeModal()
},
// 提交订单(付费券)
submitOrderTwo: util.throttle(function () {
if(this.data.dataStr>(this.data.detail.etime*1)){
return wx.showToast({
title:'优惠券已过期,不可购买!',
duration:2000,
mask:true,
icon:'none'
})
}
if(this.data.num && !/(^[1-9]\d*$)/.test(this.data.num)){
return wx.showToast({
title:'请输入正确的购买数量',
duration:2000,
mask:true,
icon:'none'
})
}
// is_type 1店铺优惠券 需要判断优惠券剩余数量 2个人优惠券不需要判断数量
if(this.data.detail.grant_last=='0' && this.data.detail.is_type=='1'){
return wx.showToast({
title:'您来晚了,当前优惠券已经售罄,请与商家联系!',
duration:2000,
mask:true,
icon:'none'
})
}
// is_type 1店铺优惠券 需要判断优惠券剩余数量 2个人优惠券不需要判断数量
if(this.data.detail.grant_last && (this.data.detail.grant_last*1)<(this.data.num*1) && this.data.detail.is_type=='1'){
return wx.showToast({
title:'优惠券剩余数量不足!',
duration:2000,
mask:true,
icon:'none'
})
}
if(this.data.num=='0'){
return wx.showToast({
title:'最少需要购买一个!',
duration:2000,
mask:true,
icon:'none'
})
}
console.log('oidoid',this.data.opt.oid);
let detail=this.data.detail
detail.name=this.data.seller.sellername;
detail.oid=this.data.opt.oid;
App.Activity.CreateNewRechargeOrder({
recharge_id:this.data.opt.id,
sellerid:this.data.opt.sid,
number:this.data.num,
price:this.data.detail.price,
type:'1',
}).then(res=>{
console.log(res);
if(res.ret=='200'){
let totalize=this.data.totalize*1;
if(totalize==0){
util.toast('购买成功','none');
wx.reLaunch({
url: '/pages/me/coupon_news'
})
return
}
// 跳转到支付页,types传参 1邀请函 2提前点餐 3商家端app分享
wx.navigateTo({
url: `./confirmPayment?sid=${this.data.opt.sid}&sellerName=${this.data.seller.sellername}&oid=${this.data.opt.oid}&rid=${this.data.opt.rid?this.data.opt.rid:''}&type=1&detail=${JSON.stringify(detail)}&combine_out_trade_no=${res.data.combine_out_trade_no}&totalize=${this.data.totalize}&types=${this.data.opt.types}`,
})
}else{
wx.showToast({
title:res.msg,
duration:2000,
mask:true
})
}
})
}, 3000),
// 领券
getActivityCouponFun(type){
let detail = this.data.detail
detail.name = this.data.seller.sellername;
detail.oid = this.data.opt.oid;
App.Activity.SetActivityAsReserve({
aid: this.data.opt.id,
rid: this.data.opt.oid,
}).then((json) => {
if (json.ret == 200) {
if(type == '1'){//核销分享人的券
this.writeOff()
}else{
this.myToast.showToast('领取成功', 2000)
this.getCoupon('1')
}
} else {
this.myToast.showToast(res.msg, 2000)
}
})
},
// 好友领取后核销
writeOff(){
App.Activity.SetActivityStatus({
id: this.data.opt.activity_id
}).then(res => {
if (res.ret == '200') {
this.myToast.showToast('领取成功', 2000)
this.getCoupon()
}
})
},
// 获取券信息
getDetail(cdate,activity_id) {
App.Activity.GetActivityById({
id: this.data.opt.id
}).then(res => {
if (res.data.length > 0) {
let couponType = '';
let detail = res.data[0];
let exp_date = util.determine(detail.exp_date);
detail.arule = detail.arule ? util.determine(detail.arule) : '';
detail.use = detail.use ? util.determine(detail.use) : '';
detail.exp_date = exp_date;
detail.expiration_date = detail.expiration_date ? util.determine(detail.expiration_date) : '';
detail.aexpiration = detail.aexpiration ? util.determine(detail.aexpiration) : '';
detail.cdate = detail.cdate * 1;
detail.useStime = '';
detail.useEtime = '';
let condition = detail.aexpiration
if (condition.valid_forever === '0' && condition.stime && condition.etime) {
detail.useStime = util.unixtoTime(condition.stime, 'YYYY-MM-DD');
detail.useEtime = util.unixtoTime(condition.etime, 'YYYY-MM-DD');
detail.useEtimeStr = useStime + '至' + useEtime;
}
if (condition.valid_forever === '1') {
detail.foreverText = '永久有效';
detail.useEtimeStr = '永久有效';
}
let t = cdate ? cdate : detail.cdate;
// today 领券当日起天数
if (condition.valid_forever === '0' && condition.today) {
let obj = this.setTimeRange(t, 0, parseInt(condition.today)-1);
detail.useStime = obj.useStime;
detail.useEtime = obj.useEtime;
detail.useEtimeStr = '领取当日起' + condition.today + '天内有效';
}
// tomorrow 领券次日起天数
if (condition.valid_forever === '0' && condition.tomorrow) {
let obj = this.setTimeRange(t, 1, condition.tomorrow*1);
detail.useStime = obj.useStime;
detail.useEtime = obj.useEtime;
detail.useEtimeStr = '领取次日起' + condition.tomorrow + '天内有效';
}
// sday 领券后第几天的开始天数 eday 领券后第几天的结束天数
if (condition.valid_forever === '0' && condition.sday && condition.eday) {
let obj = this.setTimeRange(t, parseInt(condition.sday) - 1, parseInt(condition.eday) - 1);
detail.useStime = obj.useStime;
detail.useEtime = obj.useEtime;
detail.useEtimeStr = '领取后第' + condition.sday + '天至' + condition.eday + '天可用';
}
if (exp_date && exp_date.week_data) {
let mor = this.hasConsecutiveSelect(exp_date.week_data, '1').rArr.join('、')
let noon = this.hasConsecutiveSelect(exp_date.week_data, '2').rArr.join('、')
let nig = this.hasConsecutiveSelect(exp_date.week_data, '3').rArr.join('、')
if (mor) mor = '早餐:' + mor
if (noon) noon = '午餐:' + noon
if (nig) nig = '晚餐:' + nig
detail.morText = mor;
detail.noonText = noon;
detail.nigText = nig;
}
if(detail.is_type!='3'){
detail.useStime = util.unixtoTime(detail.stime, 'YYYY-MM-DD');
detail.useEtime = util.unixtoTime(detail.etime, 'YYYY-MM-DD');
}
// is_cash 代金券 is_discount折扣券 is_dish菜品券 只有代金券有几折显示
if(detail.use){
couponType = detail.use.is_cash=='1'?'1':detail.use.is_discount=='1'?'2':detail.use.is_dish=='1'?'3':'1';
detail.use.images = detail.use.images ? util.determine(detail.use.images) : '';
}else{
couponType = '1'
}
if (detail.arule && detail.arule.is_share_lose == '0') {
wx.hideShareMenu()
}
let price = detail.price * 1;
let num = this.data.num * 1;
detail.activity_id = activity_id;
console.log('券详情--',{...detail});
this.setData({
detail,
exp_date,
totalize: (price * num).toFixed(2),
couponType,
})
}
})
},
// 点击减号
subtraction() {
let price = this.data.detail.price * 1;
let num = this.data.num * 1;
let num1 = num - 1
if (num == '1') {
return false
}
this.setData({
num: num1,
totalize: (price * num1).toFixed(2)
})
},
// 点击加号
addition() {
let price = this.data.detail.price * 1;
let num = this.data.num * 1;
let num1 = num + 1
this.setData({
num: num1,
totalize: (price * num1).toFixed(2)
})
},
// 输入事件
bindKeyNameInput(event){
let price = this.data.detail.price * 1;
const value = event.detail.value.replace(/\s+/g, "")*1;
if(value && /(^[1-9]\d*$)/.test(value)){
wx.hideToast()
this.setData({
num: value,
totalize: (price * value).toFixed(2)
})
}else{
wx.showToast({
title:'请输入正整数',
icon:'none',
duration:1000,
mask:true,
})
}
},
// 获取店铺名称
getSellerDetail() {
let that = this;
App.Customer.GetSellerDetail({
sid: this.data.opt.sid
}).then(res => {
let data = res.data;
let cateStr = []
if (data.cate && data.cate.length > 0) {
data.cate.some(cate => {
cateStr.push(cate);
if (cateStr.length > 2) {
return true
}
})
cateStr = cateStr.join('/')
}
let latitude = '';
let longitude = '';
let distance = '';
let seller = {
cateStr,
is_parking: data.is_parking,
average: data.average,
sellername: data.sellername,
is_online_reserve: data.is_online_reserve,
seller_image: data.seller_image,
phone: data.phone,
}
wx.getLocation({
type: 'wgs84', // 返回可以用于 wx.openLocation 的经纬度
success: function (res) {
latitude = res.latitude; // 纬度
longitude = res.longitude; // 经度
console.log(`当前的纬度: ${latitude}, 当前的经度: ${longitude}`);
if (latitude && longitude) {
distance = that.getDistance(latitude, longitude, data.lat, data.long);
distance = parseInt(distance)
if (distance > 1000) {
distance = (distance / 1000).toFixed(2) + 'km'
} else {
distance += 'm';
}
that.setData({
distance
})
}
},
fail: function (err) {
console.error('获取位置失败:', err);
}
});
console.log({
...seller
});
this.setData({
seller: {
...seller
}
})
})
},
// 拨打电话
callPhone() {
wx.makePhoneCall({
phoneNumber: this.data.seller.phone
})
},
// 计算免费券有效期的起始、结束日期
setTimeRange(cdate, startOffset, endOffset) {
console.log(cdate, startOffset, endOffset);
const startDate = new Date((cdate * 1 + startOffset * 86400) * 1000);
startDate.setHours(0, 0, 0, 0);
const useStime = util.unixtoTime(startDate.getTime() / 1000, 'YYYY-MM-DD');
const endDate = new Date((cdate * 1 + endOffset * 86400) * 1000);
endDate.setHours(23, 59, 59, 0);
const useEtime = util.unixtoTime(endDate.getTime() / 1000, 'YYYY-MM-DD');
return {
useStime,
useEtime
};
},
// 处理优惠券不可用日期数据
hasConsecutiveSelect(arr, type) {
let firstTitle = null;
let lastTitle = null;
let rArr = []
let num = 0
for (let i = 0; i < arr.length; i++) {
let str = type == '1' ? arr[i].selectMor : type == '2' ? arr[i].selectNoon : type == '3' ? arr[i].selectNig : '';
if (str) {
rArr.push(arr[i].title)
num++
if (num == 1) {
firstTitle = arr[i].title
}
if (num >= 5) {
lastTitle = arr[i].title
}
if (num >= 5 && i >= (arr.length - 1)) {
let fi = rArr.findIndex((item) => {
return item == firstTitle
})
rArr.splice(fi, num, firstTitle + '至' + lastTitle)
num = 0
}
} else {
if (num < 5) {
num = 0
firstTitle = null;
lastTitle = null;
} else {
let fi = rArr.findIndex((item) => {
return item == firstTitle
})
rArr.splice(fi, num, firstTitle + '至' + lastTitle)
num = 0
}
}
}
return {
rArr,
lastTitle,
firstTitle
};
},
sharesetting() {
let data = this.data.opt;
let detail = this.data.detail;
let types = detail.arule.is_share_have == '0' && detail.activity_id ? '4': data.types;
let path = `/pack-activity/pack/receiveCoupon?id=${data.id}&oid=${data.oid}&rid=${data.rid}&sid=${data.sid}&types=${types}`
if(detail.arule.is_share_have == '0' && detail.activity_id){
path += '&activity_id='+detail.activity_id
}
return {
title: `送你${this.data.seller.sellername}${detail.price}元代金券,点击查看详情,领取到卡包吧!`,
path,
imageUrl: 'https://nosqn.colorcun.com/share_money.png',
}
},
// 是否已领取
getCoupon(i) {
App.Activity.ActivityListByOpenid({
aid: this.data.opt.id,
sellerid:this.data.opt.sid
}).then(res => {
if (res.ret === 200) {
let activity_id = res.data.length > 0 ? res.data[0].activity_id : '';
this.setData({
resultCoupon: res.data.length > 0 && res.data[0].is_type == '3' ? '1' : '0',
})
if(i) this.getDetail(res.data.length > 0?res.data[0].cdate:'',activity_id);
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
this.myToast = this.selectComponent("#myToast")
this.getSellerDetail();
this.getCoupon('1')
},
// 计算2点之间的距离
getDistance(lat1, lon1, lat2, lon2) {
const R = 6371e3; // 地球半径(米)
// 将经纬度转换为弧度
const φ1 = lat1 * Math.PI / 180; // 纬度1
const φ2 = lat2 * Math.PI / 180; // 纬度2
const Δφ = (lat2 - lat1) * Math.PI / 180; // 纬度差
const Δλ = (lon2 - lon1) * Math.PI / 180; // 经度差
// Haversine 公式
const a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const distance = R * c; // 计算距离(米)
return distance; // 返回距离
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
return this.sharesetting();
}
})