【发布时间】:2016-08-27 12:11:30
【问题描述】:
我有一个服务,它给了我用户可以登录的毫秒数,SecurityServices.getUserLoginDuration(),这个持续时间显示在所有页面的顶部,所以我写了一个显示这个时间的指令。
<session-timeout value="60000"></session-timeout>
或
<session-timeout value="$root.timeoutValue"></session-timeout>
我使用这段代码在特定时间过后将用户重定向到登录页面:
$scope.timeout = $scope.timeoutValue;
$interval(decrementBySecond, 1000);
$timeout(emitSessionTimeout, $scope.timeoutValue);
function decrementBySecond() {
$scope.timeout = $scope.timeout - 1000;
}
function emitSessionTimeout() {
$rootScope.$emit('SESSION_TIMEOUT');
}
在我的应用程序的run 块中,我处理SESSION_TIMEOUT 事件,如下所示:
app.run(handleTimeOut);
function handleTimeout($rootScope, $state) {
$rootScope.$on('SESSION_TIMEOUT', function () {
$state.go('login');
});
}
应该在每次状态更改时刷新超时值,因此指令控制器中的$scope.timeout 应该初始化为其初始值。我怎样才能做到这一点?
【问题讨论】:
-
我认为这样的事情不应该在前端完成。在后端为您的会话设置过期日期,如果调用 API 时会话过期,则返回
401 UNAUTHORIZED状态。 -
@JahongirRahmonov 服务器端逻辑尚未开发,所以我需要一些方法来模拟或模仿这种行为,你所说的可以在拦截器的帮助下轻松完成,我知道这一点.. .
标签: angularjs session events angular-ui-router timeout