項目需求
項目生成訂單,需要用戶通過小程序進行支付。
解決方案
使用uni.requestPayment(),調用小程序中的付款功能。
<script> export default { data() { return { request:{ id:xxxxx, info:'xxxx' } } }, onLoad() { }, methods: { // 提交訂單 subOrder() { this.post("api/order/createOrder", this.request).then(res => { if (res.code == 0) { // 得到訂單的反參調用微信支付 let pay = JSON.parse(res.data.prepayId); uni.requestPayment({ timeStamp: pay.timeStamp, nonceStr: pay.nonceStr, package: pay.package, signType: pay.signType, paySign: pay.paySign, // 支付成功的回調 success(result) { console.log(result) if (result.errMsg == "requestPayment:ok") { uni.showToast({ icon: "success", title: "購買成功!" }) } }, // 支付失敗回調 fail(err) { uni.showToast({ icon: "none", title: "支付失敗" }) } }) } }); } } } </script>