import { Request, Response } from 'express' import resFormatter from '../utils/responseFormatter' import serverRequest from '../api/scrm/serverRequest' import commonData from '../store/data' import servicesCommon from '../services/common' import { formatMsg_V2 } from '../utils/dataMapping/mapMsgType_V2' import { TypeAppletMessage, TypeVideoChannerMessage } from '../types/typesMsg' import CONST_MSG_TYPE from '../const/CONST_MSG_TYPE' import servicesReceiveMsg from '../services/receiveMsgServices' const module = { // 获取群聊列表 receiveMsg: async (_req: Request, res: Response): Promise => { // 登录状态 if (commonData.get('wxLoginStatus') !== 1) return // 群成员新增通知 if (_req.body.type == 301 || _req.body.type == 302) { // console.log('--群成员新增/退群通知--', _req.body) try { const msgData = { msgtype: _req.body.type == 301 ? 18 : 19, content: _req.body.type == 301 ? '[群成员新增通知]' : '[群成员移除通知]', room_conversation_id: _req.body.chat_room_id, send_time: Math.floor(new Date().getTime() / 1000), msg_id: _req.body.chat_room_id.slice(-4) + '_' + _req.body.member_list[0].slice(-4) + '_' + Math.floor(Date.now() / 60000) // 每分钟相同id相同,避免重复 } servicesReceiveMsg.formatAndSaveMsg(msgData) // 群成员新增 if (_req.body.type == 301) { servicesCommon.newMemberJoinRoom(_req.body.chat_room_id, _req.body.member_list) } } catch (error) { resFormatter.error(res, error) } } else if (_req.body.type == 100) { // 消息处理 // 将2.x Hook消息格式转为1.x格式 const msgData = formatMsg_V2(_req.body) if (!msgData) return // 群聊消息,消息统一上传 // 2.x 通过 masData.sender 判断是否群聊,如果是群消息,该参数为发送者的用户ID,否则为空 if (msgData.is_room) { try { // serverRequest.saveMsg(msgData) servicesReceiveMsg.formatAndSaveMsg(msgData) } catch (error) { console.log('--接收消息--err--') resFormatter.error(res, error) } } // 额外消息处理 // 自己发送给自己 & 小程序消息 (作为意图可选小程序) if (msgData.is_self_msg == 1 && msgData.msgtype == 3) { try { const _data = formatMessage_Applet(msgData) serverRequest.submitMiniWx(_data) } catch (error) { console.log('--处理小程序消息错误--') resFormatter.error(res, error) } } // 自己发送给自己 & 视频号消息 (作为意图可选视频号) if (msgData.is_self_msg == 1 && msgData.msgtype == 4) { try { const _data = formatMessage_VideoChannel(msgData) serverRequest.submitVideoChannel(_data) } catch (error) { console.log('--处理视频号消息错误--') resFormatter.error(res, error) } } // 别人发给自己 & 私聊消息(上报数据作为白名单备选) if (msgData.is_self_msg == 0 && msgData.is_room == 0) { try { const _data = msgData const selfwxid: string = commonData.get('selfwxid') let obj if (selfwxid == _data.rereceiver) { // 如果是接收者是自己 obj = { entWxNo: selfwxid, msgContent: msgData.msgtype == 0 ? msgData.content : '[' + CONST_MSG_TYPE[msgData.msgtype] + ']', wxId: _data.sender, wxName: _data.sender_name } // console.log('--_req.body--私聊', JSON.stringify(obj)) serverRequest.putWLMsg(obj) } } catch (error) { resFormatter.error(res, error) } } } } } // 格式化小程序消息,用以小程序消息上传 function formatMessage_Applet(msgData): TypeAppletMessage { const _data = msgData.content return { appid: _data.app_id, des: _data.title || _data.app_name, title: _data.desc, conversation_id: msgData.room_conversation_id, // 现(R:10965603066639583) 原(S:1688855864556671_1688856360580472) headImgUrl: _data.avatar_url, is_room: msgData.is_room, msg_id: msgData.msg_id, msgtype: msgData.msgtype, pagepath: _data.page_path, rereceiver: msgData.rereceiver, send_time: msgData.send_time, sender: msgData.sender, size: _data.cover_size, thumbaeskey: _data.cover_aes_key, thumbfileid: _data.cover_cdn_key, thumbmd5: _data.cover_md5, wechatId: _data.wechat_id } } // 格式化小程序消息,用以小程序消息上传 function formatMessage_VideoChannel(msgData): TypeVideoChannerMessage { const _data = msgData.content return { msgtype: msgData.msgtype, msgId: msgData.msg_id, isRoom: msgData.is_room, rereceiver: msgData.rereceiver, sender: msgData.sender, sendTime: msgData.send_time, avatarUrl: _data.avatar_url, coverUrl: _data.cover_url, description: _data.desc, extras: _data.extras, nickName: _data.nick_name, thumbUrl: _data.thumb_url, videoUrl: _data.video_url } } export default module