//这是封装的
//引入api文件
api = app.requirejs('api');
//
sub_order: function() {
let ta = this,
mation = ta.mation,
info = ta.info;
let e = {
currentTarget: {
dataset: {
}
}
}
if (!mation.arti) {
e.currentTarget.dataset.type = 'goods_content';
ta.eject(e);
return false;
} else if (!ta.treaty) {
core.alert('请先同意服务协议');
return false;
}
if (ta.status == 1) {
ta.toBuy();
} else if (ta.status == 2) {
api.yidu_pay(ta, ta.order_info.order_id, 'order');
}
},
//ta代表 上面的 let ta = this , order_id代表ta.order_info.order_id,order_type 代表'order'这里是固定的所以直接写'order' order_type是后端请求过来的参数 原这样写ta.order_info.order_type
yidu_pay(ta, order_id, order_type) {
if (ta.running) {
return false
}
ta.setData({
running: true
});
//调用接口 里面的参数是从后端请求过来的
core.get('pay/createWxPay', {
order_id: order_id,
order_type: order_type,
buyer_id: getApp().globalData.getCache('userinfo').uid
//请求成功后返回参数
}, function(t) {
ta.setData({
running: false
});
//对code进行判断
if (t.code == 0) {
//调取返回的数据传到uniapp支付api的uni.requestPayment
uni.requestPayment({
provider: 'wxpay',//服务提供商
timeStamp: t.data.timeStamp,
nonceStr: t.data.nonceStr,
'package': t.data.package,
signType: t.data.signType,
paySign: t.data.paySign,
//然后获取返回参数进行判断
success: function(res) {
if (res.errMsg == "requestPayment:ok") {
if (typeof ta.success_res == 'function') {
ta.success_res(1, "支付成功");
} else {
getApp().yidu.msg('支付成功');
}
} else {
getApp().yidu.msg('支付失败!')
}
},
fail: function(res) {
if (typeof ta.success_res == 'function') {
ta.success_res(2, "机不可失,不要让宝贝等太久哦");
} else {
getApp().yidu.msg('机不可失,不要让宝贝等太久哦')
}
}
})
} else if (t.code == 2) {
if (typeof ta.success_res == 'function') {
ta.success_res(1, "支付成功");
} else {
getApp().yidu.msg('支付成功');
}
} else {
if (typeof ta.success_res == 'function') {
ta.success_res(2, t.msg);
} else {
getApp().yidu.msg(t.msg);
}
}
}, true)
},
uniapp微信支付
最新推荐文章于 2024-05-19 14:58:37 发布