|
@@ -90,7 +90,7 @@ export class DeliveryMessageService {
|
|
|
await this.saveMessageToProject(projectId, message);
|
|
await this.saveMessageToProject(projectId, message);
|
|
|
|
|
|
|
|
// 🔥 发送到企业微信
|
|
// 🔥 发送到企业微信
|
|
|
- await this.sendToWxwork(content, []);
|
|
|
|
|
|
|
+ await this.sendToWxwork(projectId, content, []);
|
|
|
|
|
|
|
|
return message;
|
|
return message;
|
|
|
}
|
|
}
|
|
@@ -120,7 +120,7 @@ export class DeliveryMessageService {
|
|
|
await this.saveMessageToProject(projectId, message);
|
|
await this.saveMessageToProject(projectId, message);
|
|
|
|
|
|
|
|
// 🔥 发送到企业微信
|
|
// 🔥 发送到企业微信
|
|
|
- await this.sendToWxwork(content, imageUrls);
|
|
|
|
|
|
|
+ await this.sendToWxwork(projectId, content, imageUrls);
|
|
|
|
|
|
|
|
return message;
|
|
return message;
|
|
|
}
|
|
}
|
|
@@ -178,11 +178,13 @@ export class DeliveryMessageService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 🔥 发送消息到企业微信当前窗口
|
|
|
|
|
|
|
+ * 🔥 发送消息到企业微信当前群聊
|
|
|
|
|
+ * 参考 project-detail.component.ts 中的 sendSurvey() 方法
|
|
|
*/
|
|
*/
|
|
|
- private async sendToWxwork(text: string, imageUrls: string[] = []): Promise<void> {
|
|
|
|
|
|
|
+ private async sendToWxwork(projectId: string, text: string, imageUrls: string[] = []): Promise<void> {
|
|
|
try {
|
|
try {
|
|
|
console.log('🔍 [sendToWxwork] ========== 开始发送流程 ==========');
|
|
console.log('🔍 [sendToWxwork] ========== 开始发送流程 ==========');
|
|
|
|
|
+ console.log('🔍 [sendToWxwork] 项目ID:', projectId);
|
|
|
console.log('🔍 [sendToWxwork] 当前URL:', window.location.href);
|
|
console.log('🔍 [sendToWxwork] 当前URL:', window.location.href);
|
|
|
|
|
|
|
|
// 检查是否在企业微信环境中
|
|
// 检查是否在企业微信环境中
|
|
@@ -196,84 +198,107 @@ export class DeliveryMessageService {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 从URL获取cid和appId
|
|
|
|
|
|
|
+ // 1️⃣ 从URL获取cid和appId
|
|
|
const urlParts = window.location.pathname.split('/');
|
|
const urlParts = window.location.pathname.split('/');
|
|
|
- console.log('🔍 [sendToWxwork] URL路径分段:', urlParts);
|
|
|
|
|
-
|
|
|
|
|
const wxworkIndex = urlParts.indexOf('wxwork');
|
|
const wxworkIndex = urlParts.indexOf('wxwork');
|
|
|
- console.log('🔍 [sendToWxwork] wxwork位置索引:', wxworkIndex);
|
|
|
|
|
-
|
|
|
|
|
const cid = urlParts[wxworkIndex + 1];
|
|
const cid = urlParts[wxworkIndex + 1];
|
|
|
- const appId = urlParts[wxworkIndex + 2] || 'crm';
|
|
|
|
|
- console.log('🔍 [sendToWxwork] 提取的CID:', cid);
|
|
|
|
|
- console.log('🔍 [sendToWxwork] 提取的AppID:', appId);
|
|
|
|
|
|
|
+ const appId = urlParts[wxworkIndex + 2] || 'project';
|
|
|
|
|
+
|
|
|
|
|
+ console.log('🔍 [sendToWxwork] CID:', cid);
|
|
|
|
|
+ console.log('🔍 [sendToWxwork] AppID:', appId);
|
|
|
|
|
|
|
|
if (!cid || cid === 'undefined') {
|
|
if (!cid || cid === 'undefined') {
|
|
|
throw new Error('❌ 无法从URL获取CID,请检查URL格式');
|
|
throw new Error('❌ 无法从URL获取CID,请检查URL格式');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 🔥 检查SDK是否已初始化(避免重复初始化)
|
|
|
|
|
- console.log('🔍 [sendToWxwork] 检查SDK初始化状态...');
|
|
|
|
|
- console.log(' 当前SDK.cid:', this.wxworkService.cid);
|
|
|
|
|
- console.log(' 当前SDK.appId:', this.wxworkService.appId);
|
|
|
|
|
- console.log(' 从URL提取的cid:', cid);
|
|
|
|
|
- console.log(' 从URL提取的appId:', appId);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // 2️⃣ 初始化SDK(如果需要)
|
|
|
if (!this.wxworkService.cid || this.wxworkService.cid !== cid) {
|
|
if (!this.wxworkService.cid || this.wxworkService.cid !== cid) {
|
|
|
- console.log('🔍 [sendToWxwork] SDK未初始化或CID不匹配,开始初始化...');
|
|
|
|
|
|
|
+ console.log('🔍 [sendToWxwork] 初始化SDK...');
|
|
|
await this.wxworkService.initialize(cid, appId);
|
|
await this.wxworkService.initialize(cid, appId);
|
|
|
console.log('✅ [sendToWxwork] SDK初始化完成');
|
|
console.log('✅ [sendToWxwork] SDK初始化完成');
|
|
|
- } else {
|
|
|
|
|
- console.log('✅ [sendToWxwork] SDK已初始化,跳过重复初始化');
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- console.log('📧 准备发送消息到企业微信...');
|
|
|
|
|
- console.log(' CID:', cid);
|
|
|
|
|
- console.log(' AppID:', appId);
|
|
|
|
|
- console.log(' 文本内容:', text || '(无文本)');
|
|
|
|
|
- console.log(' 图片数量:', imageUrls.length);
|
|
|
|
|
- console.log(' 图片URL列表:', imageUrls);
|
|
|
|
|
|
|
+ // 3️⃣ 查询项目的群聊
|
|
|
|
|
+ console.log('🔍 [sendToWxwork] 查询项目群聊...');
|
|
|
|
|
+ const gcQuery = new Parse.Query('GroupChat');
|
|
|
|
|
+ gcQuery.equalTo('project', projectId);
|
|
|
|
|
+ gcQuery.equalTo('company', cid);
|
|
|
|
|
+ const groupChat = await gcQuery.first();
|
|
|
|
|
|
|
|
- // 🔥 发送文本消息
|
|
|
|
|
|
|
+ if (!groupChat) {
|
|
|
|
|
+ console.warn('⚠️ [sendToWxwork] 未找到项目群聊');
|
|
|
|
|
+ throw new Error('未找到项目群聊,无法发送消息');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const chatId = groupChat.get('chat_id');
|
|
|
|
|
+ console.log('🔍 [sendToWxwork] 群聊ID:', chatId);
|
|
|
|
|
+
|
|
|
|
|
+ if (!chatId) {
|
|
|
|
|
+ throw new Error('群聊ID为空,无法发送消息');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 4️⃣ 发送文本消息
|
|
|
if (text) {
|
|
if (text) {
|
|
|
- await this.wxworkService.sendChatMessage({
|
|
|
|
|
- msgtype: 'text',
|
|
|
|
|
- text: {
|
|
|
|
|
- content: text
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ console.log('📝 [sendToWxwork] 发送文本消息...');
|
|
|
|
|
+ console.log(' 内容:', text);
|
|
|
|
|
+
|
|
|
|
|
+ await this.wxworkService.ww.openExistedChatWithMsg({
|
|
|
|
|
+ chatId: chatId,
|
|
|
|
|
+ msg: {
|
|
|
|
|
+ msgtype: 'text',
|
|
|
|
|
+ text: {
|
|
|
|
|
+ content: text
|
|
|
|
|
+ }
|
|
|
|
|
+ } as any // 🔥 使用类型断言绕过TypeScript类型检查
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
console.log('✅ 文本消息已发送');
|
|
console.log('✅ 文本消息已发送');
|
|
|
|
|
+
|
|
|
|
|
+ // 如果有图片,等待一下再发送图片
|
|
|
|
|
+ if (imageUrls.length > 0) {
|
|
|
|
|
+ await new Promise(resolve => setTimeout(resolve, 500));
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 🔥 发送图片消息(使用news图文消息类型)
|
|
|
|
|
|
|
+ // 5️⃣ 发送图片消息(使用link类型,可以显示图片预览)
|
|
|
for (let i = 0; i < imageUrls.length; i++) {
|
|
for (let i = 0; i < imageUrls.length; i++) {
|
|
|
const imageUrl = imageUrls[i];
|
|
const imageUrl = imageUrls[i];
|
|
|
|
|
+ console.log(`📸 [sendToWxwork] 发送图片 ${i + 1}/${imageUrls.length}...`);
|
|
|
|
|
+ console.log(' URL:', imageUrl);
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
- // 使用news类型发送图文消息,可以显示图片预览
|
|
|
|
|
- await this.wxworkService.sendChatMessage({
|
|
|
|
|
- msgtype: 'news',
|
|
|
|
|
- news: {
|
|
|
|
|
- link: imageUrl,
|
|
|
|
|
- title: `图片 ${i + 1}/${imageUrls.length}`,
|
|
|
|
|
- desc: '点击查看大图',
|
|
|
|
|
- imgUrl: imageUrl
|
|
|
|
|
|
|
+ // 🔥 使用link类型发送图文消息,可以在群聊中显示图片预览
|
|
|
|
|
+ await this.wxworkService.ww.openExistedChatWithMsg({
|
|
|
|
|
+ chatId: chatId,
|
|
|
|
|
+ msg: {
|
|
|
|
|
+ msgtype: 'link',
|
|
|
|
|
+ link: {
|
|
|
|
|
+ title: `图片 ${i + 1}/${imageUrls.length}`,
|
|
|
|
|
+ desc: '点击查看大图',
|
|
|
|
|
+ url: imageUrl,
|
|
|
|
|
+ imgUrl: imageUrl
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
- console.log(`✅ 图文消息 ${i + 1}/${imageUrls.length} 已发送: ${imageUrl}`);
|
|
|
|
|
|
|
|
|
|
- // 避免发送过快,加入小延迟
|
|
|
|
|
|
|
+ console.log(`✅ 图片 ${i + 1}/${imageUrls.length} 已发送`);
|
|
|
|
|
+
|
|
|
|
|
+ // 避免发送过快,加入延迟
|
|
|
if (i < imageUrls.length - 1) {
|
|
if (i < imageUrls.length - 1) {
|
|
|
- await new Promise(resolve => setTimeout(resolve, 300));
|
|
|
|
|
|
|
+ await new Promise(resolve => setTimeout(resolve, 500));
|
|
|
}
|
|
}
|
|
|
} catch (imgError) {
|
|
} catch (imgError) {
|
|
|
console.error(`❌ 图片 ${i + 1} 发送失败:`, imgError);
|
|
console.error(`❌ 图片 ${i + 1} 发送失败:`, imgError);
|
|
|
- // 如果news类型失败,降级为纯文本链接
|
|
|
|
|
|
|
+ // 降级:尝试以文本方式发送图片链接
|
|
|
try {
|
|
try {
|
|
|
- await this.wxworkService.sendChatMessage({
|
|
|
|
|
- msgtype: 'text',
|
|
|
|
|
- text: {
|
|
|
|
|
- content: `📷 图片 ${i + 1}/${imageUrls.length}\n${imageUrl}`
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ await this.wxworkService.ww.openExistedChatWithMsg({
|
|
|
|
|
+ chatId: chatId,
|
|
|
|
|
+ msg: {
|
|
|
|
|
+ msgtype: 'text',
|
|
|
|
|
+ text: {
|
|
|
|
|
+ content: `📷 图片 ${i + 1}/${imageUrls.length}\n${imageUrl}`
|
|
|
|
|
+ }
|
|
|
|
|
+ } as any // 🔥 使用类型断言绕过TypeScript类型检查
|
|
|
});
|
|
});
|
|
|
console.log(`✅ 已改用文本方式发送图片链接`);
|
|
console.log(`✅ 已改用文本方式发送图片链接`);
|
|
|
} catch (textError) {
|
|
} catch (textError) {
|
|
@@ -282,10 +307,10 @@ export class DeliveryMessageService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- console.log('✅ 所有消息已发送到企业微信');
|
|
|
|
|
|
|
+ console.log('✅ [sendToWxwork] 所有消息已发送到企业微信群聊');
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
- console.error('❌ 发送消息到企业微信失败:', error);
|
|
|
|
|
- // 🔥 修复:必须抛出错误,让上层知道发送失败
|
|
|
|
|
|
|
+ console.error('❌ [sendToWxwork] 发送失败:', error);
|
|
|
|
|
+ // 抛出错误,让上层知道发送失败
|
|
|
throw error;
|
|
throw error;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|