wechatPay.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/user/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. // 支付成功重定向
  58. uni.setStorageSync('orderInfo', this.hanldOrder.order)
  59. if (payFlag) {
  60. uni.redirectTo({ url: '/pages/user/order/success' })
  61. } else {
  62. uni.redirectTo({ url: '/pages/user/order/error' })
  63. }
  64. } catch (error) {
  65. // 微信支付失败
  66. if (error.msg === 'error') {
  67. uni.setStorageSync('orderInfo', this.hanldOrder.order)
  68. uni.redirectTo({ url: '/pages/user/order/error' })
  69. } else this.$util.msg(error.msg, 2000)
  70. } finally {
  71. this.isSubLoading = false
  72. }
  73. }
  74. }
  75. }
  76. export default wechatPay