Shopware 6 iframe中如何获取用户配置文件时区(非浏览器时区)
获取Shopware后台用户个人资料时区(iframe扩展中)
目前Shopware Admin Extension SDK暂未提供直接获取用户个人资料时区的API,但可以通过以下可靠方法实现:
- 调用Admin API获取当前用户详情
用户设置的时区信息会存储在个人资料中,你可以通过/api/user/me接口获取当前登录用户的完整信息,其中包含timezone字段。在iframe扩展里,可借助SDK提供的sw.httpClient发起请求:
注:后台用户默认拥有读取自身资料的API权限,无需额外配置。// 示例:获取用户时区并格式化日期 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); }
内容的提问来源于stack exchange,提问作者Cuong Huynh
相关产品推荐
相关产品推荐

