From 34057c91dded209eba970c993f8cc9474fbb5269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=90=B4?= <8402134@qq.com> Date: Tue, 21 Mar 2023 20:37:45 +0800 Subject: [PATCH] =?UTF-8?q?1.web.dll=E7=BC=93=E5=AD=98=E6=9B=BF=E6=8D=A2(?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=9F=90=E4=BA=9B=E6=83=85=E5=86=B5=E4=B8=8B?= =?UTF-8?q?=E6=97=A0=E9=99=90=E5=8F=91=E9=80=81=E7=BB=99=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E6=B6=88=E6=81=AF)=E5=9D=91=202.=E5=B1=8F?= =?UTF-8?q?=E8=94=BD=E5=BE=AE=E4=BF=A1=E6=9B=B4=E6=96=B0=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=E5=85=B3=E9=97=AD=203.=E4=BC=98=E5=8C=96url=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=204.=E5=85=B6=E4=BB=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PCRobot/ControlExtension.cs | 156 +++++++++++++++ PCRobot/PCRobot.csproj | 1 + PCRobot/PCRobotForm.cs | 28 ++- .../Enterprise/Wechat_Xiaoxie_QY_Analysis.cs | 2 + .../Routine/Wechat_Xiaoxie_Analysis.cs | 1 + .../Routine/Wechat_Xiaoxie_Override.cs | 4 +- PCRobot/PCWechat/WechatClient.cs | 7 +- PCRobot/Utils/CacheHelper.cs | 181 ++++++++++++++++++ PCRobot/Utils/WeChatActivateHelperV2.cs | 2 +- 9 files changed, 376 insertions(+), 6 deletions(-) create mode 100644 PCRobot/ControlExtension.cs create mode 100644 PCRobot/Utils/CacheHelper.cs diff --git a/PCRobot/ControlExtension.cs b/PCRobot/ControlExtension.cs new file mode 100644 index 0000000..dacba61 --- /dev/null +++ b/PCRobot/ControlExtension.cs @@ -0,0 +1,156 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Windows.Forms; + +namespace System.Windows.Forms +{ + /// + /// + /// + public static class ControlExtension + { + //private static NLog.ILogger log = NLog.LogManager.GetLogger("ControlExtension"); + /// + /// 控件跨线程访问 + /// + /// + /// + /// + /// + public static void SafeInvoke(this Control uiElement, Action updater, bool forceSynchronous = true,bool isOutputLog=true) + { + try + { + if (uiElement == null) + { + throw new ArgumentNullException("uiElement"); + } + + if (uiElement.InvokeRequired) + { + if (forceSynchronous) + { + uiElement.Invoke((Action)delegate { SafeInvoke(uiElement, updater, forceSynchronous); }); + } + else + { + uiElement.BeginInvoke((Action)delegate { SafeInvoke(uiElement, updater, forceSynchronous); }); + } + } + else + { + if (uiElement.IsDisposed) + { + return; + throw new ObjectDisposedException("Control is already disposed."); + } + + updater(); + } + } + catch (Exception ex) + { + if (isOutputLog) + { + //log.Error(ex); + } + } + } + /// + /// 控件跨线程访问 + /// + /// + /// + /// + /// + /// + /// + public static void SafeInvoke(this Control uiElement, Action updater, T data, bool forceSynchronous = true, bool isOutputLog = true) + { + try + { + if (uiElement == null) + { + throw new ArgumentNullException("uiElement"); + } + + if (uiElement.InvokeRequired) + { + if (forceSynchronous) + { + uiElement.Invoke(updater, data); + } + else + { + uiElement.BeginInvoke(updater, data); + } + } + else + { + if (uiElement.IsDisposed) + { + return; + throw new ObjectDisposedException("Control is already disposed."); + } + updater(data); + } + } + catch (Exception ex) + { + if (isOutputLog) + { + //log.Error(ex); + } + } + } + + /// + /// 自动禁止启动执行 + /// + /// + /// + /// + public static void SafeEnabledInvoke(this ToolStripItem uiElement, Action updater, string hintText = "正在执行.") + { + string value = uiElement.Text; + uiElement.Enabled = false; + uiElement.Text = hintText; + try + { + updater(); + } + catch (Exception ex) + { + //log.Error(ex); + } + uiElement.Text = value; + uiElement.Enabled = true; + } + + /// + /// 自动禁止启动控件执行 + /// + /// + /// + /// + public static void SafeEnabledInvoke(this Control uiElement, Action updater,string hintText= "正在执行.") + { + uiElement.Enabled = false; + string value = uiElement.Text; + uiElement.Text = hintText; + try + { + updater(); + } + catch (Exception ex) + { + //log.Error(ex); + } + uiElement.Text = value; + uiElement.Enabled = true; + } + } +} \ No newline at end of file diff --git a/PCRobot/PCRobot.csproj b/PCRobot/PCRobot.csproj index f787408..a188d49 100644 --- a/PCRobot/PCRobot.csproj +++ b/PCRobot/PCRobot.csproj @@ -156,6 +156,7 @@ + diff --git a/PCRobot/PCRobotForm.cs b/PCRobot/PCRobotForm.cs index d87bba5..763a70e 100644 --- a/PCRobot/PCRobotForm.cs +++ b/PCRobot/PCRobotForm.cs @@ -817,7 +817,30 @@ namespace PCRobot //刷新机器人列表 private void RefUserMethod(WechatUser user) { - this.Invoke(new Action(delegate + //this.Invoke(new Action(delegate + //{ + // try + // { + // this.listView1.Items.Clear(); + // int i = 1; + // foreach (var item in WechatClient.Users.Values.ToList()) + // { + // var view = new ListViewItem(new string[] { i.ToString(), item.Wxid, item.Nickname, item.Type == WechatType.Xiaoxie_QY ? "企业" : "个人", WechatClient.GetApi(item.Wxid).GetVersion(), "正式" }); + // view.Tag = item; + // this.listView1.Items.Add(view); + // i++; + // //if (item.Type == WechatType.Xiaoxie) + // // //BakWechatInfo(item.Wxid, item.Nickname); + // // WechatLoginHelper.EndbeginLogin(item.Wxid, item.Nickname); + // } + // } + // catch (Exception ex) + // { + // LogHelper.GetSingleObj().Error("RefUserMethod", ex.Message + " " + ex.StackTrace); + // } + //})); + + this.SafeInvoke(() => { try { @@ -838,7 +861,8 @@ namespace PCRobot { LogHelper.GetSingleObj().Error("RefUserMethod", ex.Message + " " + ex.StackTrace); } - })); + }); + } ///// diff --git a/PCRobot/PCWechat/Enterprise/Wechat_Xiaoxie_QY_Analysis.cs b/PCRobot/PCWechat/Enterprise/Wechat_Xiaoxie_QY_Analysis.cs index 0420cd4..01bf754 100644 --- a/PCRobot/PCWechat/Enterprise/Wechat_Xiaoxie_QY_Analysis.cs +++ b/PCRobot/PCWechat/Enterprise/Wechat_Xiaoxie_QY_Analysis.cs @@ -2189,6 +2189,8 @@ namespace PCRobot.PCWechat.Enterprise var path = HttpExtend.MapFile("WXWork.exe", $"WXWork\\{Version}"); LogHelper.GetSingleObj().Debug("注入前", $"DPath = {DllPath},Path = {path}"); return InjectWxWorkMultiOpen(Encoding.UTF8.GetBytes(DllPath), Encoding.UTF8.GetBytes(path)); + + } } } diff --git a/PCRobot/PCWechat/Routine/Wechat_Xiaoxie_Analysis.cs b/PCRobot/PCWechat/Routine/Wechat_Xiaoxie_Analysis.cs index ee56160..665d029 100644 --- a/PCRobot/PCWechat/Routine/Wechat_Xiaoxie_Analysis.cs +++ b/PCRobot/PCWechat/Routine/Wechat_Xiaoxie_Analysis.cs @@ -1171,6 +1171,7 @@ namespace PCRobot.PCWechat.Routine { //if (CheckUrlCache.Count > 500) CheckUrlCache.Clear(); //{"data":{"status":-2005,"url":"https://ycs9d.kuaizhan.com/?id=07E4060844959"},"type":11123} + //{"data":{"real_url":"https://weixin110.qq.com/cgi-bin/mmspamsupport-bin/newredirectconfirmcgi?bancode=8ccb2264069bef5fdcc9c0719ad3798c5b88335ea2a12377abf9bd215e65cc76&click=4ae1648419d4a13e24536fa01e1ce6b4&bankey=114c01a82dc3cdd5885ea6c46628840d&midpagecode=e2faa0ee03e19e6efecf869b7f9ca052b990cded1c952f94349a5a44c082fae8e21c2209712a3ef61136eb6e471da12e&exportkey=n_ChQIAhIQ1tFi%2F1uM69z6NizdOhCl4xKMAgIE97dBBAEAAAAAAE6mNsznB%2BEAAAAOpnltbLcz9gKNyK89dVj0z8ETSi%2B0H%2BUFvmE6zliXCF5w8wK3uPU6uGcyAwcnqGEuj72wh8nSZ0yiZpu9ekd0%2F6b9880eC8%2BTQTRbNK%2Ft%2BSSQN5o517dJqrslUSNkuIfeaS7rvIMeBLf48eQCTjnPS6Bys93eks%2Bt0cNXGWqr4%2F9UCknIlhrDJyFq3jzUM2sfs5oJvLRx8sAKIRv84ENcYx2%2BduSZ3Ob7F2ZFyL43YTsbGs5%2Fkltid6uRi0PyeMT4hg3LzYcutcbh%2FNv1mSoIV2rvPguuNEtul%2BP1TRx9MMuRc9O%2B6dAeiVoXDq%2B1oYPtbthI6GE%3D&pass_ticket=m%2BTP5b10sYur0Fh%2BN4BeHqCfQCdC1ZHjLcceEAEikUhmhZGDPdAjv1ZY2xgZzlCvUDFrvY25t%2FNi4cBH8DffhA%3D%3D&wechat_real_lang=zh_CN&wx_header=0","status":-2005,"url":"http://6k7c8.kuaizhan.com"},"type":11123} if (data["status"] != null) { var url = data["url"]?.ToString(); diff --git a/PCRobot/PCWechat/Routine/Wechat_Xiaoxie_Override.cs b/PCRobot/PCWechat/Routine/Wechat_Xiaoxie_Override.cs index bfead33..78eb75b 100644 --- a/PCRobot/PCWechat/Routine/Wechat_Xiaoxie_Override.cs +++ b/PCRobot/PCWechat/Routine/Wechat_Xiaoxie_Override.cs @@ -1275,7 +1275,9 @@ namespace PCRobot.PCWechat.Routine if (CacheHelper.Exist(url)) { - return CacheHelper.Get(url); + var status = CacheHelper.Get(url); + CacheHelper.Remove(url); + return status; } } while (awaitTime >= DateTime.Now); return -1; diff --git a/PCRobot/PCWechat/WechatClient.cs b/PCRobot/PCWechat/WechatClient.cs index 4fa0a5c..1a6cf47 100644 --- a/PCRobot/PCWechat/WechatClient.cs +++ b/PCRobot/PCWechat/WechatClient.cs @@ -75,7 +75,7 @@ namespace PCRobot.PCWechat } } - + /// /// 获取用户 /// @@ -138,7 +138,10 @@ namespace PCRobot.PCWechat { Users.Remove(user.Wxid); } - RefUserEvent?.Invoke(user); + + Task.Run(() => { RefUserEvent?.Invoke(user); }); + + //RefUserEvent?.Invoke(user); if ((IntPtr)user.Pid != IntPtr.Zero && kill) { diff --git a/PCRobot/Utils/CacheHelper.cs b/PCRobot/Utils/CacheHelper.cs new file mode 100644 index 0000000..e795b79 --- /dev/null +++ b/PCRobot/Utils/CacheHelper.cs @@ -0,0 +1,181 @@ +using System; +using System.Collections.Concurrent; +using System.Linq; + +namespace PCRobot.Utils +{ + /// + /// 本地缓存 + /// + public class CacheHelper + { + + + private static DateTime _nextRemoveDatetime = DateTime.Now.AddMinutes(30); + /// + /// 删除 过期 + /// + private static void RemoveTimeOut() + { + if (DateTime.Now < _nextRemoveDatetime) + { + return; + } + var array = _caches.Values.ToArray().Where(w => w.ExpirationTime < DateTime.Now).ToArray(); + foreach (var v in array) + { + _caches.TryRemove(v.Key, out _); + } + _nextRemoveDatetime = DateTime.Now.AddMinutes(30); + } + + private class CacheItem + { + /// + /// 缓存KEY + /// + public string Key { get; set; } + /// + /// 过期时间 + /// + public DateTime ExpirationTime { get; set; } + + /// + /// 对象 + /// + public object Object { get; set; } + } + private static readonly ConcurrentDictionary _caches = new ConcurrentDictionary(); + + + /// + /// 重置缓存,让这个程序的缓存全部失效 + /// + public static void ResetCache() + { + _caches.Clear(); + } + + /// + /// 增加缓存 + /// + /// key + /// 值 + /// 值(秒) + public static bool Add(string key, object value, int time = 60) + { + _caches.AddOrUpdate(key, (k) => + { + return new CacheItem() + { + Key = k, + Object = value, + ExpirationTime = time < 0 ? DateTime.MaxValue : DateTime.Now.AddSeconds(time) + }; + }, (k, v) => + { + v.Object = value; + v.ExpirationTime = time < 0 ? DateTime.MaxValue : DateTime.Now.AddSeconds(time); + return v; + }); + return true; + } + + /// + /// 获取缓存数据(如果不存在返回默认值) + /// + /// + /// + /// + public static T Get(string key) + { + if (string.IsNullOrWhiteSpace(key)) + { + return default(T); + } + if (_caches.TryGetValue(key, out var result)) + { + if (result.ExpirationTime < DateTime.Now) + { + _caches.TryRemove(key, out _); + return default(T); + } + return (T)result.Object; + } + return default(T); + } + + /// + /// 获取缓存 + /// + /// key + /// + public static object GetValue(string key) + { + RemoveTimeOut(); + if (string.IsNullOrWhiteSpace(key)) + { + return null; + } + if (_caches.TryGetValue(key, out var result)) + { + if (result.ExpirationTime < DateTime.Now) + { + _caches.TryRemove(key, out _); + return null; + } + return result.Object; + } + return null; + } + /// + /// 获取缓存,不存在则新增 + /// + /// + /// + /// + /// + /// + /// + public static T GetValueOrAdd(string key, Func defFunc, int time = 60) where T : class + { + if (string.IsNullOrWhiteSpace(key)) + { + throw new Exception("缓存key不能为空"); + } + if (string.IsNullOrWhiteSpace(key)) + { + return null; + } + if (_caches.TryGetValue(key, out var result)) + { + if (result.ExpirationTime > DateTime.Now) + { + _caches.TryRemove(key, out _); + var obj = defFunc(); + Add(key, obj, time); + return obj; + } + return (T)result.Object; + } + else + { + var obj = defFunc(); + Add(key, obj, time); + return obj; + } + } + /// + ///移除缓存 + /// + /// + public static void Remove(string key) + { + _caches.TryRemove(key, out _); + } + public static bool Exist(string key) + { + return _caches.ContainsKey(key); + } + } +} \ No newline at end of file diff --git a/PCRobot/Utils/WeChatActivateHelperV2.cs b/PCRobot/Utils/WeChatActivateHelperV2.cs index 7e597b1..464b6a0 100644 --- a/PCRobot/Utils/WeChatActivateHelperV2.cs +++ b/PCRobot/Utils/WeChatActivateHelperV2.cs @@ -347,7 +347,7 @@ namespace PCRobot.Utils ShieldingWeChatUpdateFunction(); //开启自动关闭微信更新进程 - StartCheckUpdateThread(); + //StartCheckUpdateThread(); return true; }