<template>
  <div>
    <van-field v-model="school" label="学校" readonly placeholder="学校" />
    <van-field v-model="name" label="学生姓名" readonly placeholder="姓名" />
    <van-field v-model="userNum" label="用户编号" readonly placeholder="用户编号" />
    <van-field v-model="codeNum" label="研学码" readonly placeholder="研学码">
      <van-button slot="button" size="small" native-type="button" @click="saoYiSao">扫一扫</van-button>
    </van-field>
    <van-button type="primary" block @click="getbind">提交</van-button>
  </div>
</template>

<script>
export default {
  data () {
    return {
      codeNum: '',
      userNum: '',
      school: '',
      name: ''
    }
  },
  created () {

    let studentInfo = JSON.parse(localStorage.getItem('StudentDetialInfo'));
    this.userNum = studentInfo.travelerNum
    this.school = studentInfo.schoolName
    this.name = studentInfo.travelerName
    this.codeNum = this.GetQueryString('qrresult') || studentInfo.studyCode;
  },
  methods: {
    getbind () {
      if (this.userNum == '' || this.codeNum == '') {
        return this.$toast('请先填写完整信息！');
      }
      this.yxAxios.get('https://proxy.shunzhi.net/prod/user/info/bingStudyCode?userNum=' + this.userNum + '&code=' + this.codeNum)
        .then((res) => {
          console.log('接口返回', res.data)
          if (res.data.code == '200') {
            this.$toast('操作成功');
            this.$router.push({ name: 'YanxueCode' })
          } else {
            this.$toast('操作失败:'+ res.data.message);
          }
        })
    },
    //扫一扫
    saoYiSao () {
      ZWJSBridge.scan({
        type: 'qrCode',
      })
        .then((res) => {
          if(res.qrcode&&!isNaN(Number(res.qrcode))){
            this.$router.push({ name: 'YanxueInfo', query: { qrresult: res.qrcode } })
          }else{
            this.$toast.fail('扫码失败')
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 获取链接参数
    GetQueryString (name) {
      var reg = new RegExp("\\b" + name + "=([^&]*)");
      var r = location.href.match(reg);
      if (r != null) return decodeURIComponent(r[1]);
    }
  }
}
</script>

<style>
</style>