用户反馈:project-loader无法正常使用
该组件是企微项目管理的入口页面,从企业客户联系群组进入,负责:
对比 /home/ryan/workspace/nova/nova-admin/projects/nova-crm/src/modules/chat/page-chat-context/page-chat-context.component.ts
发现最佳实践:
关键模式:
// 1. 使用 wxdebug 进行详细日志记录
import { wxdebug } from 'fmode-ng';
// 2. 注册 ionicons 图标
import { addIcons } from 'ionicons';
constructor() {
addIcons({ rocketOutline, addCircleOutline, ... });
}
// 3. 正确的 SDK 调用顺序
this.wxwork = new WxworkSDK({ cid: this.cid, appId: this.appId });
this.wecorp = new WxworkCorp(this.cid);
// 4. 分步获取上下文
this.currentUser = await this.wxwork.getCurrentUser();
this.currentChat = await this.wxwork.getCurrentChat();
// 5. 根据类型分别同步
if (this.currentChat?.type === "chatId" && this.currentChat?.group) {
this.groupChat = await this.wxwork.syncGroupChat(this.currentChat.group);
} else if (this.currentChat?.type === "userId" && this.currentChat?.id) {
const contactInfo = await this.wecorp.externalContact.get(this.currentChat.id);
this.contact = await this.wxwork.syncContact(contactInfo);
}
在 /home/ryan/workspace/nova/yss-project/src/modules/project/pages/project-loader/project-loader.component.ts
中发现:
❌ 缺失 wxdebug
❌ 缺失 addIcons
❌ 错误的 SDK 方法调用
getCurrentChatObject()
方法getCurrentChat()
→ syncGroupChat()
/syncContact()
❌ 错误的路由路径
// 错误
await this.router.navigate(['/wxwork', this.cid, 'customer', this.contact!.id]);
// 正确
await this.router.navigate(['/wxwork', this.cid, 'contact', this.contact!.id]);
❌ 错误处理不够详细
修改位置: 第 8 行,第 117-221 行
import { wxdebug } from 'fmode-ng';
async loadData() {
wxdebug('1. SDK初始化完成', { cid: this.cid, appId: this.appId });
try {
this.currentUser = await this.wxwork.getCurrentUser();
wxdebug('2. 获取当前用户成功', this.currentUser?.toJSON());
} catch (err) {
wxdebug('2. 获取当前用户失败', err);
throw new Error('获取用户信息失败,请重试');
}
this.currentChat = await this.wxwork.getCurrentChat();
wxdebug('3. getCurrentChat返回', this.currentChat);
if (this.currentChat?.type === "chatId" && this.currentChat?.group) {
wxdebug('4. 检测到群聊场景', this.currentChat.group);
this.groupChat = await this.wxwork.syncGroupChat(this.currentChat.group);
wxdebug('5. 群聊同步完成', this.groupChat?.toJSON());
} else if (this.currentChat?.type === "userId" && this.currentChat?.id) {
wxdebug('4. 检测到联系人场景', { id: this.currentChat.id });
const contactInfo = await this.wecorp!.externalContact.get(this.currentChat.id);
wxdebug('5. 获取完整联系人信息', contactInfo);
this.contact = await this.wxwork.syncContact(contactInfo);
wxdebug('6. 联系人同步完成', this.contact?.toJSON());
}
}
效果:
修改位置: 第 9-18 行,第 78-86 行
import { addIcons } from 'ionicons';
import {
rocketOutline,
addCircleOutline,
timeOutline,
personCircleOutline,
alertCircleOutline,
refreshOutline,
chevronForwardOutline
} from 'ionicons/icons';
constructor(
private router: Router,
private route: ActivatedRoute
) {
addIcons({
rocketOutline,
addCircleOutline,
timeOutline,
personCircleOutline,
alertCircleOutline,
refreshOutline,
chevronForwardOutline
});
}
效果:
修改位置: 第 130-177 行
// 3️⃣ 加载当前聊天上下文
this.loadingMessage = '获取会话信息...';
try {
this.currentChat = await this.wxwork.getCurrentChat();
wxdebug('3. getCurrentChat返回', this.currentChat);
} catch (err) {
console.error('getCurrentChat失败:', err);
wxdebug('3. getCurrentChat失败', err);
}
// 4️⃣ 根据场景同步数据
if (this.currentChat?.type === "chatId" && this.currentChat?.group) {
// 群聊场景
wxdebug('4. 检测到群聊场景', this.currentChat.group);
this.loadingMessage = '同步群聊信息...';
try {
this.chatType = 'group';
this.groupChat = await this.wxwork.syncGroupChat(this.currentChat.group);
wxdebug('5. 群聊同步完成', this.groupChat?.toJSON());
// 处理群聊场景
await this.handleGroupChatScene();
} catch (err) {
console.error('群聊同步失败:', err);
wxdebug('5. 群聊同步失败', err);
throw new Error('群聊信息同步失败');
}
} else if (this.currentChat?.type === "userId" && this.currentChat?.id) {
// 联系人场景
wxdebug('4. 检测到联系人场景', { id: this.currentChat.id });
this.loadingMessage = '同步联系人信息...';
try {
this.chatType = 'contact';
// 获取完整联系人信息
const contactInfo = await this.wecorp!.externalContact.get(this.currentChat.id);
wxdebug('5. 获取完整联系人信息', contactInfo);
this.contact = await this.wxwork.syncContact(contactInfo);
wxdebug('6. 联系人同步完成', this.contact?.toJSON());
// 处理联系人场景
await this.handleContactScene();
} catch (err) {
console.error('联系人同步失败:', err);
wxdebug('联系人同步失败', err);
throw new Error('联系人信息同步失败');
}
}
变化:
getCurrentChatObject()
(不存在的方法)getCurrentChat()
→ syncGroupChat()
/ syncContact()
修改位置: 第 247 行
async handleContactScene() {
wxdebug('联系人场景,跳转客户画像', {
contactId: this.contact!.id,
contactName: this.contact!.get('name')
});
// 跳转客户画像页面
await this.router.navigate(['/wxwork', this.cid, 'contact', this.contact!.id], {
queryParams: {
profileId: this.currentUser!.id
}
});
}
变化:
/wxwork/:cid/customer/:contactId
/wxwork/:cid/contact/:contactId
匹配路由配置:
// app.routes.ts
{
path: 'contact/:contactId',
loadComponent: () => import('../modules/project/pages/contact/contact.component')
.then(m => m.CustomerProfileComponent),
title: '客户画像'
}
修改位置: 第 119-188 行
每个步骤都添加了:
loadingMessage
("获取用户信息..."、"同步群聊信息..." 等)✅ 用户友好的错误提示
// 2️⃣ 加载当前登录员工信息(由 WxworkAuthGuard 自动登录)
this.loadingMessage = '获取用户信息...';
try {
this.currentUser = await this.wxwork.getCurrentUser();
wxdebug('2. 获取当前用户成功', this.currentUser?.toJSON());
} catch (err) {
console.error('获取当前用户失败:', err);
wxdebug('2. 获取当前用户失败', err);
throw new Error('获取用户信息失败,请重试');
}
修改位置: 第 179-188 行
else {
// 未检测到有效场景
wxdebug('4. 未检测到有效场景', {
currentChat: this.currentChat,
type: this.currentChat?.type,
hasGroup: !!this.currentChat?.group,
hasContact: !!this.currentChat?.contact,
hasId: !!this.currentChat?.id
});
throw new Error('无法识别当前会话类型,请在群聊或联系人会话中打开');
}
效果:
✅ 构建成功 - 无 TypeScript 错误
npm run build
# 输出:Application bundle generation complete. [103.473 seconds]
Lazy chunk files | Names | Raw size | Estimated transfer size
chunk-NWSP7TJL.js | project-loader-component | 9.16 MB | 1.86 MB
说明:
调试能力:
用户体验:
代码质量:
群聊场景:
联系人场景:
错误场景:
在浏览器控制台查看 wxdebug 输出:
1. SDK初始化完成 { cid: "cDL6R1hgSi", appId: "crm" }
2. 获取当前用户成功 { id: "xxx", name: "xxx", ... }
3. getCurrentChat返回 { type: "chatId", group: {...} }
4. 检测到群聊场景 { chatId: "xxx", ... }
5. 群聊同步完成 { id: "xxx", name: "xxx", ... }
群聊 → 项目详情:
/wxwork/cDL6R1hgSi/project/PROJECT_ID?groupId=GROUP_ID&profileId=USER_ID
联系人 → 客户画像:
/wxwork/cDL6R1hgSi/contact/CONTACT_ID?profileId=USER_ID
/home/ryan/workspace/nova/nova-admin/projects/nova-crm/src/modules/chat/page-chat-context/page-chat-context.component.ts
/home/ryan/workspace/nova/yss-project/src/app/app.routes.ts
node_modules/fmode-ng/
建议进行以下测试:
真实企微环境测试
日志监控
用户体验验证
修复完成!✨