<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: '' // 提前设定
        }
      }
    }
  },
  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)
      })
    },
    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 Pay.getOrderId(this.requestParam).then(res => {
        obj.data = res
        this.wxWakeUpToPay()
        // return
      }).catch(e => {
        obj.code = 5001
        obj.msg = e.message
        return obj
      })
    },
    wxWakeUpToPay () {
      /* 直接调用微信支付 */
      const timestamp = parseInt(new Date().getTime() / 1000)
      const noncestr = parseInt(Math.random() * 10000000000000000)
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest', {
          appId: this.requestParam.app_id, // 公众号名称，由商户传入
          timeStamp: timestamp + '', // 时间戳，自1970年以来的秒数
          nonceStr: noncestr + '', // 随机串
          package: 'prepay_id=' + this.requestParam.prepay_id,
          signType: 'MD5', // 微信签名方式：
          paySign: '70EA570631E4BB79628FBCA90534C63FF7FADD89' // 微信签名
        },
        function (res) {
          if (res.err_msg === 'get_brand_wcpay_request:ok') {
            // 使用以上方式判断前端返回,微信团队郑重提示：
            // res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
          }
        }
      )
    }
  }
}
</script>

<style lang="scss">

</style>
