<template>
  <div id="app" v-cloak>
    <router-view />
  </div>
</template>

<script>
import { JudgeEnvironment } from "@/utils/validate";
import { getCode } from "@/utils/dingding";
export default {
  name: "App",
  data() {
    return {
      code: "",
    };
  },
  async created() {
    if (JudgeEnvironment() === "isDingDing") {
      await this.testing();
    } else {
      await this.getMenu();
    }
  },
  methods: {
    async testing() {
      await getCode(async (code) => {
        this.code = code;
        if (this.code != null && this.code != "") {
          await this.setcode(this.code);
        } else {
          await this.logout();
        }
      });
    },
    async setcode(code) {
      let model = { code: code };
      this.$store
        .dispatch("user/dingUserInfor", model)
        .then(async (res) => {
          if (res === "success") {
            await this.getMenu();
          } else {
            await this.logout();
          }
        })
        .catch(async (err) => {
          await this.logout();
        });
    },
    async getMenu() {
      this.$store
        .dispatch("user/getMenuList", this)
        .then(async (res) => {
          if (res === "noToken") {
            await this.logout();
          } else if (res === "success") {
            if (
              this.$route.path === "/loadingPage" ||
              this.$route.path === "/" ||
              this.$route.path === "/login"
            ) {
              window.vm.$router.replace("/welcome");
            }
          } else {
            await this.logout();
          }
        })
        .catch(async (err) => {
          await this.logout();
        });
    },
    async logout() {
      if (this.$route.path !== "/login") {
        await this.$store.dispatch("user/logout");
        this.$router.push(`/login`);
      }
    },
  },
};
</script>
<style lang="scss">
@import "./assets/css/index.scss";
</style>
