<script setup>
import { apiSystemCommon, apiSystemRole } from '@/api/system'
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from 'element-plus'


const route = useRoute()
const router = useRouter()

// 获取菜单跳转到第一个菜单
function getMenuList() {
  apiSystemRole.menuData().then(res => {
    if (res.Ret === 200) {
      const arr = res.Data || []
      if (arr.length === 0) {
        ElMessage.warning('您暂无任何菜单权限请联系管理员')
        return
      }
      let path = ''
      if (arr[0]?.Children[0]) {
        path = arr[0].Children[0].Path
      } else {
        path = arr[0].Path
      }
      router.replace(path)
    }
  })
}

async function init() {
  if (!route.query.code) {
    router.replace('/login')
    return
  }
  const t = new Date().getTime()
  const res = await apiSystemCommon.login({
    LoginType: 'sso',
    Code: route.query.code,
    ReqTime: `${t}`,
  })
  if (res.Ret === 200) {
    localStorage.setItem('token', res.Data.Authorization)
    localStorage.setItem('userName', res.Data.SysRealName)
    getMenuList()
  }
}
init()



</script>

<template>
  <div
    class="auto-login-page"
    v-loading="true"
    element-loading-text="登录中..."
  ></div>
</template>

<style lang="scss" scoped>
.auto-login-page {
  width: 100%;
  height: 100%;
}
</style>