<template>
  <!-- 暂时无用 -->
  <div class="pay-box"></div>
</template>
<script>
import Pay from '../action'
export default {
  name: 'PayH5',
  components: {},
  data () {
    const _this = this
    return {
      isCard: false,
      opaKey: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
      remainingTime: '01:00:00',
      currentNum: 1,
      isAnalysis: false,
      swiperOptions: {
        observer: true,
        on: {
          slideChangeTransitionStart: function() {
            document.getElementById('bottom-view').scrollTop = 0
            _this.isAnalysis = false
            _this.currentNum = this.activeIndex + 1
          }
        }
      },
      questionsData: {
        questions: []
      },
      requestData: {},
      requestData2: {},
      clockCount: null,
      isExamEnd: true,
      // examIsShow: false,
      isExamSubPop: false,
      timePopIsShow: false,
      initTime: null,
      cache: null
    }
  },
  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) => {
        const _callback = obj.callback || function () {}
        const _obj = this.goToPay()
        _callback(_obj)
      })
    },
    async goToPay () {
      const obj = { code: 0, msg: '', data: {} }
      if (!this.requestParam.product_id) {
        obj.code = 3001
        obj.msg = '没有商品ID'
        return obj
      }
      if (!this.requestParam.open_id) {
        obj.code = 3002
        obj.msg = '没有小程序用户openid'
        return obj
      }
      return await 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)
        }
      }).catch(e => {
        obj.code = 5001
        obj.msg = e.message
        return obj
      })
    },
    async wxWakeUpToPay (obj) {
      /* 直接调用微信支付 */
      // 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.msg = res.err_msg
          return obj
          // if (res.err_msg === 'get_brand_wcpay_request:ok') {
          //   // 使用以上方式判断前端返回,微信团队郑重提示：
          //   // res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
          // }
        }
      )
    }
  }
}
</script>

<style lang="scss">

</style>
