wx-pay.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. console.log(response,9996665);
  21. uni.hideLoading();
  22. uni.requestPayment({
  23. provider: "wxpay",
  24. appid: response.appid,
  25. timeStamp: response.timeStamp,
  26. nonceStr: response.nonceStr,
  27. package: response.package,
  28. signType: response.signType,
  29. paySign: response.paySign,
  30. success: (e) => {
  31. uni.showToast({
  32. icon: "none",
  33. title: "支付成功!",
  34. });
  35. // 之后成功后跳转到支付成功页面
  36. uni.redirectTo({
  37. url:
  38. "/pages/cart/payment/success?paymentMethod=WECHAT" +
  39. "&payPrice=" +
  40. this.data.price,
  41. });
  42. },
  43. fail: (e) => {
  44. this.exception = e;
  45. // 支付异常或支付失败之后跳转到订单页面
  46. uni.showModal({
  47. content: "支付失败,如果您已支付,请勿反复支付",
  48. showCancel: false,
  49. success: () => {
  50. uni.redirectTo({
  51. url: "/pages/order/myOrder?status=0",
  52. });
  53. },
  54. });
  55. },
  56. });
  57. });
  58. };
  59. }
  60. }
  61. export default LiLiWXPay;