<template>
  <view class="wh-full">
    <BasicLoading
      :isShow="isShow"
      bgColor="#fff"
      isShowTitle
      textColor="#000"
      title="启动中..."
      :type="1"
    />
  </view>
</template>

<script lang="ts" setup>
import { onLoad } from "@dcloudio/uni-app";
import pages from "@/pages.json";
import { useDataStore } from "@/store/modules/data";
import { useUserStore } from "@/store/modules/user";
import { checkOpenId, findJsByPhoneApi } from "@/api/system/login";
import { isTabBar } from "@/utils/uniapp";

const { setGlobal } = useDataStore();
const { afterLoginAction } = useUserStore();
const { setFile, getFile } = useDataStore();
const isShow = ref(true);

function toHome() {
  if (isTabBar()) {
    uni.switchTab({
      url: "/" + (pages as any).tabBar.list[0].pagePath,
    });
  } else {
    uni.reLaunch({
      url: "/" + pages.pages[1].path,
    });
  }
}
onLoad(async (data: any) => {
  data.openId = "123";
  if (data && data.openId) {
    setGlobal(data);
    checkOpenId({ openId: data.openId, appCode: "JS" })
      .then(async (res) => {
        if (res.resultCode == 1) {
          if (res.result) {
            afterLoginAction(res.result);
            const findJsByPhoneResult = await findJsByPhoneApi({
              phone: res.result.telephone,
            });
            if (findJsByPhoneResult.resultCode == 1) {
              if (findJsByPhoneResult.result) {
                if (findJsByPhoneResult.result["confirmStatus"] == "A") {
                  toHome();
                } else {
                  setFile({
                    ...findJsByPhoneResult.result,
                    ...getFile,
                  });
                  setTimeout(() => {
                    uni.reLaunch({
                      url: "/pages/view/hr/teacherProfile/index",
                    });
                  }, 1500);
                }
              } else {
                toHome();
              }
            }
          } else {
            uni.reLaunch({
              url: "/pages/system/login/login",
            });
          }
        }
      })
      .catch((err) => {});
  }
});
</script>
