receiveCoupon.js
14.3 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
// 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: {},
resultCoupon: '', //0未领取 1已领取
seller: {},
distance: '',
},
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好友赠送的券
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 () {
if (this.data.detail.aexpiration && this.data.detail.aexpiration.valid_forever != '1' && (this.data.dataStr > (this.data.detail.etime * 1))) {
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()
},
// 领券
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()
}
} 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() {
App.Activity.GetActivityById({
id: this.data.opt.id
}).then(res => {
if (res.data.length > 0) {
let detail = res.data[0];
let exp_date = util.determine(detail.exp_date);
detail.arule = detail.arule ? util.determine(detail.arule) : '';
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');
}
if (condition.valid_forever === '1') {
detail.foreverText = '永久有效';
}
if (condition.valid_forever === '0' && condition.today) {
let obj = this.setTimeRange(detail.cdate, 0, parseInt(condition.today)-1);
detail.useStime = obj.useStime;
detail.useEtime = obj.useEtime;
}
if (condition.valid_forever === '0' && condition.tomorrow) {
let obj = this.setTimeRange(detail.cdate, 1, condition.tomorrow*1);
detail.useStime = obj.useStime;
detail.useEtime = obj.useEtime;
}
if (condition.valid_forever === '0' && condition.sday && condition.eday) {
let eday = condition.sday == '1' ? (parseInt(condition.eday)-1) : (parseInt(condition.eday)+1)
let obj = this.setTimeRange(detail.cdate, parseInt(condition.sday)-1, eday);
detail.useStime = obj.useStime;
detail.useEtime = obj.useEtime;
}
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.arule && detail.arule.is_share_lose == '0') {
wx.hideShareMenu()
}
this.setData({
detail,
exp_date,
})
}
})
},
// 获取店铺名称
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() {
App.Activity.ActivityListByOpenid({
aid: this.data.opt.id,
sellerid:this.data.opt.sid
}).then(res => {
if (res.ret === 200) {
let detail = this.data.detail;
detail.activity_id = res.data.length > 0 ? res.data[0].activity_id : '';
this.setData({
resultCoupon: res.data.length > 0 ? '1' : '0',
})
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
this.myToast = this.selectComponent("#myToast")
this.getDetail();
this.getSellerDetail();
this.getCoupon()
},
// 计算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();
}
})