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

<script>
import { onMounted } from "vue";
import { showFailToast, showSuccessToast, Dialog, Button } from 'vant';
import { useRoute, useRouter } from "vue-router";
import { useI18n } from "vue-i18n";
import { setLocal } from "../common/js/utils";
import { getUserDetailByWxCode } from "@/service/login";

/**
 * 微信登录跳板
 */
export default {
  setup() {
    const { t } = useI18n();
    const router = useRouter();
    const route = useRoute();
    const code = route.query.code;
    // 页面初始化
    onMounted(() => {
      console.log('获取页面参数code：', code);
      if (code) {
        loginByCode(code);
      }
    });
    // 登录
    const loginByCode = async (code) => {
      const { data } = await getUserDetailByWxCode(code);
      if (data.code === "00000") {
        setLocal("loginUser", JSON.stringify(data.data));
        console.log('loginUser JSON：', JSON.stringify(data.data));
        showSuccessToast(t('login.loginSucess'));
        setTimeout(() => {
          router.push("/home");
        }, 200);
      } else {
        showFailToast(data.message);
      }
    };

  },
  components: {
    [Button.name]: Button,
    [Dialog.name]: Dialog
  },
};
</script>


