角度4中的会话超时问题

时间:2019-02-22 09:02:31

标签: javascript angular typescript

我为会话超时实现了此代码,例如,如果系统空闲,则系统应重定向到登录页面 但是当系统进入空闲状态时,它不会重定向到登录页面,而是卡在系统上,无法单击任何内容,当我刷新页面时,它将进入登录页面。

这是我相关的服务-

private interval: any;
private TIMEOUT_IN: number = 1000 * 60 * 1.5; // 1.5 minutes
private redirectUrl: string;

constructor(public router: Router) {}

refreshTimer() {
    if (this.interval) {
        clearInterval(this.interval);
    }

    let lastTimeoutRefreshTimestamp = Number(
        sessionStorage.getItem("lastTimeoutRefreshTimestamp")
    );

    this._validateLastRefreshTimestamp(lastTimeoutRefreshTimestamp);
    this._updateLastRefreshTimestamp(lastTimeoutRefreshTimestamp);

    this.interval = setInterval(() => {
        sessionStorage.clear();
        clearInterval(this.interval);
        this._toTimeoutRoute();
    }, this.TIMEOUT_IN);
}

setRedirectUrl(url: string) {
    this.redirectUrl = url;
}

/**
 * Validates the last saved session refresh timestamp
 */
private _validateLastRefreshTimestamp(lastTimeoutRefreshTimestamp) {
    let currentTimestamp = Date.now();
    if (
        lastTimeoutRefreshTimestamp !== 0 &&
        lastTimeoutRefreshTimestamp < currentTimestamp
    ) {
        sessionStorage.clear();
        this._toTimeoutRoute();
    }
}

/**
 * Updates the timeout refresh timestamp stored in session storage
 */
private _updateLastRefreshTimestamp(lastTimeoutRefreshTimestamp) {
    lastTimeoutRefreshTimestamp = Date.now() + this.TIMEOUT_IN;
    sessionStorage.setItem(
        "lastTimeoutRefreshTimestamp",
        String(lastTimeoutRefreshTimestamp)
    );
}

/**
 * Correctly redirect to timeout modal depending the base bath in     redirectUrl
 */
private _toTimeoutRoute() {
    if (this.redirectUrl.indexOf("portal") > -1) {
        this.router.navigate(["/timeout"]);
    } else if (this.redirectUrl.indexOf("admin-portal") > -1) {
        this.router.navigate(["/admin-portal/timeout"]);
    } else {
        console.log("Redirect url was not set. Something went wrong.");
        throw new Error("redirectUrl not set in session-timeout service");
    }
}

还有我的组成部分-

this.sessionTimeoutService.setRedirectUrl("/admin-portal/login");

this.mousemoveRemoveListener = this.renderer.listen(
    "window",
    "mousemove",
    () => this.sessionTimeoutService.refreshTimer()
);

this.mousedownRemoveListener = this.renderer.listen(
    "window",
    "mousedown",
    () => this.sessionTimeoutService.refreshTimer()
);

this.keypressRemoveListener = this.renderer.listen(
    "window",
    "keypress",
    () => this.sessionTimeoutService.refreshTimer()
);

this.mousewheelRemoveListener = this.renderer.listen(
    "window",
    "mousewheel",
    () => this.sessionTimeoutService.refreshTimer()
);

当清除会话时页面卡住,刷新时进入登录页面 并且错误是未设置重定向网址 有人能帮我吗?谢谢

0 个答案:

没有答案