<template>
    <div>
        <div class="center_loading" v-loading:body="loading"></div>
    </div>
</template>

<script setup>
import { onMounted } from 'vue'
const { VITE_EDGE_AUTH_URL, VITE_EDGE_CLIENT_ID, VITE_EDGE_REDIRECT_URL } = import.meta.env
import { useUserStore } from '@/store/user.js'
const loading = ref(true)
onMounted(() => {
    let geturl = window.location.href
    console.log(geturl)
    let getqyinfo = geturl.split('?')[1] //  截取到参数部分
    let getqys = new URLSearchParams('?' + getqyinfo) //将参数放在URLSearchParams函数中
    let code = getqys.get('code')
    let redirect_url = getqys.get('redirect_url')
    if (redirect_url) {
        localStorage.setItem('edgeRedirect', redirect_url)
    }
    if (code) {
        goLoginEplat(code)
    } else {
        let url = 
            VITE_EDGE_AUTH_URL +
            '?response_type=code&client_id=' +
            VITE_EDGE_CLIENT_ID +
            '&redirect_uri=' +
            VITE_EDGE_REDIRECT_URL +
            '&scope=all'
        console.log(url)
        window.location.href = url
    }
})
const router = useRouter()
function goLoginEplat(code) {
    console.log(code)
    useUserStore().edgeLogin(code).then(() => {
        const redirect_url = localStorage.getItem('edgeRedirect')
        if (redirect_url) {
            router.push(redirect_url)
            localStorage.removeItem('edgeRedirect')
        } else {
            router.push('/middleBackstage')
        }
    }).catch(err => {})
}
</script>

<style lang='scss' scoped>
.center_loading {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 100%; /* 调整宽度 */
  height: 100px; /* 调整高度 */
  //   background-color: #ccc; /* 背景颜色 */
  //   border: 1px solid #000; /* 边框 */
}
</style>