buyCoupon.js
8.89 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
// pack-activity/pack/buyCoupon.js
import App from '../../utils/api';
import util from '../../utils/util'
Page({
/**
* 页面的初始数据
*/
data: {
opt: {
id:'',
oid:'',
sid:'',
types:'1',//1是邀请函过来的(邀请函的券或者点击意见反馈买券)2提前点餐 3是商家APP分享的
rid:'',
},
use:{},
detail:{},
exp_date:{},
num: 1,
totalize: '',
sellername:'',
dataStr: Date.now() * 1 / 1000,
},
/**
* 生命周期函数--监听页面加载
*/
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分享的或者短信的链接
rid:options.rid?options.rid:'',
}
})
console.log('opt',{...this.data.opt});
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
this.getDetail();
this.getSellerDetail();
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
// 获取店铺名称
getSellerDetail(){
App.Customer.GetSellerDetail({
sid: this.data.opt.sid
}).then(res=>{
this.setData({
sellername:res.data.sellername
})
})
},
// 获取券信息
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);
let price = detail.price * 1;
let num = this.data.num * 1;
let use=util.determine(detail.use)
if(use && use.images){
use.images=util.determine(use.images)
}
this.setData({
detail,
exp_date,
totalize: (price * num).toFixed(2),
use,
})
console.log('detail',{...detail});
console.log('exp_date',{...exp_date});
console.log('totalize',this.data.totalize);
console.log('use',use);
}
})
},
// 点击减号
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,
})
}
},
// 提交订单
submitOrder() {
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.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.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
})
}
})
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
let data=this.data.opt;
let use=this.data.use;
let detail=this.data.detail
let imageUrl,title;
if(use.is_cash=='1' || use.is_cash){
imageUrl='https://nosqn.colorcun.com/share_money.png';
title=detail.price == '0'?`免费获取${this.data.sellername}${use.cash}元代金券,点击查看详情。`:`仅需${detail.price}元,获取${this.data.sellername}${use.cash}元代金券,点击查看详情。`
}
if(use.is_discount=='1' || use.is_discount){
imageUrl='https://nosqn.colorcun.com/share_discount.png';
title=detail.price == '0'?`免费获取${this.data.sellername}${use.discount}折折扣券,点击查看详情。`:`仅需${detail.price}元,获取${this.data.sellername}${use.discount}折折扣券,点击查看详情。`
}
if(use.is_dish=='1' || use.is_dish){
imageUrl='https://nosqn.colorcun.com/share_dish.png';
title=detail.price == '0'?`免费获取${this.data.sellername}${detail.aname},点击查看详情。`:`仅需${detail.price}元,获取${this.data.sellername}${detail.aname},点击查看详情。`
}
return {
title,
path:`/pack-activity/pack/buyCoupon?use=${JSON.stringify(use)}&id=${data.id}&oid=${data.oid}&rid=${data.rid}&sid=${data.sid}&types=${data.types}`,
imageUrl,
}
}
})