绿满眶商城微信小程序-uniapp
peng
17 小时以前 a6c950ba797dffec842cf7e923fc439868645766
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
 * 微信小程序支付
 * 此处针对于微信小程序开发的支付插件
 * 第一次支付成功后会跳出订阅的消息 如果用户拒绝或同意都会跳转到支付成功页面
 * 如果点击订阅 会将状态写进缓存 之后不再提醒。
 * 
 * @param {sn,price}
 */
 
import { getWeChatMpMessage } from "@/api/message.js";
import { initiatePay } from "@/api/trade";
class LiLiWXPay {
  constructor(...payList) {
    this.data = payList[0];
    console.log(payList);
    // 调用支付
    this.pay = () => {
      uni.showLoading({
        title: "加载中",
      });
 
      let submitData = {
        sn: this.data.sn,
        orderType: this.data.orderType || "TRADE",
        clientType: "WECHAT_MP",
      };
      const paymentMethod = "WECHAT";
      const paymentClient = "MP";
      // 调用支付
      initiatePay(paymentMethod, paymentClient, submitData).then((res) => {
        let response = res.data.result;
        uni.hideLoading();
        uni.requestPayment({
          provider: "wxpay",
          appid: response.appid,
          timeStamp: response.timeStamp,
          nonceStr: response.nonceStr,
          package: response.package,
          signType: response.signType,
          paySign: response.paySign,
          success: (e) => {
            uni.showToast({
              icon: "none",
              title: "支付成功!",
            });
            sendMessage(payList[0].price);
          },
          fail: (e) => {
            this.exception = e;
            // 支付异常或支付失败之后跳转到订单页面
            uni.showModal({
              content: "支付失败,如果您已支付,请勿反复支付",
              showCancel: false,
              success: () => {
                uni.redirectTo({
                  url: "/pages/order/myOrder?status=0",
                });
              },
            });
          },
        });
      });
    };
  }
}
 
function sendMessage(price) {
 
 
  //订阅消息
  getWeChatMpMessage().then((res) => {
    var message = res.data.result;
    var templateid = message.map((item) => item.code);
    uni.requestSubscribeMessage({
      tmplIds: templateid,
      success: (res) => {
      
      },
      fail: (res) => {
        console.log('fail', res)
        uni.showToast({
          icon: "none",
          title: "订阅消息失败",
        })
      },
      complete: (res) => {
        console.log('complete', res)
 
        /**
         * 已经支付成功
         */
        uni.redirectTo({
          url:
            "/pages/cart/payment/success?paymentMethod=WECHAT" +
            "&payPrice=" +
            price,
        });
      },
    });
  });
 
}
 
export default LiLiWXPay;