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

<script setup>
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
import { SET_TOKEN,SET_USER_ROLES,SET_USER_CUR_ROLE } from "@/store/modules/app/type";
import { onMounted } from "vue";

const router = useRouter();
const route = useRoute();
const store = useStore();

let userId = route.query.userId;

onMounted(async () => {
    const token = route.query.token
    const targetUrl = route.query.targetUrl ? route.query.targetUrl : '/home';
    if (!token) {
        const { data } = await VE_API.system.devLogin({ userId: userId });
        store.dispatch(`app/${SET_TOKEN}`, data.token);
        store.dispatch(`app/${SET_USER_ROLES}`, data.roles);
        store.dispatch(`app/${SET_USER_CUR_ROLE}`, data.roles[0] || 0);
        localStorage.setItem("localUserInfo", JSON.stringify(data));
    }
    // 存userId
    let pointXy = route.query.point.split(",")
    // 刷新后仍保留id和point
    localStorage.setItem("MINI_USER_ID", userId)
    if(route.query.roles){
        store.dispatch(`app/${SET_USER_ROLES}`,JSON.parse(route.query.roles));
        store.dispatch(`app/${SET_USER_CUR_ROLE}`, JSON.parse(route.query.roles)[0] || 0);
    }
    localStorage.setItem("MINI_USER_LOCATION", route.query.point)
    localStorage.setItem("MINI_USER_LOCATION_POINT", `POINT(${pointXy[0]} ${pointXy[1]})`)
    store.commit("home/SET_MINI_USER_LOCATION", route.query.point);
    store.commit("home/SET_MINI_USER_ID", userId);
    store.commit("home/SET_MINI_USER_LOCATION_POINT", `POINT(${pointXy[0]} ${pointXy[1]})`);
    router.push(`${targetUrl}?miniJson=${JSON.stringify(route.query)}`);
})
</script>

<style scoped></style>
