orderConfirm.js
9.14 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
/**
* orderConfirm.js
*@author dennis
*@createtime 2018/10/26
*@description 订单支付确认界面
*/
import orderService from '../orderService'
import {getImgUrl} from '../../../utils';
import router from '../../../router/index.js'
import { prePay } from '../../order/wxpay.js'
import Yas from '../../../utils/yas'
let yas;
const ACTIVITY_TYPE = 18; // 砍价
const api = new orderService();
Page({
data: {
payLabel: '待支付:',
payButton: '去支付',
protocolLabel: '我已阅读并同意',
protocolText: '买家协议',
agreeProtocol: false,
checkImage: '../../../assets/images/select@2x.png',
unCheckImage: '../../../assets/images/unselect@2x.png',
isStore: 0,
storeId: 0,
skup: '',
coupons: [],
selectCouponCode: '',
selectCouponAmount: '',
selectingCoupon: false,
promotionid: '',
user_activity_id: '' // 有值代表砍价
},
onLoad: async function (option) {
yas = new Yas(this);
yas.pageOpenReport();
this.setData({
isStore: Number(option.is_store) || 0,
storeId: option.store_id || 0,
skup: option.skup || '',
product_id: option.product_id || '',
hasAddress: false,
user_activity_id: option.user_activity_id || ''
})
},
async onShow() {
let userInfo = wx.getStorageSync('userInfo');
if (!(userInfo && userInfo.uid && userInfo.session_key)) {
router.go('nativeLogin');
return;
}
//选中优惠卷后,切到后台,再切回前台页面,不在请求数据
if (!this.data.selectCouponCode) {
await this.fetchData(this.data.isStore);
if (!this.data.isStore && !this.data.hasAddress) {
await this.fetchAddress()
}
}
},
showSelectCoupon: function () {
this.setData({
selectingCoupon: true
});
},
async confirmSelectCoupon({detail}) {
let code = detail.code;
let codeType = detail.codeType;
let amount = detail.amount || '';
let couponsList = this.data.couponList;
if (couponsList){
couponsList.forEach((item, index) => {
if (code === item.coupon_code) {
item.isChosen = true;
item.selected = 'Y'
}
});
}
this.setData({
couponsList,
selectCouponAmount: amount,
selectCouponCode: code,
selectingCoupon: false,
selectingCouponType: codeType
});
// 区分线上线下
let info;
//如果选择了非运费券,促销不选择
let promotion_id = '';
if (this.data && this.data.promotionTips && this.data.promotionTips.promotionIds) {
promotion_id = this.data.promotionTips.promotionIds;
}
//判断优惠劵的类型
if (code && codeType !== 110){
promotion_id = '';
}
try {
if (this.data.isStore) {
info = await api.orderOfflineCompute({
skup: this.data.skup,
coupon_code: code,
promotionId: promotion_id,
app_version: '1'
}, () => wx.hideLoading());
} else {
const params = {
skup: this.data.skup,
coupon_code: code,
promotionid: promotion_id
};
if (this.data.user_activity_id) {
Object.assign(params, {
user_activity_id: this.data.user_activity_id,
activity_type: ACTIVITY_TYPE
});
}
info = await api.orderCompute(params, () => wx.hideLoading());
}
} catch(e) {}
this.setData({
amount: info.amount,
promotionFormulaList: info.promotionFormulaList
});
},
cancelSelectCoupon: function () {
this.setData({
selectingCoupon: false
});
},
async fetchData(isStore) {
wx.showLoading({
title: '',
});
let params = {
skup: this.data.skup,
store_id: this.data.storeId,
api_version: '1'
}
if (this.data.user_activity_id) {
Object.assign(params, {
user_activity_id: this.data.user_activity_id,
activity_type: ACTIVITY_TYPE
})
}
let data;
if (isStore) {
data = await api.createOfflinePayment(params, () => wx.hideLoading())
let index;
data.promotionFormulaList.forEach((value, i) => {
if (value.promotion === '运费') {
index = i;
}
})
data.promotionFormulaList.splice(index,1)
} else {
try {
data = await api.createPaymentInfo(params, () => wx.hideLoading())
} catch (e) {
console.log(e)
wx.showModal({
title: '失败',
content: e.message,
showCancel: false,
})
return;
}
}
// await api.orderSelectCoupon(this.data.skup, () => wx.hideLoading()).then(data => {
// this.setData(coupons)
// });
let that = this;
if (data && data.couponList && data.couponList.length > 0){
let coupons = [];
data.couponList.forEach((item, index) => {
let selected = item.selected;
if (selected === 'Y'){
item.isChosen = true;
data.selectCouponAmount = item.coupon_value_str;
data.selectCouponCode = item.coupon_code;
}else {
item.isChosen = false;
}
});
}
if (data.good && data.good.goodImg) {
data.good.goodImg = getImgUrl(data.good.goodImg, 270, 270)
}
this.setData(data)
},
async fetchAddress() {
wx.showLoading({
title: '',
})
let params = {}
await api.yohoApi().buyerGetAddress(params, () => wx.hideLoading())
.then(data => {
if (Array.isArray(data)) {
for (let item of data) {
let index = data.indexOf(item)
if (item.is_default === 'Y') {
this.setData({
address: item,
hasAddress: true,
})
break
}
if (index == data.length - 1) {
this.setData({
address: item,
hasAddress: true,
})
}
}
}
})
.catch(error => {
console.log(error)
})
api.defaultApi();
},
async submit() {
if (!this.data.agreeProtocol) return;
let data;
try {
let promotionId = '';
if (this.data && this.data.promotionTips && this.data.promotionTips.promotionIds){
promotionId = this.data.promotionTips.promotionIds;
}
if (this.data.selectingCouponType && this.data.selectingCouponType !== 110) {
promotionId = '';
}
if (this.data.isStore) {
this.check = this.selectComponent("#check");
let checkRes = await this.check.init();
if (!checkRes.result) return;
let param = checkRes.data;
param.skup = this.data.skup;
param.api_version = '1';
param.coupon_code = this.data.selectCouponCode || '';
param.promotionId = promotionId;
// wx.showLoading({
// title: '',
// })
data = await api.storeBuyerSubmit(param, () => {
wx.hideLoading()
})
} else {
if (!this.data.hasAddress) {
wx.showToast({
title: '请选择地址'
})
return
}
let addressId = this.data.hasAddress && this.data.address && this.data.address.address_id
let skup = this.data.skup
let channelNo = '';
let couponCode = this.data.selectCouponCode || '';
let extra = null;
if (this.data.user_activity_id) {
extra = {
user_activity_id: this.data.user_activity_id,
activity_type: ACTIVITY_TYPE
};
}
// wx.showLoading({
// title: '',
// })
data = await api.buyerSubmit({ skup, channelNo, addressId, couponCode, promotionId, extra}, () => wx.hideLoading());
}
if (data && data.orderCode) {
let skup = this.data.skup
let orderCode = data.orderCode;
let productId = data.productId ? data.productId : '';
let params = {
RPD_SKUP: skup,
ORD_NUM: orderCode + ''
};
yas.report('YB_SC_ORD', params);
prePay(productId, orderCode, 0, this.data.isStore > 0);
}
} catch (error) {
wx.showToast({
title: error.message,
icon: 'none'
})
}
},
chooseAddress: function () {
let addressId = this.data.address ? this.data.address.address_id : ''
let params = {
currentMode: 'modeSelect',
address_id: addressId
}
router.go('addressManager', params);
},
selectComplete: function (data) {
if (data) {
this.setData({
address: data,
hasAddress: true
})
}
},
checkProtocol: function () {
this.setData({
agreeProtocol: !this.data.agreeProtocol
})
},
goProtocol: function () {
let url = `http://m.yohobuy.com/activity/student/detail/renzhen?openby:yohobuy={\"action\":\"go.h5\",\"params\":{\"url\":\"https://activity.yoho.cn/feature/3189.html?title=买家协议\"}}`;
if (this.data.isStore) {
url = `http://m.yohobuy.com/activity/student/detail/renzhen?openby:yohobuy={\"action\":\"go.h5\",\"params\":{\"url\":\"https://activity.yoho.cn/feature/4281.html?title=买家协议\"}}`;
}
router.goUrl(url);
},
})