<template>
  <div class="login-open">正在登录……</div>
</template>

<script>
/**
 * returnUrl
 * 所有需要重新登录的地方都要使用returnUrl。包括：
 * axios
 * router
 * home:logout
 */

import { QUESTION_API } from "@/constants/constants";
import { USER_SIGNIN } from "../store/user";
import { mapActions } from "vuex";

export default {
  name: "LoginOpen",
  data() {
    return {};
  },
  created() {
    this.autoLogin();
  },
  methods: {
    ...mapActions([USER_SIGNIN]),
    async autoLogin() {
      const query = this.$route.query;
      let data = {};
      Object.keys(query).forEach((key) => {
        data[key] = decodeURIComponent(query[key]);
      });

      if (data["returnUrl"]) {
        window.sessionStorage.setItem("returnUrl", data["returnUrl"]);
      }

      const url = QUESTION_API + "/sso/login";

      data.path = "/admin/login-open";
      const res = await this.$httpWithMsg.post(url, data).catch(() => {});

      if (!res) {
        // window.history.go(-1);
        return;
      }

      this.USER_SIGNIN(res.data);
      this.$router.replace({ path: "/questions/tips" });
      this.$notify({
        message: "登录成功",
        type: "success",
        duration: 1000,
      });
    },
  },
};
</script>
