<template>
  <div>
    <div class="" v-if="insert">
      <p style="margin: 0" @click="uploadImg">
        <van-icon name="scan" size="24px" color="#1989fa" />
      </p>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import { getOptionByResult } from '@/api/index';

export default {
  name: 'storeCode',
  props: {
    index: {
      type: Number,
      default: -1,
    },
    tid: {
      type: String,
      default: '',
    },
    cid: {
      type: Number,
      default: -1,
    },
    code: {
      type: String,
      default: '',
    },
    index1: {
      type: [String, Number],
      default: '',
    },
    index2: {
      type: [String, Number],
      default: '',
    },
    index3: {
      type: [String, Number],
      default: '',
    },
    index4: {
      type: [String, Number],
      default: '',
    },
    index5: {
      type: [String, Number],
      default: '',
    },
    index6: {
      type: [String, Number],
      default: '',
    },
    index7: {
      type: [String, Number],
      default: '',
    },
    index8: {
      type: [String, Number],
      default: '',
    },
    taskId: {
      type: String,
      default: '',
    },
    collectionId: {
      type: String,
      default: '',
    },
    type: {
      type: String,
      default: '',
    },
    insert: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      shows: false,
      url: '',
    };
  },
  methods: {
    uploadImg() {
      let url = window.location.href;
      let that = this;
      let wx = this.wx;
      let qiyeData;
      console.log(that.code);

      const instance = axios.create();
      instance.defaults.headers.common['userId'] = localStorage.getItem('loginName');
      instance
        .get(process.env.VUE_APP_BASE_API + 'mobile/wx/ticket', {
          params: {
            url: url,
          },
        })
        .then((response) => {
          if (response.status == 200) {
            qiyeData = response.data.data;
            wx.config({
              beta: true, // 必须这么写，否则wx.invoke调用形式的jsapi会有问题
              debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
              appId: qiyeData.appId, // 必填，企业微信的corpID
              timestamp: qiyeData.timestamp, // 必填，生成签名的时间戳
              nonceStr: qiyeData.nonceStr, // 必填，生成签名的随机串
              signature: qiyeData.signature, // 必填，签名，见 附录-JS-SDK使用权限签名算法
              jsApiList: ['ready', 'scanQRCode'],
            });
            wx.ready(function () {
              wx.scanQRCode({
                desc: 'scanQRCode desc',
                needResult: 1,
                scanType: ['barCode'],
                success: function (res) {
                  console.log(res);
                  var result = res.resultStr;
                  console.log(result);
                  var success = false;
                  if (that.code == result && that.code != '' && that.code != null) {
                    success = true;
                  }
                  let loading1 = that.$toast.loading({
                    duration: 0,
                    message: '数据加载中...',
                    forbidClick: true,
                  });
                  getOptionByResult({
                    taskId: that.tid,
                    collectionId: that.cid,
                    smResult: success,
                  }).then((response) => {
                    loading1.clear();
                    if (response.code == 200) {
                      that.$emit('newcode', {
                        cid: response.data.collectionId,
                        cpid: response.data.collectionOptionId,
                        nocpid: response.data.diffCollectionOptionId,
                        type: that.type,
                        index: that.index,
                        code: that.code + ',' + result,
                        success: success,
                        index1: that.index1,
                        index2: that.index2,
                        index3: that.index3,
                        index4: that.index4,
                        index5: that.index5,
                        index6: that.index6,
                        index7: that.index7,
                        index8: that.index8,
                      });
                    }
                  });
                },
                error: function (res) {
                  if (res.errMsg.indexOf('function_not_exist') > 0) {
                    alert('版本过低请升级');
                  }
                },
              });
            });
          }
        });
    },
  },
};
</script>

<style scoped>
.cameraDiv1 {
  position: relative;
  height: 164px;
  width: 100%;
}

.cameraDiv1 img {
  position: absolute;
  width: 100%;
  display: block;
  height: 164px;
  top: 0;
}

.imgPre {
  height: 164px;
  width: 100%;
  background-color: white;
  border-radius: 6px;
  overflow: hidden;
}

.photos1 {
  margin: 70px auto;
  left: 50%;
  margin-left: -14px;
}

.photobrowsing {
  position: absolute;
  padding: 4px;
  right: 0;
  top: 0;
  z-index: 99;
  background-color: rgba(255, 255, 255, 0.8);
  border-bottom-left-radius: 3px;
  border-top-left-radius: 3px;
}

.coverImg {
  text-align: center;
  position: absolute;
  top: 0;
  left: 0;
  height: 164px;
  width: 100%;
}

.coverImg .ico {
  top: 42%;
}
</style>
