<template>
  <!-- 柯桥研学研学授权 -->
  <div>
    <p class="authorize">授权中...</p>
  </div>
</template>
<script>
export default {
  data () {
    return {
      appId: 'wx1305e88d2bc74073'
    }
  },
  mounted () {
    let code = this.$route.query.code;
    let openId = sessionStorage.getItem('openIdKQ');
    if (!openId) {
      if (!code) {
        location.href = `${this.proxyUrl}/prod/api/wx/${this.appId}/getCode/kq`;
      } else {
        // 通过code获取openId和token
        this.$toast.loading({
          message: '加载中...',
          duration: 0,
          forbidClick: true
        })
        this.yxAxios.get(`${this.proxyUrl}/prod/api/wx/${this.appId}/getMsgByCode?appId=${this.appId}&code=${code}`).then((res) => {
          this.$toast.clear()
          if (res.data.code == 200) {
            sessionStorage.setItem('openIdKQ', res.data.data.openid)
            sessionStorage.setItem('unionId', res.data.data.unionId)
            sessionStorage.setItem('nicknameUser', res.data.data.nickname)
            sessionStorage.setItem('headImgUrl', res.data.data.headImgUrl)
            this.backPage()
          } else {
            sessionStorage.removeItem('openIdKQ')
            location.href = `${this.proxyUrl}/prod/api/wx/${this.appId}/getCode/kq`;
          }
        })
      }
    } else {
      this.backPage()
    }
  },
  methods: {
    backPage () {
      let prePage = sessionStorage.getItem('prePage');
      let prePageQuery = sessionStorage.getItem('prePageQuery');
      if (prePage) {
        this.$router.push({ name: prePage, query: JSON.parse(prePageQuery) })
      }

    },
  }
}
</script>
<style lang="scss">
.authorize {
  font-size: 32px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>