using PCRobot.Pack; using PCRobot.Utils; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using PCRobot.PCWechat.Enterprise; using PCRobot.PCWechat.Routine; namespace PCRobot.PCWechat { public class WechatClient { public static bool IsDebug { get; set; } public static BeiyongType BeiYongT { get; set; } = BeiyongType.正版; public static bool IsGzh { get; set; } public static bool IsLog { get; set; } static WechatClient() { Users = new Dictionary(); } public static Dictionary Users { get; private set; } public delegate void RefUser(WechatUser user); /// /// 更新用户信息事件 /// public static RefUser RefUserEvent; public static void OnRefUserEvent(WechatUser user, Dictionary WaitLoginResult = null) { if (RefUserEvent != null) { RefUserEvent?.Invoke(user); var socketClient = EasySoc.GetSocket(); if (socketClient != null) { var msg = new WechatStatus(); msg.Cmd = user.Type == WechatType.Xiaoxie_QY ? PCRobotCMD.offline_workWeChat : PCRobotCMD.offline; msg.RobotUsername = user.Wxid; msg.RobotUsernick = user.Nickname; msg.RobotType = user.Type == WechatType.Xiaoxie_QY ? RobotType.客户端企业微信 : RobotType.客户端微信; msg.Status = Status.在线; msg.Uin = user.Uin; socketClient.Send(msg); } if (WaitLoginResult != null) { if (WaitLoginResult.ContainsKey(user.Pid.ToString())) { Thread.Sleep(50); var msgId = WaitLoginResult[user.Pid.ToString()]; var msg = new CommonResult(); msg.RobotUsername = user.Wxid; msg.RobotUsernick = user.Nickname; msg.RobotType = user.Type == WechatType.Xiaoxie_QY ? RobotType.客户端企业微信 : RobotType.客户端微信; msg.MsgId = msgId; msg.Cmd = user.Type == WechatType.Xiaoxie_QY ? PCRobotCMD.rcvLoginCode_workWeChat : PCRobotCMD.rcvLoginCode; msg.Data = "成功"; socketClient.Send(msg); } } new LuDogService().Handle(user); } } /// /// 获取用户 /// /// wxid /// 需要更新的DwClientId /// public static WechatUser GetUser(string wxid, uint dwClientId, WechatType type) { WechatUser u = null; if (Users.ContainsKey(wxid)) { u = Users[wxid]; } else { u = new WechatUser() { Wxid = wxid }; Users.Add(wxid, u); // RefUserEvent?.Invoke(u); } u.Type = type; u.DwClientId = dwClientId; return u; } /// /// 通过dwClientId获取登陆的机器人对象 /// /// /// /// public static WechatUser GetUser(uint dwClientId, WechatType type) { return Users.Values.FirstOrDefault(f => f.DwClientId == dwClientId && f.Type == type); } public static WechatUser GetUser(string wxid) { return Users.FirstOrDefault(f => f.Key == wxid).Value; } public static void RemoveUser(WechatUser user, bool kill = false) { try { var socketClient = EasySoc.GetSocket(); if (socketClient != null) { WechatStatus msg = new WechatStatus(); msg.Cmd = user.Type == WechatType.Xiaoxie_QY ? PCRobotCMD.offline_workWeChat : PCRobotCMD.offline; msg.RobotUsername = user.Wxid; msg.RobotUsernick = user.Nickname; msg.RobotType = user.Type == WechatType.Xiaoxie_QY ? RobotType.客户端企业微信 : RobotType.客户端微信; msg.Status = Status.退出; msg.Device = Common.GetDevice(); msg.Uin = user.Uin; socketClient.Send(msg); } if (Users.ContainsKey(user.Wxid)) { Users.Remove(user.Wxid); } Task.Run(() => { RefUserEvent?.Invoke(user); }); //RefUserEvent?.Invoke(user); if ((IntPtr)user.Pid != IntPtr.Zero && kill) { Process[] myproc = Process.GetProcesses(); foreach (Process items in myproc) { try { if (items.Id == user.Pid) { LogHelper.GetSingleObj().Info("系统", $"杀死进程.:{items.Id},Uin:{user.Uin},微信:{user.Nickname}({user.Wxid})"); items.Kill(); break; } } catch (Exception) { } } } } catch (Exception) { } } /// /// 是否为辅助输入,或者用户发送的消息 /// /// 消息来源 /// 发送给谁 /// 消息内容 /// public static bool IsAuxiliaryInput(string from_wxid, string to_wxid, string message) { //Console.WriteLine($"from_wxid:{from_wxid} to_wxid:{to_wxid} message:{message}"); //消息来源 - 为当前机器人列表,极有可能是辅助输入 //if (Users.ContainsKey(from_wxid) && !to_wxid.Contains("@chatroom")) if (from_wxid == to_wxid) return false; if (Users.ContainsKey(from_wxid)) { if (Regex.IsMatch(message.Trim(), @"[\w\W]*?([\w\W]*?)")) return false; var robot = Users[from_wxid];//机器人 //本消息,是否通过机器人自动发送过,如果为true. 表示非辅助输入 if (Common.GetCache(robot.Wxid, to_wxid, message)) return false; } //Console.WriteLine("z" + message); return true; } #region 其他 /// /// 判断是否为辅助 /// /// 发送者的微信 /// 接收者的微信 /// 1.如果发送者的微信为机器人,那么为辅助返回1。 2.如果发送者和接收者的微信都为机器人那么不操作,返回-1。 3.如果发送者非机器人,接受者为机器人,那么为客户输入,返回0 public static Assistance IsAssistance(string from_wxid, string to_wxid, uint dwClientId, WechatType wechatType = WechatType.Xiaoxie) { var ass = new Assistance() { type = AssistanceType.非辅助 }; try { var robotName = string.Empty; var robotNick = string.Empty; var fromName = string.Empty; var fromNaick = string.Empty;//TODO 昵称未赋值 var user = WechatClient.GetUser(dwClientId, wechatType); if (user != null && from_wxid == user.Wxid)//机器人自己发送的消息 { //if (to_wxid.Contains("@chatroom"))//在群里不直接终止 if (to_wxid.IsQun())//在群里不直接终止 { ass.robotName = from_wxid; ass.robotNick = user.Nickname; ass.fromName = from_wxid; ass.fromNick = user.Nickname; ass.type = AssistanceType.辅助; } else { //如果为辅助发送,需要将用户信息和机器人信息反回来 ass.robotName = from_wxid; ass.robotNick = user.Nickname; ass.fromName = to_wxid; ass.type = AssistanceType.辅助; } return ass; } else if (user != null) { ass.robotName = user.Wxid; ass.robotNick = user.Nickname; ass.fromName = from_wxid; } } catch (Exception ex) { } return ass; } /// /// 保存辅助结果 /// public class Assistance { public AssistanceType type { get; set; } = AssistanceType.非辅助; public string robotName { get; set; } = string.Empty; public string robotNick { get; set; } = string.Empty; public string fromName { get; set; } = string.Empty; public string fromNick { get; set; } = string.Empty;//昵称未赋值 } public enum AssistanceType { 终止 = 0, 辅助 = 1, 非辅助 = 2 } #endregion public static WechatBase GetApi(WechatUser user) { return GetApi(user.Wxid); } public static WechatBase GetApi(string wxid) { var v = Users.ContainsKey(wxid) ? Users[wxid] : null; if (v != null) { switch (v.Type) { case WechatType.Xiaoxie: return new Wechat_Xiaoxie(v); case WechatType.Xiaoxie_QY: return new Wechat_Xiaoxie_QY(v); default: break; } } return null; } public static void Install() { //if (Util.IsLinshi()) Task.Factory.StartNew(delegate { Wechat_Dazong.Install(); }); //else Task.Factory.StartNew(delegate { Wechat_Xiaoxie.Install(); }); try { Task.Factory.StartNew(delegate { Wechat_Xiaoxie.Install(); }); Task.Factory.StartNew(delegate { Wechat_Xiaoxie_QY.Install(); }); } catch (Exception ex) { } } public static void UnInstall() { try { Wechat_Xiaoxie.UnInstall(); Wechat_Xiaoxie_QY.UnInstall(); } catch (Exception) { } } public static uint StartWechat(WechatType type, string wxid = "") { try { switch (type) { case WechatType.Xiaoxie: return Wechat_Xiaoxie.StartWechat(wxid); case WechatType.Xiaoxie_QY: return Wechat_Xiaoxie_QY.StartWechat(); default: break; } } catch (Exception ex) { LogHelper.GetSingleObj().Error("StartWechat异常", $"{ex.Message} - {ex.StackTrace}"); throw ex; } return 0; } } }