<template>
    <div></div>
</template>

<script setup>
    import { onMounted } from 'vue'
    import { useRouter,useRoute } from 'vue-router'
    import { setToken } from '@/utils'
    import { login } from '@/api/user'
    import { useUserStore } from '@/store'
    import { ElMessage } from 'element-plus'

    const { query } = useRoute();
    const router = useRouter()
    const user_store = useUserStore()

    onMounted(async () => {
        const openid = query.openid ?? ''
        if(openid) {
            const {data: res} = await login({openid: openid,type: 'wechat'})
            if(res.error == 0) {
                setToken(res.data.token)
                user_store.setInfo(res.data.user)
                ElMessage.success('登录成功')
                router.push('/home')
            } else {
                ElMessage.error(res.msg)
                user_store.logout()
                removeToken()
                router.push('/login')
            }
        } else {
            ElMessage.error('无效的微信用户！')
            user_store.logout()
            removeToken()
            router.push('/login')
        }
    })
</script>

<style lang="scss" scoped>
</style>