Ejemplo n.º 1
0
        public Recv_Decrypt_ImageEntity ParseDecryptImage(object data)
        {
            try
            {
                Recv_Decrypt_ImageEntity decrypt_ImageEntity = ConvertObjToModel <Recv_Decrypt_ImageEntity>(data);

                return(decrypt_ImageEntity);
            }
            catch (Exception)
            {
                throw;
            }
        }
Ejemplo n.º 2
0
        // 接收消息回调
        void WxRecvCallback(uint dwClient, IntPtr intPtr, uint dwSize)
        {
            //if (CommonCacheConfig.Login_WeChat_UserInfo.Select(t => t.wx_clientid).Contains(dwClient) || CommonCacheConfig.Login_WeChat_UserInfo.Count >= CommonCacheConfig.WeChatCount)//防止漏洞双开多获取个微信的数据
            //    return;

            String recvData = Marshal.PtrToStringAnsi(intPtr);

            try
            {
                string new_RecvData = UnicodeHelper.DeUnicode(recvData);

                WeChat_ResponseInfo reponseInfo     = JsonConvert.DeserializeObject <WeChat_ResponseInfo>(new_RecvData);
                MessageTypeEnum     messageTypeEnum = (MessageTypeEnum)reponseInfo.type;

                string logText = "";

                switch (messageTypeEnum)
                {
                case MessageTypeEnum.MT_DEBUG_LOG:
                    logText = "微信启动成功!";
                    break;

                case MessageTypeEnum.MT_USER_LOGIN:
                    WeChat_UserInfo wXInfo = weChatParseHelper.ParseCurrentUserInfo(reponseInfo.data, dwClient);
                    CallBackWeChatMessage(wXInfo, dwClient);
                    logText = "【" + wXInfo.nickname + "】登录成功!";

                    if (!CommonCacheConfig.WeChat_ImageList.IsEmpty())
                    {
                        CommonCacheConfig.WeChat_ImageList.Clear();
                    }

                    #region 获取好友和群组消息
                    WeChatTools.GetFriendInfoList(dwClient);
                    WeChatTools.GetChatRoomInfoList(dwClient);
                    #endregion
                    break;

                case MessageTypeEnum.MT_USER_LOGOUT:
                    LogoutWeChat logoutWeChat = reponseInfo.data.ToString().ToJsonObject <LogoutWeChat>();
                    CallBackWeChatMessage(logoutWeChat, dwClient);

                    WeChat_UserInfo exit_wXInfo = CommonCacheConfig.Login_WeChat_UserInfo.Where(t => t.wxid == logoutWeChat.wxid).FirstOrDefault();
                    if (!exit_wXInfo.IsEmpty())
                    {
                        CommonCacheConfig.Login_WeChat_UserInfo.Remove(exit_wXInfo);
                    }
                    break;

                case MessageTypeEnum.MT_SQL_QUERY:
                    break;

                case MessageTypeEnum.MT_DATA_OWNER_MSG:
                    break;

                case MessageTypeEnum.MT_DATA_WXID_MSG:
                    break;

                case MessageTypeEnum.MT_DATA_FRIENDS_MSG:
                    TaskHelper.ExcuteNewTask(() => { List <FriendInfoEntity> friendInfoList = weChatParseHelper.ParseCurrentFriendList(reponseInfo.data, dwClient.GetWxidByClientID()); CallBackWeChatMessage(friendInfoList, dwClient); }, 500);
                    break;

                case MessageTypeEnum.MT_DATA_CHATROOMS_MSG:
                    TaskHelper.ExcuteNewTask(() => { List <ChatRoomInfoEntity> chatRoomInfoList = weChatParseHelper.ParseCurrentChatRoomList(reponseInfo.data, dwClient.GetWxidByClientID()); CallBackWeChatMessage(chatRoomInfoList, dwClient); }, 500);
                    break;

                case MessageTypeEnum.MT_DATA_CHATROOM_MEMBERS_MSG:
                    TaskHelper.ExcuteNewTask(() =>
                    {
                        GroupMemberResponseEntity groupMemberResponseEntity = WeChatParseHelper.ConvertObjToModel <GroupMemberResponseEntity>(reponseInfo.data);
                        CallBackWeChatMessage(groupMemberResponseEntity, dwClient);
                        //GroupMemberService.BatchUpdateGroupMember(groupMemberResponseEntity);
                    }, 50);
                    break;

                case MessageTypeEnum.MT_DATA_PUBLICS_MSG:
                    break;

                case MessageTypeEnum.MT_UPDATE_WXID_MSG:
                    break;

                case MessageTypeEnum.MT_UPDATE_ROOM_MEMBER_MSG:
                    break;

                case MessageTypeEnum.MT_SEND_TEXTMSG:
                    break;

                case MessageTypeEnum.MT_SEND_CHATROOM_ATMSG:
                    break;

                case MessageTypeEnum.MT_SEND_CARDMSG:
                    break;

                case MessageTypeEnum.MT_SEND_LINKMSG:
                    break;

                case MessageTypeEnum.MT_SEND_IMGMSG:
                    break;

                case MessageTypeEnum.MT_SEND_FILEMSG:
                    break;

                case MessageTypeEnum.MT_SEND_VIDEOMSG:
                    break;

                case MessageTypeEnum.MT_SEND_GIFMSG:
                    break;

                case MessageTypeEnum.MT_RECV_TEXT_MSG:
                    Recv_Text_MsgEntity recv_Text_MsgEntity = weChatParseHelper.ParseRecvTextMsg(reponseInfo.data);
                    //logText = "消息文本->" + recv_Text_MsgEntity.from_wxid + "->" + recv_Text_MsgEntity.msg;
                    if ((!recv_Text_MsgEntity.room_wxid.IsQun() && recv_Text_MsgEntity.from_wxid.IsSelf(dwClient)) || (recv_Text_MsgEntity.room_wxid.IsQun() && CommonCacheConfig.ZhongZhuan_QunID != recv_Text_MsgEntity.room_wxid) && recv_Text_MsgEntity.from_wxid.IsSelf(dwClient))
                    {
                        return;
                    }
                    TaskHelper.ExcuteNewTask(() =>
                    {
                        if (!recv_Text_MsgEntity.from_wxid.FilterMessage())
                        {
                            CallBackWeChatMessage(recv_Text_MsgEntity, dwClient);
                        }
                    }, 20);
                    break;

                case MessageTypeEnum.MT_RECV_PICTURE_MSG:
                    Recv_Image_MsgEntity recv_Image_MsgEntity = weChatParseHelper.ParseImageMsg(reponseInfo.data);
                    //CallBackWeChatMessage(recv_Image_MsgEntity, dwClient);
                    if ((!recv_Image_MsgEntity.room_wxid.IsQun() && recv_Image_MsgEntity.from_wxid.IsSelf(dwClient)) || (recv_Image_MsgEntity.room_wxid.IsQun() && CommonCacheConfig.ZhongZhuan_QunID != recv_Image_MsgEntity.room_wxid) && recv_Image_MsgEntity.from_wxid.IsSelf(dwClient))
                    {
                        return;
                    }

                    CallBackWeChatMessage(recv_Image_MsgEntity, dwClient);
                    break;

                case MessageTypeEnum.MT_RECV_VOICE_MSG:
                    //Recv_Video_MsgEntity recv_Video_MsgEntity = weChatParseHelper.ParseRecvVideo(reponseInfo.data);
                    //logText = "语音消息:" + recv_Video_MsgEntity.video;
                    break;

                case MessageTypeEnum.MT_RECV_FRIEND_MSG:
                    NewFriendInfo newFriendInfo = weChatParseHelper.ParseAcceptFriend(reponseInfo.data);
                    newFriendInfo.sourcewxid = dwClient.GetWxidByClientID();    //保存该条数据来源于哪条微信

                    CallBackWeChatMessage(newFriendInfo, dwClient);
                    //NewFriendService.AddNewFriend(newFriendInfo);
                    break;

                case MessageTypeEnum.MT_RECV_CARD_MSG:
                    //SendAutoAddCard(dwClient, 1);
                    break;

                case MessageTypeEnum.MT_RECV_VIDEO_MSG:
                    Recv_Video_MsgEntity recv_Video_MsgEntity = weChatParseHelper.ParseRecvVideo(reponseInfo.data);
                    if ((!recv_Video_MsgEntity.room_wxid.IsQun() && recv_Video_MsgEntity.from_wxid.IsSelf(dwClient)) || (recv_Video_MsgEntity.room_wxid.IsQun() && CommonCacheConfig.ZhongZhuan_QunID != recv_Video_MsgEntity.room_wxid) && recv_Video_MsgEntity.from_wxid.IsSelf(dwClient))
                    {
                        return;
                    }
                    TaskHelper.ExcuteNewTask((() =>
                    {
                        CallBackWeChatMessage(recv_Video_MsgEntity, dwClient);

                        /*string video_url = RequstDataOperate.UploadFile(recv_Video_MsgEntity.video);
                         * logText = "视频地址:" + video_url;
                         * recv_Video_MsgEntity.video = video_url;
                         #region 发送消息到客服
                         * SocketHelper.SendCustomerMessage(recv_Video_MsgEntity, GetWxidByClientID(dwClient));
                         #endregion*/
                    }), 500);
                    break;

                case MessageTypeEnum.MT_RECV_EMOJI_MSG:
                    Recv_Emoji_MsgEntity recv_Emoji_MsgEntity = weChatParseHelper.ParseRecvEmoji(reponseInfo.data);
                    if (!recv_Emoji_MsgEntity.IsEmpty())
                    {
                        if (recv_Emoji_MsgEntity.from_wxid.IsSelf(dwClient))
                        {
                            return;
                        }
                        CallBackWeChatMessage(recv_Emoji_MsgEntity, dwClient);
                    }

                    break;

                case MessageTypeEnum.MT_RECV_LOCATION_MSG:
                    break;

                case MessageTypeEnum.MT_RECV_LINK_MSG:
                    Recv_Text_MsgEntity recv_Text_MsgEntity_card = weChatParseHelper.ParseRecvTextMsg(reponseInfo.data);
                    if (recv_Text_MsgEntity_card.from_wxid.IsSelf(dwClient))
                    {
                        return;
                    }
                    TaskHelper.ExcuteNewTask(() =>
                    {
                        CardInfoEntity cardInfoEntity = weChatParseHelper.ParseRecvCard(reponseInfo.data);
                        recv_Text_MsgEntity_card.msg  = cardInfoEntity.url;

                        CallBackWeChatMessage(recv_Text_MsgEntity_card, dwClient);

                        //MessageOperateHelper.OperateMessage(recv_Text_MsgEntity_card, dwClient, GetWxidByClientID(dwClient));
                        logText = "卡牌标题:" + cardInfoEntity.title;
                    }, 50);
                    break;

                case MessageTypeEnum.MT_RECV_FILE_MSG:
                    Recv_File_MsgEntity recv_File_MsgEntity = weChatParseHelper.ParseFileMsg(reponseInfo.data);
                    if (recv_File_MsgEntity.from_wxid.IsSelf(dwClient))
                    {
                        return;
                    }
                    TaskHelper.ExcuteNewTask((() =>
                    {
                        //string file_url = RequstDataOperate.UploadFile(recv_File_MsgEntity.file);
                        //logText = "文件地址:" + file_url;
                        //recv_File_MsgEntity.file = file_url;
                        CallBackWeChatMessage(recv_File_MsgEntity, dwClient);
                    }), 500);
                    break;

                case MessageTypeEnum.MT_RECV_MINIAPP_MSG:    //小程序
                    Recv_MINIAPP_MsgEntity recv_MINIAPP_MsgEntity = weChatParseHelper.ParseMiniAppMsg(reponseInfo.data);
                    CallBackWeChatMessage(recv_MINIAPP_MsgEntity, dwClient);
                    break;

                case MessageTypeEnum.MT_RECV_WCPAY_MSG:
                    break;

                case MessageTypeEnum.MT_RECV_SYSTEM_MSG:    //接受系统消息(处理新人进群)
                    TaskHelper.ExcuteNewTask(() =>
                    {
                        Recv_System_MsgEntity recv_System_MsgEntity = JsonConvert.DeserializeObject <Recv_System_MsgEntity>(reponseInfo.data.ToString());
                        CallBackWeChatMessage(recv_System_MsgEntity, dwClient);
                        string raw_msg = recv_System_MsgEntity.raw_msg.Trim();
                        if (recv_System_MsgEntity.wx_type == 10000 && ((raw_msg.Contains("邀请") && raw_msg.Contains("加入了群聊")) || (raw_msg.Contains("通过扫描") && raw_msg.Contains("分享的二维码加入群聊"))))
                        {
                            //if (CommonCache.robot_BaseSettingEntity.base_info.in_qun_wmsg_switch == 1)
                            //{//开启欢迎新人进群
                            //    MessageOperateHelper.WelComePersonInChatRoom(recv_System_MsgEntity, dwClient);
                            //}

                            #region 只要有人进群就更新群成员信息
                            //CommonCache.weChatHelper.GetChatRoomInfoList(dwClient, recv_System_MsgEntity.room_wxid);
                            #endregion
                        }
                        else if (recv_System_MsgEntity.wx_type == 10000 && raw_msg.Contains("修改群名为"))
                        {
                            /*string chatRoomName = RegularHelper.getValue("(?<=“).*?(?=”)", raw_msg);
                             * recv_System_MsgEntity.room_wxid*/
                        }
                        else if (raw_msg.Contains("拍了拍你"))
                        {
                            //MessageOperateHelper.PaiMessage(recv_System_MsgEntity, dwClient);
                        }
                        else if (raw_msg.StartsWith("你已添加了") && raw_msg.EndsWith(",现在可以开始聊天了。"))
                        {
                            //非自动同意状态下才用此回复
                            //if (!CommonCache.robot_BaseSettingEntity.add_friend_info.IsEmpty() && CommonCache.robot_BaseSettingEntity.add_friend_info.auto_pass_switch == 0 && CommonCache.robot_BaseSettingEntity.add_friend_info.hand_pass_switch == 1)
                            //{
                            //    MessageOperateHelper.SendWelcomeMessage(dwClient, 1, recv_System_MsgEntity.from_wxid, new Dictionary<string, string>());
                            //}
                        }
                    }, 50);
                    break;

                case MessageTypeEnum.MT_RECV_REVOKE_MSG:
                    break;

                case MessageTypeEnum.MT_RECV_OTHER_MSG:
                    break;

                case MessageTypeEnum.MT_RECV_OTHER_APP_MSG:
                    break;

                case MessageTypeEnum.MT_ADD_FRIEND_MSG:
                    break;

                case MessageTypeEnum.MT_MOD_FRIEND_REMARK_MSG:
                    break;

                case MessageTypeEnum.MT_DEL_FRIEND_MSG:
                    break;

                case MessageTypeEnum.MT_ACCEPT_FRIEND_MSG:
                    break;

                case MessageTypeEnum.MT_ACCEPT_WCPAY_MSG:
                    break;

                case MessageTypeEnum.MT_ACCEPT_ROOM_MSG:
                    break;

                case MessageTypeEnum.MT_CREATE_ROOM_MSG:
                    break;

                case MessageTypeEnum.MT_INVITE_TO_ROOM_MSG:
                    break;

                case MessageTypeEnum.MT_INVITE_TO_ROOM_REQ_MSG:
                    break;

                case MessageTypeEnum.MT_DEL_ROOM_MEMBER_MSG:
                    break;

                case MessageTypeEnum.MT_MOD_ROOM_NAME_MSG:
                    break;

                case MessageTypeEnum.MT_MOD_ROOM_NOTICE_MSG:
                    break;

                case MessageTypeEnum.MT_MOD_ROOM_MEMBER_NAME_MSG:
                    break;

                case MessageTypeEnum.MT_MOD_ROOM_SHOW_NAME_MSG:
                    break;

                case MessageTypeEnum.MT_SAVE_ROOM_TO_CONTACT_MSG:
                    break;

                case MessageTypeEnum.MT_QUIT_DEL_ROOM_MSG:
                    break;

                case MessageTypeEnum.MT_MOD_RECV_NOTIFY_MSG:
                    break;

                case MessageTypeEnum.MT_MOD_CHAT_SESSION_TOP_MSG:
                    break;

                case MessageTypeEnum.MT_ZOMBIE_CHECK_MSG:
                    break;

                case MessageTypeEnum.MT_AUTO_ACCEPT_FRIEND_MSG:
                    break;

                case MessageTypeEnum.MT_AUTO_ACCEPT_WCPAY_MSG:
                    break;

                case MessageTypeEnum.MT_AUTO_ACCEPT_ROOM_MSG:
                    break;

                case MessageTypeEnum.MT_AUTO_ACCPET_CARD_MSG:
                    break;

                case MessageTypeEnum.MT_DECRYPT_IMG_MSG:
                    Recv_Decrypt_ImageEntity decrypt_ImageEntity = weChatParseHelper.ParseDecryptImage(reponseInfo.data);
                    if (decrypt_ImageEntity.status == 1)
                    {
                        FileInfo fileInfo  = new FileInfo(decrypt_ImageEntity.src_file);
                        string   key_image = fileInfo.Name;

                        AddLogs("解密成功." + key_image);

                        if (CommonCacheConfig.WeChat_ImageList.ContainsKey(key_image))
                        {
                            Recv_Image_MsgEntity recv_Image_MsgEntity_decry = CommonCacheConfig.WeChat_ImageList[key_image];
                            CommonCacheConfig.WeChat_ImageList.Remove(key_image);

                            recv_Image_MsgEntity_decry.image_url = decrypt_ImageEntity.dest_file;
                            CallBackWeChatMessage(recv_Image_MsgEntity_decry, dwClient);
                        }
                    }
                    else
                    {
                        AddLogs("图片解密失败." + reponseInfo.data);
                    }
                    break;

                case MessageTypeEnum.MT_OPEN_BROWSER_MSG:
                    break;

                case MessageTypeEnum.MT_RECV_QRCODE_MSG:
                    break;

                default:
                    break;
                }

                if (logText != "")
                {
                    AddLogs(logText);
                }

                if (CommonCacheConfig.Log_Debug)
                {
                    LogHelper.WriteLog("微信消息", new_RecvData);
                }
            }
            catch (Exception ex)
            {
                AddLogs(ex.Message);
            }
        }