<template>
<div>
  <van-loading style="position: absolute;top: 10%;left: 50%;transform: translateX(-50%);" size="24px">请在微信端打开...</van-loading>
</div>
</template>

<script>
import {
  getWxApiToken,
  getUSerInfo,
  getCourseCodeDetail
} from "../api";
import { isWxEnv } from "../utils";
import {mapState,mapGetters} from 'vuex'
export default {
  name: '',
  components: {},
    props: [],
  data () {
    return {
      pid:null,
      code:null,
    }
  },
  created () { 
    console.log(this.$route.query)
    this.init()
  },
  computed:{
    ...mapGetters(['wxid'])
  },
  methods: { 
    // 根据wxid获取微信个人数据
    getUerInfo(wxid) {
      getWxApiToken(wxid).then(token => {
        getUSerInfo(token).then(res => {
          this.user_info = JSON.parse(localStorage.getItem("wx_user_info"));
          if (this.$route.query && this.$route.query.pid&&!this.user_info.marketable) {
            localStorage.setItem("pid", this.$route.query.pid);
            this.$router.replace(`/courseLogin?pid=${this.$route.query.pid}`);
          }else if(this.$route.query && this.$route.query.code){
            let codeArr =  this.$route.query.code.split('-')
            this.$router.replace(`/course/courseDeail/${codeArr[1]}?code=${codeArr[0]}`);
            // getCourseCodeDetail(this.$route.query.code).then(res=>{
            //   if(res != null){
            //     this.$router.replace(`/course/courseDeail/${res.subjectId}?code=${this.$route.query.code}`);
            //   }else{
            //     this.$toast.fail("课程码无效")
            //     this.$router.replace(`/courseHome`);
            //   }
            // })
          }else{
            this.$router.replace(`/courseHome`);
          }
        });
      });
    },
    // 微信环境判断
    wxAuth(type,data){
      isWxEnv(type,data).then(()=>{
        this.$router.replace(`/courseHome`);
      }).catch(()=>{
        this.getUerInfo(this.wxid)
      })
    },
    //初始化判断
    init(){
      if(this.$route.query && this.$route.query.pid){
        this.wxAuth('pid',this.$route.query.pid)
      }else if(this.$route.query && this.$route.query.code){
        this.wxAuth('code',this.$route.query.code)
      }else{
        this.wxAuth()
      }
    }
   },
}
</script>
<style scoped lang='scss'>

</style>
