<template>
  <div>
    <div @click="scanCode">扫码</div>
  </div>
</template>

<script>
import { getPlatform } from "@/utils";
import { initWxJsSdkConfig } from '@/utils/login'

export default {
  created() {
    // 初始化微信
    initWxJsSdkConfig(['scanQRCode']);
  },
  methods: {
    // 扫码
    scanCode() {
      const runScanFn = ( res ) => {
        if ( res.scanType === 'QR_CODE' && res.scanType ) {
          console.log(res.result);
          const url = res.result;
          if ( url.indexOf('auth/') === -1 ) {
            this.getPaperCouponInfo(res.result);
          } else {
            const start = url.indexOf('auth/');
            const end = url.indexOf('?');
            const params = url.slice(start,end).split('/');
            if ( params && params.length ) {
              const couponCode = params[1];
              this.getPaperCouponInfo(couponCode);
            }
          }
        }
      };
      this.$wx.scanQRCode({
        needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
        scanType: ['qrCode','barCode'], // 可以指定扫二维码还是一维码，默认二者都有
        success: ( res ) => {
          console.log(35,res);
          runScanFn(res);
          // this.formMsg.deviceCode = res.resultStr;
        },
      });
    },
  }
}
</script>
