likes
comments
collection
share

如何在 SAP Spartacus 中编写 ASM-Compatible 的代码

作者站长头像
站长
· 阅读数 276

要编写与 ASM 兼容的代码,开发人员需要使用 UserIdService 中的 takeUserId() 函数来确定 OCC 调用中使用的 userId。 这通常在一个服务中完成,该服务调度一个在有效负载中包含 userId 的 Action.

在 Spartacus 官方支持 ASM 场景之前,在代表经过身份验证的用户发送的请求中,OCC userId 是特殊的 current OCC 用户,由 OCC_USER_ID_CURRENT 常量表示。 这可以在以下示例中看到:

  /**
   * Retrieves user's addresses
   */
  loadAddresses(): void {
    this.store.dispatch(new UserActions.LoadUserAddresses(OCC_USER_ID_CURRENT));
  }

如何在 SAP Spartacus 中编写 ASM-Compatible 的代码

借助 Spartacus 中的官方 ASM 支持,确定 OCC userId 的正确方法是调用 UserIdService.takeUserId()。 使用前面的示例作为起点,Spartacus 现在确定 OCC 用户 ID,如下所示:

  /**
   * Retrieves user's addresses
   */
  loadAddresses(): void {
    this.userIdService.takeUserId().subscribe((userId) => {
      this.store.dispatch(new UserActions.LoadUserAddresses(userId));
    });
  }

小窍门:如果 OCC_USER_ID_CURRENT 直接在服务中使用,这意味着它可能会被对 takeUserId() 的调用所取代。

为了支持 Spartacus 中的 ASM 以及未来可能的其他功能,facade service 在调用各种操作时不能简单地使用这个 current 的特殊 userId. 需要应用一些逻辑来确定正确的 OCC userId 以传递给触发后端调用的操作。 因此,确定正确 OCC userId 的逻辑集中在 UserIdService 的 takeUserId() 函数中。

当客户支持代理登录时,ASM UI 中会出现一个会话超时计时器。 默认值为 600 秒(10 分钟),但可以更改会话超时持续时间,如下例所示:

provideConfig({
  asm: {
    agentSessionTimer: {
      startingDelayInSeconds: 720,
    },
  },
});

在此示例中,会话计时器的持续时间已设置为 720 秒(12 分钟)。

ASM客户搜索中的结果数量可以自定义,如下例所示:

provideConfig({
  asm: {
    customerSearch: {
      maxResults: 20,
    },
  },
});

在 Spartacus UI 启动 ASM 模式的方法:

要在 Spartacus 店面中调用 ASM UI,请将 ?asm=true 后缀添加到 URL。 例如,对于 electronics 示例商店,可以使用以下 URL 在主页上调用 ASM UI:

https://{hostname}/electronics-spa/en/USD/?asm=true

目前 Spartacus 暂时不支持对 ASM 进行扩展。

ASM Customer Emulation 不适用于 Spartacus 中的 CMS 内容规则和限制。 如果存在通常基于客户 ID 或客户组 ID 应用的内容规则或限制,则在 ASM Customer Emulation 期间不会应用这些规则和限制。 相反,CMS endpoint 根据允许客户支持代理查看的内容提供内容。

为了显示 CMS 内容,Spartacus 依赖于 OCC 的 CMS endpoint. 发送请求时,CMS 端点不接受可以定义模拟用户(即客户)的 userId 参数。 CMS 端点仅将经过身份验证的用户识别为请求的发送者,并且在 ASM Customer Emulation 会话中,经过身份验证的用户是客户支持代理(Customer Support Agent)。

OCC CMS 端点不接受 userId 参数,因此模拟客户不可能在 ASM 模拟会话期间触发 CMS 规则和限制(rules and restrictions).