import Vue from 'vue'
import store from '@/store'
import router from '@/router'
import { welcome } from '@/utils/util'
import { ACCESS_TOKEN, USER_INFO, USER_NAME } from '@/store/mutation-types'

/**
 * AutoAuthLogin - 子应用自动授权登录
 * @author Matrix
 * @since 2022/05/27
 */
function autoAuthLogin() {
  const token = JSON.parse(localStorage.getItem(process.env.VUE_APP_ADMIN_TOKEN))
  const user = JSON.parse(localStorage.getItem(process.env.VUE_APP_ADMIN_USER))
  Vue.ls.set(ACCESS_TOKEN, token, 7 * 24 * 60 * 60 * 1000)
  Vue.ls.set(USER_NAME, user.username, 7 * 24 * 60 * 60 * 1000)
  Vue.ls.set(USER_INFO, user, 7 * 24 * 60 * 60 * 1000)
  store.commit('SET_TOKEN', token)
  store.commit('SET_INFO', user)
  store.commit('SET_NAME', { username: user.username, realname: user.realname, welcome: welcome() })
  store.commit('SET_AVATAR', user.avatar)
  router.push({ path: '/dashboard/analysis' })
}

export default autoAuthLogin
