<template>
  <div class="loginBox">
    <iframe src="//edu.cocorobo.cn/course/login?type=2" frameborder="0"></iframe>
    <!-- <button @click="handleLogin">登录</button> -->
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import Cookies from 'js-cookie'

export default {
  data() {
    return {
      redirect: undefined,
      timer: null
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    ...mapActions({
      login: 'user/login'
    }),
    handleLogin() {
      this.$router.push({ path: this.redirect || '/' })
    },
    async getLogin() {
      const userid = await this.login()
      let redirect_uri = Cookies.get('redirectUri')
      if (!redirect_uri || redirect_uri === 'undefined') {
        redirect_uri = ''
      }
      console.log(redirect_uri)
      if (userid) {
        if (redirect_uri) {
          // Cookies.remove('redirectUri')
          window.location.href = redirect_uri
        } else {
          this.$router.push({ path: this.redirect || '/' })
        }
      }
      // eduGet().then(res => {})
    }
  },
  beforeDestroy() {
    clearInterval(this.timer)
    this.timer = null
  },
  mounted() {
    Cookies.set('isWeChat', '1')
    this.getLogin()
    this.timer = setInterval(() => {
      this.getLogin()
    }, 2000)
  }
}
</script>

<style lang="scss" scoped>
.loginBox {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;

  > iframe {
    width: 100%;
    height: 100%;
    border: 0;
  }
}
</style>
