import { MessageBox } from "element-ui";
import store from "@/store";
import router from "@/router";

var time = 30 * 60 * 1000 * 4;
var timeOut = null;
var isLoginOutStatus = false;

// 检测请求,若有请求,则开始重新计时
export function waitLogOut() {
  if (timeOut != null) {
    clearTimeout(timeOut);
    timeOut = null;
  }
  if (isLoginOutStatus === false) {
    timeOut = window.setTimeout(() => {
      MessageBox.confirm("您已长时间无操作,请重新登录!", "提示", {
        confirmButtonText: "确定",
        showCancelButton: false,
        type: "warning",
        center: true,
      })
        .then(async () => {
          await store.dispatch("user/logout");
          router.push(`/login`);
        })
        .catch(async () => {
          console.log(888);
          await store.dispatch("user/logout");
          router.push(`/login`);
        });
    }, time);
  }
}

// 退出登录时,不在设定定时器,防止logout请求进入死循环
export function isLogOut() {
  isLoginOutStatus = true;
}

// 登录时,开始检测请求,允许设置定时器
export function notLogOut() {
  isLoginOutStatus = false;
}
