<template>
  <el-config-provider :locale="currentLocale">
    <router-view />
  </el-config-provider>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import { ElConfigProvider } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";

export default defineComponent({
  name: "app",
  components: {
    [ElConfigProvider.name]: ElConfigProvider
  },
  computed: {
    currentLocale() {
      return zhCn;
    }
  }
});
</script>

<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { loadEnv } from "@build/index";
import * as dd from "dingtalk-jsapi";
const { VITE_CORP_ID } = loadEnv();
import { JudgeEnvironment } from "/@/utils/validate";
import { useUserStoreHook } from "/@/store/modules/user";
import { storageSession } from "@pureadmin/utils";
// import { getToken, removeToken } from "/@/utils/auth";
// if (getToken() ?? "") {
//   removeToken();
//   router.replace({ path: "/login" });
// }
const code = ref("");
function testing() {
  dd?.ready(() => {
    //使用SDK 获取免登授权码
    dd?.runtime?.permission?.requestAuthCode({
      corpId: VITE_CORP_ID,
      onSuccess: info => {
        // alert(JSON.stringify(info));
        code.value = info?.code ?? "";
        // 根据钉钉提供的api 获得code后,再次调用这个callback方法
        // 由于是钉钉获取code是异步操作,不知道什么时候执行完毕
        // callback 函数会等他执行完毕后在自己调用自己
        // alert(code.value);
        setcode(code.value);
      },
      onFail: err => {
        alert("fail");
        alert(JSON.stringify(err));
      }
    });
  });
}
function setcode(code) {
  const model = { code };
  useUserStoreHook()
    .dingUserInfor(model)
    .then(res => {
      if (res !== "error") {
        const { data } = res;
        // alert(JSON.stringify(data));
        const { nickname, token } = data ?? {};
        // alert(JSON.stringify(token));
        storageSession.setItem("info", {
          username: nickname ?? "",
          accessToken: token ?? ""
        });
      }
      console.log(res);
    })
    .catch(() => {});
}
onMounted(() => {
  if (JudgeEnvironment() === "isDingDing") {
    testing();
  }
});
</script>
