wechatPay.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. function payFaildRedirect(orderInfo) {
  22. setTimeout(() => {
  23. uni.setStorageSync('orderInfo', orderInfo)
  24. uni.redirectTo({ url: '/pages/user/order/success' })
  25. }, 2000)
  26. }
  27. // 微信支付
  28. const wechatPay = {
  29. data() {
  30. return {
  31. loadingText: '请稍等...',
  32. isSubLoading: false,
  33. }
  34. },
  35. methods: {
  36. // 验证微信支付是否可用
  37. miniWxPayFor(data) {
  38. this.isSubLoading = true
  39. this.PayService.PayOrderOnLineSwitch().then(response => {
  40. if (response.data === 1) {
  41. this.weChatMiniWxPay(data)
  42. } else {
  43. this.isSubLoading = false
  44. this.$api.navigateTo(`/pages/user/order/order-payment?money=${data.payableAmount}`)
  45. }
  46. })
  47. },
  48. // 获取微信支付payment
  49. async weChatMiniWxPay(data) {
  50. if (this.loadingText) this.loadingText = '等待支付中'
  51. try {
  52. // 获取微信code
  53. const wechatCode = await authorize.getCode('weixin')
  54. // 微信支付请求 返回支付信息
  55. const response = await this.PayService.WeChatMiniWxPay({
  56. payAmount: data.payableAmount * 100,
  57. payWay: 'WEIXIN',
  58. code: wechatCode,
  59. orderId: data.orderId
  60. })
  61. // 处理支付信息 调用微信支付
  62. const payment = JSON.parse(response.data.data.payInfo)
  63. const payFlag = await wxRequestPayment(payment)
  64. // 支付重定向
  65. uni.reLaunch({ url: '/pages/tabBar/index/index' })
  66. } catch (error) {
  67. // 微信支付失败
  68. if (error.msg === 'error') payFaildRedirect(this.hanldOrder.order)
  69. else this.$util.msg(error.msg, 2000)
  70. } finally {
  71. this.isSubLoading = false
  72. }
  73. }
  74. }
  75. }
  76. export default wechatPay