wxpay.js
1.67 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
import orderService from './orderService.js'
import router from '../../router/index.js'
// const api = new orderService();
/**
* orderCode 订单号
* fromWhere 支付来源页面 0:确认订单页面,1:订单列表,2:订单详情
*/
export const prePay = function (productId, orderCode,fromWhere) {
let api = new orderService()
wx.showLoading({
title: '',
})
let openID = '';
try {
openID = wx.getStorageSync('openid')
} catch (e) {
}
if (openID) {
api.prePay(orderCode, openID, () => wx.hideLoading())
.then(data => {
if (data) {
callWxPay(data, productId, orderCode, fromWhere)
}
})
.catch(error => {
console.log(error)
})
} else {
//提示重新登录
}
}
function callWxPay(data, productId, orderCode, fromWhere) {
let that = this;
// let orderCode = ;
wx.requestPayment({
...data,
success: function (res) {
console.log('orderCode:' + orderCode + '==productID:' + productId);
let params = {
orderCode: orderCode,
product_id: productId
}
router.go('orderSuccess', params);
},
fail: function (res) {
console.log(res)
let params = {
orderCode: orderCode
}
if (res.errMsg == 'requestPayment:fail cancel') {
if(fromWhere == 0){
router.go('orderDetail', params);
}
} else {
wx.showModal({
content: res.errMsg,
showCancel: false,
confirmText: "确定",
complete: function (res) {
if (fromWhere == 0) {
router.go('orderDetail', params);
}
}
})
}
}
})
}