<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/dist/locale/zh-cn.mjs'
export default defineComponent({
  name: "app",
  components: {
    [ElConfigProvider.name]: ElConfigProvider
  },
  computed: {
    currentLocale() {
      return zhCn;
    }
  }
});
</script>

<script lang="ts" setup>
import { useUserStoreHook } from "/@/store/modules/user";
import { JudgeEnvironment } from "/@/utils/validate";
import { storageSession } from "@pureadmin/utils";
import { loadEnv } from "@build/index";
import { ref, onMounted } from "vue";
import * as dd from "dingtalk-jsapi";
// import { getToken, removeToken } from "/@/utils/auth";
// if (getToken() ?? "") {
//   removeToken();
//   router.replace({ path: "/login" });
// }
const { VITE_CORP_ID } = loadEnv();
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;
        const { nickname, token } = data ?? {};
        storageSession.setItem("info", {
          username: nickname ?? "",
          accessToken: token ?? ""
        });
      }
      console.log(res);
    })
    .catch(() => {});
}
onMounted(() => {
  //钉钉登录
  if (JudgeEnvironment() === "isDingDing") {
    testing();
  }
});
</script>
