using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using System.Web; using System.Web.Caching; using CsharpHttpHelper; using Newtonsoft.Json; using PCRobot.Entitys; using PCRobot.Entitys.Enterprise; using PCRobot.Pack; using PCRobot.Utils; using static PCRobot.PCWechat.Routine.Wechat_Xiaoxie_Type; namespace PCRobot.PCWechat.Routine { public partial class Wechat_Xiaoxie { public override string GetVersion() { return Version; } public override void GetContact(bool isUpdata) { try { var result = GetFriendInfos().Result; var socketClient = EasySoc.GetSocket(); CommonResult msg = new CommonResult(); msg.RobotUsername = User.Wxid; msg.RobotUsernick = User.Nickname; msg.RobotType = RobotType.客户端微信; msg.MsgId = ServerMsgID; msg.Cmd = PCRobotCMD.rcvFriendList; msg.Data = result == null ? string.Empty : HttpHelper.ObjectToJson(result); socketClient.Send(msg); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void CheckZombiz() { if (User.IsZombizRun) return; try { User.IsZombizRun = true; var fDic = GetFriendInfos().Result; var friends = User.Friends.Where(f => !f.Key.IsQun() && f.Value.is_zombie == -1).Select(f => f.Value).ToList(); foreach (var item in friends) { var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_CARD_XMLMSG, data = new { to_wxid = item.wxid, xml = @" ".Replace("[wxid]", item.wxid).Replace("[name]", ".") } }); var u = User.Friends.FirstOrDefault(f => f.Key == item.wxid).Value; if (u != null && u.is_zombie == -1) { SendWeChatData(User.DwClientId, json); Thread.Sleep(800 + new Random().Next(100, 200)); } } } catch (Exception) { } finally { User.IsZombizRun = false; } } public override void SendMessage(string wxid, string message, string msgId = "", string usernick = "", string quoteMess = "") { try { var json = string.Empty; message = message.Replace("\r\n", "\n"); if (Common.isXML(message)) { //var msgs = Regex.Matches(message, "(<.*?>)(.*?)()"); //foreach (Match item in msgs) //{ // if (item.Success && !string.IsNullOrEmpty(item.Groups[2].Value) && !item.Groups[2].Value.StartsWith("" + item.Groups[3]; // message = message.Replace(item.Groups[1].Value,temp); // } //} //wxfedd75bfcbbcd58c //wx13e41a437b8a1d2e //pages/index/index.html?orderid=[领取地址] if (Regex.IsMatch(message, @"(?[^<]+)") || Regex.IsMatch(message, @"(.+?)")) json = CsharpHttpHelper.HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_XMLMSG, data = new { to_wxid = wxid, xml = Common.XMLDispose(message.Replace("&", "&")) } }); else { message = message.Replace("&", "&"); var title = string.Empty; var desc = string.Empty; var url = string.Empty; var image_url = string.Empty; var reg = Regex.Match(message, @"(?<标题>[\w\W]+?)"); if (reg.Success) title = reg.Groups["标题"].Value; reg = Regex.Match(message, @"(?<介绍>[\w\W]+?)"); if (reg.Success) desc = reg.Groups["介绍"].Value; reg = Regex.Match(message, @"(?<链接>[\w\W]+?)"); if (reg.Success) url = reg.Groups["链接"].Value; reg = Regex.Match(message, @"(?<图片链接>[\w\W]+?)"); if (reg.Success) image_url = reg.Groups["图片链接"].Value; json = CsharpHttpHelper.HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_LINKMSG, data = new { to_wxid = wxid, title = $"{title.XMLReplace()}", desc = $"{desc.XMLReplace()}", url = url.XMLReplace(), image_url = image_url.XMLReplace() } }); } } else { var atUsername = new List(); var regs = Regex.Matches(message, @"\[@([^\]]+?)\]"); var flag = true; if (wxid.IsQun()) { //[@wujiahua0876]你懂得,我只是测试一下 foreach (Match item in regs) { atUsername.Add(item.Groups[1].Value.Trim()); } if (atUsername.Count != 0) { flag = false; message = Regex.Replace(message, @"(\[@[^\]]+?\])", "{$@}"); json = HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_CHATROOM_ATMSG, data = new { to_wxid = wxid, content = message, at_list = atUsername.ToArray() } }); } } if (flag) { message = Regex.Replace(message, @"(\[@[^\]]+?\])", ""); json = HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_TEXTMSG, data = new { to_wxid = wxid, content = message } }); } } SendWeChatData(User.DwClientId, json); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void SendFile(string username, string file) { try { var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_FILEMSG, data = new { to_wxid = username, file = file } }); SendWeChatData(User.DwClientId, json); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void SendCard(string username, string cardwxid) { try { var json = string.Empty; if (cardwxid.IsQun()) //json = CsharpHttpHelper.HttpHelper.ObjectToJson(new { type = MsgType.MT_INVITE_TO_ROOM_REQ_MSG, data = new { room_wxid = card_wxid, member_list = new string[] { to_wxid } } }); { InviteMemberToGroup_40Up(cardwxid, new string[] { username }); return; } else json = CsharpHttpHelper.HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_CARDMSG, data = new { to_wxid = username, card_wxid = cardwxid } }); SendWeChatData(User.DwClientId, json); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void SendLinkMessage(string username, string title, string desc, string url, string image_url) { var json = CsharpHttpHelper.HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_LINKMSG, data = new { to_wxid = username, title = $"{title.XMLReplace()}", desc = $"{desc.XMLReplace()}", url = url.XMLReplace(), image_url = image_url.XMLReplace() } }); SendWeChatData(User.DwClientId, json); } public override void SendImage(string wxid, string file) { try { if (file.StartsWith("http", StringComparison.InvariantCultureIgnoreCase)) { byte[] ImangByte = null; string extension = Path.GetExtension(file); extension = string.IsNullOrWhiteSpace(extension) ? ".jpg" : extension; ImangByte = new System.Net.WebClient().DownloadData(file); byte[] imageHash = new MD5CryptoServiceProvider().ComputeHash(ImangByte); var path = Common.CacheFilePath(User.Wxid, wxid, "IMAGE", Guid.NewGuid().ToString("N"), extension); if (!File.Exists(path)) { File.WriteAllBytes(path, ImangByte); } if (File.Exists(path)) file = path; } else { if (!File.Exists(file)) { if (Common.IsBase64(file)) { var path = Common.CacheFilePath(User.Wxid, wxid, "IMAGE", Guid.NewGuid().ToString("N"), "jpg"); if (Common.Base64ToFileAndSave(file, path) && File.Exists(path)) { file = path; } } else { } } } var json = string.Empty; if (file.Trim().EndsWith(".gif", StringComparison.CurrentCultureIgnoreCase)) json = HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_GIFMSG, data = new { to_wxid = wxid, file = file } }); else json = HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_IMGMSG, data = new { to_wxid = wxid, file = file } }); SendWeChatData(User.DwClientId, json); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void SendVideo(string username, string file) { try { var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_VIDEOMSG, data = new { to_wxid = username, file = file } }); SendWeChatData(User.DwClientId, json); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void ModifyGroupNotice(string groupid, string notice) { try { var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_MOD_ROOM_NOTICE_MSG, data = new { room_wxid = groupid, notice = notice } }); SendWeChatData(this.User.DwClientId, json); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void Ser_CreateGroup(string[] usernames) { var result = CreateGroup(usernames).Result; var socketClient = EasySoc.GetSocket(); var msg = new CreateRoom(); msg.RobotUsername = User.Wxid; msg.RobotUsernick = User.Nickname; msg.RobotType = RobotType.客户端微信; msg.MsgId = ServerMsgID; msg.Cmd = PCRobotCMD.rcvCreateRoom; msg.GroupId = result?.GroupId; msg.MemberTotal = 0; socketClient.Send(msg); } public override Task CreateGroup(string[] usernames) { if (usernames != null && usernames.Length != 0) { try { var type = RobotIsRunType.创建群; //这里判断是否已经存在在获取的群邀请信息,没有就去获取 if (!User.IsGetDic.ContainsKey(type)) { User.IsGetDic[type] = false;//这里添加要查询的群ID,状态为flase(还没收到微信那边返回的数据) var json = JsonConvert.SerializeObject(new { type = MsgType.MT_CREATE_ROOM_MSG, data = usernames }); SendWeChatData(User.DwClientId, json); } return Task.Factory.StartNew(delegate () { var keyMd5 = HttpExtend.GetMD5String($"CACHE_CREATEGROUP{type}"); DateTime endtime = DateTime.Now.AddSeconds(20); do { if (User.IsGetDic.ContainsKey(type) && User.IsGetDic[type])//等待收到微信查询的消息 { do { if (!User.IsGetDic.ContainsKey(type))//等到解析完消息,解析完消息IsGetDic中的消息会被删除 { var createGroupInfo = Common.GetCache(keyMd5); Common.RemoveCache(keyMd5); return createGroupInfo; } Thread.Sleep(200); } while (endtime > DateTime.Now); break; } Thread.Sleep(20); } while (endtime > DateTime.Now); return null; }); } catch (Exception ex) { LogHelper.GetSingleObj().Error($"获取创建群信息", ex.Message); } } return Task.FromResult(null); } public override void DeleteGroupMember(string groupid, string[] usernames) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_DEL_ROOM_MEMBER_MSG, data = new { room_wxid = groupid, member_list = usernames } }); SendWeChatData(User.DwClientId, json); } public override void QuitAndDelGroup(string groupid) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_QUIT_DEL_ROOM_MSG, data = new { room_wxid = groupid } }); SendWeChatData(User.DwClientId, json); } public override void ModifyGroupName(string groupid, string newName) { try { var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_MOD_ROOM_NAME_MSG, data = new { room_wxid = groupid, name = newName } }); SendWeChatData(this.User.DwClientId, json); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void Ser_GetSelfInfo() { var result = GetSelfInfo().Result; var data = string.Empty; if (result != null) data = HttpHelper.ObjectToJson(result as FriendInfo); var socketClient = EasySoc.GetSocket(); var msg = new WechatContact(); msg.RobotUsername = User.Wxid; msg.RobotUsernick = User.Nickname; msg.RobotType = RobotType.客户端微信; msg.MsgId = ServerMsgID; msg.Cmd = PCRobotCMD.rcvContact; msg.Message = data; socketClient.Send(msg); } public override Task GetSelfInfo() { try { //这里判断是否已经存在在获取的群邀请信息,没有就去获取 if (!User.IsGetContactDic.ContainsKey(User.Wxid)) { User.IsGetContactDic[User.Wxid] = false;//这里添加要查询的群ID,状态为flase(还没收到微信那边返回的数据) var json = JsonConvert.SerializeObject(new { type = MsgType.MT_DATA_OWNER_MSG, data = new { } }); SendWeChatData(User.DwClientId, json); } return Task.Factory.StartNew(delegate () { DateTime endtime = DateTime.Now.AddSeconds(20); do { if (User.IsGetContactDic.ContainsKey(User.Wxid) && User.IsGetContactDic[User.Wxid])//等待收到微信查询的消息 { do { if (!User.IsGetContactDic.ContainsKey(User.Wxid))//等到解析完消息,解析完消息IsGetContactDic中的消息会被删除 { if (User.Friends.ContainsKey(User.Wxid)) return User.Friends[User.Wxid]; return null; } Thread.Sleep(200); } while (endtime > DateTime.Now); break; } Thread.Sleep(20); } while (endtime > DateTime.Now); return null; }); } catch (Exception ex) { LogHelper.GetSingleObj().Error($"获取自己基础信息", ex.Message); } return Task.FromResult(null); } public override void Ser_GetContact(string username) { var data = string.Empty; if (User.Friends.ContainsKey(username)) data = HttpHelper.ObjectToJson(User.Friends[username]); else { if (username.IsQun()) { var g = GetGroupInfos().Result; if (User.Friends.ContainsKey(username)) data = HttpHelper.ObjectToJson(User.Friends[username]); } else { var result = GetFriendInfo(username).Result; if (result != null) data = HttpHelper.ObjectToJson((result as FriendInfo)); } } var socketClient = EasySoc.GetSocket(); var msg = new WechatContact(); msg.RobotUsername = User.Wxid; msg.RobotUsernick = User.Nickname; msg.RobotType = RobotType.客户端微信; msg.MsgId = ServerMsgID; msg.Cmd = PCRobotCMD.rcvContact; msg.Message = data; socketClient.Send(msg); } public override Task GetFriendInfo(string username) { try { //这里判断是否已经存在在获取的群邀请信息,没有就去获取 if (!User.IsGetContactDic.ContainsKey(username)) { User.IsGetContactDic[username] = false;//这里添加要查询的群ID,状态为flase(还没收到微信那边返回的数据) var json = JsonConvert.SerializeObject(new { type = MsgType.MT_DATA_WXID_MSG, data = new { wxid = username } }); SendWeChatData(User.DwClientId, json); } return Task.Factory.StartNew(delegate () { DateTime endtime = DateTime.Now.AddSeconds(20); do { if (User.IsGetContactDic.ContainsKey(username) && User.IsGetContactDic[username])//等待收到微信查询的消息 { do { if (!User.IsGetContactDic.ContainsKey(username))//等到解析完消息,解析完消息IsGetContactDic中的消息会被删除 { if (User.Friends.ContainsKey(username)) return User.Friends[username]; return null; } Thread.Sleep(200); } while (endtime > DateTime.Now); break; } Thread.Sleep(20); } while (endtime > DateTime.Now); return null; }); } catch (Exception ex) { LogHelper.GetSingleObj().Error($"获取单个用户信息", ex.Message); } return Task.FromResult(null); } public override void GetFriendInfo(string userName, string groupId, bool IsFlush = false) { var result = Task.Factory.StartNew(() => { try { if (string.IsNullOrWhiteSpace(userName)) return null; if (string.IsNullOrWhiteSpace(groupId)) return null; if (IsFlush) { var gInfo = GetGroupMemberInfo(groupId).Result; if (gInfo == null) return null; var g = gInfo as GroupInfo; if (g == null) return null; var ftmp = g.friends.FirstOrDefault(f => f.wxid == userName); if (ftmp != null) return new GroupInfo() { wxid = groupId, friends = new List() { ftmp }, total_member = User.Groups[groupId].total_member, nickname = User.Groups[groupId].nickname }; } if (User.Groups.ContainsKey(groupId)) { if (User.Groups[groupId].friends == null) return null; var ftmp = User.Groups[groupId].friends.FirstOrDefault(f => f.wxid == userName); if (ftmp != null) return new GroupInfo() { wxid = groupId, friends = new List() { ftmp }, total_member = User.Groups[groupId].total_member, nickname = User.Groups[groupId].nickname }; } } catch (Exception ex) { LogHelper.GetSingleObj().Error($"获取群用户信息", ex.Message); } return null; }).Result; var socketClient = EasySoc.GetSocket(); var msg = new WechatContact(); msg.RobotUsername = User.Wxid; msg.RobotUsernick = User.Nickname; msg.RobotType = RobotType.客户端微信; msg.MsgId = ServerMsgID; msg.Cmd = PCRobotCMD.rcvContact; msg.Message = result == null ? string.Empty : HttpHelper.ObjectToJson(result); socketClient.Send(msg); } public override Task> GetGroupInfos() { try { var type = RobotIsRunType.获取群列表; //这里判断是否已经存在在获取的群邀请信息,没有就去获取 if (!User.IsGetDic.ContainsKey(type)) { User.IsGetDic[type] = false;//这里添加要查询的群ID,状态为flase(还没收到微信那边返回的数据) var json = JsonConvert.SerializeObject(new { type = MsgType.MT_DATA_CHATROOMS_MSG, data = new { } }); SendWeChatData(User.DwClientId, json); } return Task.Factory.StartNew(delegate () { DateTime endtime = DateTime.Now.AddSeconds(20); do { if (User.IsGetDic.ContainsKey(type) && User.IsGetDic[type])//等待收到微信查询的消息 { do { if (!User.IsGetDic.ContainsKey(type))//等到解析完消息,解析完消息IsGetDic中的消息会被删除 { Dictionary dic = new Dictionary(); if (User.Groups != null) { foreach (var item in User.Groups) { dic.Add(item.Key, item.Value); } } return dic; } Thread.Sleep(200); } while (endtime > DateTime.Now); break; } Thread.Sleep(20); } while (endtime > DateTime.Now); return null; }); } catch (Exception ex) { LogHelper.GetSingleObj().Error($"获取所有群组列表", ex.Message); } return Task.FromResult>(null); } public override Task GetGroupMemberInfo(string groupid) { try { //这里判断是否已经存在在获取的群邀请信息,没有就去获取 if (!User.IsGetContactDic.ContainsKey(groupid)) { User.IsGetContactDic[groupid] = false;//这里添加要查询的群ID,状态为flase(还没收到微信那边返回的数据) var json = JsonConvert.SerializeObject(new { type = MsgType.MT_DATA_CHATROOM_MEMBERS_MSG, data = new { room_wxid = groupid } }); SendWeChatData(User.DwClientId, json); } return Task.Factory.StartNew(delegate () { DateTime endtime = DateTime.Now.AddSeconds(20); do { if (User.IsGetContactDic.ContainsKey(groupid) && User.IsGetContactDic[groupid])//等待收到微信查询的消息 { do { if (!User.IsGetContactDic.ContainsKey(groupid))//等到解析完消息,解析完消息IsGetGroupMemberDic中的消息会被删除 { if (User.Groups.ContainsKey(groupid)) return User.Groups[groupid]; } Thread.Sleep(200); } while (endtime > DateTime.Now); break; } Thread.Sleep(20); } while (endtime > DateTime.Now); return null; }); } catch (Exception ex) { LogHelper.GetSingleObj().Error($"获取指定群组成员信息", ex.Message); } return Task.FromResult(null); } public override void GetGroupsNotInMember() { try { //获取群列表(数据库读取) var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_DATA_CHATROOMS_MSG, data = new { } }); SendWeChatData(User.DwClientId, json); var result = Task.Factory.StartNew(delegate () { Thread.Sleep(4000); var groups = User.Friends.Where(f => f.Key.IsQun()).Select(f => f.Value).ToList(); return groups == null ? string.Empty : HttpHelper.ObjectToJson(new Dictionary>() { { "data", groups } }); }).Result; if (result == null) result = HttpHelper.ObjectToJson(new Dictionary>() { { "data", User.Friends.Where(f => f.Key.IsQun()).Select(f => f.Value).ToList() } }); var socketClient = EasySoc.GetSocket(); var msg = new WechatContact(); msg.RobotUsername = User.Wxid; msg.RobotUsernick = User.Nickname; msg.RobotType = RobotType.客户端微信; msg.MsgId = ServerMsgID; msg.Cmd = PCRobotCMD.rcvContactList; msg.Message = result; socketClient.Send(msg); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void OutLogin() { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_QUIT_LOGIN_MSG, data = new { } }); SendWeChatData(User.DwClientId, json); } public override void OutLogin(string message) { try { var v = WechatClient.Users.FirstOrDefault(f => f.Value.DwClientId == User.DwClientId && f.Value.Type == WechatType.Xiaoxie).Value; if (v != null) { LogHelper.GetSingleObj().Info("系统", $"CloseWechat事件退出,Uin:{v.Uin},微信:{v.Nickname}({v.Wxid})"); WechatClient.RemoveUser(v, true); if (!string.IsNullOrWhiteSpace(message)) PCRobotForm.ErrorMessAction?.Invoke(message); } } catch (Exception ex) { LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void SendProgramMessage(string username, string msg) { var json = CsharpHttpHelper.HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_XMLMSG, data = new { to_wxid = username, xml = Common.XMLDispose(msg.Replace("&", "&")) } }); SendWeChatData(User.DwClientId, json); } public override void SendXml(string username, string xml, string customSource = "Window wechat") { if (xml.Trim().StartsWith("", StringComparison.CurrentCultureIgnoreCase | StringComparison.InvariantCultureIgnoreCase)) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_SEND_XMLMSG, data = new { to_wxid = username, xml = $@"{User.Wxid}0{Common.XMLDispose(xml.Replace("&", "&"))}1{customSource}" } }); SendWeChatData(User.DwClientId, json); } } public override void SendCardXml(string username, string xml) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_SEND_CARD_XMLMSG, data = new { to_wxid = username, xml = xml } }); SendWeChatData(User.DwClientId, json); } public override void SendQuoteMessage(string username, string replyMess, WechatMsgType messageType, string quoteMess, string quoteMsgid, string quoteUsername, string quoteUsernick) { var mess = quoteMess; switch (messageType) { case WechatMsgType.文本: mess = quoteMess; break; case WechatMsgType.语音: case WechatMsgType.视频: case WechatMsgType.图片: case WechatMsgType.文件: case WechatMsgType.红包: case WechatMsgType.表情: case WechatMsgType.名片: case WechatMsgType.小程序: case WechatMsgType.链接: case WechatMsgType.群邀请: mess = messageType.ToString(); break; default: mess = "未知消息"; break; } var quoteXml = "[回复内容]view570001[引用的消息id][群号][引用用户的微信id][引用用户的昵称]<msgsource><silence>1</silence><membercount>7</membercount></msgsource>[引用的内容]000000nullnull0nullnullnullnull00000000000" .Replace("[回复内容]", replyMess) .Replace("[引用的消息id]", quoteMsgid) .Replace("[引用用户的微信id]", quoteUsername) .Replace("[引用用户的昵称]", quoteUsernick) .Replace("[引用的内容]", mess) .Replace("[群号]", username); SendXml(username, quoteXml); } public override void SendLinkMessage(string username, string title, string desc, string url, string imageUrl, string customSource) { var xml = @" [标题] [描述] view 5 0 [地址] [图片地址] 0 " .Replace("[标题]", title) .Replace("[描述]", desc) .Replace("[地址]", url) .Replace("[图片地址]", imageUrl); SendXml(username, xml); } public override void InviteMemberToGroup_40Down(string groupid, string[] usernames) { if (usernames != null && usernames.Length != 0) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_INVITE_TO_ROOM_MSG, data = new { room_wxid = groupid, member_list = usernames } }); SendWeChatData(User.DwClientId, json); } } public override void InviteMemberToGroup_40Up(string groupid, string[] usernames) { if (usernames != null && usernames.Length != 0) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_INVITE_TO_ROOM_REQ_MSG, data = new { room_wxid = groupid, member_list = usernames } }); SendWeChatData(User.DwClientId, json); } } public override void ModifyGroupMyNick(string groupid, string newNick) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_MOD_ROOM_NOTICE_MSG, data = new { room_wxid = groupid, notice = newNick } }); SendWeChatData(User.DwClientId, json); } public override void AutoAcceptGroup(bool isAuto) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_AUTO_ACCEPT_ROOM_MSG, data = new { auto = isAuto ? 1 : 0 } }); SendWeChatData(User.DwClientId, json); } public override void ModifyGroupShowNick(string groupid, bool isShow) { try { var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_MOD_ROOM_SHOW_NAME_MSG, data = new { room_wxid = groupid, status = isShow ? 1 : 0 } }); SendWeChatData(this.User.DwClientId, json); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void SaveGroupToContact(string groupid, bool status) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_SAVE_ROOM_TO_CONTACT_MSG, data = new { room_wxid = groupid, status = status ? 1 : 0 } }); SendWeChatData(User.DwClientId, json); } public override void ForwardAny(string username, string msgid) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_FORWARD_ANY_MSG, data = new { to_wxid = username, msgid = msgid } }); SendWeChatData(User.DwClientId, json); } public override string TransVoice(string msgid) { try { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_TRANS_VOICE_MSG, data = new { msgid = msgid } }); SendWeChatData(User.DwClientId, json); return Task.Factory.StartNew(delegate () { DateTime endtime = DateTime.Now.AddSeconds(20); do { var text = Common.GetCache($"TransVoiceResult-Wechat-{msgid}"); if (!string.IsNullOrEmpty(text)) { return text; } Thread.Sleep(500); } while (endtime > DateTime.Now); return null; }).Result; } catch (Exception ex) { LogHelper.GetSingleObj().Error($"语音转文本", ex.Message); } return null; } public override void ModifyRecvNotify(string username, bool status) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_MOD_RECV_NOTIFY_MSG, data = new { wxid = username, status = status ? 1 : 0 } }); SendWeChatData(User.DwClientId, json); } public override void ModifyChatTop(string username, bool status) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_MOD_CHAT_SESSION_TOP_MSG, data = new { wxid = username, status = status ? 1 : 0 } }); SendWeChatData(User.DwClientId, json); } public override void AddPublicUser(string username, string source_type = "") { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_ADD_PUBLIC_USER_MSG, data = new { wxid = username, source_type = source_type } }); SendWeChatData(User.DwClientId, json); } public override void AddFriend(string username, string remark, int source_type, string groupid) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_ADD_FRIEND_MSG, data = new { wxid = username, remark = remark, source_type = source_type, groupid = groupid } }); SendWeChatData(User.DwClientId, json); } public override void ModifyFriendRemark(string username, string remark) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_MOD_FRIEND_REMARK_MSG, data = new { wxid = username, remark = remark } }); SendWeChatData(User.DwClientId, json); } public override void DeleteFriend(string username) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_DEL_FRIEND_MSG, data = new { wxid = username } }); SendWeChatData(User.DwClientId, json); } //public override Task AcceptFriend(string username, string encryptusername, string ticket, string scene) public override void AcceptFriend(string text) { var v1 = string.Empty; var v2 = string.Empty; var scene = 0; var reg = Regex.Match(text, @"encryptusername=\\?""([^""]+?)\\?"""); if (reg.Success) v1 = reg.Groups[1].Value; reg = Regex.Match(text, @"ticket=\\?""([^""]+?)\\?"""); if (reg.Success) v2 = reg.Groups[1].Value; reg = Regex.Match(text, @"scene=\\?""([^""]+?)\\?"""); if (reg.Success) scene = int.Parse(reg.Groups[1].Value); LogHelper.GetSingleObj().Info("------", $"v1 = {v1} v2 = {v1} scene = {scene}"); var json = CsharpHttpHelper.HttpHelper.ObjectToJson(new { type = MsgType.MT_ACCEPT_FRIEND_MSG, data = new { v1 = v1, v2 = v2, scene = scene } }); //var json = CsharpHttpHelper.HttpHelper.ObjectToJson(new { type = MsgType.MT_ACCEPT_FRIEND_MSG, data = new { v1 = v1, v2 = v2 } }); SendWeChatData(User.DwClientId, json); } public override void AcceptFriendTransfer(string data) { try { var transferid = string.Empty; var dic = HttpExtend.JsonToDictionary(data); if (dic.ContainsKey("Transferid")) transferid = dic["Transferid"].ToString(); var json = CsharpHttpHelper.HttpHelper.ObjectToJson(new { type = MsgType.MT_ACCEPT_WCPAY_MSG, data = new { transferid = transferid } }); SendWeChatData(User.DwClientId, json); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void RefuseFriendTransfer(string transferid) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_REFUSE_FRIEND_WCPAY_MSG, data = new { transferid = transferid } }); SendWeChatData(User.DwClientId, json); } public override void AcceptFriendInviteToGroup(string username, string inviteUrl) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_ACCEPT_ROOM_MSG, data = new { wxid = username, invite_url = inviteUrl } }); SendWeChatData(User.DwClientId, json); } public override void AddOnewayFriend(string username) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_ACCEPT_ONEWAY_FRIEND_MSG, data = new { wxid = username } }); SendWeChatData(User.DwClientId, json); } public override Task> GetFriendInfos() { try { var type = RobotIsRunType.获取好友列表; //这里判断是否已经存在在获取所有好友信息,没有就去获取 if (!User.IsGetDic.ContainsKey(type)) { User.IsGetDic[type] = false;//这里添加要查询类型,状态为flase(还没收到微信那边返回的数据) var json = JsonConvert.SerializeObject(new { type = MsgType.MT_DATA_FRIENDS_MSG, data = new { } }); SendWeChatData(User.DwClientId, json); } return Task.Factory.StartNew>(delegate () { DateTime endtime = DateTime.Now.AddSeconds(20); do { if (User.IsGetDic.ContainsKey(type) && User.IsGetDic[type])//等待收到微信查询的消息 { do { if (!User.IsGetDic.ContainsKey(type))//等到解析完消息,解析完消息IsGetDic中的消息会被删除 { return User.Friends; } Thread.Sleep(200); } while (endtime > DateTime.Now); break; } Thread.Sleep(20); } while (endtime > DateTime.Now); return null; }); } catch (Exception ex) { LogHelper.GetSingleObj().Error($"获取好友列表", ex.Message); } return Task.FromResult>(null); } public override Task UpdateFriendInfo(string username) { //这里判断是否已经存在在获取的群邀请信息,没有就去获取 if (!User.IsGetContactDic.ContainsKey(username)) { User.IsGetContactDic[username] = false;//这里添加要查询的群ID,状态为flase(还没收到微信那边返回的数据) var json = JsonConvert.SerializeObject(new { type = MsgType.MT_UPDATE_WXID_MSG, data = new { wxid = username } }); SendWeChatData(User.DwClientId, json); } return Task.Factory.StartNew(delegate () { DateTime endtime = DateTime.Now.AddSeconds(20); do { if (User.IsGetContactDic.ContainsKey(username) && User.IsGetContactDic[username])//等待收到微信查询的消息 { do { if (!User.IsGetContactDic.ContainsKey(username))//等到解析完消息,解析完消息IsGetContactDic中的消息会被删除 { if (User.Friends.ContainsKey(username)) return User.Friends[username]; return null; } Thread.Sleep(200); } while (endtime > DateTime.Now); break; } Thread.Sleep(20); } while (endtime > DateTime.Now); return null; }); } public override void UpdateGroupMemberInfo(string groupid) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_UPDATE_ROOM_MEMBER_MSG, data = new { room_wxid = groupid } }); SendWeChatData(User.DwClientId, json); } public override Task CheckZombie(string username) { try { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_ZOMBIE_CHECK_MSG, data = new { wxid = username } }); SendWeChatData(User.DwClientId, json); return Task.Factory.StartNew(delegate () { DateTime endtime = DateTime.Now.AddSeconds(20); do { var text = Common.GetCache($"CheckZombieResult-Wechat-{User.Wxid}-{username}"); if (!string.IsNullOrEmpty(text)) { return text; } Thread.Sleep(500); } while (endtime > DateTime.Now); return null; }); } catch (Exception ex) { LogHelper.GetSingleObj().Error($"僵尸粉检测:{username}", ex.Message); } return Task.FromResult(null); } public override void AutoAcceptFriend(bool isAuto) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_AUTO_ACCEPT_FRIEND_MSG, data = new { auto = isAuto ? 1 : 0 } }); SendWeChatData(User.DwClientId, json); } public override void AutoAcceptFriendTransfer(bool isAuto) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_AUTO_ACCEPT_WCPAY_MSG, data = new { auto = isAuto ? 1 : 0 } }); SendWeChatData(User.DwClientId, json); } public override void AutoAcceptCard(bool isAuto) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_AUTO_ACCPET_CARD_MSG, data = new { auto = isAuto ? 1 : 0 } }); SendWeChatData(User.DwClientId, json); } public override void DecryptImage(string src_file, string dest_file) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_DECRYPT_IMG_MSG, data = new { src_file = src_file, dest_file = dest_file } }); SendWeChatData(User.DwClientId, json); } public override void OpenBrowser(string url) { url = url.Trim(); if (!string.IsNullOrWhiteSpace(url)) { var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_OPEN_BROWSER_MSG, data = new { url = url } }); SendWeChatData(User.DwClientId, json); } } public override Task GetUnReadMsgCount() { try { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_UNREAD_MSG_COUNT_CHANGE_MSG, data = new { } }); SendWeChatData(User.DwClientId, json); return Task.Factory.StartNew(delegate () { DateTime endtime = DateTime.Now.AddSeconds(20); do { var text = Common.GetCache($"UnReadMsgCountResult-Wechat-{User.Wxid}"); if (!string.IsNullOrEmpty(text)) { return int.Parse(text); } Thread.Sleep(500); } while (endtime > DateTime.Now); return 0; }); } catch (Exception ex) { LogHelper.GetSingleObj().Error($"获取未读取消息数量:{User.Nickname}({User.Wxid})", ex.Message); } return Task.FromResult(0); } public override void SetMsgReaded(string username) { try { var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_SET_SESSION_READED_MSG, data = new { wxid = username } }); SendWeChatData(this.User.DwClientId, json); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void ToChatWindow(string username) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_TO_CHAT_MSG, data = new { to_wxid = username } }); SendWeChatData(this.User.DwClientId, json); } public override void ClearChatHistoryMsg() { try { var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_CLEAR_CHAT_HISTORY_MSG, data = new { } }); SendWeChatData(this.User.DwClientId, json); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void AutoAuthMiniAPPLogin(string appid) { try { var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_SNS_AUTO_AUTH_MINIAPP_LOGIN, data = new { appid = appid } }); SendWeChatData(User.DwClientId, json); var result = Task.Factory.StartNew(delegate () { DateTime end_time = DateTime.Now.AddSeconds(20); do { if (User.AutoAuthLoginMiNiApps.ContainsKey(appid)) { return User.AutoAuthLoginMiNiApps[appid]; } Thread.Sleep(400); } while (end_time > DateTime.Now); return null; }).Result; var socketClient = EasySoc.GetSocket(); var msg = new CommonResult(); msg.RobotUsername = User.Wxid; msg.RobotUsernick = User.Nickname; msg.RobotType = RobotType.客户端微信; msg.MsgId = ServerMsgID; msg.Cmd = PCRobotCMD.rcvMiniAppCode; msg.Data = result; socketClient.Send(msg); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void DisableRevoke(bool status) { try { var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_DISABLE_REVOKE_MSG, data = new { status = status ? 1 : 0 } }); SendWeChatData(this.User.DwClientId, json); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void AddSearchContact(string v1, string v2, string remark, int source_type) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_ADD_SEARCH_CONTACT_MSG, data = new { v1 = v1, v2 = v2, remark = remark, source_type = source_type } }); SendWeChatData(this.User.DwClientId, json); } public override void GerFriendDetails(string username) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_UPDATE_WXID_MSG, data = new { wxid = username } }); SendWeChatData(this.User.DwClientId, json); } public override void CheckUrlStatus(string url) { try { url = url.Trim(); if (!string.IsNullOrWhiteSpace(url)) { var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_CHECK_URL_STATUS_MSG, data = new { url = url } }); //var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_CHECK_URL_STATUS2_MSG, data = new { url = url } }); SendWeChatData(this.User.DwClientId, json); } var result = Task.Factory.StartNew(delegate () { var awaitTime = DateTime.Now.AddSeconds(10); do { Thread.Sleep(200); //if (CheckUrlCache.ContainsKey(url)) return CheckUrlCache[url]; Cache cache = HttpRuntime.Cache; object item = cache[url]; if (item != null) return int.Parse(item.ToString()); } while (awaitTime >= DateTime.Now); return -1; }).Result; var socketClient = EasySoc.GetSocket(); var msg = new CheckUrl(); msg.RobotUsername = User.Wxid; msg.RobotUsernick = User.Nickname; msg.RobotType = RobotType.客户端微信; msg.MsgId = ServerMsgID; msg.Cmd = PCRobotCMD.rcvCheckUrl; msg.Status = result; socketClient.Send(msg); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } } public override void Ser_GetCircles(string max_id = "0", string username = "") { var result = GetCircles(max_id, username).Result; var socketClient = EasySoc.GetSocket(); var msg = new CommonResult(); msg.RobotUsername = User.Wxid; msg.RobotUsernick = User.Nickname; msg.RobotType = RobotType.客户端微信; msg.MsgId = ServerMsgID; msg.Cmd = PCRobotCMD.rcvCircleData; msg.Data = result; socketClient.Send(msg); } public override Task GetCircles(string max_id = "0", string username = "") { try { var key = username; if (!string.IsNullOrWhiteSpace(username)) { User.IsOperCirCleInfoDic[key] = false; var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_SNS_TIMELINE_USER_MSG, data = new { username = username, first_page_md5 = "", max_id = max_id } }); SendWeChatData(User.DwClientId, json); } else { key = "0"; //这里判断是否已经存在在获取的群邀请信息,没有就去获取 if (!User.IsOperCirCleInfoDic.ContainsKey(key)) { User.IsOperCirCleInfoDic[key] = false;//这里添加要查询的群ID,状态为flase(还没收到微信那边返回的数据) var json = JsonConvert.SerializeObject(new { type = MsgType.MT_SNS_TIMELINE_MSG, data = new { max_id = max_id } }); SendWeChatData(User.DwClientId, json); } } return Task.Factory.StartNew(delegate () { DateTime endtime = DateTime.Now.AddSeconds(20); do { if (User.IsOperCirCleInfoDic.ContainsKey(key) && User.IsOperCirCleInfoDic[key])//等待收到微信查询的消息 { do { if (!User.IsOperCirCleInfoDic.ContainsKey(key))//等到解析完消息,解析完消息IsOperCirCleInfoDic中的消息会被删除 { if (User.CirCleInfoOriDic.ContainsKey(key)) return User.CirCleInfoOriDic[key]; return null; } Thread.Sleep(200); } while (endtime > DateTime.Now); break; } Thread.Sleep(20); } while (endtime > DateTime.Now); return null; }); } catch (Exception ex) { LogHelper.GetSingleObj().Error($"获取朋友圈数据,{max_id} - {username}", ex.Message); } return Task.FromResult(null); } public override void CircleLink(string object_id) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_SNS_LIKE_MSG, data = new { object_id = object_id } }); SendWeChatData(User.DwClientId, json); } public override void CircleComment(string object_id, string content) { if (string.IsNullOrWhiteSpace(content)) return; var comments = content.Split(new string[] { "[分段]" }, StringSplitOptions.RemoveEmptyEntries); foreach (var comment in comments) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_SNS_COMMENT_MSG, data = new { object_id = object_id, content = comment } }); SendWeChatData(User.DwClientId, json); Thread.Sleep(1000); } } public override void CircleUpLoadImage(string data) { try { var path = string.Empty; Bitmap bitmap = null; if (data.StartsWith("http", StringComparison.CurrentCultureIgnoreCase)) { HttpHelper http = new HttpHelper(); var item = http.GetItem(data); item.ResultType = CsharpHttpHelper.Enum.ResultType.Byte; using (MemoryStream ms = new MemoryStream(http.GetHtml(item).ResultByte)) { using (Image mImage = Image.FromStream(ms, true)) { bitmap = new Bitmap(mImage); path = HttpExtend.MapFile($@"{DateTime.Now.ToString("MMddhhmmssfff")}.{Common.GetImageExt(bitmap)}", @"Cache\IMAGE"); bitmap.Save(path); bitmap.Dispose(); } } } else { bitmap = Common.ConvertBase64ToImage(data); using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, ImageFormat.Jpeg); byte[] data1 = new byte[stream.Length]; stream.Seek(0, SeekOrigin.Begin); stream.Read(data1, 0, Convert.ToInt32(stream.Length)); bitmap = Common.CompressionImage(new MemoryStream(data1), 25); } path = HttpExtend.MapFile($@"{DateTime.Now.ToString("MMddhhmmssfff")}.{Common.GetImageExt(bitmap)}", @"Cache\IMAGE"); bitmap.Save(path); bitmap.Dispose(); } if (!string.IsNullOrWhiteSpace(path) && File.Exists(path)) { LogHelper.GetSingleObj().Info(new StackTrace().GetFrame(0).GetMethod().Name, $"开始上传朋友圈图片:" + path); var json = HttpHelper.ObjectToJson(new { type = MsgType.MT_SNS_UPLOAD_IMAGE_MSG, data = new { path = path } }); SendWeChatData(User.DwClientId, json); var result = Task.Factory.StartNew(delegate () { DateTime end_time = DateTime.Now.AddSeconds(60); do { if (User.UpLoadImages.ContainsKey(path)) { return User.UpLoadImages[path]; } Thread.Sleep(400); } while (end_time > DateTime.Now); return null; }).Result; var socketClient = EasySoc.GetSocket(); var msg = new CommonResult(); msg.RobotUsername = User.Wxid; msg.RobotUsernick = User.Nickname; msg.RobotType = RobotType.客户端微信; msg.MsgId = ServerMsgID; msg.Cmd = PCRobotCMD.rcvCircleUploadImageUrl; msg.Data = result; socketClient.Send(msg); } else { } } catch (Exception ex) { LogHelper.GetSingleObj().Error(new StackTrace().GetFrame(0).GetMethod().Name, $"上传朋友圈图片异常:{ex.Message},{ex.StackTrace}"); } } public override void SendCircle(string object_desc) { try { var json = CsharpHttpHelper.HttpHelper.ObjectToJson(new { type = MsgType.MT_SNS_POST_MSG, data = new { object_desc = object_desc } }); User.SendCircleId = string.Empty; SendWeChatData(User.DwClientId, json); var result = Task.Factory.StartNew(delegate () { DateTime end_time = DateTime.Now.AddSeconds(20); do { if (!string.IsNullOrWhiteSpace(User.SendCircleId)) { return User.SendCircleId; } Thread.Sleep(400); } while (end_time > DateTime.Now); return null; }).Result; var socketClient = EasySoc.GetSocket(); var msg = new CommonResult(); msg.RobotUsername = User.Wxid; msg.RobotUsernick = User.Nickname; msg.RobotType = RobotType.客户端微信; msg.MsgId = ServerMsgID; msg.Cmd = PCRobotCMD.rcvCircleReturnId; msg.Data = result; socketClient.Send(msg); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, ex.Message); } finally { User.SendCircleId = string.Empty; } } } }