wechatPay.js 2.7 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. 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.reLaunch({ url: '/pages/tabBar/index/index' })
  59. } catch (error) {
  60. // 微信支付失败
  61. if (error.msg === 'error') this.payFaildRedirect()
  62. else this.$util.msg(error.msg, 2000)
  63. } finally {
  64. this.isSubLoading = false
  65. }
  66. },
  67. // 支付信息提示页面重定向
  68. payFaildRedirect() {
  69. setTimeout(() => {
  70. const redirectData = JSON.stringify({ orderInfo: this.hanldOrder.order })
  71. this.$api.redirectTo(`/pages/user/order/success?data=${redirectData}`)
  72. }, 2000)
  73. }
  74. },
  75. }
  76. export default wechatPay