wxpay.js
2.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
import orderService from './orderService.js'
import router from '../../router/index.js'
import Taro from '@tarojs/taro';
import wx from '../../libs/wx';
// const api = new orderService();
/**
* orderCode 订单号
* fromWhere 支付来源页面 0:确认订单页面,1:订单列表,2:订单详情
*/
export const prePay = function (productId, orderCode, fromWhere, isStore) {
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, isStore)
} else {
throw new Error('ufo.order.pay fail.')
}
})
.catch(() => {
// 调用出错
Taro.showToast({
title: '订单不可支付',
duration: 1500,
icon: 'none'
});
let currentRoute;
let currentPages = getCurrentPages();
if (currentPages && currentPages.length) {
currentRoute = currentPages[currentPages.length - 1].route;
}
let noRedirectRoutes = ['pages/orderDetail/index', 'pages/orderList/index'];
if (noRedirectRoutes.indexOf(currentRoute) > -1) {
return;
}
setTimeout(() => {
wx.redirectTo({
url: '/pages/orderDetail/index?orderCode=' + orderCode
});
}, 1500);
})
} else {
//提示重新登录
}
}
function callWxPay(data, productId, orderCode, fromWhere ,isStore) {
let that = this;
// let orderCode = ;
wx.requestPayment({
...data,
success: function (res) {
console.log('orderCode:' + orderCode + '==productID:' + productId);
let params = {
orderCode: orderCode,
product_id: productId,
isStore
}
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, 'redirectTo');
}
} else {
wx.showModal({
content: res.errMsg,
showCancel: false,
confirmText: "确定",
complete: function (res) {
if (fromWhere == 0) {
router.go('orderDetail', params, 'redirectTo');
}
}
})
}
}
})
}