<template>
  <div class="wx-login"></div>
</template>
<script name="WxLogin" setup>
import { useRoute, useRouter } from "vue-router";
import { useUserStore } from "@/store";
import { wxLogin, fetchOpenId } from "@/api/user";
const userStore = useUserStore();
const route = useRoute();
const router = useRouter();
const code = route.query.code;
async function _wxLogin() {
  showLoadingToast({
    forbidClick: true,
    loadingType: "spinner",
    duration: 0,
  });
  const openId = await fetchOpenId(code).catch(() => {});
  if (openId) {
    userStore.setOpenId(openId);
    let res = await wxLogin(openId);
    closeToast();
    if (res?.id) {
      userStore.setLoginInfo(res);
      router.replace("/index");
    } else {
      router.replace("/login");
    }
  } else {
    closeToast();
    router.replace("/login");
  }
}
code && _wxLogin();
</script>
<style lang="less" scoped></style>
