<script setup lang="ts">
import { onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useUserStoreHook } from "/@/store/modules/user";
import { loadEnv } from "@build/index";
import CryptoJS from "crypto-js";

const { loginByToken } = useUserStoreHook();
const { query } = useRoute();
const { push } = useRouter();

const { VITE_SECRET_KEY } = loadEnv();

onMounted(async () => {
  try {
    const decData = CryptoJS.enc.Base64.parse(query.t).toString(
      CryptoJS.enc.Utf8
    );

    const decJson = CryptoJS.AES.decrypt(decData, VITE_SECRET_KEY).toString(
      CryptoJS.enc.Utf8
    );
    const { token } = JSON.parse(decJson);
    await loginByToken(token);
    push("/welcome");
  } catch (e) {
    push("/login");
  }
});
</script>

<template>
  <div
    class="w-[100vw] h-[100vh]"
    element-loading-text="登录中"
    v-loading="true"
  />
</template>
