<template>
  <div class="page-authentication">
    <!-- 自动登录页面 -->
  </div>
</template>

<script>
export default {
  name: 'authentication',
  created () {
        sessionStorage.setItem("token",this.getUrlKey("token"));
     console.log("location url "+location.href)
     console.log("token:"+this.getUrlKey("token"))
     var url=window.location.href;                    //获取当前页面的url
        if(url.indexOf("?")!=-1){                        //判断是否存在参数
            url = url.replace(/(\?|#)[^'"]*/, '');           //去除参数
            window.history.pushState({},0,url);
        }
    this.login();
  },
  methods: {
    login() {
  //     console.log(this.redirect)
  //  this.$router.replace({
  //       path: this.redirect,
  //     });
      this.$post('/login/index').then(this.loginSuccess).catch(this.loginFail)
    },
    loginSuccess({data}) {
      this.$store.commit('setUserData', data);
      this.$router.replace({
        path: this.redirect,
      });
    },
    loginFail(error) {
      this.$message.error(error.message || '请登录');
      this.$router.replace({
        path: '/login',
        query: {
          redirect: this.redirect,
        }
      });
    },

getUrlKey (name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null
}


  },
  computed: {
    isLogin() {
      return this.$store.state.isLogin
    }
  },
  data() {
    return {
      redirect: this.$route.query.redirect,
      loading: true,
    }
  }
}
</script>
