<template>
  <!-- 暂时无用 -->
  <div class="pay-box"></div>
</template>
<script>
import Pay from '../action'
export default {
  name: 'PayH5',
  data () {},
  props: {
    requestParam: {
      type: Object,
      required: false,
      default () {
        return {
          product_id: '', // 提前设定
          open_id: '', // 通过微信授权获取
          prepay_id: '', // 通过调用接口获取
          app_id: '', // 提前设定
          app_secret: '', // 提前设定
          // 新增字段 接口返回
          timeStamp: '',
          nonceStr: '',
          signType: '',
          paySign: ''
        }
      }
    }
  },
  beforeDestroy() {
  },
  mounted () {
    /* 判别是否引入JSSDK */
    // if (typeof WeixinJSBridge === 'undefined') {
    //   console.log('尚未引入微信JSSDK或JSSDK未执行')
    //   if (document.addEventListener) {
    //     document.addEventListener('WeixinJSBridgeReady', this.onBridgeReady.bind(this), false)
    //   } else if (document.attachEvent) {
    //     document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady.bind(this))
    //     document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady.bind(this))
    //   }
    // } else {
    //   this.onBridgeReady()
    // }
    this.onBridgeReady()
  },
  methods: {
    onBridgeReady () {
      this.VueEvent.$off('pay-wx-h5').$on('pay-wx-h5', (obj) => {
        this.goToPay()
      })
    },
    goToPay () {
      const obj = { code: 0, msg: '', data: {} }
      if (!this.requestParam.product_id) {
        obj.code = 3001
        obj.msg = '没有商品ID'
        this.VueEvent.$emit('pay-wx-h5-callback', obj)
        return false
      }
      if (!this.requestParam.open_id) {
        obj.code = 3002
        obj.msg = '没有小程序用户openid'
        this.VueEvent.$emit('pay-wx-h5-callback', obj)
        return false
      }
      Pay.getOrderId(this.requestParam).then(res => {
        if (res.code) {
          // this.requestParam.product_id
          // this.requestParam.open_id
          this.requestParam.prepay_id = res.data.prepay_id
          this.requestParam.app_id = res.data.options.appId
          // this.requestParam.app_secret

          this.requestParam.timeStamp = res.data.options.timeStamp
          this.requestParam.nonceStr = res.data.options.nonceStr
          this.requestParam.signType = res.data.options.signType
          this.requestParam.paySign = res.data.options.paySign
          this.wxWakeUpToPay(obj)
        } else {
          obj.code = 3003
          obj.msg = '获取订单ID失败'
          this.VueEvent.$emit('pay-wx-h5-callback', obj)
        }
      }).catch(e => {
        obj.code = 5001
        obj.msg = e.message
        this.VueEvent.$emit('pay-wx-h5-callback', obj)
        return obj
      })
    },
    wxWakeUpToPay (obj) {
      if (typeof WeixinJSBridge === 'undefined') {
        obj.code = 4001
        obj.msg = '缺少微信api'
        this.VueEvent.$emit('pay-wx-h5-callback', obj)
        return obj
      }
      const _this = this
      /* 直接调用微信支付 */
      // const timestamp = parseInt(new Date().getTime() / 1000)
      // const noncestr = parseInt(Math.random() * 10000000000000000)
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest', {
          appId: this.requestParam.app_id, // 公众号名称，由商户传入
          timeStamp: this.requestParam.timeStamp, // 时间戳，自1970年以来的秒数
          nonceStr: this.requestParam.nonceStr, // 随机串
          package: 'prepay_id=' + this.requestParam.prepay_id,
          signType: this.requestParam.signType, // 微信签名方式：MD5
          paySign: this.requestParam.paySign // 微信签名
        },
        function (res) {
          /* 小程序调用失败 */
          obj.code = 4000
          obj.msg = res.err_msg
          if (res.err_msg === 'get_brand_wcpay_request:ok') {
            // 使用以上方式判断前端返回,微信团队郑重提示：
            // res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
            obj.code = 200
          }
          _this.VueEvent.$emit('pay-wx-h5-callback', obj)
        }
      )
    }
  }
}
</script>

<style lang="scss">

</style>
