wechatPay.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import authorize from '@/common/authorize.js'
  2. // 调用微信支付
  3. function wxRequestPayment(payData) {
  4. return new Promise((resolve, reject) => {
  5. uni.requestPayment({
  6. timeStamp: payData.timeStamp,
  7. nonceStr: payData.nonceStr,
  8. package: payData.package,
  9. signType: payData.signType,
  10. paySign: payData.paySign,
  11. success: () => {
  12. resolve(true)
  13. },
  14. fail: () => {
  15. reject({ msg: 'error' })
  16. }
  17. })
  18. })
  19. }
  20. // 微信支付
  21. const wechatCouponPay = {
  22. data() {
  23. },
  24. methods: {
  25. // 获取微信支付payment
  26. async weChatMiniWxPay(data) {
  27. try {
  28. // 获取微信code
  29. const wechatCode = await authorize.getCode('weixin')
  30. // 微信支付请求 返回支付信息
  31. const response = await this.PayService.WeChatCouponMiniWxPay({
  32. userId: this.listQuery.userId,
  33. couponId: coupon.couponId,
  34. payWay: 'WEIXIN',
  35. code: wechatcode
  36. })
  37. // 处理支付信息 调用微信支付
  38. const payment = JSON.parse(response.data.data.payInfo)
  39. const payFlag = await wxRequestPayment(payment)
  40. // debugger
  41. // console.log(this.hanldOrder)
  42. // 支付成功重定向
  43. uni.setStorageSync('orderInfo', this.hanldOrder.order)
  44. if (payFlag) {
  45. // 微信支付成功回调 在页面onload中通过this.$on监听orderPaySuccess事件回调
  46. uni.reLaunch({url: '/pages/tabBar/user/user'})
  47. } else {
  48. uni.reLaunch({ url: '/pages/order/error' })
  49. }
  50. } catch (error) {
  51. // 微信支付失败
  52. this.$util.msg(error.msg, 2000)
  53. }
  54. }
  55. }
  56. }
  57. export default {
  58. wechatCouponPay
  59. }