Compare commits
	
		
			2 Commits
		
	
	
		
			edc03710b0
			...
			355cfc4aa4
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 355cfc4aa4 | |||
| 8853f117fc | 
| @ -35,6 +35,10 @@ class ContactInfoController { | ||||
|             view.webContents.send("message-from-main", args.nickName); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     async getBatchContactNicknames(args, event) { | ||||
|         return await contactInfoService.getBatchContactNicknames(args, event); | ||||
|     } | ||||
| } | ||||
| ContactInfoController.toString = () => '[class ContactInfoController]'; | ||||
|  | ||||
|  | ||||
							
								
								
									
										241
									
								
								electron/controller/hybridQuickReply.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										241
									
								
								electron/controller/hybridQuickReply.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,241 @@ | ||||
| 'use strict'; | ||||
|  | ||||
| const { QuickReplyService } = require('../service/quickreply'); | ||||
| const HttpQuickReplyService = require('../service/httpQuickReply'); | ||||
| const { logger } = require('ee-core/log'); | ||||
| const { app } = require('electron'); | ||||
|  | ||||
| /** | ||||
|  * 混合快捷回复控制器 | ||||
|  * 根据配置选择使用本地SQLite或远程HTTP API | ||||
|  * @class | ||||
|  */ | ||||
| class HybridQuickReplyController { | ||||
|   constructor() { | ||||
|     this.localService = new QuickReplyService(); | ||||
|     this.httpService = new HttpQuickReplyService(); | ||||
|      | ||||
|     // 从配置中读取是否使用远程API | ||||
|     this.useRemoteApi = this._shouldUseRemoteApi(); | ||||
|      | ||||
|     logger.info(`快捷回复服务模式: ${this.useRemoteApi ? 'HTTP API' : '本地SQLite'}`); | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 判断是否应该使用远程API | ||||
|    */ | ||||
|   _shouldUseRemoteApi() { | ||||
|     try { | ||||
|       // 检查配置文件中的设置 | ||||
|       const config = app.config || {}; | ||||
|  | ||||
|       // 如果明确配置了使用远程API | ||||
|       if (config.quickReply?.useRemoteApi === true) { | ||||
|         return true; | ||||
|       } | ||||
|  | ||||
|       // 如果配置了API基础URL且不是默认值,则尝试使用远程API | ||||
|       if (config.api?.baseUrl && | ||||
|           config.api.baseUrl !== 'http://127.0.0.1:8000/api' && | ||||
|           config.quickReply?.useRemoteApi !== false) { | ||||
|         return true; | ||||
|       } | ||||
|  | ||||
|       // 默认使用本地SQLite | ||||
|       return false; | ||||
|     } catch (error) { | ||||
|       logger.warn('读取快捷回复配置失败,使用本地SQLite:', error.message); | ||||
|       return false; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 获取当前使用的服务实例 | ||||
|    */ | ||||
|   _getService() { | ||||
|     return this.useRemoteApi ? this.httpService : this.localService; | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 安全执行方法,如果远程API失败则回退到本地 | ||||
|    */ | ||||
|   async _safeExecute(methodName, args, event) { | ||||
|     const service = this._getService(); | ||||
|      | ||||
|     try { | ||||
|       const result = await service[methodName](args, event); | ||||
|        | ||||
|       // 如果使用远程API且失败,尝试回退到本地 | ||||
|       if (this.useRemoteApi && (!result || !result.status)) { | ||||
|         logger.warn(`远程API ${methodName} 失败,尝试回退到本地SQLite`); | ||||
|         return await this.localService[methodName](args, event); | ||||
|       } | ||||
|        | ||||
|       return result; | ||||
|     } catch (error) { | ||||
|       logger.error(`${this.useRemoteApi ? '远程API' : '本地SQLite'} ${methodName} 执行失败:`, error); | ||||
|        | ||||
|       // 如果使用远程API且出错,回退到本地 | ||||
|       if (this.useRemoteApi) { | ||||
|         logger.warn(`回退到本地SQLite执行 ${methodName}`); | ||||
|         try { | ||||
|           return await this.localService[methodName](args, event); | ||||
|         } catch (localError) { | ||||
|           logger.error(`本地SQLite ${methodName} 也失败:`, localError); | ||||
|           return { | ||||
|             status: false, | ||||
|             message: `操作失败:${error.message}` | ||||
|           }; | ||||
|         } | ||||
|       } | ||||
|        | ||||
|       return { | ||||
|         status: false, | ||||
|         message: `操作失败:${error.message}` | ||||
|       }; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   async getGroups(args, event) { | ||||
|     return await this._safeExecute('getGroups', args, event); | ||||
|   } | ||||
|  | ||||
|   async getContentByGroupId(args, event) { | ||||
|     return await this._safeExecute('getContentByGroupId', args, event); | ||||
|   } | ||||
|  | ||||
|   async addGroup(args, event) { | ||||
|     return await this._safeExecute('addGroup', args, event); | ||||
|   } | ||||
|  | ||||
|   async editGroup(args, event) { | ||||
|     return await this._safeExecute('editGroup', args, event); | ||||
|   } | ||||
|  | ||||
|   async deleteGroup(args, event) { | ||||
|     return await this._safeExecute('deleteGroup', args, event); | ||||
|   } | ||||
|  | ||||
|   async addReply(args, event) { | ||||
|     return await this._safeExecute('addReply', args, event); | ||||
|   } | ||||
|  | ||||
|   async editReply(args, event) { | ||||
|     return await this._safeExecute('editReply', args, event); | ||||
|   } | ||||
|  | ||||
|   async deleteReply(args, event) { | ||||
|     return await this._safeExecute('deleteReply', args, event); | ||||
|   } | ||||
|  | ||||
|   async deleteAllReply(args, event) { | ||||
|     return await this._safeExecute('deleteAllReply', args, event); | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 切换服务模式 | ||||
|    */ | ||||
|   async switchMode(useRemoteApi = false) { | ||||
|     this.useRemoteApi = useRemoteApi; | ||||
|     logger.info(`快捷回复服务模式已切换为: ${this.useRemoteApi ? 'HTTP API' : '本地SQLite'}`); | ||||
|     return { | ||||
|       status: true, | ||||
|       message: `已切换到${this.useRemoteApi ? '远程API' : '本地SQLite'}模式` | ||||
|     }; | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 获取当前模式状态 | ||||
|    */ | ||||
|   getMode() { | ||||
|     return { | ||||
|       status: true, | ||||
|       data: { | ||||
|         useRemoteApi: this.useRemoteApi, | ||||
|         mode: this.useRemoteApi ? 'HTTP API' : '本地SQLite', | ||||
|         apiUrl: this.useRemoteApi ? this.httpService.baseUrl : null | ||||
|       } | ||||
|     }; | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 测试远程API连接 | ||||
|    */ | ||||
|   async testRemoteConnection() { | ||||
|     if (!this.useRemoteApi) { | ||||
|       return { | ||||
|         status: false, | ||||
|         message: '当前未使用远程API模式' | ||||
|       }; | ||||
|     } | ||||
|  | ||||
|     try { | ||||
|       // 尝试获取分组列表来测试连接 | ||||
|       const result = await this.httpService.getGroups({}, null); | ||||
|       return { | ||||
|         status: true, | ||||
|         message: '远程API连接正常', | ||||
|         data: result | ||||
|       }; | ||||
|     } catch (error) { | ||||
|       return { | ||||
|         status: false, | ||||
|         message: `远程API连接失败: ${error.message}` | ||||
|       }; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 数据同步:从本地同步到远程或从远程同步到本地 | ||||
|    */ | ||||
|   async syncData(direction = 'local-to-remote') { | ||||
|     try { | ||||
|       if (direction === 'local-to-remote') { | ||||
|         // 从本地同步到远程 | ||||
|         const localGroups = await this.localService.getGroups({}, null); | ||||
|         if (!localGroups.status) { | ||||
|           return { status: false, message: '获取本地数据失败' }; | ||||
|         } | ||||
|  | ||||
|         let syncCount = 0; | ||||
|         for (const group of localGroups.data) { | ||||
|           // 同步分组 | ||||
|           const groupResult = await this.httpService.addGroup({ name: group.name }, null); | ||||
|           if (groupResult.status) { | ||||
|             syncCount++; | ||||
|              | ||||
|             // 同步分组下的内容 | ||||
|             for (const content of group.contents || []) { | ||||
|               await this.httpService.addReply({ | ||||
|                 remark: content.remark, | ||||
|                 content: content.content, | ||||
|                 type: content.type, | ||||
|                 url: content.url, | ||||
|                 groupId: groupResult.data.id | ||||
|               }, null); | ||||
|             } | ||||
|           } | ||||
|         } | ||||
|  | ||||
|         return { | ||||
|           status: true, | ||||
|           message: `成功同步 ${syncCount} 个分组到远程` | ||||
|         }; | ||||
|       } else { | ||||
|         // 从远程同步到本地的逻辑可以在这里实现 | ||||
|         return { | ||||
|           status: false, | ||||
|           message: '暂不支持从远程同步到本地' | ||||
|         }; | ||||
|       } | ||||
|     } catch (error) { | ||||
|       logger.error('数据同步失败:', error); | ||||
|       return { | ||||
|         status: false, | ||||
|         message: `数据同步失败: ${error.message}` | ||||
|       }; | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
| module.exports = HybridQuickReplyController; | ||||
| @ -1,42 +1,75 @@ | ||||
| 'use strict'; | ||||
|  | ||||
| const { logger } = require('ee-core/log'); | ||||
| const {quickReplyService} = require("../service/quickreply"); | ||||
| const HybridQuickReplyController = require('./hybridQuickReply'); | ||||
|  | ||||
| /** | ||||
|  * 快捷回复 api | ||||
|  * 快捷回复 API 控制器 | ||||
|  * 使用混合模式,支持本地SQLite和远程HTTP API | ||||
|  * @class | ||||
|  */ | ||||
| class QuickReplyController { | ||||
|     constructor() { | ||||
|         this.hybridController = new HybridQuickReplyController(); | ||||
|     } | ||||
|  | ||||
|     async getGroups(args,event) { | ||||
|         return await quickReplyService.getGroups(args,event); | ||||
|     async getGroups(args, event) { | ||||
|         return await this.hybridController.getGroups(args, event); | ||||
|     } | ||||
|     async getContentByGroupId(args,event) { | ||||
|         return await quickReplyService.getContentByGroupId(args,event); | ||||
|  | ||||
|     async getContentByGroupId(args, event) { | ||||
|         return await this.hybridController.getContentByGroupId(args, event); | ||||
|     } | ||||
|     async addGroup(args,event) { | ||||
|         return await quickReplyService.addGroup(args,event); | ||||
|  | ||||
|     async addGroup(args, event) { | ||||
|         return await this.hybridController.addGroup(args, event); | ||||
|     } | ||||
|     async editGroup(args,event) { | ||||
|         return await quickReplyService.editGroup(args,event); | ||||
|  | ||||
|     async editGroup(args, event) { | ||||
|         return await this.hybridController.editGroup(args, event); | ||||
|     } | ||||
|     async deleteGroup(args,event) { | ||||
|         return await quickReplyService.deleteGroup(args,event); | ||||
|  | ||||
|     async deleteGroup(args, event) { | ||||
|         return await this.hybridController.deleteGroup(args, event); | ||||
|     } | ||||
|     async addReply(args,event) { | ||||
|         return await quickReplyService.addReply(args,event); | ||||
|  | ||||
|     async addReply(args, event) { | ||||
|         return await this.hybridController.addReply(args, event); | ||||
|     } | ||||
|     async editReply(args,event) { | ||||
|         return await quickReplyService.editReply(args,event); | ||||
|  | ||||
|     async editReply(args, event) { | ||||
|         return await this.hybridController.editReply(args, event); | ||||
|     } | ||||
|     async deleteReply(args,event) { | ||||
|         return await quickReplyService.deleteReply(args,event); | ||||
|  | ||||
|     async deleteReply(args, event) { | ||||
|         return await this.hybridController.deleteReply(args, event); | ||||
|     } | ||||
|     async deleteAllReply(args,event) { | ||||
|         return await quickReplyService.deleteAllReply(args,event); | ||||
|  | ||||
|     async deleteAllReply(args, event) { | ||||
|         return await this.hybridController.deleteAllReply(args, event); | ||||
|     } | ||||
|  | ||||
|     // 新增的管理方法 | ||||
|     async switchMode(args, event) { | ||||
|         const { useRemoteApi } = args; | ||||
|         return await this.hybridController.switchMode(useRemoteApi); | ||||
|     } | ||||
|  | ||||
|     async getMode(args, event) { | ||||
|         return this.hybridController.getMode(); | ||||
|     } | ||||
|  | ||||
|     async testRemoteConnection(args, event) { | ||||
|         return await this.hybridController.testRemoteConnection(); | ||||
|     } | ||||
|  | ||||
|     async syncData(args, event) { | ||||
|         const { direction } = args; | ||||
|         return await this.hybridController.syncData(direction); | ||||
|     } | ||||
| } | ||||
|  | ||||
| module.exports = QuickReplyController; | ||||
| QuickReplyController.toString = () => '[class QuickReplyController]'; | ||||
|  | ||||
| module.exports = QuickReplyController; | ||||
|  | ||||
| @ -25,6 +25,9 @@ contextBridge.exposeInMainWorld("electronAPI", { | ||||
|   getContactInfo: (args) => { | ||||
|     return ipcRenderer.invoke("get-contact-info", args); | ||||
|   }, | ||||
|   getBatchContactNicknames: (args) => { | ||||
|     return ipcRenderer.invoke("get-batch-contact-nicknames", args); | ||||
|   }, | ||||
|   collapseRightSidebar: () => { | ||||
|     ipcRenderer.send("collapse-right-sidebar"); | ||||
|   }, | ||||
|  | ||||
| @ -344,6 +344,11 @@ const ipcMainListener = () => { | ||||
|     return { status: true, data: userInfo }; | ||||
|   }); | ||||
|  | ||||
|   // 批量获取联系人昵称配置 | ||||
|   ipcMain.handle("get-batch-contact-nicknames", async (event, args) => { | ||||
|     return await contactInfoService.getBatchContactNicknames(args, event); | ||||
|   }); | ||||
|  | ||||
|   ipcMain.handle("get-language", async (event, args) => { | ||||
|     const languageObj = await app.sdb.selectOne('language_list', args) | ||||
|     return { status: true, data: languageObj }; | ||||
| @ -583,6 +588,23 @@ const ipcMainListener = () => { | ||||
|       logger.error("记录状态更新失败", error); | ||||
|     } | ||||
|   }); | ||||
|  | ||||
|   // 打开快捷回复配置页面 | ||||
|   ipcMain.handle("open-quick-reply-config", async (event, args) => { | ||||
|     try { | ||||
|       const mainWindow = getMainWindow(); | ||||
|       if (mainWindow) { | ||||
|         // 发送导航事件到前端 | ||||
|         mainWindow.webContents.send("navigate-to-quick-reply-config"); | ||||
|         return { status: true, message: "已打开快捷回复配置页面" }; | ||||
|       } else { | ||||
|         return { status: false, message: "主窗口未找到" }; | ||||
|       } | ||||
|     } catch (error) { | ||||
|       logger.error("打开快捷回复配置失败:", error); | ||||
|       return { status: false, message: `打开失败: ${error.message}` }; | ||||
|     } | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| /** | ||||
|  | ||||
| @ -945,8 +945,14 @@ const sessionChange = async () => { | ||||
|   const currentUserId = getCurrentUserId(); | ||||
|   const args = { platform: "WhatsApp", userId: currentUserId }; | ||||
|   console.log("会话切换 sessionChange args", args); | ||||
|  | ||||
|   // 检查是否为新打开的会话 | ||||
|   const isNewSession = isAppFirstLoad || !sessionStates.has(currentUserId); | ||||
|  | ||||
|   // 先更新配置信息 | ||||
|   ipc.infoUpdate(args); | ||||
|   await updateConfigInfo(); | ||||
|  | ||||
|   const myNode = document.getElementById("custom-translate-textarea"); | ||||
|   if (!myNode) { | ||||
|     addTranslatePreview(); | ||||
| @ -954,8 +960,43 @@ const sessionChange = async () => { | ||||
|   } | ||||
|   styledTextarea.initData(); | ||||
|  | ||||
|   // 初始化用户名片 | ||||
|   // 立即初始化用户名片,减少闪烁 | ||||
|   initUserInfoCard(args); | ||||
|  | ||||
|   // 处理滚动逻辑 | ||||
|   if (isNewSession) { | ||||
|     console.log(`WhatsApp: 新打开会话 ${currentUserId},将滚动到底部`); | ||||
|     // 新打开的会话,滚动到底部 - 使用多次延迟确保完全加载 | ||||
|  | ||||
|     // 第一次滚动 - 早期尝试 | ||||
|     setTimeout(() => { | ||||
|       scrollToBottom(); | ||||
|     }, 300); | ||||
|  | ||||
|     // 第二次滚动 - 确保DOM完全加载 | ||||
|     setTimeout(() => { | ||||
|       scrollToBottom(); | ||||
|     }, 800); | ||||
|  | ||||
|     // 第三次滚动 - 最终确保 | ||||
|     setTimeout(() => { | ||||
|       scrollToBottom(); | ||||
|     }, 1500); | ||||
|  | ||||
|     // 标记会话已打开 | ||||
|     sessionStates.set(currentUserId, { | ||||
|       isFirstOpen: false, | ||||
|       scrollPosition: 0 | ||||
|     }); | ||||
|  | ||||
|     // 标记应用不再是首次加载 | ||||
|     if (isAppFirstLoad) { | ||||
|       isAppFirstLoad = false; | ||||
|     } | ||||
|   } else { | ||||
|     console.log(`WhatsApp: 切换到已打开的会话 ${currentUserId},保持原滚动位置`); | ||||
|     // 已打开的会话切换,不进行滚动操作,保持原位置 | ||||
|   } | ||||
| }; | ||||
| const debouncedSessionChange = debounce(sessionChange, 200); | ||||
|  | ||||
| @ -1198,6 +1239,150 @@ const expandLongMessage = async (node) => { | ||||
| // 当前选中的会话DOM节点 | ||||
| let currentNode = null; | ||||
|  | ||||
| // 会话状态跟踪 | ||||
| let sessionStates = new Map(); // 存储会话状态 {userId: {isFirstOpen: boolean, scrollPosition: number}} | ||||
| let isAppFirstLoad = true; // 标记应用是否首次加载 | ||||
|  | ||||
| // 滚动到聊天底部的函数 | ||||
| const scrollToBottom = (force = false) => { | ||||
|   try { | ||||
|     console.log('WhatsApp: 开始执行滚动到底部操作'); | ||||
|  | ||||
|     // 更新的WhatsApp聊天消息容器选择器(基于最新DOM结构) | ||||
|     const possibleSelectors = [ | ||||
|       // 最新的WhatsApp DOM结构选择器 | ||||
|       '#main div[data-tab="1"]', // 主聊天区域 | ||||
|       '#main div[role="application"]', // 应用容器 | ||||
|       '#main div[class*="copyable-area"]', // 可复制区域 | ||||
|       '#main div[class*="message-list"]', // 消息列表 | ||||
|       '#main div[class*="copyable-text"]', // 可复制文本区域 | ||||
|       '#main > div > div > div > div[class*="copyable"]', // 深层选择器 | ||||
|       '#main > div:nth-child(2) > div > div', // 更深层的聊天区域 | ||||
|       '#main > div:nth-child(2)', // 第二个子元素通常是聊天区域 | ||||
|     ]; | ||||
|  | ||||
|     let chatContainer = null; | ||||
|     for (const selector of possibleSelectors) { | ||||
|       const element = document.querySelector(selector); | ||||
|       if (element) { | ||||
|         // 检查元素是否真的可以滚动 | ||||
|         const style = window.getComputedStyle(element); | ||||
|         if (style.overflowY === 'auto' || style.overflowY === 'scroll' || | ||||
|             element.scrollHeight > element.clientHeight) { | ||||
|           chatContainer = element; | ||||
|           console.log(`WhatsApp: 找到可滚动聊天容器,使用选择器: ${selector}`); | ||||
|           console.log(`WhatsApp: 容器信息 - scrollHeight: ${element.scrollHeight}, clientHeight: ${element.clientHeight}, scrollTop: ${element.scrollTop}`); | ||||
|           break; | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|  | ||||
|     // 如果没找到特定容器,尝试查找包含消息的父容器 | ||||
|     if (!chatContainer) { | ||||
|       console.log('WhatsApp: 未找到预定义容器,尝试通过消息元素查找'); | ||||
|       const messageElements = document.querySelectorAll("div[role='row']"); | ||||
|       console.log(`WhatsApp: 找到 ${messageElements.length} 个消息元素`); | ||||
|  | ||||
|       if (messageElements.length > 0) { | ||||
|         // 从最后一个消息元素开始向上查找可滚动容器 | ||||
|         const lastMessage = messageElements[messageElements.length - 1]; | ||||
|         let parent = lastMessage.parentElement; | ||||
|  | ||||
|         while (parent && parent !== document.body) { | ||||
|           const style = window.getComputedStyle(parent); | ||||
|           if (style.overflowY === 'auto' || style.overflowY === 'scroll' || | ||||
|               parent.scrollHeight > parent.clientHeight) { | ||||
|             chatContainer = parent; | ||||
|             console.log('WhatsApp: 通过消息元素找到滚动容器'); | ||||
|             console.log(`WhatsApp: 容器信息 - scrollHeight: ${parent.scrollHeight}, clientHeight: ${parent.clientHeight}, scrollTop: ${parent.scrollTop}`); | ||||
|             break; | ||||
|           } | ||||
|           parent = parent.parentElement; | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|  | ||||
|     if (chatContainer) { | ||||
|       // 记录滚动前的状态 | ||||
|       const beforeScrollTop = chatContainer.scrollTop; | ||||
|       const maxScrollTop = chatContainer.scrollHeight - chatContainer.clientHeight; | ||||
|       console.log(`WhatsApp: 滚动前状态 - scrollTop: ${beforeScrollTop}, maxScrollTop: ${maxScrollTop}`); | ||||
|  | ||||
|       // 使用最可靠的滚动方法 | ||||
|       const scrollMethods = [ | ||||
|         () => { | ||||
|           // 方法1: 直接设置为最大滚动值 | ||||
|           chatContainer.scrollTop = chatContainer.scrollHeight; | ||||
|           console.log(`WhatsApp: 方法1执行后 scrollTop: ${chatContainer.scrollTop}`); | ||||
|         }, | ||||
|         () => { | ||||
|           // 方法2: 使用scrollTo方法滚动到底部 | ||||
|           chatContainer.scrollTo({ | ||||
|             top: chatContainer.scrollHeight, | ||||
|             behavior: 'auto' | ||||
|           }); | ||||
|           console.log(`WhatsApp: 方法2执行后 scrollTop: ${chatContainer.scrollTop}`); | ||||
|         }, | ||||
|         () => { | ||||
|           // 方法3: 查找最后一条消息并正确滚动到它(修复block参数) | ||||
|           const lastMessage = chatContainer.querySelector("div[role='row']:last-child"); | ||||
|           if (lastMessage) { | ||||
|             // 修复:使用 'end' 确保最后一条消息显示在视窗底部 | ||||
|             lastMessage.scrollIntoView({ | ||||
|               behavior: 'auto', | ||||
|               block: 'end',  // 使用 'end' 让最后一条消息显示在视窗底部 | ||||
|               inline: 'nearest' | ||||
|             }); | ||||
|             console.log(`WhatsApp: 方法3执行后 scrollTop: ${chatContainer.scrollTop}`); | ||||
|           } | ||||
|         } | ||||
|       ]; | ||||
|  | ||||
|       // 执行滚动方法 | ||||
|       scrollMethods.forEach((method, index) => { | ||||
|         try { | ||||
|           method(); | ||||
|           console.log(`WhatsApp: 执行滚动方法 ${index + 1} 完成`); | ||||
|         } catch (error) { | ||||
|           console.warn(`WhatsApp: 滚动方法 ${index + 1} 失败:`, error); | ||||
|         } | ||||
|       }); | ||||
|  | ||||
|       // 最终验证和强制滚动 | ||||
|       setTimeout(() => { | ||||
|         const finalScrollTop = chatContainer.scrollTop; | ||||
|         const finalMaxScrollTop = chatContainer.scrollHeight - chatContainer.clientHeight; | ||||
|         const isAtBottom = finalScrollTop >= (finalMaxScrollTop - 10); | ||||
|  | ||||
|         console.log(`WhatsApp: 滚动完成验证 - scrollTop: ${finalScrollTop}, maxScrollTop: ${finalMaxScrollTop}, 是否在底部: ${isAtBottom}`); | ||||
|  | ||||
|         // 如果还没到底部,执行最终强制滚动 | ||||
|         if (!isAtBottom) { | ||||
|           chatContainer.scrollTop = chatContainer.scrollHeight; | ||||
|           console.log(`WhatsApp: 执行最终强制滚动,新scrollTop: ${chatContainer.scrollTop}`); | ||||
|         } | ||||
|       }, 200); // 增加延迟确保DOM完全更新 | ||||
|  | ||||
|       console.log('WhatsApp: 滚动到聊天底部操作执行完成'); | ||||
|       return true; | ||||
|     } else { | ||||
|       console.warn('WhatsApp: 未找到聊天容器,无法滚动'); | ||||
|       // 尝试输出当前DOM结构信息用于调试 | ||||
|       const mainElement = document.querySelector('#main'); | ||||
|       if (mainElement) { | ||||
|         console.log('WhatsApp: #main元素存在,子元素数量:', mainElement.children.length); | ||||
|         console.log('WhatsApp: #main元素HTML结构:', mainElement.outerHTML.substring(0, 500) + '...'); | ||||
|       } else { | ||||
|         console.log('WhatsApp: #main元素不存在'); | ||||
|       } | ||||
|       return false; | ||||
|     } | ||||
|   } catch (error) { | ||||
|     console.error('WhatsApp: 滚动到底部时出错:', error); | ||||
|     return false; | ||||
|   } | ||||
| }; | ||||
|  | ||||
| const monitorMainNode = () => { | ||||
|   // 监听整个 body 的 DOM 变化,等待 #main 节点的出现 | ||||
|   const observer = new MutationObserver(async (mutationsList, observer) => { | ||||
| @ -1915,13 +2100,15 @@ const initWhatsAppObserver = () => { | ||||
|   }); | ||||
|   console.log(`WhatsApp: 首次加载未读消息总数: ${lastSentTotalUnread}`); | ||||
|  | ||||
|   // 首次加载时立即更新昵称 | ||||
|   setTimeout(() => { | ||||
|     updateUserListNicknames(); | ||||
|   }, 2000); // 延迟2秒确保页面完全加载 | ||||
|  | ||||
|   // 减少频率,避免过度更新,使用新的批量接口 | ||||
|   setInterval(async () => { | ||||
|     const res = await ipc.getContactInfo({ platform: "WhatsApp" }); | ||||
|     if (res.status) { | ||||
|       const data = res.data; | ||||
|       replaceUsername(data); | ||||
|     } | ||||
|   }, 1000); | ||||
|     await updateUserListNicknames(); | ||||
|   }, 5000); // 改为5秒更新一次,减少频率 | ||||
| }; | ||||
|  | ||||
| // 监听whatsapp消息 | ||||
| @ -1929,8 +2116,134 @@ const initWhatsAppObserver = () => { | ||||
| initWhatsAppObserver(); | ||||
| onlineStatusCheck(); | ||||
|  | ||||
| // 替换用户名 | ||||
| // 监听页面刷新/重新加载,重置会话状态 | ||||
| window.addEventListener('beforeunload', () => { | ||||
|   sessionStates.clear(); | ||||
|   isAppFirstLoad = true; | ||||
|   console.log('WhatsApp: 页面重新加载,重置会话状态'); | ||||
| }); | ||||
|  | ||||
| // 监听页面加载完成,确保首次打开时滚动到底部 | ||||
| window.addEventListener('load', () => { | ||||
|   console.log('WhatsApp: 页面加载完成,标记为首次加载状态'); | ||||
|   isAppFirstLoad = true; | ||||
|   sessionStates.clear(); | ||||
| }); | ||||
|  | ||||
| // 从联系人列表项目中提取用户ID | ||||
| function extractUserIdFromListItem(item) { | ||||
|   try { | ||||
|     // 方法1: 从React属性中获取用户ID | ||||
|     for (let key in item) { | ||||
|       if (key.startsWith("__reactProps$")) { | ||||
|         const reactProps = item[key]; | ||||
|         if (reactProps && reactProps.children && reactProps.children.key) { | ||||
|           let userId = reactProps.children.key; | ||||
|           userId = userId.replace(/@.*/, ""); // 移除@后面的部分 | ||||
|           return userId; | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|  | ||||
|     // 方法2: 从data-id属性获取 | ||||
|     const dataId = item.getAttribute("data-id"); | ||||
|     if (dataId) { | ||||
|       return dataId.replace(/@.*/, ""); | ||||
|     } | ||||
|  | ||||
|     // 方法3: 尝试从子元素的属性中获取 | ||||
|     const titleElement = item.querySelector('div[role="gridcell"] span[title]'); | ||||
|     if (titleElement) { | ||||
|       // 检查是否已经存储了用户ID | ||||
|       const storedUserId = titleElement.getAttribute("data-user-id"); | ||||
|       if (storedUserId) { | ||||
|         return storedUserId; | ||||
|       } | ||||
|     } | ||||
|  | ||||
|     return null; | ||||
|   } catch (error) { | ||||
|     console.error('提取用户ID失败:', error); | ||||
|     return null; | ||||
|   } | ||||
| } | ||||
|  | ||||
| // 批量获取并更新用户列表昵称 | ||||
| async function updateUserListNicknames() { | ||||
|   try { | ||||
|     const res = await ipc.getBatchContactNicknames({ | ||||
|       platform: "WhatsApp" | ||||
|     }); | ||||
|  | ||||
|     if (res.status && res.data && res.data.nicknameMap) { | ||||
|       const nicknameMap = res.data.nicknameMap; | ||||
|       console.log('WhatsApp: 获取到昵称配置:', nicknameMap); | ||||
|  | ||||
|       // 定位Whatsapp的用户名元素 | ||||
|       const listItem = document.querySelectorAll( | ||||
|         "div#pane-side div[role='listitem']" | ||||
|       ); | ||||
|  | ||||
|       listItem.forEach((item) => { | ||||
|         const titleElement = item.querySelector('div[role="gridcell"] span[title]'); | ||||
|         if (!titleElement) return; | ||||
|  | ||||
|         const title = titleElement.innerText.replace(/\s*/g, ""); | ||||
|         const originalTitle = titleElement.getAttribute("title") || title; | ||||
|  | ||||
|         // 检查是否已经有临时修改的昵称(优先级最高) | ||||
|         const existingRemark = titleElement.getAttribute("data-remark"); | ||||
|         const existingUserId = titleElement.getAttribute("data-user-id"); | ||||
|  | ||||
|         // 提取当前联系人的用户ID | ||||
|         const currentUserId = extractUserIdFromListItem(item); | ||||
|  | ||||
|         console.log(`联系人: ${title}, 提取的用户ID: ${currentUserId}`); | ||||
|  | ||||
|         // 如果成功提取到用户ID,检查是否有对应的昵称配置 | ||||
|         if (currentUserId && nicknameMap[currentUserId]) { | ||||
|           const nickName = nicknameMap[currentUserId]; | ||||
|  | ||||
|           // 如果已经有临时修改的昵称且用户ID匹配,保持临时昵称不变 | ||||
|           if (existingRemark && existingUserId === currentUserId && titleElement.innerText === existingRemark) { | ||||
|             console.log(`保持临时昵称: ${existingRemark} for ${currentUserId}`); | ||||
|             return; | ||||
|           } | ||||
|  | ||||
|           // 检查是否已经是昵称,避免重复设置 | ||||
|           if (titleElement.innerText !== nickName) { | ||||
|             titleElement.innerText = nickName; | ||||
|             titleElement.style.fontWeight = "bold"; | ||||
|             titleElement.style.color = "#1976d2"; // 使用更好的颜色 | ||||
|             // 保存原始信息到data属性 | ||||
|             titleElement.setAttribute("data-original-name", originalTitle); | ||||
|             titleElement.setAttribute("data-remark", nickName); | ||||
|             titleElement.setAttribute("data-user-id", currentUserId); // 添加userId标识 | ||||
|             console.log(`更新昵称: ${currentUserId} -> ${nickName}`); | ||||
|           } | ||||
|         } else { | ||||
|           // 如果没有找到昵称配置,但有临时昵称,保持临时昵称 | ||||
|           if (existingRemark && existingUserId) { | ||||
|             console.log(`保持临时昵称: ${existingRemark} for ${existingUserId}`); | ||||
|           } else if (currentUserId) { | ||||
|             console.log(`用户 ${currentUserId} 没有配置昵称`); | ||||
|           } | ||||
|         } | ||||
|       }); | ||||
|     } | ||||
|   } catch (error) { | ||||
|     console.error('WhatsApp: 批量更新昵称失败:', error); | ||||
|   } | ||||
| } | ||||
|  | ||||
| // 替换用户名(保留原有函数作为兼容) | ||||
| function replaceUsername(contactInfo = null) { | ||||
|   // 如果没有传入联系人信息,使用新的批量接口 | ||||
|   if (!contactInfo) { | ||||
|     updateUserListNicknames(); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   // 定位Whatsapp的用户名元素 | ||||
|   const listItem = document.querySelectorAll( | ||||
|     "div#pane-side div[role='listitem']" | ||||
| @ -1942,22 +2255,38 @@ function replaceUsername(contactInfo = null) { | ||||
|     const title = titleElement.innerText.replace(/\s*/g, ""); | ||||
|     const originalTitle = titleElement.getAttribute("title") || title; | ||||
|  | ||||
|     // 检查是否已经有临时修改的昵称(优先级最高) | ||||
|     const existingRemark = titleElement.getAttribute("data-remark"); | ||||
|     const existingUserId = titleElement.getAttribute("data-user-id"); | ||||
|  | ||||
|     // 遍历联系人信息,如果联系人信息中包含用户名,则替换用户名 | ||||
|     if (contactInfo) { | ||||
|       contactInfo.forEach((info) => { | ||||
|         const { userId, nickName } = info; | ||||
|         // 更精确的匹配逻辑 | ||||
|         // 更精确的匹配逻辑:确保完全匹配,避免部分匹配导致的错误 | ||||
|         if ( | ||||
|           (title.includes(userId) || originalTitle.includes(userId)) && | ||||
|           userId && | ||||
|           nickName && | ||||
|           nickName.trim() | ||||
|           nickName.trim() && | ||||
|           (title === userId || originalTitle === userId || | ||||
|            title.endsWith(userId) || originalTitle.endsWith(userId)) | ||||
|         ) { | ||||
|           titleElement.innerText = nickName; | ||||
|           titleElement.style.fontWeight = "bold"; | ||||
|           titleElement.style.color = "#1976d2"; // 使用更好的颜色 | ||||
|           // 保存原始信息到data属性 | ||||
|           titleElement.setAttribute("data-original-name", originalTitle); | ||||
|           titleElement.setAttribute("data-remark", nickName); | ||||
|           // 如果已经有临时修改的昵称且用户ID匹配,保持临时昵称不变 | ||||
|           if (existingRemark && existingUserId === userId && titleElement.innerText === existingRemark) { | ||||
|             console.log(`保持临时昵称: ${existingRemark} for ${userId}`); | ||||
|             return; // 跳过更新,保持临时昵称 | ||||
|           } | ||||
|  | ||||
|           // 检查是否已经是昵称,避免重复设置 | ||||
|           if (titleElement.innerText !== nickName) { | ||||
|             titleElement.innerText = nickName; | ||||
|             titleElement.style.fontWeight = "bold"; | ||||
|             titleElement.style.color = "#1976d2"; // 使用更好的颜色 | ||||
|             // 保存原始信息到data属性 | ||||
|             titleElement.setAttribute("data-original-name", originalTitle); | ||||
|             titleElement.setAttribute("data-remark", nickName); | ||||
|             titleElement.setAttribute("data-user-id", userId); // 添加userId标识 | ||||
|           } | ||||
|         } | ||||
|       }); | ||||
|     } | ||||
| @ -1969,10 +2298,29 @@ async function initUserInfoCard(args) { | ||||
|   if (res.status && res.data.length > 0) { | ||||
|     let info = res.data[0]; | ||||
|     const name = document.querySelector('#main header span[dir="auto"]'); | ||||
|     if (name && info.nickName) { | ||||
|       name.innerText = info.nickName; | ||||
|       name.style.fontWeight = "bold"; | ||||
|       name.style.color = "red"; | ||||
|     if (name && info.nickName && info.nickName.trim()) { | ||||
|       // 检查是否有临时修改的昵称 | ||||
|       const currentUserId = getCurrentUserId(); | ||||
|       const currentContactElement = currentNode?.querySelector('div[role="gridcell"] span[title]'); | ||||
|       const tempRemark = currentContactElement?.getAttribute("data-remark"); | ||||
|       const tempUserId = currentContactElement?.getAttribute("data-user-id"); | ||||
|  | ||||
|       // 优先使用临时修改的昵称 | ||||
|       let displayName = info.nickName; | ||||
|       if (tempRemark && tempUserId === currentUserId) { | ||||
|         displayName = tempRemark; | ||||
|         console.log(`使用临时昵称: ${tempRemark} for ${currentUserId}`); | ||||
|       } | ||||
|  | ||||
|       // 避免闪烁:只在昵称不同时才更新 | ||||
|       if (name.innerText !== displayName) { | ||||
|         name.innerText = displayName; | ||||
|         name.style.fontWeight = "bold"; | ||||
|         name.style.color = "red"; | ||||
|         // 保存昵称信息到顶部元素 | ||||
|         name.setAttribute("data-display-name", displayName); | ||||
|         name.setAttribute("data-user-id", currentUserId); | ||||
|       } | ||||
|  | ||||
|       // 检查是否已插入名片图标,避免重复 | ||||
|       if ( | ||||
| @ -2055,15 +2403,30 @@ async function initUserInfoCard(args) { | ||||
|  | ||||
| // 更新联系人昵称,接收主进程消息通知 | ||||
| ipc.onMessageFromMain((newNickName) => { | ||||
|   if (currentNode) { | ||||
|     const listName = currentNode.querySelector('div[role="gridcell"] span'); | ||||
|   if (currentNode && newNickName) { | ||||
|     const currentUserId = getCurrentUserId(); | ||||
|  | ||||
|     // 更精确地选择当前选中联系人的昵称元素 | ||||
|     const listName = currentNode.querySelector('div[role="gridcell"] span[title]'); | ||||
|     if (listName) { | ||||
|       listName.innerText = newNickName; | ||||
|       listName.style.fontWeight = "bold"; | ||||
|       listName.style.color = "#1976d2"; | ||||
|       // 更新data属性,标记为临时修改的昵称 | ||||
|       listName.setAttribute("data-remark", newNickName); | ||||
|       listName.setAttribute("data-user-id", currentUserId); | ||||
|       console.log(`临时更新昵称: ${newNickName} for ${currentUserId}`); | ||||
|     } | ||||
|  | ||||
|     // 更新顶部聊天窗口的昵称 | ||||
|     const cardName = document.querySelector('#main header span[dir="auto"]'); | ||||
|     if (cardName) { | ||||
|       cardName.innerText = newNickName; | ||||
|       cardName.style.fontWeight = "bold"; | ||||
|       cardName.style.color = "red"; | ||||
|       // 保存临时昵称信息 | ||||
|       cardName.setAttribute("data-display-name", newNickName); | ||||
|       cardName.setAttribute("data-user-id", currentUserId); | ||||
|     } | ||||
|   } | ||||
| }); | ||||
|  | ||||
| @ -141,6 +141,58 @@ class ContactInfoService { | ||||
|     const count = await app.sdb.delete('follow_record',{id:id}); | ||||
|     return {status:true,message:'删除成功'}; | ||||
|   } | ||||
|  | ||||
|   // 批量获取联系人昵称配置 | ||||
|   async getBatchContactNicknames(args, event) { | ||||
|     const { platform, userIds } = args; | ||||
|  | ||||
|     if (!platform?.trim()) { | ||||
|       return { status: false, message: '平台参数不能为空' }; | ||||
|     } | ||||
|  | ||||
|     try { | ||||
|       let contactInfos = []; | ||||
|  | ||||
|       if (userIds && Array.isArray(userIds) && userIds.length > 0) { | ||||
|         // 如果提供了用户ID列表,只查询这些用户 | ||||
|         const placeholders = userIds.map(() => '?').join(','); | ||||
|         const sql = `SELECT userId, nickName FROM contact_info WHERE platform = ? AND userId IN (${placeholders}) AND nickName IS NOT NULL AND nickName != ''`; | ||||
|         const params = [platform, ...userIds]; | ||||
|         contactInfos = await app.sdb.query(sql, params); | ||||
|       } else { | ||||
|         // 如果没有提供用户ID列表,查询该平台所有有昵称的联系人 | ||||
|         contactInfos = await app.sdb.select('contact_info', { | ||||
|           platform: platform | ||||
|         }, { | ||||
|           columns: ['userId', 'nickName'], | ||||
|           where: 'nickName IS NOT NULL AND nickName != ""' | ||||
|         }); | ||||
|       } | ||||
|  | ||||
|       // 转换为更方便使用的格式 | ||||
|       const nicknameMap = {}; | ||||
|       contactInfos.forEach(info => { | ||||
|         if (info.nickName && info.nickName.trim()) { | ||||
|           nicknameMap[info.userId] = info.nickName; | ||||
|         } | ||||
|       }); | ||||
|  | ||||
|       return { | ||||
|         status: true, | ||||
|         message: '查询成功', | ||||
|         data: { | ||||
|           nicknameMap: nicknameMap, | ||||
|           contactInfos: contactInfos | ||||
|         } | ||||
|       }; | ||||
|     } catch (error) { | ||||
|       console.error('批量获取联系人昵称失败:', error); | ||||
|       return { | ||||
|         status: false, | ||||
|         message: `查询失败: ${error.message}` | ||||
|       }; | ||||
|     } | ||||
|   } | ||||
| } | ||||
| ContactInfoService.toString = () => '[class ContactInfoService]'; | ||||
|  | ||||
|  | ||||
							
								
								
									
										345
									
								
								electron/service/httpQuickReply.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										345
									
								
								electron/service/httpQuickReply.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,345 @@ | ||||
| 'use strict'; | ||||
| const { logger } = require('ee-core/log'); | ||||
| const axios = require('axios'); | ||||
| const { app } = require('electron'); | ||||
|  | ||||
| class HttpQuickReplyService { | ||||
|   constructor() { | ||||
|     // 从配置中获取API基础URL | ||||
|     const config = app.config || {}; | ||||
|     this.baseUrl = config.api?.baseUrl || 'http://127.0.0.1:8000/api'; | ||||
|     this.timeout = config.api?.timeout || 30000; | ||||
|      | ||||
|     // 创建axios实例 | ||||
|     this.client = axios.create({ | ||||
|       baseURL: this.baseUrl, | ||||
|       timeout: this.timeout, | ||||
|       headers: { | ||||
|         'Content-Type': 'application/json' | ||||
|       } | ||||
|     }); | ||||
|  | ||||
|     // 添加请求拦截器 | ||||
|     this.client.interceptors.request.use( | ||||
|       (config) => { | ||||
|         // 这里可以添加认证token等 | ||||
|         // if (token) { | ||||
|         //   config.headers.Authorization = `Bearer ${token}`; | ||||
|         // } | ||||
|         logger.info(`HTTP请求: ${config.method?.toUpperCase()} ${config.url}`); | ||||
|         return config; | ||||
|       }, | ||||
|       (error) => { | ||||
|         logger.error('HTTP请求错误:', error); | ||||
|         return Promise.reject(error); | ||||
|       } | ||||
|     ); | ||||
|  | ||||
|     // 添加响应拦截器 | ||||
|     this.client.interceptors.response.use( | ||||
|       (response) => { | ||||
|         logger.info(`HTTP响应: ${response.status} ${response.config.url}`); | ||||
|         return response; | ||||
|       }, | ||||
|       (error) => { | ||||
|         logger.error('HTTP响应错误:', error.message); | ||||
|         return Promise.reject(error); | ||||
|       } | ||||
|     ); | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 获取快捷回复分组列表 | ||||
|    */ | ||||
|   async getGroups(args, event) { | ||||
|     try { | ||||
|       const response = await this.client.get('/QuickReplyGroupModelViewSet/groups_with_contents/'); | ||||
|       if (response.data && response.data.status) { | ||||
|         return { | ||||
|           status: true, | ||||
|           message: '查询成功', | ||||
|           data: response.data.data || [] | ||||
|         }; | ||||
|       } | ||||
|       return { | ||||
|         status: false, | ||||
|         message: response.data?.message || '获取分组失败' | ||||
|       }; | ||||
|     } catch (error) { | ||||
|       logger.error('获取快捷回复分组失败:', error); | ||||
|       return { | ||||
|         status: false, | ||||
|         message: `网络错误:${error.message}` | ||||
|       }; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 根据分组ID获取快捷回复内容 | ||||
|    */ | ||||
|   async getContentByGroupId(args, event) { | ||||
|     try { | ||||
|       const { groupId } = args; | ||||
|       if (!groupId) { | ||||
|         return { status: false, message: '分组ID不能为空' }; | ||||
|       } | ||||
|  | ||||
|       const response = await this.client.get(`/QuickReplyModelViewSet/?group_id=${groupId}`); | ||||
|       if (response.data && response.data.status) { | ||||
|         return { | ||||
|           status: true, | ||||
|           message: '查询成功', | ||||
|           data: response.data.data || [] | ||||
|         }; | ||||
|       } | ||||
|       return { | ||||
|         status: false, | ||||
|         message: response.data?.message || '获取内容失败' | ||||
|       }; | ||||
|     } catch (error) { | ||||
|       logger.error('获取快捷回复内容失败:', error); | ||||
|       return { | ||||
|         status: false, | ||||
|         message: `网络错误:${error.message}` | ||||
|       }; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 添加快捷回复分组 | ||||
|    */ | ||||
|   async addGroup(args, event) { | ||||
|     try { | ||||
|       const { name } = args; | ||||
|       if (!name) { | ||||
|         return { status: false, message: '分组名称不能为空' }; | ||||
|       } | ||||
|  | ||||
|       const response = await this.client.post('/QuickReplyGroupModelViewSet/', { | ||||
|         name: name, | ||||
|         sort_order: 0 | ||||
|       }); | ||||
|  | ||||
|       if (response.status === 201) { | ||||
|         return { | ||||
|           status: true, | ||||
|           message: '新增成功', | ||||
|           data: response.data | ||||
|         }; | ||||
|       } | ||||
|       return { | ||||
|         status: false, | ||||
|         message: response.data?.message || '添加分组失败' | ||||
|       }; | ||||
|     } catch (error) { | ||||
|       logger.error('添加快捷回复分组失败:', error); | ||||
|       return { | ||||
|         status: false, | ||||
|         message: `网络错误:${error.message}` | ||||
|       }; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 编辑快捷回复分组 | ||||
|    */ | ||||
|   async editGroup(args, event) { | ||||
|     try { | ||||
|       const { id, name } = args; | ||||
|       if (!id || !name) { | ||||
|         return { status: false, message: '分组ID和名称不能为空' }; | ||||
|       } | ||||
|  | ||||
|       const response = await this.client.put(`/QuickReplyGroupModelViewSet/${id}/`, { | ||||
|         name: name | ||||
|       }); | ||||
|  | ||||
|       if (response.status === 200) { | ||||
|         return { | ||||
|           status: true, | ||||
|           message: '修改成功', | ||||
|           data: response.data | ||||
|         }; | ||||
|       } | ||||
|       return { | ||||
|         status: false, | ||||
|         message: response.data?.message || '修改分组失败' | ||||
|       }; | ||||
|     } catch (error) { | ||||
|       logger.error('修改快捷回复分组失败:', error); | ||||
|       return { | ||||
|         status: false, | ||||
|         message: `网络错误:${error.message}` | ||||
|       }; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 删除快捷回复分组 | ||||
|    */ | ||||
|   async deleteGroup(args, event) { | ||||
|     try { | ||||
|       const { id } = args; | ||||
|       if (!id) { | ||||
|         return { status: false, message: '分组ID不能为空' }; | ||||
|       } | ||||
|  | ||||
|       const response = await this.client.delete(`/QuickReplyGroupModelViewSet/${id}/`); | ||||
|       if (response.status === 204 || response.status === 200) { | ||||
|         return { | ||||
|           status: true, | ||||
|           message: '删除成功' | ||||
|         }; | ||||
|       } | ||||
|       return { | ||||
|         status: false, | ||||
|         message: response.data?.message || '删除分组失败' | ||||
|       }; | ||||
|     } catch (error) { | ||||
|       logger.error('删除快捷回复分组失败:', error); | ||||
|       return { | ||||
|         status: false, | ||||
|         message: `网络错误:${error.message}` | ||||
|       }; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 添加快捷回复 | ||||
|    */ | ||||
|   async addReply(args, event) { | ||||
|     try { | ||||
|       const { remark, content, url, type, groupId } = args; | ||||
|       if (!groupId) { | ||||
|         return { status: false, message: '分组ID不能为空' }; | ||||
|       } | ||||
|  | ||||
|       const response = await this.client.post('/QuickReplyModelViewSet/', { | ||||
|         group: groupId, | ||||
|         remark: remark || '', | ||||
|         content: content || '', | ||||
|         type: type || 'text', | ||||
|         url: url || '', | ||||
|         sort_order: 0 | ||||
|       }); | ||||
|  | ||||
|       if (response.status === 201) { | ||||
|         return { | ||||
|           status: true, | ||||
|           message: '新增成功', | ||||
|           data: response.data | ||||
|         }; | ||||
|       } | ||||
|       return { | ||||
|         status: false, | ||||
|         message: response.data?.message || '添加快捷回复失败' | ||||
|       }; | ||||
|     } catch (error) { | ||||
|       logger.error('添加快捷回复失败:', error); | ||||
|       return { | ||||
|         status: false, | ||||
|         message: `网络错误:${error.message}` | ||||
|       }; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 编辑快捷回复 | ||||
|    */ | ||||
|   async editReply(args, event) { | ||||
|     try { | ||||
|       const { id, remark, content, url, type, groupId } = args; | ||||
|       if (!id) { | ||||
|         return { status: false, message: '快捷回复ID不能为空' }; | ||||
|       } | ||||
|  | ||||
|       const response = await this.client.put(`/QuickReplyModelViewSet/${id}/`, { | ||||
|         group: groupId, | ||||
|         remark: remark || '', | ||||
|         content: content || '', | ||||
|         type: type || 'text', | ||||
|         url: url || '' | ||||
|       }); | ||||
|  | ||||
|       if (response.status === 200) { | ||||
|         return { | ||||
|           status: true, | ||||
|           message: '修改成功', | ||||
|           data: response.data | ||||
|         }; | ||||
|       } | ||||
|       return { | ||||
|         status: false, | ||||
|         message: response.data?.message || '修改快捷回复失败' | ||||
|       }; | ||||
|     } catch (error) { | ||||
|       logger.error('修改快捷回复失败:', error); | ||||
|       return { | ||||
|         status: false, | ||||
|         message: `网络错误:${error.message}` | ||||
|       }; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 删除快捷回复 | ||||
|    */ | ||||
|   async deleteReply(args, event) { | ||||
|     try { | ||||
|       const { id } = args; | ||||
|       if (!id) { | ||||
|         return { status: false, message: '快捷回复ID不能为空' }; | ||||
|       } | ||||
|  | ||||
|       const response = await this.client.delete(`/QuickReplyModelViewSet/${id}/`); | ||||
|       if (response.status === 204 || response.status === 200) { | ||||
|         return { | ||||
|           status: true, | ||||
|           message: '删除成功' | ||||
|         }; | ||||
|       } | ||||
|       return { | ||||
|         status: false, | ||||
|         message: response.data?.message || '删除快捷回复失败' | ||||
|       }; | ||||
|     } catch (error) { | ||||
|       logger.error('删除快捷回复失败:', error); | ||||
|       return { | ||||
|         status: false, | ||||
|         message: `网络错误:${error.message}` | ||||
|       }; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * 删除分组下所有快捷回复 | ||||
|    */ | ||||
|   async deleteAllReply(args, event) { | ||||
|     try { | ||||
|       const { groupId } = args; | ||||
|       if (!groupId) { | ||||
|         return { status: false, message: '分组ID不能为空' }; | ||||
|       } | ||||
|  | ||||
|       const response = await this.client.delete(`/QuickReplyGroupModelViewSet/${groupId}/clear_contents/`); | ||||
|       if (response.status === 200) { | ||||
|         return { | ||||
|           status: true, | ||||
|           message: response.data?.message || '清空成功' | ||||
|         }; | ||||
|       } | ||||
|       return { | ||||
|         status: false, | ||||
|         message: response.data?.message || '清空失败' | ||||
|       }; | ||||
|     } catch (error) { | ||||
|       logger.error('清空快捷回复失败:', error); | ||||
|       return { | ||||
|         status: false, | ||||
|         message: `网络错误:${error.message}` | ||||
|       }; | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
| module.exports = HttpQuickReplyService; | ||||
| @ -4,18 +4,24 @@ const { app, BrowserWindow } = require('electron') | ||||
| class QuickReplyService { | ||||
|  | ||||
|   async getGroups(args,event) { | ||||
|     logger.info('本地SQLite:开始获取快捷回复分组'); | ||||
|     const groups = await app.sdb.select('group_manage'); | ||||
|     logger.info(`本地SQLite:找到 ${groups.length} 个分组`); | ||||
|     for (let group of groups) { | ||||
|       const records = await app.sdb.select('quick_reply_record',{groupId:group.id}) | ||||
|       // 确保groupId类型匹配,将数字转换为字符串 | ||||
|       const records = await app.sdb.select('quick_reply_record',{groupId:String(group.id)}) | ||||
|       group.contents = records; | ||||
|       group.contentCount = records.length; | ||||
|       logger.info(`本地SQLite:分组 ${group.name}(ID:${group.id}) 包含 ${records.length} 条快捷回复`); | ||||
|     } | ||||
|     logger.info('本地SQLite:快捷回复分组查询完成'); | ||||
|     return {status:true,message:'查询成功',data:groups}; | ||||
|   } | ||||
|  | ||||
|   async getContentByGroupId(args,event) { | ||||
|     const {groupId} = args; | ||||
|     const records = await app.sdb.select('quick_reply_record',{groupId:groupId}) | ||||
|     // 确保groupId类型匹配,将其转换为字符串 | ||||
|     const records = await app.sdb.select('quick_reply_record',{groupId:String(groupId)}) | ||||
|     return {status:true,message:'查询成功',data:records}; | ||||
|   } | ||||
|  | ||||
| @ -34,7 +40,8 @@ class QuickReplyService { | ||||
|   async deleteGroup(args,event) { | ||||
|     const {id} = args; | ||||
|     await app.sdb.delete('group_manage',{id:id}) | ||||
|     await app.sdb.delete('quick_reply_record',{groupId:id}) | ||||
|     // 确保groupId类型匹配,将其转换为字符串 | ||||
|     await app.sdb.delete('quick_reply_record',{groupId:String(id)}) | ||||
|     return {status:true,message:'删除成功'}; | ||||
|   } | ||||
|  | ||||
|  | ||||
| @ -77,6 +77,9 @@ const ipcApiRoute = { | ||||
|   // 修改联系人备注,同步更新页面 | ||||
|   updateContactRemark: 'controller/contactInfo/updateContactRemark', | ||||
|  | ||||
|   // 批量获取联系人昵称配置 | ||||
|   getBatchContactNicknames: 'controller/contactInfo/getBatchContactNicknames', | ||||
|  | ||||
|   //快捷回复相关 | ||||
|   getGroups: 'controller/quickreply/getGroups', | ||||
|   getContentByGroupId: 'controller/quickreply/getContentByGroupId', | ||||
|  | ||||
| @ -269,7 +269,9 @@ export default { | ||||
|     tooltipContent: 'Click to translate in input box\nDouble click to send original text', | ||||
|     searchPlaceholder: 'Filter by title or content', | ||||
|     noData: 'No Data', | ||||
|     send: 'Send' | ||||
|     send: 'Send', | ||||
|     fillInput: 'Fill Input', | ||||
|     addReply: 'Add Quick Reply' | ||||
|   }, | ||||
|   userInfo: { | ||||
|     title: 'Contact Information', | ||||
|  | ||||
| @ -229,7 +229,9 @@ export default { | ||||
|     tooltipContent: 'ចុចដើម្បីបកប្រែក្នុងប្រអប់បញ្ចូល\nចុចទ្វេដងដើម្បីផ្ញើអត្ថបទដើម', | ||||
|     searchPlaceholder: 'ត្រងតាមចំណងជើងឬមាតិកា', | ||||
|     noData: 'គ្មានទិន្នន័យ', | ||||
|     send: 'ផ្ញើ' | ||||
|     send: 'ផ្ញើ', | ||||
|     fillInput: 'បំពេញប្រអប់បញ្ចូល', | ||||
|     addReply: 'បន្ថែមឆ្លើយតបរហ័ស' | ||||
|   }, | ||||
|   userInfo: { | ||||
|     title: 'ព័ត៌មានទំនាក់ទំនង', | ||||
|  | ||||
| @ -264,7 +264,9 @@ export default { | ||||
|     tooltipContent: '单击到输入框进行翻译\n双击按钮发送原文', | ||||
|     searchPlaceholder: '请输入标题或者关键内容过滤', | ||||
|     noData: '暂无数据', | ||||
|     send: '发送' | ||||
|     send: '发送', | ||||
|     fillInput: '输入框提示', | ||||
|     addReply: '添加快捷回复' | ||||
|   }, | ||||
|   userInfo: { | ||||
|     title: '联系人信息', | ||||
|  | ||||
| @ -164,10 +164,6 @@ | ||||
|                     :value="child?.msgCount"></el-badge> | ||||
|                 </div> | ||||
|               </div> | ||||
|               <div v-if="isCollapse" class="child-menu-item-icon margin-top-10"> | ||||
|                 <el-badge v-if="child?.msgCount > 0" :offset="[-15, 0]" type="error" :show-zero="false" :max="99" | ||||
|                   :value="child?.msgCount"></el-badge> | ||||
|               </div> | ||||
|             </div> | ||||
|           </template> | ||||
|         </div> | ||||
| @ -391,6 +387,9 @@ const filteredChildren = computed(() => (children, menuId) => { | ||||
| const isActive = (menuId) => menuStore.currentMenu === menuId | ||||
|  | ||||
| const hasActiveChild = (menu) => { | ||||
|   if (!menu || !menu.children || !Array.isArray(menu.children)) { | ||||
|     return false | ||||
|   } | ||||
|   const nMenus = menu.children.filter( | ||||
|     child => child.windowStatus === 'true' | ||||
|   ) || [] | ||||
| @ -398,6 +397,9 @@ const hasActiveChild = (menu) => { | ||||
| } | ||||
|  | ||||
| const hasChildren = (menu) => { | ||||
|   if (!menu || !menu.children || !Array.isArray(menu.children)) { | ||||
|     return false | ||||
|   } | ||||
|   const nMenus = menu.children.filter( | ||||
|     child => child.windowStatus === 'true' | ||||
|   ) || [] | ||||
| @ -513,10 +515,17 @@ const networkErrorCount = ref(0) // 新增网络错误计数 | ||||
| const checkLogin = async () => { | ||||
|   const authKey = menuStore.userInfo?.authKey | ||||
|   if (authKey) { | ||||
|     const res = await ipc.invoke(ipcApiRoute.login, { authKey }) | ||||
|     try { | ||||
|       const res = await ipc.invoke(ipcApiRoute.login, { authKey }) | ||||
|  | ||||
|     // 处理网络错误的特殊情况 | ||||
|     if (!res.status && res.message === 'login.errors.networkError') { | ||||
|       // 检查响应是否有效 | ||||
|       if (!res) { | ||||
|         console.warn('登录检查返回空响应') | ||||
|         return | ||||
|       } | ||||
|  | ||||
|       // 处理网络错误的特殊情况 | ||||
|       if (!res.status && res.message === 'login.errors.networkError') { | ||||
|       networkErrorCount.value++ | ||||
|       // 只有当网络错误超过3次才执行登出逻辑 | ||||
|       if (networkErrorCount.value >= 3) { | ||||
| @ -539,6 +548,10 @@ const checkLogin = async () => { | ||||
|       menuStore.setUserInfo(res.data) | ||||
|     } | ||||
|     console.log('check user auth:', res.data) | ||||
|     } catch (error) { | ||||
|       console.error('登录检查出错:', error) | ||||
|       // 网络错误或其他异常,不立即登出,等待下次检查 | ||||
|     } | ||||
|   } else { | ||||
|     clearTimer() | ||||
|     await ipc.invoke(ipcApiRoute.hiddenSession, {}) | ||||
| @ -595,6 +608,15 @@ onMounted(async () => { | ||||
|   ipc.removeAllListeners('msg-count-notify') | ||||
|   ipc.on('msg-count-notify', handleMsgCountNotify) | ||||
|  | ||||
|   // 处理快捷回复配置页面导航 | ||||
|   const handleNavigateToQuickReplyConfig = () => { | ||||
|     // 切换到快捷回复菜单 | ||||
|     toggleBottomMenu('QuickReply') | ||||
|   } | ||||
|  | ||||
|   ipc.removeAllListeners('navigate-to-quick-reply-config') | ||||
|   ipc.on('navigate-to-quick-reply-config', handleNavigateToQuickReplyConfig) | ||||
|  | ||||
|   initMenuSessions() | ||||
|  | ||||
|   clearTimer() | ||||
| @ -603,7 +625,7 @@ onMounted(async () => { | ||||
|   // 检查后端配置是否允许显示翻译配置菜单 | ||||
|   try { | ||||
|     const res = await ipc.invoke(ipcApiRoute.getSystemConfig, { configKey: 'base.allow_translate_config' }) | ||||
|     showTranslateConfig.value = res.status && res.data === 'true' | ||||
|     showTranslateConfig.value = res && res.status && res.data === 'true' | ||||
|   } catch (e) { | ||||
|     showTranslateConfig.value = false | ||||
|   } | ||||
| @ -611,7 +633,7 @@ onMounted(async () => { | ||||
|   // 检查后端配置是否允许显示 TikTok 菜单 | ||||
|   try { | ||||
|     const res = await ipc.invoke(ipcApiRoute.getSystemConfig, { configKey: 'base.allow_tiktok' }) | ||||
|     showTikTokMenu.value = res.status && res.data === 'true' | ||||
|     showTikTokMenu.value = res && res.status && res.data === 'true' | ||||
|   } catch (e) { | ||||
|     showTikTokMenu.value = false | ||||
|   } | ||||
| @ -623,10 +645,14 @@ onUnmounted(() => { | ||||
|  | ||||
| const initMenuSessions = async () => { | ||||
|   for (let menu of menuStore.menus) { | ||||
|     const res = await ipc.invoke(ipcApiRoute.getSessions, { platform: menu.id }) | ||||
|     if (res.status) { | ||||
|       const arr = res.data.sessions | ||||
|       menuStore.setMenuChildren(menu.id, arr) | ||||
|     try { | ||||
|       const res = await ipc.invoke(ipcApiRoute.getSessions, { platform: menu.id }) | ||||
|       if (res && res.status) { | ||||
|         const arr = res.data.sessions | ||||
|         menuStore.setMenuChildren(menu.id, arr) | ||||
|       } | ||||
|     } catch (error) { | ||||
|       console.error(`获取菜单 ${menu.id} 的会话失败:`, error) | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @ -666,17 +692,27 @@ const getIsMenuShow = (menuId) => { | ||||
|  | ||||
| const startSession = async (child) => { | ||||
|   startingSessionId.value = child.partitionId | ||||
|   const res = await ipc.invoke(ipcApiRoute.startSession, { platform: child.platform, partitionId: child.partitionId }) | ||||
|   if (res.status) { | ||||
|     menuStore.updateChildrenMenu(res.data) | ||||
|   } else { | ||||
|   try { | ||||
|     const res = await ipc.invoke(ipcApiRoute.startSession, { platform: child.platform, partitionId: child.partitionId }) | ||||
|     if (res && res.status) { | ||||
|       menuStore.updateChildrenMenu(res.data) | ||||
|     } else { | ||||
|       ElMessage({ | ||||
|         message: `${res?.message || '启动会话失败'}`, | ||||
|         type: 'error', | ||||
|         offset: 40 | ||||
|       }) | ||||
|     } | ||||
|   } catch (error) { | ||||
|     console.error('启动会话出错:', error) | ||||
|     ElMessage({ | ||||
|       message: `${res.message}`, | ||||
|       message: '启动会话失败,请重试', | ||||
|       type: 'error', | ||||
|       offset: 40 | ||||
|     }) | ||||
|   } finally { | ||||
|     startingSessionId.value = null | ||||
|   } | ||||
|   startingSessionId.value = null | ||||
| } | ||||
|  | ||||
| </script> | ||||
|  | ||||
| @ -152,9 +152,13 @@ const currentGroupId = ref(0); | ||||
| const groups = ref([]) | ||||
| const tableData = ref([]) | ||||
| const initData = async () => { | ||||
|   console.log('开始初始化快捷回复数据') | ||||
|   const res = await ipc.invoke(ipcApiRoute.getGroups,{}) | ||||
|   console.log('获取分组结果:', res) | ||||
|   if (res.status) { | ||||
|     groups.value = res.data | ||||
|   } else { | ||||
|     console.error('获取快捷回复分组失败:', res.message) | ||||
|   } | ||||
|   await getTableData(); | ||||
| } | ||||
| @ -405,10 +409,12 @@ const handleDeleteReply = async (row)=>{ | ||||
|   } | ||||
| } | ||||
| onMounted(async () => { | ||||
|   console.log('快捷回复组件已挂载') | ||||
|   await initData() | ||||
|   if (currentGroupId.value === 0 && groups.value.length > 0) { | ||||
|     currentGroupId.value = groups.value[0].id; | ||||
|   } | ||||
|   console.log('快捷回复组件初始化完成,分组数量:', groups.value.length) | ||||
| }) | ||||
| watch( | ||||
|     () => currentGroupId.value, | ||||
|  | ||||
| @ -13,6 +13,15 @@ | ||||
|           <el-icon><QuestionFilled /></el-icon> | ||||
|         </el-tooltip> | ||||
|       </div> | ||||
|       <div class="header-actions"> | ||||
|         <el-button | ||||
|             size="small" | ||||
|             type="primary" | ||||
|             @click="handleAddQuickReply" | ||||
|             :icon="Plus"> | ||||
|           {{ t('quickReply.addReply') }} | ||||
|         </el-button> | ||||
|       </div> | ||||
|     </div> | ||||
|     <div class="header-search"> | ||||
|       <el-input  | ||||
| @ -63,9 +72,14 @@ | ||||
|                     </el-text> | ||||
|                   </div> | ||||
|                   <div class="right"> | ||||
|                     <el-button  | ||||
|                         size="small"   | ||||
|                         @click.stop="handleSend(record)"  | ||||
|                     <el-button | ||||
|                         size="small" | ||||
|                         @click.stop="handleFillInput(record)" | ||||
|                         plain>{{ t('quickReply.fillInput') }}</el-button> | ||||
|                     <el-button | ||||
|                         size="small" | ||||
|                         @click.stop="handleSend(record)" | ||||
|                         type="primary" | ||||
|                         plain>{{ t('quickReply.send') }}</el-button> | ||||
|                   </div> | ||||
|                 </div> | ||||
| @ -96,7 +110,7 @@ | ||||
|   </div> | ||||
| </template> | ||||
| <script setup> | ||||
| import {ArrowDown, ArrowRight, CaretBottom, CaretTop, QuestionFilled, Search} from "@element-plus/icons-vue"; | ||||
| import {ArrowDown, ArrowRight, CaretBottom, CaretTop, QuestionFilled, Search, Plus} from "@element-plus/icons-vue"; | ||||
| import {computed, onMounted, ref} from "vue"; | ||||
| import {ipc} from "@/utils/ipcRenderer"; | ||||
| import {ipcApiRoute} from "@/api"; | ||||
| @ -110,9 +124,20 @@ onMounted(async () => { | ||||
| }) | ||||
| const groups = ref([]); | ||||
| const initData = async () => { | ||||
|   const res = await ipc.invoke(ipcApiRoute.getGroups, {}) | ||||
|   if (res.status) { | ||||
|     groups.value = res.data | ||||
|   try { | ||||
|     console.log('右侧栏快捷回复:开始获取分组数据') | ||||
|     const res = await ipc.invoke(ipcApiRoute.getGroups, {}) | ||||
|     console.log('右侧栏快捷回复:获取分组结果:', res) | ||||
|     if (res && res.status) { | ||||
|       groups.value = res.data || [] | ||||
|       console.log('右侧栏快捷回复:成功获取分组数量:', groups.value.length) | ||||
|     } else { | ||||
|       console.warn('获取快捷回复分组失败:', res?.message || '未知错误') | ||||
|       groups.value = [] | ||||
|     } | ||||
|   } catch (error) { | ||||
|     console.error('获取快捷回复分组出错:', error) | ||||
|     groups.value = [] | ||||
|   } | ||||
| } | ||||
| const searchKey = ref('') | ||||
| @ -149,6 +174,29 @@ const handleSend = (record) => { | ||||
|   } | ||||
|   ipc.invoke('send-msg',args) | ||||
| } | ||||
|  | ||||
| const handleFillInput = (record) => { | ||||
|   const args = { | ||||
|     text: record.content, | ||||
|     partitionId: menuStore.currentPartitionId, | ||||
|     type:'input', | ||||
|   } | ||||
|   ipc.invoke('send-msg',args) | ||||
| } | ||||
|  | ||||
| const handleAddQuickReply = async () => { | ||||
|   try { | ||||
|     // 打开快捷回复配置页面 | ||||
|     const result = await ipc.invoke('open-quick-reply-config') | ||||
|     if (result && !result.status) { | ||||
|       console.warn('打开快捷回复配置失败:', result.message) | ||||
|     } | ||||
|   } catch (error) { | ||||
|     console.error('打开快捷回复配置出错:', error) | ||||
|     // 如果IPC调用失败,可以考虑其他方式,比如路由跳转 | ||||
|     // router.push('/quick-reply-config') | ||||
|   } | ||||
| } | ||||
| </script> | ||||
| <style scoped lang="less"> | ||||
| .border-top { | ||||
| @ -176,22 +224,26 @@ const handleSend = (record) => { | ||||
|   } | ||||
|   .header-container { | ||||
|     width: 100%; | ||||
|     height: 30px; | ||||
|     min-height: 30px; | ||||
|     display: flex; | ||||
|     align-items: center; | ||||
|     flex-direction: column; | ||||
|     margin-bottom: 20px; | ||||
|     user-select: none; | ||||
|     justify-content: flex-start; | ||||
|     gap: 10px; | ||||
|     :deep(.el-text) { | ||||
|       --el-text-color: var(--el-text-color-primary); | ||||
|     } | ||||
|     .header-title { | ||||
|       display: flex; | ||||
|       align-items: center; | ||||
|       flex:1; | ||||
|       gap: 5px; | ||||
|       height: 30px; | ||||
|     } | ||||
|     .header-actions { | ||||
|       display: flex; | ||||
|       justify-content: flex-end; | ||||
|       width: 100%; | ||||
|     } | ||||
|   } | ||||
|   .header-search { | ||||
|     width: 100%; | ||||
|  | ||||
| @ -171,15 +171,6 @@ const getTimeStr = (date = new Date())=> { | ||||
| } | ||||
| const userInfo = ref({}) | ||||
|  | ||||
| watch( | ||||
|     () => menuStore.currentPartitionId, | ||||
|     async (newValue, oldValue) => { | ||||
|       if (newValue) { | ||||
|         await getUserInfo() | ||||
|       } | ||||
|     } | ||||
| ); | ||||
|  | ||||
| const hasUserId = computed(()=>{ | ||||
|   return !userInfo.value.id; | ||||
| }) | ||||
| @ -206,38 +197,62 @@ let watchers = []; // 存储所有字段的监听器 | ||||
| // 初始化字段监听逻辑 | ||||
| const addWatchers = ()=> { | ||||
|   removeWatchers(); // 确保不会重复绑定监听器 | ||||
|   watchers = propertiesToWatch.map((property) => | ||||
|       watch( | ||||
|           () => unref(userInfo.value[property]), | ||||
|           (newValue, oldValue) => { | ||||
|             if (newValue !== "" && newValue !== oldValue) { | ||||
|               handlePropertyChange(property, newValue); | ||||
|             } | ||||
|           } | ||||
|       ) | ||||
|   ); | ||||
|  | ||||
|   // 只有当userInfo有有效数据时才添加监听器 | ||||
|   if (userInfo.value && Object.keys(userInfo.value).length > 0) { | ||||
|     watchers = propertiesToWatch.map((property) => | ||||
|         watch( | ||||
|             () => unref(userInfo.value[property]), | ||||
|             (newValue, oldValue) => { | ||||
|               if (newValue !== "" && newValue !== oldValue) { | ||||
|                 handlePropertyChange(property, newValue); | ||||
|               } | ||||
|             }, | ||||
|             { immediate: false } // 不立即执行,避免初始化时触发 | ||||
|         ) | ||||
|     ); | ||||
|   } | ||||
| } | ||||
| // 自定义逻辑 | ||||
| const handlePropertyChange = async (property, value) => { | ||||
|   if (userInfo && userInfo.value?.id){ | ||||
|     const args = {key: property, value: value, id: userInfo.value.id}; | ||||
|     await ipc.invoke(ipcApiRoute.updateContactInfo, args); | ||||
|     if (property === 'nickName') { | ||||
|       // 更新昵称时调用 updateContactRemark | ||||
|       const args = {partitionId: menuStore.currentPartitionId, nickName: value}; | ||||
|       await ipc.invoke(ipcApiRoute.updateContactRemark, args); | ||||
|   // 确保有有效的用户信息和ID | ||||
|   if (userInfo && userInfo.value?.id && menuStore.currentPartitionId){ | ||||
|     try { | ||||
|       const args = {key: property, value: value, id: userInfo.value.id}; | ||||
|       const result = await ipc.invoke(ipcApiRoute.updateContactInfo, args); | ||||
|  | ||||
|       if (result.status && property === 'nickName') { | ||||
|         // 更新昵称时调用 updateContactRemark | ||||
|         const remarkArgs = {partitionId: menuStore.currentPartitionId, nickName: value}; | ||||
|         await ipc.invoke(ipcApiRoute.updateContactRemark, remarkArgs); | ||||
|       } | ||||
|     } catch (error) { | ||||
|       console.error('更新联系人信息失败:', error); | ||||
|     } | ||||
|   } | ||||
| } | ||||
| // 移除所有字段的监听器 | ||||
| const removeWatchers = ()=> { | ||||
|   watchers.forEach((stopWatcher) => stopWatcher()); // 调用每个监听器的停止方法 | ||||
|   watchers = []; | ||||
|   if (watchers && watchers.length > 0) { | ||||
|     watchers.forEach((stopWatcher) => { | ||||
|       if (typeof stopWatcher === 'function') { | ||||
|         stopWatcher(); // 调用每个监听器的停止方法 | ||||
|       } | ||||
|     }); | ||||
|     watchers = []; | ||||
|   } | ||||
| } | ||||
| const followRecordArr = ref([]) | ||||
|  | ||||
| const getUserInfo = async () => { | ||||
|   removeWatchers() | ||||
|   try { | ||||
|     // 确保有有效的平台和分区ID | ||||
|     if (!menuStore.platform || !menuStore.currentPartitionId) { | ||||
|       console.warn('缺少必要的平台或分区ID信息'); | ||||
|       return; | ||||
|     } | ||||
|  | ||||
|     const args = { | ||||
|       platform: menuStore.platform, | ||||
|       userId: '', | ||||
| @ -245,14 +260,25 @@ const getUserInfo = async () => { | ||||
|     } | ||||
|     const res = await ipc.invoke(ipcApiRoute.getContactInfo, args); | ||||
|     if (res.status) { | ||||
|       Object.assign(userInfo.value, res.data.userInfo); // 更新表单数据 | ||||
|       Object.assign(followRecordArr.value, res.data.records); // 更新表单数据 | ||||
|       // 清空现有数据,避免数据混合 | ||||
|       userInfo.value = {}; | ||||
|       followRecordArr.value = []; | ||||
|  | ||||
|       // 重新赋值新数据 | ||||
|       if (res.data.userInfo) { | ||||
|         Object.assign(userInfo.value, res.data.userInfo); | ||||
|       } | ||||
|       if (res.data.records) { | ||||
|         Object.assign(followRecordArr.value, res.data.records); | ||||
|       } | ||||
|     }else { | ||||
|       userInfo.value = {}; | ||||
|       followRecordArr.value = [] | ||||
|     } | ||||
|   }catch(err) { | ||||
|     console.error("获取配置失败:", error.message); | ||||
|     console.error("获取联系人信息失败:", err.message); | ||||
|     userInfo.value = {}; | ||||
|     followRecordArr.value = [] | ||||
|   }finally { | ||||
|     addWatchers() | ||||
|   } | ||||
| @ -267,20 +293,31 @@ watch( | ||||
|   } | ||||
| ); | ||||
|  | ||||
| // 生命周期管理 | ||||
| onMounted(async () => { | ||||
|   // 初始化获取用户信息 | ||||
|   await getUserInfo() | ||||
| }) | ||||
| // 生命周期 | ||||
| onMounted(async () => { | ||||
|  | ||||
|   // 设置联系人数据更新监听器 | ||||
|   await ipc.removeAllListeners('contact-data-update') | ||||
|   await ipc.on('contact-data-update', (event, args) => { | ||||
|     const {data} = args | ||||
|     userInfo.value = data.userInfo; | ||||
|     followRecordArr.value = data.records; | ||||
|     // 移除现有监听器,避免冲突 | ||||
|     removeWatchers() | ||||
|  | ||||
|     // 清空并更新数据 | ||||
|     userInfo.value = {}; | ||||
|     followRecordArr.value = []; | ||||
|     Object.assign(userInfo.value, data.userInfo); | ||||
|     Object.assign(followRecordArr.value, data.records); | ||||
|  | ||||
|     // 重新添加监听器 | ||||
|     addWatchers() | ||||
|   }) | ||||
| }) | ||||
|  | ||||
| onUnmounted(() => { | ||||
|   removeWatchers() | ||||
|   ipc.removeAllListeners('contact-data-update') | ||||
| }) | ||||
| const activityArr = ref([ | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	