ソースを参照

Merge remote-tracking branch 'origin/master'

zyy 14 時間 前
コミット
c955a4abbb
1 ファイル変更74 行追加33 行削除
  1. 74 33
      src/views/crm/customerAiChat/index.vue

+ 74 - 33
src/views/crm/customerAiChat/index.vue

@@ -683,8 +683,10 @@ export default {
 
             // 清空当前消息,显示欢迎状态
             this.messages = []
-            // 新建对话时重置关联客户
-            this.linkedCustomer = null
+            // ✅ 关键修复:新建对话时才重置关联客户,但如果已经有 pendingLinkedCustomer 则保留
+            if (!this.pendingLinkedCustomer) {
+                this.linkedCustomer = null
+            }
             this.firstMessageSent = false
 
             // 清空手动编辑标题的记录
@@ -712,8 +714,14 @@ export default {
             }
 
             try {
-                // 获取正确的 customerId
-                const customerId = this.linkedCustomer?.id || chat.linkedCustomer?.id || null
+                this.syncLock = true
+
+                // ✅ 关键修复:优先从 pendingLinkedCustomer 恢复,确保 customerId 不为空
+                if (!this.linkedCustomer && this.pendingLinkedCustomer) {
+                    this.linkedCustomer = {...this.pendingLinkedCustomer}
+                }
+                // ✅ 关键修复:获取正确的 customerId(按优先级:this.linkedCustomer > chat.linkedCustomer > pendingLinkedCustomer)
+                const customerId = this.linkedCustomer?.id || chat.linkedCustomer?.id || this.pendingLinkedCustomer?.id || null
 
                 // 如果用户已手动编辑标题,使用编辑后的标题,否则使用第一条消息
                 const hasManuallyEdited = this.manuallyEditedTitles[chat.id]
@@ -724,8 +732,6 @@ export default {
                     customerId: customerId
                 }
 
-                console.log('创建会话参数:', params)
-
                 // 等待 Promise 完成
                 const response = await createChatSession(params)
 
@@ -760,27 +766,45 @@ export default {
                         }
                     }
 
-                    // 确保关联客户与后端一致
-                    if (this.linkedCustomer) {
+                    // ✅ 关键修复:确保关联客户与后端一致
+                    if (this.linkedCustomer && this.linkedCustomer.id === customerId) {
                         chat.linkedCustomer = {...this.linkedCustomer}
                         this.customerLinkMap[chat.id] = this.linkedCustomer
+                    } else if (customerId && chat.linkedCustomer) {
+                        // ✅ 修复:如果之前已关联客户但 this.linkedCustomer 为空,从 chat 中恢复
+                        this.linkedCustomer = {...chat.linkedCustomer}
+                        this.customerLinkMap[chat.id] = chat.linkedCustomer
+                    } else if (customerId) {
+                        // ✅ 新增:如果有 customerId 但没有客户详情,尝试从其他地方查找
+                        const foundChat = this.chatList.find(c => c.linkedCustomerId === customerId && c.linkedCustomer)
+                        if (foundChat?.linkedCustomer) {
+                            this.linkedCustomer = {...foundChat.linkedCustomer}
+                            chat.linkedCustomer = this.linkedCustomer
+                            this.customerLinkMap[chat.id] = this.linkedCustomer
+                        } else {
+                            console.log('⚠️ 有 customerId 但找不到客户详情:', customerId)
+                        }
+                    } else if (this.pendingLinkedCustomer) {
+                        // ✅ 这个情况应该不会被触发了,因为已经在最开始恢复了
+                        console.log('⚠️ 异常情况:不应该到这里,pendingLinkedCustomer 应该在最开始就恢复')
+                    } else {
+                        console.log('⚠️ 无关联客户信息,customerId:', customerId, 'chat.linkedCustomer:', chat.linkedCustomer)
                     }
 
                     // 更新缓存
                     this.cacheSession(chat)
 
-                    console.log('创建后端会话成功:', chat.sessionId, '新 ID:', chat.id)
+                    console.log('创建后端会话成功 - sessionId:', chat.sessionId, '新 ID:', chat.id, '最终 linkedCustomer:', this.linkedCustomer?.name || 'null')
                     return true
                 } else {
-                    console.error('创建后端会话失败:', response.msg)
                     this.$message.error(response.msg || '创建会话失败')
                     return false
                 }
             } catch (error) {
-                console.error('创建后端会话异常:', error)
                 this.$message.error(error.message || '创建会话异常')
                 return false
             } finally {
+                this.syncLock = false
                 // 确保锁释放
                 this.sessionCreatingPromise = null
             }
@@ -844,10 +868,11 @@ export default {
 
             this.clearThinkingTimer()
 
-            // 保存当前会话的关联客户和画像缓存
+            // ✅ 关键修复:保存当前会话的关联客户和画像缓存到 map 中
             if (this.currentChatId) {
                 if (this.linkedCustomer) {
                     this.customerLinkMap[this.currentChatId] = this.linkedCustomer
+                    console.log('💾 selectChat - 保存 customerLinkMap:', this.currentChatId, '=>', this.linkedCustomer.name)
                 } else {
                     delete this.customerLinkMap[this.currentChatId]
                 }
@@ -856,13 +881,24 @@ export default {
                 }
             }
 
+            const oldChatId = this.currentChatId
             this.currentChatId = chat.id
             this.currentChatTitle = chat.title || ''
 
-            // 恢复目标会话的关联客户和画像
-            const linkedCustomer = this.customerLinkMap[chat.id] || chat.linkedCustomer || null
-            this.linkedCustomer = linkedCustomer
+            // ✅ 关键修复:优先从 customerLinkMap 恢复,其次从 chat.linkedCustomer 恢复
+            let linkedCustomer = this.customerLinkMap[chat.id] || chat.linkedCustomer || null
 
+            // ✅ 如果 chat 有 linkedCustomerId 但没有 linkedCustomer,尝试从其他会话中查找
+            if (!linkedCustomer && chat.linkedCustomerId) {
+                // 从其他会话中查找相同的 customerId
+                const foundChat = this.chatList.find(c => c.linkedCustomerId === chat.linkedCustomerId && c.linkedCustomer)
+                if (foundChat?.linkedCustomer) {
+                    linkedCustomer = foundChat.linkedCustomer
+                    console.log('✅ 从其他会话找到关联客户:', linkedCustomer.name)
+                }
+            }
+
+            this.linkedCustomer = linkedCustomer
             // 恢复客户画像缓存
             if (this.customerPortraitCache[chat.id]) {
                 if (this.linkedCustomer) {
@@ -901,6 +937,7 @@ export default {
             }
 
             this.isLoadingMore = false
+
             this.$nextTick(() => this.scrollToBottom())
         },
 
@@ -926,7 +963,6 @@ export default {
 
             const currentChat = this.chatList.find(c => c.id === this.currentChatId)
             if (!currentChat) {
-                console.error('找不到当前会话')
                 this.$message.error('会话不存在,请重试')
                 return
             }
@@ -952,7 +988,6 @@ export default {
                         }
                     }
                 } catch (error) {
-                    console.error('创建会话失败:', error)
                     this.$message.error('创建会话失败,请稍后重试')
                     this.sessionCreatingPromise = null
                     return
@@ -968,7 +1003,6 @@ export default {
                 const newTitle = userInput.substring(0, 50) + (userInput.length > 50 ? '...' : '')
                 currentChat.title = newTitle
                 this.currentChatTitle = newTitle
-                console.log('更新会话标题:', newTitle)
                 // 异步更新后端标题
                 if (currentChat.sessionId) {
                     this.updateBackendSessionTitle(currentChat.sessionId, newTitle).catch(err => {
@@ -1052,6 +1086,7 @@ export default {
                         // 停止计时器并重置状态
                         this.clearThinkingTimer()
 
+                        console.log('调用 AI 返回数据:', JSON.stringify(response.data))
                         // 创建 AI 消息对象
                         const aiMessage = {
                             type: 'ai',
@@ -1098,17 +1133,15 @@ export default {
                         })
                     }
                 }).catch(error => {
-                    console.error('获取 AI 回复失败:', error)
                     // 异常时也重置状态
                     this.clearThinkingTimer()
                     this.isLoading = false
-                    this.$message.error('获取 AI 回复失败,请稍后重试')
+                    this.$message.error('获取 AI 回复失败:' + error)
                 });
             } catch (error) {
-                console.error('获取 AI 回复失败:', error)
                 this.clearThinkingTimer()
                 this.isLoading = false
-                this.$message.error('获取 AI 回复失败,请稍后重试')
+                this.$message.error('获取 AI 回复异常:' + error)
             } finally {
                 this.syncLock = false
             }
@@ -1318,8 +1351,8 @@ export default {
                 params.attritionLevel = this.customerFilter.attritionLevel
             }
             listAllAnalyze(params).then(response => {
-                this.customerList = response.rows || []
-                this.customerPagination.total = response.total || 0
+                this.customerList = response.data.list || []
+                this.customerPagination.total = response.data.total || 0
                 this.customerLoading = false
             }).catch(() => {
                 this.customerLoading = false
@@ -1370,6 +1403,9 @@ export default {
                 // ✅ 立即更新当前关联客户
                 this.linkedCustomer = newLinkedCustomer
 
+                // ✅ 保存到 pendingLinkedCustomer,确保新建会话时不会丢失
+                this.pendingLinkedCustomer = newLinkedCustomer
+
                 // ✅ 更新当前会话的关联客户(包括 linkedCustomerId)
                 const currentChat = this.chatList.find(c => c.id === this.currentChatId)
                 if (currentChat) {
@@ -1384,9 +1420,10 @@ export default {
                                 console.log('更新会话客户关联成功:', customer.customerId)
                             })
                             .catch(error => {
-                                console.error('更新会话客户关联失败:', error)
-                                this.$message.warning('客户关联已更新,但保存到服务器失败')
+                                this.$message.warning('客户关联已更新,但保存到服务器失败:' + error)
                             })
+                    } else {
+                        console.log('⚠️ 当前会话还没有 sessionId,等待发送消息时创建')
                     }
                 }
 
@@ -1396,7 +1433,9 @@ export default {
                 }
 
                 this.customerDialogVisible = false
-                this.$message.success('已成功关联客户-' + customer.customerName)
+
+                // ✅ 强制刷新视图
+                this.$forceUpdate()
             }
         },
         // 获取图标类名
@@ -1501,8 +1540,7 @@ export default {
 
                     this.$message.success('缓存已清除')
                 } catch (error) {
-                    console.error('清除缓存失败:', error)
-                    this.$message.error('清除缓存失败,请重试')
+                    this.$message.error('清除缓存失败,原因:' + error)
                 }
             }).catch(() => {
                 // 用户取消
@@ -1542,7 +1580,6 @@ export default {
                 console.warn('sessionId 不存在,等待会话创建')
                 await new Promise(resolve => setTimeout(resolve, 100))
                 if (!currentChat.sessionId) {
-                    console.error('sessionId 创建失败')
                     this.$message.error('会话初始化失败,请重试')
                     return
                 }
@@ -1725,11 +1762,14 @@ export default {
 
                             if (customerIds.length > 0) {
                                 listAllAnalyze({
-                                    customerIds: customerIds
+                                    customerIds: customerIds,
+                                    pageNum: 1,
+                                    pageSize: 10
                                 }).then(response => {
-                                    if (response.rows && response.rows.length > 0) {
+                                    // ✅ 关键修复:接口返回的是 response.data.list
+                                    if (response.data && response.data.list && response.data.list.length > 0) {
                                         const customersMap = {}
-                                        response.rows.forEach(customer => {
+                                        response.data.list.forEach(customer => {
                                             customersMap[customer.customerId] = {
                                                 id: customer.customerId,
                                                 name: customer.customerName,
@@ -1756,6 +1796,7 @@ export default {
                                         const firstChat = this.chatList[0]
                                         this.selectChat(firstChat)
                                     } else {
+                                        console.log('⚠️ 未查询到关联客户信息')
                                         const firstChat = this.chatList[0]
                                         this.selectChat(firstChat)
                                     }