You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spartacus中ASM闲置10分钟自动登出后无法跳转/login页如何处理

解决方案

你当前遇到的问题本质是闲置超时触发的是被动登出,OOTB的ASM登出重定向逻辑仅覆盖主动点击登出按钮的交互场景,且依赖登出前存在的模拟用户token判断,而被动登出时token已被提前清空,因此调用isCustomerEmulationSessionInProgress()无法拿到正确状态。

Spartacus 规范实现方案

Spartacus框架推荐用事件驱动的方式扩展这类全局会话逻辑,具体实现步骤如下:

  • 提前存储坐席会话标识:不要在登出阶段再查询ASM会话状态,而是在坐席登录成功时就将isAgentSession标记存储在服务内存或localStorage中
  • 订阅全局登出事件:Spartacus的AuthLogoutEvent会在所有登出场景下触发(包含主动登出、超时被动登出、token失效登出等),直接订阅该事件即可覆盖你的闲置超时场景
  • 匹配场景执行重定向:收到AuthLogoutEvent后如果判断当前是坐席会话,直接调用RoutingService跳转到/login页面即可

参考代码实现

import { Injectable, OnDestroy } from '@angular/core';
import { EventService, RoutingService, AuthLogoutEvent } from '@spartacus/core';
import { AsmAuthService } from '@spartacus/asm';
import { Subscription } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class CustomAgentSessionHandler implements OnDestroy {
  private subscriptions = new Subscription();
  // 提前存储坐席会话标记,避免登出后无法查询token状态
  private isAgentSessionActive = false;

  constructor(
    private eventService: EventService,
    private routingService: RoutingService,
    private asmAuthService: AsmAuthService
  ) {
    // 监听坐席登录成功事件,更新会话标记
    this.subscriptions.add(
      this.asmAuthService.agentLoginSuccess().subscribe(() => {
        this.isAgentSessionActive = true;
      })
    );

    // 监听全局登出事件,统一处理重定向逻辑
    this.subscriptions.add(
      this.eventService.get(AuthLogoutEvent).subscribe(() => {
        if (this.isAgentSessionActive) {
          this.isAgentSessionActive = false;
          this.routingService.go('/login');
        }
      })
    );
  }

  ngOnDestroy(): void {
    this.subscriptions.unsubscribe();
  }
}

该方案完全符合Spartacus的扩展规范,无需修改OOTB原生代码,同时能覆盖所有登出场景下的重定向需求,不会和现有ASM逻辑冲突。

内容的提问来源于stack exchange,提问作者NAR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 03:36:03