<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 { useDataStore } from "@/store/modules/data";
import { useUserStore } from "@/store/modules/user";
import { checkOpenId, findJsByPhoneApi } from "@/api/system/login";

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

function toHome(data: any) {
  if (data.type == 1) {
    uni.reLaunch({
      url: "/pages/base/groupTeaching/zhujiao",
    });
  } else {
    uni.switchTab({
      url: "/pages/base/message/index",
    });
  }
}
onLoad(async (data: any) => {
  if (data && data.openId) {
    setGlobal(data);
    checkOpenId({
      openId: data.openId,
      appCode: data.type == 1 ? "ZB" : "JS",
    })
      .then(async (res) => {
        if (res.resultCode == 1) {
          if (res.result) {
            afterLoginAction(res.result);
            if (data.type == 1) {
              toHome(data);
            } else {
              const findJsByPhoneResult = await findJsByPhoneApi({
                phone: res.result.telephone,
              });
              if (findJsByPhoneResult.resultCode == 1) {
                if (findJsByPhoneResult.result) {
                  if (findJsByPhoneResult.result["confirmStatus"] == "A") {
                    toHome(data);
                  } else {
                    setFile({
                      ...findJsByPhoneResult.result,
                      ...getFile,
                    });
                    setTimeout(() => {
                      uni.reLaunch({
                        url: "/pages/view/hr/teacherProfile/index",
                      });
                    }, 1500);
                  }
                } else {
                  toHome(data);
                }
              }
            }
          } else {
            uni.reLaunch({
              url: "/pages/system/login/login",
            });
          }
        }
      })
      .catch((err) => {});
  }
});
</script>
