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

Shopware 6 iframe中如何获取用户配置文件时区(非浏览器时区)

获取Shopware后台用户个人资料时区(iframe扩展中)

目前Shopware Admin Extension SDK暂未提供直接获取用户个人资料时区的API,但可以通过以下可靠方法实现:

  • 调用Admin API获取当前用户详情
    用户设置的时区信息会存储在个人资料中,你可以通过/api/user/me接口获取当前登录用户的完整信息,其中包含timezone字段。在iframe扩展里,可借助SDK提供的sw.httpClient发起请求:
    // 示例:获取用户时区并格式化日期
    async function getUserTimezone() {
      try {
        const response = await sw.httpClient.get('/api/user/me');
        return response.data.timezone; // 返回用户设置的时区,如"Europe/Berlin"
      } catch (error) {
        console.error('获取用户时区失败:', error);
        return null;
      }
    }
    
    // 使用时区格式化日期
    const userTimezone = await getUserTimezone();
    if (userTimezone) {
      const formattedDateTime = new Intl.DateTimeFormat('zh-CN', {
        timeZone: userTimezone,
        year: 'numeric',
        month: 'long',
        day: 'numeric',
        hour: '2-digit',
        minute: '2-digit'
      }).format(new Date());
      console.log('按用户时区格式化的时间:', formattedDateTime);
    }
    
    注:后台用户默认拥有读取自身资料的API权限,无需额外配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 22:14:51