wx-pay.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { initiatePay } from "@/api/trade";
  2. class LiLiWXPay {
  3. constructor(...payList) {
  4. this.data = payList[0];
  5. // 调用支付
  6. this.pay = () => {
  7. uni.showLoading({
  8. title: "加载中",
  9. });
  10. let submitData = {
  11. sn: this.data.sn,
  12. orderType: this.data.orderType || "TRADE",
  13. clientType: "WECHAT_MP",
  14. };
  15. const paymentMethod = "WECHAT";
  16. const paymentClient = "MP";
  17. // 调用支付
  18. initiatePay(paymentMethod, paymentClient, submitData).then((res) => {
  19. let response = res.data.result;
  20. uni.hideLoading();
  21. uni.requestPayment({
  22. provider: "wxpay",
  23. appid: response.appid,
  24. timeStamp: response.timeStamp,
  25. nonceStr: response.nonceStr,
  26. package: response.package,
  27. signType: response.signType,
  28. paySign: response.paySign,
  29. success: (e) => {
  30. uni.showToast({
  31. icon: "none",
  32. title: "支付成功!",
  33. });
  34. // 之后成功后跳转到支付成功页面
  35. uni.redirectTo({
  36. url:
  37. "/pages/cart/payment/success?paymentMethod=WECHAT" +
  38. "&payPrice=" +
  39. this.data.price,
  40. });
  41. },
  42. fail: (e) => {
  43. this.exception = e;
  44. // 支付异常或支付失败之后跳转到订单页面
  45. uni.showModal({
  46. content: "支付失败,如果您已支付,请勿反复支付",
  47. showCancel: false,
  48. success: () => {
  49. uni.redirectTo({
  50. url: "/pages/order/myOrder?status=0",
  51. });
  52. },
  53. });
  54. },
  55. });
  56. });
  57. };
  58. }
  59. }
  60. export default LiLiWXPay;