wechatPay.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 * 100,
  50. payWay: 'WEIXIN',
  51. code: wechatCode,
  52. orderId: data.orderId
  53. })
  54. // 处理支付信息 调用微信支付
  55. const payment = JSON.parse(response.data.data.payInfo)
  56. const payFlag = await wxRequestPayment(payment)
  57. // debugger
  58. // console.log(this.hanldOrder)
  59. // 支付成功重定向
  60. uni.setStorageSync('orderInfo', this.hanldOrder.order)
  61. if (payFlag) {
  62. // 微信支付成功回调 在页面onload中通过this.$on监听orderPaySuccess事件回调
  63. this.$emit('orderPaySuccess', this.hanldOrder.order)
  64. } else {
  65. uni.reLaunch({ url: '/pages/order/error' })
  66. }
  67. } catch (error) {
  68. // 微信支付失败
  69. if (error.msg === 'error') {
  70. uni.setStorageSync('orderInfo', this.hanldOrder.order)
  71. uni.reLaunch({ url: '/pages/order/error' })
  72. } else this.$util.msg(error.msg, 2000)
  73. } finally {
  74. this.isSubLoading = false
  75. }
  76. }
  77. }
  78. }
  79. export default wechatPay