<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
import { queryInfo, getKeyInfo } from '@/api/user.js'
export default {
  name: 'App',
  data() {
    return {
    }
  },
  created() {
    this.getQueryInfo()
    this.getKeyInfo()
    var token = this.getUrlKey('token')
    console.log(token, '----------->')
    if (token) {
      this.$store.dispatch('user/setUrlToken', token).then(() => {
        this.$router.push('/')
      })
    } else {
      this.$router.push('/login')
      return
    }
  },
  methods: {
    getKeyInfo() {
      getKeyInfo({}).then(res => {
        const reslut = {
          rsaPublicKey: res.rsaPublicKey || '', // RSA公钥（2048位）
          aesKey: res.aesKey || '' // AES密钥（256位）
        }
        this.$store.commit('user/SET_KEY', reslut)
      }).catch(() => {
        const reslut = {
          rsaPublicKey: '', // RSA公钥（2048位）
          aesKey: '' // AES密钥（256位）
        }
        this.$store.commit('user/SET_KEY', reslut)
      })
    },
    getQueryInfo() {
      queryInfo({}).then(res => {
        this.$store.commit('user/SET_INITSTATE', res.userHasBeenInit || true)
      }).catch(() => {
        this.$store.commit('user/SET_INITSTATE', true)
      })
    },
    getUrlKey(name) {
      return (
        decodeURIComponent(
          (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(
            decodeURIComponent(location.href)
            // eslint-disable-next-line no-sparse-arrays
          ) || [, ''])[1].replace(/\+/g, '%20')
        ) || null
      )
    }
  }
}
</script>
<style>
body{
  font-family: "Adobe-MyriadPro-Regular";
}
</style>
