修复时间:2025年11月2日
状态:✅ 已采用组长端同样方案
设计师端导航条只显示 userid,没有显示用户名和头像,而组长端已经正常工作。
设计师端使用了过于复杂的多层降级方案,但组长端使用的是 WxworkAuth.currentProfile() 方法,直接获取当前用户的 Profile 信息。
参考组长端的实现(src/app/pages/team-leader/dashboard/dashboard.ts),使用 WxworkAuth.currentProfile() 方法。
async ngOnInit(): Promise<void> {
this.route.paramMap.subscribe(async params => {
this.cid = params.get('cid') || localStorage.getItem('company') || '';
// 初始化企微认证
this.initAuth();
// ✅ 新增:加载用户Profile信息(参考组长端)
await this.loadUserProfile();
// 执行认证并加载数据
await this.authenticateAndLoadData();
});
}
async loadUserProfile(): Promise<void> {
try {
const cid = this.cid || localStorage.getItem("company");
if (!cid) {
console.warn('⚠️ 未找到公司ID,使用默认用户信息');
return;
}
console.log('🔄 开始加载用户Profile...');
const wwAuth = new WxworkAuth({ cid, appId: 'crm' });
const profile = await wwAuth.currentProfile();
if (profile) {
const name = profile.get("name") || profile.get("realname") || profile.get("mobile") || '设计师';
const avatar = profile.get("avatar");
const roleName = profile.get("roleName") || '组员';
this.displayUser = {
userid: profile.get('userid') || '',
name,
avatar: avatar || this.generateDefaultAvatar(name),
roleName,
department: profile.get('department') || profile.get('departmentName'),
position: profile.get('position')
};
console.log('✅ 用户Profile加载成功:', this.displayUser);
}
} catch (error) {
console.error('❌ 加载用户Profile失败:', error);
}
}
generateDefaultAvatar(name: string): string {
const initial = name ? name.substring(0, 2) : '设';
const bgColor = '#667eea'; // 紫色,与导航条主题一致
const textColor = '#ffffff';
const svg = `<svg width='40' height='40' xmlns='http://www.w3.org/2000/svg'>
<rect width='100%' height='100%' fill='${bgColor}'/>
<text x='50%' y='50%' font-family='Arial' font-size='16' font-weight='bold' text-anchor='middle' fill='${textColor}' dy='0.3em'>${initial}</text>
</svg>`;
return `data:image/svg+xml,${encodeURIComponent(svg)}`;
}
const wwAuth = new WxworkAuth({ cid, appId: 'crm' });
const profile = await wwAuth.currentProfile(); // ✅ 直接获取当前用户Profile
不再使用:
wwAuth.authenticateAndLogin() - 过于复杂const name = profile.get("name") // 优先
|| profile.get("realname") // 其次
|| profile.get("mobile") // 再次
|| '设计师'; // 默认
使用 SVG 格式,显示用户名前2个字:
#667eea(紫色,与导航条一致)#ffffff(白色)16px按 Ctrl+Shift+R (Windows) 或 Cmd+Shift+R (Mac)
🔄 开始加载用户Profile...
✅ 用户Profile加载成功: {
userid: 'woAs2qCQAAPjkaSBZg3GVdXjlG3vxAOg',
name: '王刚', // ← 应该显示真实姓名
avatar: 'data:image/svg+xml,...',
roleName: '组员'
}
✅ 预期效果:
┌──────────────────────────────────────────────────────┐
│ 设计师工作台 2025年11月2日星期六 [头像] 王刚 [组员] │
└──────────────────────────────────────────────────────┘
| 项目 | 修复前 | 修复后 |
|---|---|---|
| 用户名 | ❌ 显示 userid | ✅ 显示真实姓名 |
| 头像 | ❌ 不显示 | ✅ 显示默认头像(紫色圆形) |
| 实现方式 | ❌ 复杂的多层降级 | ✅ 简单的 currentProfile() |
| 代码行数 | ⚠️ ~150 行 | ✅ ~50 行 |
| 可维护性 | ⚠️ 复杂难懂 | ✅ 清晰简单 |
┌──────────────────────────────────────────────────────┐
│ 设计师工作台 2025年11月2日星期六 [王刚] 王刚 [组员] │
└──────────────────────────────────────────────────────┘
↑ 紫色圆形头像,显示"王刚"
┌─────────────────────────────────────────────────────────┐
│ 设计师工作台 2025年11月2日星期六 [13] 13812345678 [组员] │
└─────────────────────────────────────────────────────────┘
↑ 紫色圆形头像,显示"13"
┌──────────────────────────────────────────────────────────┐
│ 设计师工作台 2025年11月2日星期六 [设] 设计师 [组员] │
└──────────────────────────────────────────────────────────┘
↑ 紫色圆形头像,显示"设"
| 文件 | 修改内容 | 行数 |
|---|---|---|
src/app/pages/designer/dashboard/dashboard.ts |
添加 loadUserProfile()、generateDefaultAvatar() | +50 行 |
src/app/pages/designer/dashboard/dashboard.ts |
简化 authenticateAndLoadData() | -10 行 |
#667eea(紫色)#ffffff(白色)使用 currentProfile() 方法,直接获取当前用户信息
设计师端和组长端使用相同的实现方式,易于维护
紫色圆形头像,与导航条主题一致
即使没有真实姓名,也显示手机号或"设计师"
会导致企微认证失效
const cid = localStorage.getItem("company") || 'cDL6R1hgSi';
| 方法 | 用途 | 返回值 |
|---|---|---|
currentProfile() |
✅ 获取当前用户Profile | Profile 对象 |
authenticateAndLogin() |
执行完整的登录流程 | { user, profile } |
推荐:仅需要用户信息时,使用 currentProfile()
✅ 修复完成!
通过采用组长端的方案,我们:
现在请刷新页面,查看效果!🚀
应该看到: