<template>
  <div class="login-authen">
    <h1 class="color-white text-center">
      <i class="el-icon-loading"></i> 正在登录...
    </h1>
  </div>
</template>

<script>
import { loginAuthen, loginWdAuthen } from "../api";

export default {
  name: "login-authen",
  data() {
    return {
      code: this.$route.params.code,
      appSource: this.$route.params.appSource
    };
  },
  mounted() {
    this.login();
  },
  methods: {
    async login() {
      if (!this.code) {
        this.$notify.error({
          title: "非法操作",
          message: "code丢失"
        });
        return;
      }
      const funcs = {
        WHU_THIRD: loginWdAuthen,
        PRINT_THIRD: loginAuthen
      };
      const func = funcs[this.appSource] || loginAuthen;
      const data = await func({
        code: this.code,
        appSource: this.appSource
      });
      if (!data) return;

      if (data.orgInfo) this.$ls.set("orgId", data.orgInfo.id);
      if (data.schoolInfo) this.$ls.set("schoolId", data.schoolInfo.id);
      this.$ls.set("user", data);

      // 强制修改密码
      // if (!data.user.pwdUpdateTime) {
      //   this.$refs.ResetPwd.open();
      //   return;
      // }

      this.$ls.set("token", data.accessToken);

      this.$router.replace({
        name: "Home"
      });
    }
  }
};
</script>
