<template>
  <!-- Login 代理页面 -->
  <div>
    <h1>Login 代理页面</h1>
  </div>
</template>
<script>

export default {
  mounted () {
    this.crossBefore()
  },
  methods: {
    async crossBefore () {
      try {
        let { data } = await this.$http.get('/login/crossBefore')
        const params = data || {}
        this.proxyLogin(params.askUrl, params.askData, params.loginUrl)
      } catch (err) {
        this.$router.replace('/')
        console.log(err)
      }
    },
    proxyLogin (askUrl, askData, loginUrl) {
      const crossAfter = this.crossAfter
      this.$jsonp(`${askUrl}?askData=${askData}`, function (err, data) {
        if (err) throw err

        // 判断是否已经登录
        if (data.error) {
          // 如果未登录，跳转登录界面
          window.location.href = loginUrl
        } else {
          crossAfter(data.content)
        }
      })
    },
    async crossAfter (content) {
      if (!content && content === '') {
        this.$router.replace('/')
      }

      try {
        let { data } = await this.$http.post('/login/crossAfter', {}, { params: { replyTxt: content } })

        this.$store.dispatch('loadUserInfo', data)

        window.location.href = data.returnUrl || '/'
      } catch (err) {
        this.$router.replace('/')
        console.log(err)
      }
    }
  }
}
</script>
