| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 | "use strict";const common_vendor = require("../../common/vendor.js");const sheep_index = require("../index.js");require("../helper/index.js");const sheep_api_pay_order = require("../api/pay/order.js");class SheepPay {  constructor(payment, orderType, id) {    this.payment = payment;    this.id = id;    this.orderType = orderType;    this.payAction();  }  payAction() {    const payAction = {      WechatOfficialAccount: {        wechat: () => {          this.wechatOfficialAccountPay();        },        alipay: () => {          this.redirectPay();        },        wallet: () => {          this.walletPay();        },        mock: () => {          this.mockPay();        }      },      WechatMiniProgram: {        wechat: () => {          this.wechatMiniProgramPay();        },        alipay: () => {          this.copyPayLink();        },        wallet: () => {          this.walletPay();        },        mock: () => {          this.mockPay();        }      },      App: {        wechat: () => {          this.wechatAppPay();        },        alipay: () => {          this.alipay();        },        wallet: () => {          this.walletPay();        },        mock: () => {          this.mockPay();        }      },      H5: {        wechat: () => {          this.wechatWapPay();        },        alipay: () => {          this.redirectPay();        },        wallet: () => {          this.walletPay();        },        mock: () => {          this.mockPay();        }      }    };    return payAction[sheep_index.sheep.$platform.name][this.payment]();  }  // 预支付  prepay(channel) {    return new Promise(async (resolve, reject) => {      let data = {        id: this.id,        channelCode: channel,        channelExtras: {}      };      if (["wx_pub", "wx_lite"].includes(channel)) {        const openid = await sheep_index.sheep.$platform.useProvider("wechat").getOpenid();        if (!openid) {          this.bindWeixin();          return;        }        data.channelExtras.openid = openid;      }      sheep_api_pay_order.PayOrderApi.submitOrder(data).then((res) => {        res.code === 0 && resolve(res);        if (res.code !== 0 && res.msg.indexOf("无效的openid") >= 0) {          if (res.msg.indexOf("无效的openid") >= 0 || res.msg.indexOf("下单账号与支付账号不一致") >= 0) {            this.bindWeixin();          }        }      });    });  }  // 微信小程序支付  async wechatMiniProgramPay() {    let { code, data } = await this.prepay("wx_lite");    if (code !== 0) {      return;    }    const payConfig = JSON.parse(data.displayContent);    common_vendor.index.requestPayment({      provider: "wxpay",      timeStamp: payConfig.timeStamp,      nonceStr: payConfig.nonceStr,      package: payConfig.packageValue,      signType: payConfig.signType,      paySign: payConfig.paySign,      success: (res) => {        this.payResult("success");      },      fail: (err) => {        if (err.errMsg === "requestPayment:fail cancel") {          sheep_index.sheep.$helper.toast("支付已手动取消");        } else {          this.payResult("fail");        }      }    });  }  // 余额支付  async walletPay() {    const { code } = await this.prepay("wallet");    code === 0 && this.payResult("success");  }  // 模拟支付  async mockPay() {    const { code } = await this.prepay("mock");    code === 0 && this.payResult("success");  }  // 支付宝复制链接支付  TODO 芋艿:待接入  async copyPayLink() {    let { error, data } = await this.prepay();    if (error === 0) {      common_vendor.index.showModal({        title: "支付宝支付",        content: "复制链接到外部浏览器",        confirmText: "复制链接",        success: (res) => {          if (res.confirm) {            sheep_index.sheep.$helper.copyText(data.pay_data);          }        }      });    }  }  // 支付宝支付  TODO 芋艿:待接入  async alipay() {    let that = this;    const { error, data } = await this.prepay();    if (error === 0) {      common_vendor.index.requestPayment({        provider: "alipay",        orderInfo: data.pay_data,        //支付宝订单数据        success: (res) => {          that.payResult("success");        },        fail: (err) => {          if (err.errMsg === "requestPayment:fail [paymentAlipay:62001]user cancel") {            sheep_index.sheep.$helper.toast("支付已手动取消");          } else {            that.payResult("fail");          }        }      });    }  }  // 微信支付  TODO 芋艿:待接入  async wechatAppPay() {    let that = this;    let { error, data } = await this.prepay();    if (error === 0) {      common_vendor.index.requestPayment({        provider: "wxpay",        orderInfo: data.pay_data,        //微信订单数据(官方说是string。实测为object)        success: (res) => {          that.payResult("success");        },        fail: (err) => {          err.errMsg !== "requestPayment:fail cancel" && that.payResult("fail");        }      });    }  }  // 支付结果跳转,success:成功,fail:失败  payResult(resultType) {    sheep_index.sheep.$router.redirect("/pages/pay/result", {      id: this.id,      orderType: this.orderType,      payState: resultType    });  }  // 引导绑定微信  bindWeixin() {    common_vendor.index.showModal({      title: "微信支付",      content: "请先绑定微信再使用微信支付",      success: function(res) {        if (res.confirm) {          sheep_index.sheep.$platform.useProvider("wechat").bind();        }      }    });  }}function getPayMethods(channels) {  const payMethods = [    {      icon: "/static/img/shop/pay/wechat.png",      title: "微信支付",      value: "wechat",      disabled: true    },    {      icon: "/static/img/shop/pay/alipay.png",      title: "支付宝支付",      value: "alipay",      disabled: true    },    {      icon: "/static/img/shop/pay/wallet.png",      title: "余额支付",      value: "wallet",      disabled: true    },    {      icon: "/static/img/shop/pay/apple.png",      title: "Apple Pay",      value: "apple",      disabled: true    },    {      icon: "/static/img/shop/pay/wallet.png",      title: "模拟支付",      value: "mock",      disabled: true    }  ];  const platform = sheep_index.sheep.$platform.name;  const wechatMethod = payMethods[0];  if (platform === "WechatOfficialAccount" && channels.includes("wx_pub") || platform === "WechatMiniProgram" && channels.includes("wx_lite") || platform === "App" && channels.includes("wx_app")) {    wechatMethod.disabled = false;  }  wechatMethod.disabled = false;  const alipayMethod = payMethods[1];  if (platform === "WechatOfficialAccount" && channels.includes("alipay_wap") || platform === "WechatMiniProgram" && channels.includes("alipay_wap") || platform === "App" && channels.includes("alipay_app")) {    alipayMethod.disabled = false;  }  const walletMethod = payMethods[2];  if (channels.includes("wallet")) {    walletMethod.disabled = false;  }  const mockMethod = payMethods[4];  if (channels.includes("mock")) {    mockMethod.disabled = false;  }  return payMethods;}exports.SheepPay = SheepPay;exports.getPayMethods = getPayMethods;
 |