index.vue 6.83 KB
Newer Older
程默 committed
1
<template>
程默 committed
2
  <div class="domain">{{message}}</div>
程默 committed
3 4 5 6
</template>

<script>
import { formatTime } from "@/utils/index";
程默 committed
7
import wxPay from "@/api/wxPay";
程默 committed
8 9 10 11 12 13
export default {
  components: {
    // card
  },
  data() {
    return {
程默 committed
14
      message: "正在支付中",
程默 committed
15
      shopmixid: "",
程默 committed
16
      openId: "",
17
      options: {},
chenhui committed
18
      options_orderType:'',
19 20 21
      // 购买会员卡新页面
      options_isCard:'',
      options_isCardTitle:'',
程智春 committed
22 23
      batchNumber : '',
      totalMoney : ''
程默 committed
24 25 26
    };
  },
  onLoad(options) {
程默 committed
27 28 29
    wx.showLoading({
      title: "加载中"
    });
程默 committed
30
    Object.assign(this.$data, this.$options.data()); //mpvue 的混合周期 使用小程序生命周期数据未初始化
程默 committed
31
    console.log("onload//",options);
程智春 committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    if(options.batchNumber){
      let ordercode = JSON.parse(options.ordercode)
      ordercode.orderSn = ''
      ordercode.batchNumber = options.batchNumber
      this.options = ordercode
      this.batchNumber = ordercode.batchNumber
      console.log(JSON.parse(options.ordercode).orderSn)
      if(JSON.parse(options.ordercode).orderSn){
          this.totalMoney = Number(JSON.parse(options.ordercode).orderSn.totalAmount).toFixed(2)
        }
        
      // }
      console.log(this.totalMoney,'totalMoney')
    }else{
      this.options = JSON.parse(options.ordercode);
    }
    
chenhui committed
49
    this.options_orderType = options.orderType;
50 51
    this.options_isCard= options.isCard;
    this.options_isCardTitle= options.isCardTit;
程默 committed
52 53 54 55 56
    if(options.sessionid){
       try {
          wx.setStorageSync('sessionid', options.sessionid)
        } catch (e) { }
    }
程默 committed
57 58 59 60 61 62 63 64 65 66
    let that = this;
    wx.getStorage({
      key: "openid",
      success(res) {
        console.log(that.options, "++++");
        console.log(res, "openid");
        that.options.openId = res.data;
        that.options.tradeType = "Mini";
        console.log("支付开始", that.options);
        that.toPay();
程默 committed
67 68 69
      },
      fail(err){
        console.log('获取err',err)
程默 committed
70 71
      }
    });
程默 committed
72 73
  },
  methods: {
程默 committed
74 75 76 77
    toPay() {
      wxPay
        .to_wx_pay(this.options)
        .then(res => {
程默 committed
78 79 80
          wx.hideLoading();
          if (res.data.code == 200) {
            let payData = JSON.parse(res.data.data);
程默 committed
81
            console.log('wxPay',res.data)
程默 committed
82 83
            payData.success = res => {
              console.log("支付成功", res, this.options);
程智春 committed
84
              this.message = "支付成功";
chenhui committed
85
              const orderSn = this.options.orderSn;
程智春 committed
86 87
              let vm = this
              
chenhui committed
88
              try {
程智春 committed
89
                
chenhui committed
90 91
                const subscribeMessageObj = this.$store.state.subscribeMessageObj;
                if (subscribeMessageObj && Object.keys(subscribeMessageObj).length > 0) {
chenhui committed
92
                  // TODO 小程序订阅消息埋点
chenhui committed
93 94 95 96 97 98
                  const tmplIds = [];
                  for (var key in subscribeMessageObj) {
                    // 送礼订单
                    if (this.options_orderType == 5 && (key == 'gift_be_expire' || key == 'gift_draw')) {
                      tmplIds.push(subscribeMessageObj[key]);
                    }
程默 committed
99 100 101 102
                    // 订单发货
                    if (key == 'order_shipment') {
                      tmplIds.push(subscribeMessageObj[key]);
                    }
chenhui committed
103 104 105 106 107 108
                  }

                  if (tmplIds.length > 0) {
                    wx.requestSubscribeMessage({
                      tmplIds: tmplIds,
                      success(res) {
程智春 committed
109
                        fn(vm)
chenhui committed
110 111 112
                        console.log("message success response: ", res);
                      },
                      fail(res) {
程智春 committed
113
                        fn(vm)
chenhui committed
114 115 116
                        console.log("message fail response: ", res)
                      }
                    })
程智春 committed
117 118
                  }else{
                    fn(vm)
chenhui committed
119
                  }
程智春 committed
120 121 122
                }else{
                  console.log('来这里了')
                  fn(this)
chenhui committed
123
                }
程智春 committed
124

chenhui committed
125 126
              } catch (err) {
                console.error("subscribeMessage-err", err);
chenhui committed
127
              }
程智春 committed
128
              function fn(_this){
程智春 committed
129
                // _this.message = "支付成功";
程智春 committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
                if(!_this.options_isCard){
                  let query;
                  if(_this.batchNumber){
                    query = {
                      orderSn: _this.options.orderSn,
                      batchNumber : _this.batchNumber,
                      multiMerchantsFlag : 1,
                      totalMoney : _this.totalMoney
                    };
                  }else{
                    query = {
                      orderSn: _this.options.orderSn
                    };
                  }
                  
                  wx.redirectTo({
                    url: `../index/main?from=wxPay&backpath=/pay/paySuccess&params=${JSON.stringify(
                      query
                    )}`
                  });
                }else{
                    let query = {
                      cardTitle: _this.options_isCardTitle
                    }
                  wx.redirectTo({
                    url: `../index/main?from=wxPay&backpath=/pay/payCardSuccess&params=${JSON.stringify(
                      query
                    )}`
                  });
                }
160
              }
程默 committed
161
            };
程默 committed
162 163 164
            payData.fail = res => {
              console.log("支付失败", res, this.options.orderSn);
              this.message = "支付失败";
程默 committed
165 166 167 168 169
              if (res.errMsg='requestPayment:fail cancel') {
                wxPay.cancelPayment(this.options.orderSn).then(() => {
                  console.log("取消成功");
                });
              }
程默 committed
170
              this.toPage();
程默 committed
171 172
            };
            payData.complete = res => {
程默 committed
173 174 175 176 177 178
              console.log("支付completa", res, this.options.orderSn);
              // if (res.errMsg == "requestPayment:fail cancel") {
              //   console.log("支付取消", this.options);
              //   this.message = "支付取消";
              //   this.toPage();
              // }
程默 committed
179 180 181
            };
            wx.requestPayment(payData);
          } else {
程默 committed
182
            wx.showToast({ title: res.data.msg, icon: "none" });
程默 committed
183
          }
程默 committed
184
        })
程默 committed
185
        .catch(err => {
程默 committed
186
          wx.showToast({ title: res.data.msg, icon: "none" });
程默 committed
187
        });
程默 committed
188 189
    },
    toPage() {
程智春 committed
190 191 192 193 194 195 196 197 198 199
      console.log('支付失败来这里了',this.batchNumber)
      if(this.batchNumber){
        let query = {
          multiMerchantsFlag : 1
        }
        wx.redirectTo({
          url: `../index/main?from=wxPay&backpath=/order/orderList&params=${JSON.stringify(query)}`
        });
        return
      }
程默 committed
200 201 202 203 204 205 206 207 208 209
      let query = {
        orderSn: this.options.orderSn,
        payList: true,
        orderListToorderDetail: true
      };
      wx.redirectTo({
        url: `../index/main?from=wxPay&backpath=/order/orderDetail&params=${JSON.stringify(
          query
        )}`
      });
程默 committed
210 211 212 213 214 215
    }
  }
};
</script>

<style>
程默 committed
216
.domain {
程默 committed
217
  text-align: center;
程默 committed
218
  padding-top: 50%;
程默 committed
219 220
}
</style>