wechatPay.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import authorize from '@/common/authorize.js'
  2. // 调用微信支付
  3. function wxRequestPayment(payment) {
  4. return new Promise((resolve, reject) => {
  5. uni.requestPayment({
  6. timeStamp: payment.timeStamp,
  7. nonceStr: payment.nonceStr,
  8. package: payment.package,
  9. signType: payment.signType,
  10. paySign: payment.paySign,
  11. success: () => {
  12. resolve(true)
  13. },
  14. fail: () => {
  15. reject({ msg: 'error' })
  16. }
  17. })
  18. })
  19. }
  20. // 微信支付
  21. const wechatPay = {
  22. data() {
  23. return {
  24. loadingText: '请稍等...',
  25. isSubLoading: false,
  26. }
  27. },
  28. methods: {
  29. // 验证微信支付是否可用
  30. miniWxPayFor(data) {
  31. this.isSubLoading = true
  32. this.PayService.PayOrderOnLineSwitch().then(response => {
  33. if (response.data === 1) {
  34. this.weChatMiniWxPay(data)
  35. } else {
  36. this.isSubLoading = false
  37. this.$api.navigateTo(`/pages/order/order-payment?money=${data.payableAmount}`)
  38. }
  39. })
  40. },
  41. // 获取微信支付payment
  42. async weChatMiniWxPay(data) {
  43. if (this.loadingText) this.loadingText = '等待支付中'
  44. try {
  45. // 获取微信code
  46. const wechatCode = await authorize.getCode('weixin')
  47. // 微信支付请求 返回支付信息
  48. const response = await this.PayService.WeChatMiniWxPay({
  49. payAmount: data.payableAmount,
  50. payWay: 'WEIXIN',
  51. payType: 'XCX',
  52. code: wechatCode,
  53. orderId: data.orderId,
  54. shopOrderId: data.shopOrderId
  55. })
  56. // 处理支付信息 调用微信支付
  57. const payment = JSON.parse(response.data.rt10_payInfo)
  58. const payFlag = await wxRequestPayment(payment)
  59. // debugger
  60. // console.log(this.hanldOrder)
  61. // 支付成功重定向
  62. uni.setStorageSync('orderInfo', this.hanldOrder.order)
  63. if (payFlag) {
  64. // 微信支付成功回调 在页面onload中通过this.$on监听orderPaySuccess事件回调
  65. this.$emit('orderPaySuccess', this.hanldOrder.order)
  66. } else {
  67. uni.reLaunch({ url: '/pages/order/error' })
  68. }
  69. } catch (error) {
  70. // 微信支付失败
  71. if (error.msg === 'error') {
  72. uni.setStorageSync('orderInfo', this.hanldOrder.order)
  73. uni.reLaunch({ url: '/pages/order/error' })
  74. } else this.$util.msg(error.msg, 2000)
  75. } finally {
  76. this.isSubLoading = false
  77. }
  78. }
  79. }
  80. }
  81. export default wechatPay