using System; using System.Collections.Generic; using System.Diagnostics; 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 CsharpHttpHelper; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using PCRobot.Entitys.Enterprise; using PCRobot.Pack; using PCRobot.Utils; namespace PCRobot.PCWechat.Enterprise { public partial class Wechat_Xiaoxie_QY { public override void GetContact(bool isUpdata) { try { var result = GetExternalFirends().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_workWeChat; 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 SendMessage(string username, string message, string msgId = "", string usernick = "", string quoteMess = "") { try { //S:1688853956558440_7881301364988874 //{"data":{"content":"我们","content_type":2,"conversation_id":"S:1688853956558440_7881301364988874","local_id":"61","receiver":"1688853956558440","send_time":"1584028171","sender":"7881301364988874","sender_name":"18","server_id":"1000653"},"type":11041} var conversation_id = username; if (!username.StartsWith("R:")) { conversation_id = $"S:{User.Wxid}_{username}"; } else { } var json = string.Empty; message = message.Replace("\r\n", "\n"); if (Common.isXML(message)) { 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.Replace("(?<链接>[\w\W]+?)"); if (!reg.Success) 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; if (message.Contains("")) { var image_key1 = string.Empty; var image_key2 = string.Empty; var image_key3 = string.Empty; var image_size = 0; var ghid = string.Empty; var pagepath = string.Empty; var regKey = Regex.Match(message, @"(?[\w\W]+?)", RegexOptions.IgnoreCase); if (regKey.Success) image_key1 = regKey.Groups["image_key1"].Value; regKey = Regex.Match(message, @"(?[\w\W]+?)", RegexOptions.IgnoreCase); if (regKey.Success) image_key2 = regKey.Groups["image_key2"].Value; regKey = Regex.Match(message, @"(?[\w\W]+?)", RegexOptions.IgnoreCase); if (regKey.Success) image_key3 = regKey.Groups["image_key3"].Value; regKey = Regex.Match(message, @"(?[\w\W]+?)", RegexOptions.IgnoreCase); if (regKey.Success) image_size = int.Parse(regKey.Groups["image_size"].Value.XMLReplace(false)); regKey = Regex.Match(message, @"(?[\w\W]+?)", RegexOptions.IgnoreCase); if (regKey.Success) ghid = regKey.Groups["username"].Value; regKey = Regex.Match(message, @"(?[\w\W]+?)", RegexOptions.IgnoreCase); if (regKey.Success) pagepath = regKey.Groups["pagepath"].Value; var weapppagethumbrawurl = string.Empty; regKey = Regex.Match(message, @"(?[\w\W]+?)", RegexOptions.IgnoreCase); if (regKey.Success) weapppagethumbrawurl = regKey.Groups["weapppagethumbrawurl"].Value; //小程序 SendProgramMessage(conversation_id, ghid.XMLReplace(false), ghid.XMLReplace(false), title, image_url, desc, pagepath); return; } else { image_url = json = CsharpHttpHelper.HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_LINK_MSG, data = new { conversation_id = conversation_id, title = $"{title.XMLReplace(false)}", desc = $"{desc.XMLReplace(false)}", url = url.XMLReplace(false), image_url = image_url.XMLReplace(false) } }); } } else { var atUsername = new List(); var regs = Regex.Matches(message, @"\[@([^\]]+?)\]"); if (regs.Count != 0 && username.IsQun()) { foreach (Match item in regs) { atUsername.Add(item.Groups[1].Value.Trim()); } message = Regex.Replace(message, @"(\[@[^\]]+?\])", "{$@}"); json = CsharpHttpHelper.HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_ROOM_AT_MSG, data = new { conversation_id = conversation_id, content = message, at_list = atUsername.ToArray() } }); } else json = CsharpHttpHelper.HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_TEXT_MSG, data = new { conversation_id = conversation_id, content = message } }); } SendWxWorkData(User.DwClientId, json); //LogHelper.GetSingleObj().Debug("发送状态", ($"发送:{conversation_id} - {flag}, 内容:{message}")); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, ex.Message + "-."); } } public override void SendFile(string username, string file) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_SEND_FILE_MSG, data = new { conversation_id = GetConversationId(username), file = file } }); SendWxWorkData(User.DwClientId, json); } public override void SendCard(string username, string cardwxid) { //群名片 if (cardwxid.IsQun()) { if (!username.IsQun()) InviteMemberToGroup(cardwxid, new string[] { username }); return; } var json = JsonConvert.SerializeObject(new { type = MsgType.MT_SEND_PERSON_CARD_MSG, data = new { conversation_id = GetConversationId(username), share_user_id = cardwxid } }); SendWxWorkData(User.DwClientId, json); } public override void SendLinkMessage(string username, string title, string desc, string url, string image_url) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_SEND_LINK_MSG, data = new { conversation_id = GetConversationId(username), url = url, image_url = image_url, title = title, desc = desc } }); SendWxWorkData(User.DwClientId, json); } public override void SendImage(string wxid, string file) { //var json = string.Empty; //if (imgPath.EndsWith(".gif", StringComparison.CurrentCultureIgnoreCase | StringComparison.OrdinalIgnoreCase)) // json = JsonConvert.SerializeObject(new { type = MsgType.MT_SEND_GIF_MSG, data = new { conversation_id = GetConversationId(username), file = imgPath } }); //else // json = JsonConvert.SerializeObject(new { type = MsgType.MT_SEND_IMAGE_MSG, data = new { conversation_id = GetConversationId(username), file = imgPath } }); //SendWxWorkData(User.DwClientId, json); try { var conversation_id = wxid; if (!wxid.StartsWith("R:")) conversation_id = $"S:{User.Wxid}_{wxid}"; 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, conversation_id, "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.Exists(file)) { if (file.Trim().EndsWith(".gif", StringComparison.CurrentCultureIgnoreCase)) json = HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_GIF_MSG, data = new { conversation_id = conversation_id, file = file } }); else json = HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_IMAGE_MSG, data = new { conversation_id = conversation_id, file = file } }); } else json = HttpHelper.ObjectToJson(new { type = MsgType.MT_SEND_IMAGE_MSG, data = new { conversation_id = conversation_id, file = file } }); SendWxWorkData(User.DwClientId, json); } catch (Exception ex) { LogHelper.GetSingleObj().Debug("发送图片异常", ex.Message + ".-" + ex.StackTrace); } } public override void SendVideo(string username, string path) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_SEND_VIDEO_MSG, data = new { conversation_id = GetConversationId(username), file = path } }); SendWxWorkData(User.DwClientId, json); } public override void ModifyGroupNotice(string groupid, string notice) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_MOD_ROOM_NOTICE_MSG, data = new { room_conversation_id = GetConversationId(groupid), notice = notice } }); SendWxWorkData(User.DwClientId, json); } 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_workWeChat; 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 }); SendWxWorkData(User.DwClientId, json); } var result = 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 null; } public override void DeleteGroupMember(string groupid, string[] usernames) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_ROOM_DEL_MEMBERS_MSG, data = new { conversation_id = groupid, user_list = usernames } }); SendWxWorkData(User.DwClientId, json); } public override void QuitAndDelGroup(string groupid) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_QUIT_ROOM, data = new { room_conversation_id = GetConversationId(groupid) } }); SendWxWorkData(User.DwClientId, json); } public override void ModifyGroupName(string groupid, string newName) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_MODIFY_ROOM_NAME_MSG, data = new { conversation_id = groupid, name = newName } }); SendWxWorkData(User.DwClientId, json); } public override void Ser_GetSelfInfo() { var result = GetSelfInfo().Result; var data = string.Empty; if (result != null) data = HttpHelper.ObjectToJson(result as WorkFriendInfo); var socketClient = EasySoc.GetSocket(); var msg = new WorkWechatContact(); msg.RobotUsername = User.Wxid; msg.RobotUsernick = User.Nickname; msg.RobotType = RobotType.客户端企业微信; msg.MsgId = ServerMsgID; msg.Cmd = PCRobotCMD.rcvContact_workWeChat; msg.Message = result == null ? string.Empty : HttpHelper.ObjectToJson(result); 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_GET_OWNER_INFO_MSG, data = new { } }); SendWxWorkData(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.WorkFriends.ContainsKey(User.Wxid)) return User.WorkFriends[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 null; } /// /// 获取群或者好友信息 /// /// public override void Ser_GetContact(string username) { var data = string.Empty; if (User.WorkFriends.ContainsKey(username)) data = HttpHelper.ObjectToJson(User.WorkFriends[username]); else { if (username.IsQun()) { var g = GetGroupInfos().Result; if (User.WorkFriends.ContainsKey(username)) data = HttpHelper.ObjectToJson(User.WorkFriends[username]); } else { var result = GetFriendInfo(username).Result; if (result != null) data = HttpHelper.ObjectToJson((result as WorkFriendInfo)); } } var socketClient = EasySoc.GetSocket(); var msg = new WorkWechatContact(); msg.RobotUsername = User.Wxid; msg.RobotUsernick = User.Nickname; msg.RobotType = RobotType.客户端企业微信; msg.MsgId = ServerMsgID; msg.Cmd = PCRobotCMD.rcvContact_workWeChat; 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_GET_USER_DETAIL_MSG, data = new { user_id = username } }); SendWxWorkData(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.WorkFriends.ContainsKey(username)) return User.WorkFriends[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 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 WorkGroupInfo; if (g == null) return null; var ftmp = g.friends.FirstOrDefault(f => f.user_id == userName); if (ftmp != null) return new WorkGroupInfo() { conversation_id = groupId, friends = new List() { ftmp }, total = User.WorkGroups[groupId].total, GroupNick = User.WorkGroups[groupId].GroupNick }; } if (User.WorkGroups.ContainsKey(groupId)) { if (User.WorkGroups[groupId].friends == null) return null; var ftmp = User.WorkGroups[groupId].friends.FirstOrDefault(f => f.user_id == userName); if (ftmp != null) return new WorkGroupInfo() { conversation_id = groupId, friends = new List() { ftmp }, total = User.WorkGroups[groupId].total, GroupNick = User.WorkGroups[groupId].GroupNick }; } } catch (Exception ex) { LogHelper.GetSingleObj().Error($"企业获取群用户信息", ex.Message); } return null; }).Result; var socketClient = EasySoc.GetSocket(); var msg = new WorkWechatContact(); msg.RobotUsername = User.Wxid; msg.RobotUsernick = User.Nickname; msg.RobotType = RobotType.客户端企业微信; msg.MsgId = ServerMsgID; msg.Cmd = PCRobotCMD.rcvContact_workWeChat; 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_GET_ROOMS_MSG, data = new { page_num = 1, page_size = 100000 } }); SendWxWorkData(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.WorkGroups != null) { foreach (var item in User.WorkGroups) { 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_GET_ROOM_MEMBERS_MSG, data = new { conversation_id = groupid, page_num = 1, page_size = 100000 } }); SendWxWorkData(User.DwClientId, json); } return Task.Factory.StartNew(delegate () { DateTime endtime = DateTime.Now.AddSeconds(20); do { try { if (User.IsGetContactDic.ContainsKey(groupid) && User.IsGetContactDic[groupid])//等待收到微信查询的消息 { do { if (!User.IsGetContactDic.ContainsKey(groupid))//等到解析完消息,解析完消息IsGetContactDic中的消息会被删除 { if (User.WorkGroups.ContainsKey(groupid)) return User.WorkGroups[groupid]; } Thread.Sleep(200); } while (endtime > DateTime.Now); break; } } catch (Exception ex) { } Thread.Sleep(20); } while (endtime > DateTime.Now); return null; }); } catch (Exception ex) { LogHelper.GetSingleObj().Error($"企业获取指定群组成员信息", $"群:{groupid} - {ex.Message} - {ex.StackTrace}"); } return Task.FromResult(null); } public override void OutLogin() { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_QUIT_LOGIN_MSG, data = new { } }); SendWxWorkData(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_QY).Value; if (v != null) { LogHelper.GetSingleObj().Info("系统", $"CloseWechatQY事件退出,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 System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, ex.Message + "//"); } } public override string GetVersion() { return Version; } public override void SendDirectory(string username, string dirPath) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_SEND_FOLDER_MSG, data = new { conversation_id = GetConversationId(username), folder = dirPath } }); SendWxWorkData(User.DwClientId, json); } public override void SendProgramMessage(string username, string msg) { //new { data = new { conversation_id = username, ghid = ghid, wxid = wxid, name = name, title = title, enterpoint = enterpoint, headimg = headimg, image_key1 = image_key1, image_key2 = image_key2, image_key3 = image_key3, image_size = image_size } } var jObj = JObject.Parse(msg); var data = jObj["data"]; var conversation_id = data["conversation_id"].ToString(); var ghid = data["ghid"].ToString(); var wxid = data["wxid"].ToString(); var name = data["name"].ToString(); var title = data["title"].ToString(); var enterpoint = data["enterpoint"].ToString(); var headimg = data["headimg"].ToString(); SendProgramMessage(username, ghid, wxid, name, headimg, title, enterpoint); } private void SendProgramMessage(string username, string appUsername, string appId, string appName, string appicon, string title, string page_path) { try { #region 数据结构 /* { "type":11162, "data":{ "conversation_id":"S:168885xxx28682_788130xxxx912998", "username":"gh_52b46203a658@app", "appid":"wx35f2a3937c9df889", "appname":"博鸟绘本", "appicon":"http:\/\/wx.qlogo.cn\/mmhead\/Q3auHgzwzM42Aa6BAq7rguLCx66IVTm7Qibr6paWic3rl9bfalX7gQLw\/96", "title":"博鸟绘本,最新最全的儿童绘本租赁平台~", "page_path":"pages\/shouye\/shouye.html", // cdn参数为小程序封面,可以从cdn上传图片处获得 "file_id":"3069020102046230600201000204283cabaa02030f424102043c986eb4020461967039042431376332326538612d656533342d346537642d623531332d37393762376436336531663702010002030ec8700410059b34e084cf19a38a92a677177863d20201010201000400", "aes_key":"61393132623366353662313133653735", "md5":"059b34e084cf19a38a92a677177863d2", "size": 11122 } } */ #endregion FileC2cCdnInfo cdn = GetCdnByUpLoadFile(appicon, 1).Result; if (cdn == null) throw new Exception("小程序图片获取Cdn失败"); var _json = JsonConvert.SerializeObject(new { type = Wechat_Xiaoxie_QY.MsgType.MT_SEND_MINI_PROGRAM_MSG, data = new { conversation_id = GetConversationId(username), username = appUsername, appid = appId, appName = appName, appicon = appicon, title = title, page_path = page_path, file_id = cdn.file_id, aes_key = cdn.file_aes_key, md5 = cdn.file_md5, size = cdn.file_size } }); SendWxWorkData(User.DwClientId, _json); } catch (Exception ex) { LogHelper.GetSingleObj().Error(new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name, $"发送小程序:{ex.Message}"); } } public override void SendVideoHao(string username, string avatar, string cover_url, string desc, int feed_type, string nickname, string thumb_url, string url, string extras) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_SEND_VIDEOHAO_MSG, data = new { conversation_id = GetConversationId(username), cover_url = cover_url, desc = desc, feed_type = feed_type, nickname = nickname, thumb_url = thumb_url, url = url, extras = extras } }); SendWxWorkData(User.DwClientId, json); } public override void TransRoom(string username, string user_id) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_TRANS_ROOM_MSG, data = new { room_conversation_id = GetConversationId(username), user_id = user_id } }); SendWxWorkData(User.DwClientId, json); } public override void OnOffRoomInvite(string username, bool status) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_DISABLE_INVITEROOMCONFIRM_MSG, data = new { room_conversation_id = GetConversationId(username), status = status ? 1 : 0 } }); SendWxWorkData(User.DwClientId, json); } public override void OnOffRoomModNick(string username, bool status) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_DISABLE_MODROOMNICK_MSG, data = new { room_conversation_id = GetConversationId(username), status = status ? 1 : 0 } }); SendWxWorkData(User.DwClientId, json); } public override void InviteMemberToGroup(string groupid, string[] usernames) { if (groupid.IsQun()) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_ROOM_ADD_MEMBERS_MSG, data = new { conversation_id = groupid, user_list = usernames } }); SendWxWorkData(User.DwClientId, json); } } public override void AddGroupMemberToFriend(string groupid, string username, string corp_id, string verify) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_ADD_ROOM_USER_MSG, data = new { room_conversation_id = groupid, user_id = username, corp_id = corp_id, verify = verify } }); SendWxWorkData(User.DwClientId, json); } public override void ModifyFriendRemark(string username, string remark) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_MODIFY_USER_REMARK_MSG, data = new { user_id = username, remark = remark } }); SendWxWorkData(User.DwClientId, json); } public override void ModifyFriendDesc(string username, string desc) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_MODIFY_USER_DESC_MSG, data = new { user_id = username, desc = desc } }); SendWxWorkData(User.DwClientId, json); } public override void ModifyFriendPhone(string username, string[] phone_list) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_MODIFY_EXTERNAl_USER_PHONES_MSG, data = new { user_id = username, phone_list = phone_list } }); SendWxWorkData(User.DwClientId, json); } public override void ModifyExternalFriendCompanyNick(string username, string company) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_MODIFY_EXTERNAl_USER_COMPANY_MSG, data = new { user_id = username, company = company } }); SendWxWorkData(User.DwClientId, json); } public override Task SearchContactMobile(string search) { try { //这里判断是否已经存在在获取的群邀请信息,没有就去获取 if (!User.IsGetContactDic.ContainsKey(search)) { User.IsGetContactDic[search] = false;//这里添加要查询的群ID,状态为flase(还没收到微信那边返回的数据) var json = JsonConvert.SerializeObject(new { type = MsgType.MT_SEARCH_USER_MSG, data = new { keyword = search } }); SendWxWorkData(User.DwClientId, json); } return Task.Factory.StartNew(delegate () { DateTime endtime = DateTime.Now.AddSeconds(20); do { try { if (User.IsGetContactDic.ContainsKey(search) && User.IsGetContactDic[search])//等待收到微信查询的消息 { do { if (!User.IsGetContactDic.ContainsKey(search))//等到解析完消息,解析完消息IsGetContactDic中的消息会被删除 { if (User.FriendHandlerDic.ContainsKey(search)) { var SeartchFriendInfo = User.FriendHandlerDic[search]; //if (SeartchFriendInfo == null)//如果搜索的时候为空,就直接删除 User.FriendHandlerDic.Remove(search);//处理完直接删除 return SeartchFriendInfo; } } Thread.Sleep(200); } while (endtime > DateTime.Now); break; } } catch (Exception ex) { } Thread.Sleep(20); } while (endtime > DateTime.Now); return null; }); } catch (Exception ex) { LogHelper.GetSingleObj().Error($"企业搜索用户信息信息", $"搜索:{search} - {ex.Message} - {ex.StackTrace}"); } return Task.FromResult(null); } public override void AddRoutineSearchContact(string username, string openid, string wx_ticket, string verify) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_ADD_SEARCH_USER_MSG, data = new { user_id = username, openid = openid, wx_ticket = wx_ticket, verify = verify } }); SendWxWorkData(User.DwClientId, json); } public override void AddEnterpriseSearchContact(string username, string corp_id, string verify) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_ADD_SEARCH_E_USER_MSG, data = new { user_id = username, corp_id = corp_id, verify = verify } }); SendWxWorkData(User.DwClientId, json); } public override void AddCardFriendContact(string username, string corp_id, string from_user_id, string verify) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_ADD_CARDFRIEND_MSG, data = new { user_id = username, corp_id = corp_id, from_user_id = from_user_id, verify = verify } }); SendWxWorkData(User.DwClientId, json); } public override void AddDelFriendContact(string username, string corp_id, string verify) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_ADD_DELFRIEND_MSG, data = new { user_id = username, corp_id = corp_id, verify = verify } }); SendWxWorkData(User.DwClientId, json); } public override void DeleteFriend(string username) { var ftmp = User.WorkFriends.FirstOrDefault(f => f.Key == username).Value; var json = JsonConvert.SerializeObject(new { type = MsgType.MT_DEL_FRIEND, data = new { user_id = username, corp_id = ftmp != null ? ftmp.corp_id : "0" } }); SendWxWorkData(User.DwClientId, json); } public override void AcceptFriend(string msg) { try { var reg = Regex.Match(msg, @"{""user_id"":""(?[^""]+?)"",""corp_id"":""(?[^""]+?)""}"); if (reg.Success) { var user_id = reg.Groups["user_id"].Value; var corp_id = reg.Groups["corp_id"].Value; var json = JsonConvert.SerializeObject(new { type = MsgType.MT_ACCEPT_FRIEND_REQUEST, data = new { user_id = user_id, corp_id = corp_id } }); SendWxWorkData(User.DwClientId, json); } } catch (Exception ex) { LogHelper.GetSingleObj().Error($"接受好友申请", ex.Message); } } public override Task CreateExternalGroup() { try { var type = RobotIsRunType.创建空外部群; //这里判断是否已经存在在获取的群邀请信息,没有就去获取 if (!User.IsGetDic.ContainsKey(type)) { User.IsGetDic[type] = false;//这里添加要查询的群ID,状态为flase(还没收到微信那边返回的数据) var json = JsonConvert.SerializeObject(new { type = MsgType.MT_CREATE_NULL_ROOM_MSG, data = new { } }); SendWxWorkData(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 Task> GetInnerFirends() //{ // try // { // var type = RobotIsRunType.获取内部好友列表; // //这里判断是否已经存在在获取所有好友信息,没有就去获取 // if (!User.IsGetDic.ContainsKey(type)) // { // User.IsGetDic[type] = false;//这里添加要查询类型,状态为flase(还没收到微信那边返回的数据) // var json = JsonConvert.SerializeObject(new { type = MsgType.MT_GET_INNER_USERS_MSG, data = new { page_num = 1, page_size = 100000 } }); // SendWxWorkData(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中的消息会被删除 // {//这里只返回外部的好友 // if (FriendDic != null) // { // var innerFirends = FriendDic.Where(f => f.Value.FriendType == EnterpriseFriendType.内部好友).ToDictionary(f => f.Key, z => z.Value); // return innerFirends; // } // return FriendDic; // } // 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> GetExternalFirends() { try { var type = RobotIsRunType.获取好友列表; //这里判断是否已经存在在获取所有好友信息,没有就去获取 if (!User.IsGetDic.ContainsKey(type)) { User.IsGetDic[type] = false;//这里添加要查询类型,状态为flase(还没收到微信那边返回的数据) var json = JsonConvert.SerializeObject(new { type = MsgType.MT_GET_EXTERNAL_USERS_MSG, data = new { page_num = 1, page_size = 100000 } }); SendWxWorkData(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中的消息会被删除 { if (User.WorkFriends != null) { return User.WorkFriends; } 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 Task GetOwnQrImage() //{ // try // { // var type = RobotIsRunType.获取自己的二维码; // //这里判断是否已经存在在获取所有好友信息,没有就去获取 // if (!User.IsGetDic.ContainsKey(type)) // { // QrImageInfo = null; // User.IsGetDic[type] = false;//这里添加要查询类型,状态为flase(还没收到微信那边返回的数据) // var json = JsonConvert.SerializeObject(new { type = MsgType.MT_GET_OWN_QRIMAGE, data = new { } }); // SendWxWorkData(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 QrImageInfo; // 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 FlushLoginQrImage() { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_FLUSH_LOGINQRIMAGE_MSG, data = new { } }); SendWxWorkData(User.DwClientId, json); } public override void DissolveGroup(string GroupId) { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_DISSOLVE_ROOM, data = new { room_conversation_id = GetConversationId(GroupId) } }); SendWxWorkData(User.DwClientId, json); } public override Task CdnDown(string url, string auth_key, string aes_key, int size, FileSuffixType fileSuffixType) { #region 数据结构 /* { "type":11171, "data":{ "url":"https:\/\/imunion.weixin.qq.com\/cgi-bin\/mmae-bin\/tpdownloadmedia?param=v1_84d696afdc73fbf997931c6eb898facc4830ae5cb891060fdd5cca540beccd2de56cbc84207058d80f2af266f925e458ca19263f710bc0905881589f0b816c5def9bb2a039e0127bb680b6f3c8db2f93abf477d369374598cea92a001adf3f5977ba47cfa347643a36b05f96c4fa54ebc7f4f00767a0ba2a859ac82b6c0e9efacd07eb5c15c098837f4a19f27a0a897a890394f88ae7dd215f75e4199ac4c5e9c893358e5aee215a07ac0882aad2e72e249cacaaf0e2929ebf15dc147fceb386f8560923cba106cea3f5c0548ace640455df853ab7606f6e88acf48cdcb9593d7873c487536327e8988d969a360da527c09d7765dfc382ed98071720c3b3ba108cc2c3504907d095575676333a64ae6a2fdf9a2af2138e1c0d953a56d6a3c281348a7e71fd10f7b80220e4b4a4c1473ac5f00e07d132ad000289d96bd8abc40a", "auth_key":"v1_84d696afdc73fbf997931c6eb898facc4830ae5cb891060fdd5cca540beccd2d2d7fa866eb7f785638f4c69ac9c904a43685d48a39b7162bbab469dc884dd1e8", "aes_key":"9385a744af20cba0e59a52d51e3cacd1", "size":16504, "save_path":"e:\\cdn\\3.jpg" } } */ #endregion return CdnDown(Common.CreateNewFilePath(fileSuffixType), url, auth_key, aes_key, size); } public override Task CdnDown(string url, string auth_key, string aes_key, int size, string fileName) { var filePath = HttpExtend.MapFile(fileName, $"Cache\\Other"); return CdnDown(filePath, url, auth_key, aes_key, size); } /// /// Cdn实际处理的地方 /// /// /// private Task CdnDown(string save_path, string url, string auth_key, string aes_key, int size) { try { var md5 = HttpExtend.GetMD5String(save_path); //这里判断是否已经存在在获取的群邀请信息,没有就去获取 if (!User.IsGetContactDic.ContainsKey(md5)) { User.IsGetContactDic[md5] = false;//这里添加要查询的群ID,状态为flase(还没收到微信那边返回的数据) var json = JsonConvert.SerializeObject(new { type = MsgType.MT_CDN_DOWN_MSG, data = new { url = url, auth_key = auth_key, aes_key = aes_key, size = size, save_path = save_path } }); SendWxWorkData(User.DwClientId, json); } return Task.Factory.StartNew(delegate () { DateTime endtime = DateTime.Now.AddSeconds(20); do { if (User.IsGetContactDic.ContainsKey(md5) && User.IsGetContactDic[md5])//等待收到微信查询的消息 { do { if (!User.IsGetContactDic.ContainsKey(md5)) //等到解析完消息,解析完消息IsGetContactDic中的消息会被删除 //return Common.GetCache(md5) ? save_path : null; return save_path; Thread.Sleep(200); } while (endtime > DateTime.Now); break; } Thread.Sleep(20); } while (endtime > DateTime.Now); return save_path; }); } catch (Exception ex) { LogHelper.GetSingleObj().Error($"企业Cdn下载", ex.Message); } return Task.FromResult(save_path); } public override Task C2cCdnDown(string aes_key, string file_id, int file_size, QyCdnFileType file_type, FileSuffixType fileSuffixType) { return C2cCdnDown(Common.CreateNewFilePath(fileSuffixType), aes_key, file_id, file_size, file_type); } private Task C2cCdnDown(string save_path, string aes_key, string file_id, int file_size, QyCdnFileType file_type) { #region 第一版,请求以后返回的数据异常..现在用第二版,等修复以后可以使用第一版 //try //{ // var md5 = HttpExtend.GetMD5String(save_path); // //这里判断是否已经存在在获取的群邀请信息,没有就去获取 // if (!IsGetContactDic.ContainsKey(md5)) // { // IsGetContactDic[md5] = false;//这里添加要查询的群ID,状态为flase(还没收到微信那边返回的数据) // var json = JsonConvert.SerializeObject(new { type = MsgType_QY.MT_C2CCDN_DOWN_MSG, data = new { aes_key = aes_key, file_id = file_id, save_path = save_path, file_size = file_size, file_type = (int)file_type } }); // SendClient(CoreRobotMsgEnum.Cdn文件下载, json); // } // return Task.Factory.StartNew(delegate () // { // DateTime endtime = DateTime.Now.AddSeconds(20); // do // { // if (IsGetContactDic.ContainsKey(md5) && IsGetContactDic[md5])//等待收到微信查询的消息 // { // do // { // if (!IsGetContactDic.ContainsKey(md5))//等到解析完消息,解析完消息IsGetContactDic中的消息会被删除 // return CacheClient.SigleClient.Cache.ContainsKey(md5) ? save_path : null; // Thread.Sleep(200); // } while (endtime > DateTime.Now); // break; // } // Thread.Sleep(20); // } while (endtime > DateTime.Now); // return null; // }); //} //catch (Exception ex) //{ // LogHelper.WriteLog($"企业小程序图片Cdn下载", ex); //} //return null; #endregion #region 第二版 try { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_C2CCDN_DOWN_MSG, data = new { aes_key = aes_key, file_id = file_id, save_path = save_path, file_size = file_size, file_type = (int)file_type } }); SendWxWorkData(User.DwClientId, json); return Task.Factory.StartNew(delegate () { DateTime endtime = DateTime.Now.AddSeconds(10); do { if (File.Exists(save_path)) return save_path; Thread.Sleep(500); } while (endtime > DateTime.Now); return save_path; }); } catch (Exception ex) { LogHelper.GetSingleObj().Error($"企业小程序图片Cdn下载", ex.Message); } #endregion return Task.FromResult(save_path); } public override Task BigCdnDown(string file_path, string file_type) { //SendWxWorkData(User.DwClientId, json); return null; } public override Task GetCdnByUpLoadFile(string file_path, int file_type) { try { var md5 = HttpExtend.GetMD5String(file_path); if (User.FileC2cCdnInfoDic.ContainsKey(md5)) return Task.FromResult(User.FileC2cCdnInfoDic[md5]); var imgPathTmp = Common.CreateNewFilePath(FileSuffixType.jpg, md5); //这里判断是否已经存在在获取的群邀请信息,没有就去获取 if (!User.IsGetContactDic.ContainsKey(md5)) { if (Common.DownFile(file_path, imgPathTmp)) //这里下载文件的名称以传入的文件本地路径或Url的md5,在收到回调后以这个为依据 { var json = JsonConvert.SerializeObject(new { type = MsgType.MT_GETCDN_UPLOADFILE, data = new { file_path = imgPathTmp, file_type = file_type } }); SendWxWorkData(User.DwClientId, json); } else throw new Exception("传递的文件信息无法解析,终止获取文件Cdn"); } return Task.Factory.StartNew(delegate () { DateTime endtime = DateTime.Now.AddSeconds(20); do { if (User.IsGetContactDic.ContainsKey(md5) && User.IsGetContactDic[md5])//等待收到微信查询的消息 { do { if (!User.IsGetContactDic.ContainsKey(md5))//等到解析完消息,解析完消息IsGetContactDic中的消息会被删除 { if (User.FileC2cCdnInfoDic.ContainsKey(md5)) return User.FileC2cCdnInfoDic[md5]; 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($"企业上传文件获取Cdn信息", ex.Message); } return Task.FromResult(null); } } }