<template>
    <!-- 宝信单点登录 -->
    <div>
        <div class="center_loading" v-loading:body="loading"></div>
    </div>
</template>

<script setup>
import { onMounted } from 'vue'
const { VITE_BX_AUTH_URL, VITE_BX_CLIENT_ID, VITE_BX_REDIRECT_URL } =import.meta.env
import { useUserStore } from '@/store/user.js'
const loading = ref(true)
const router = useRouter()
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_BX_AUTH_URL +
        '?response_type=code&client_id=' +
        VITE_BX_CLIENT_ID +
        '&redirect_uri=' +
        VITE_BX_REDIRECT_URL +
        '&scope=write'
        console.log(url)
        window.location.href = url
    }
})
function goLoginEplat(code) {
    console.log(code)
    useUserStore().bxLogin(code).then(() => {
        const redirect_url = localStorage.getItem('edgeRedirect')
        if (redirect_url) {
            router.push(redirect_url)
            localStorage.removeItem('edgeRedirect')
        } else {
            router.push('/')
        }
    }).catch(err => {
        router.push('/noPermission')
        //  console.log("登录成功")
    })
}
</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>