1.web.dll缓存替换(解决某些情况下无限发送给客户推送消息)坑

2.屏蔽微信更新窗口关闭
3.优化url检测
4.其他
This commit is contained in:
老道 2023-03-21 20:37:45 +08:00
parent 9d7a615a7f
commit 34057c91dd
9 changed files with 376 additions and 6 deletions

156
PCRobot/ControlExtension.cs Normal file
View File

@ -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
{
/// <summary>
///
/// </summary>
public static class ControlExtension
{
//private static NLog.ILogger log = NLog.LogManager.GetLogger("ControlExtension");
/// <summary>
/// 控件跨线程访问
/// </summary>
/// <param name="uiElement"></param>
/// <param name="updater"></param>
/// <param name="forceSynchronous"></param>
/// <param name="isOutputLog"></param>
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);
}
}
}
/// <summary>
/// 控件跨线程访问
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="uiElement"></param>
/// <param name="updater"></param>
/// <param name="data"></param>
/// <param name="forceSynchronous"></param>
/// <param name="isOutputLog"></param>
public static void SafeInvoke<T>(this Control uiElement, Action<T> 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);
}
}
}
/// <summary>
/// 自动禁止启动执行
/// </summary>
/// <param name="uiElement"></param>
/// <param name="updater"></param>
/// <param name="hintText"></param>
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;
}
/// <summary>
/// 自动禁止启动控件执行
/// </summary>
/// <param name="uiElement"></param>
/// <param name="updater"></param>
/// <param name="hintText"></param>
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;
}
}
}

View File

@ -156,6 +156,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ControlExtension.cs" />
<Compile Include="EasySoc_.cs" />
<Compile Include="Entitys\Display_Real.cs" />
<Compile Include="Entitys\Enterprise\CreateGroupInfo.cs" />

View File

@ -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);
}
}));
});
}
///// <summary>

View File

@ -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));
}
}
}

View File

@ -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();

View File

@ -1275,7 +1275,9 @@ namespace PCRobot.PCWechat.Routine
if (CacheHelper.Exist(url))
{
return CacheHelper.Get<int>(url);
var status = CacheHelper.Get<int>(url);
CacheHelper.Remove(url);
return status;
}
} while (awaitTime >= DateTime.Now);
return -1;

View File

@ -75,7 +75,7 @@ namespace PCRobot.PCWechat
}
}
/// <summary>
/// 获取用户
/// </summary>
@ -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)
{

View File

@ -0,0 +1,181 @@
using System;
using System.Collections.Concurrent;
using System.Linq;
namespace PCRobot.Utils
{
/// <summary>
/// 本地缓存
/// </summary>
public class CacheHelper
{
private static DateTime _nextRemoveDatetime = DateTime.Now.AddMinutes(30);
/// <summary>
/// 删除 过期
/// </summary>
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
{
/// <summary>
/// 缓存KEY
/// </summary>
public string Key { get; set; }
/// <summary>
/// 过期时间
/// </summary>
public DateTime ExpirationTime { get; set; }
/// <summary>
/// 对象
/// </summary>
public object Object { get; set; }
}
private static readonly ConcurrentDictionary<string, CacheItem> _caches = new ConcurrentDictionary<string, CacheItem>();
/// <summary>
/// 重置缓存,让这个程序的缓存全部失效
/// </summary>
public static void ResetCache()
{
_caches.Clear();
}
/// <summary>
/// 增加缓存
/// </summary>
/// <param name="key">key</param>
/// <param name="value">值</param>
/// <param name="time">值(秒)</param>
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;
}
/// <summary>
/// 获取缓存数据(如果不存在返回默认值)
/// </summary>
/// <param name="key"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T Get<T>(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);
}
/// <summary>
/// 获取缓存
/// </summary>
/// <param name="key">key</param>
/// <returns></returns>
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;
}
/// <summary>
/// 获取缓存,不存在则新增
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="defFunc"></param>
/// <param name="time"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static T GetValueOrAdd<T>(string key, Func<T> 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;
}
}
/// <summary>
///移除缓存
/// </summary>
/// <param name="key"></param>
public static void Remove(string key)
{
_caches.TryRemove(key, out _);
}
public static bool Exist(string key)
{
return _caches.ContainsKey(key);
}
}
}

View File

@ -347,7 +347,7 @@ namespace PCRobot.Utils
ShieldingWeChatUpdateFunction();
//开启自动关闭微信更新进程
StartCheckUpdateThread();
//StartCheckUpdateThread();
return true;
}