using Api.Framework; using Api.Framework.Model; using Api.Framework.SDK; using Api.Framework.Tools; using Chat.Framework.WXSdk; using Chat.Framework.WXSdk.Implement; using CleaningTools.Entity; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace CleaningTools { class CleaningThread : TimerTask { /// /// 清理缓存时间 /// private DateTime delete_cache_time = DateTime.MinValue; /// /// 清理微信聊天记录时间 /// private DateTime clear_msg_time = DateTime.MinValue; //public bool delete_friend_ing = false; public override void Run(object state, bool timedOut) { try { if (Class1.Config.timing) { //清理缓存 if (delete_cache_time.Day != DateTime.Now.Day) { if (DateTime.Now.Hour >= 2 && DateTime.Now.Hour < 3 && DateTime.Now.Minute >= 30) { try { //清理图片数据 var tools = new Tools(); var cachePath = Util.MapPath(Util.MapPath(@"Cache")); if (Directory.Exists(cachePath)) { tools.DelectDir(cachePath); } cachePath = Util.MapPath(Util.MapPath(@"Assembly\apifile")); if (Directory.Exists(cachePath)) { tools.DelectDir(cachePath, true); } //清理超过7天的加人数据 var session = ApiClient.GetSession(); //if (session.TableExist("fl_plugin_userfission_applyfriend_info")) session.ExcuteSQL("delete from fl_plugin_userfission_applyfriend_info where crt_time <= @crttime", new { crttime = DateTime.Now.AddDays(-60).ToString("yyyy-MM-dd HH:mm:ss") }); //清理问答7天未响应数据 if (session.TableExist("fl_plugin_autoanswer__log")) session.ExcuteSQL("delete from fl_plugin_autoanswer__log where datetime <= @crttime", new { crttime = DateTime.Now.AddDays(-4).ToString("yyyy-MM-dd HH:mm:ss") }); //清理聊天记录3天之前的 if (session.TableExist("fl_plugin_messagesupervises_messinfo")) session.ExcuteSQL("delete from fl_plugin_messagesupervises_messinfo where send_time <= @time", new { time = DateTime.Now.AddDays(-3) }); //清理查询记录7天前的所有记录 if (session.TableExist("fl_query_hist")) session.ExcuteSQL("delete from fl_query_hist where crt_time <= @time", new { time = DateTime.Now.Date.AddDays(-7).ToString("yyyy-MM-dd HH:mm:ss") }); //清理Debug调试数据 if (session.TableExist("fl_debug_log")) session.ExcuteSQL("delete from fl_debug_log where crt_time <= @crt_time", new { crt_time = DateTime.Now.AddDays(-1) }); EventClient.OnEvent(this, "清理管家:定时清理执行完毕!"); } catch (Exception ex) { EventClient.OnEvent(this, "清理管家:" + ex.Message); } finally { delete_cache_time = DateTime.Now;//最后一次更新时间 } } } } if (Class1.Config.isAutoClearChatMsg) { //清理微信聊天记录 if (clear_msg_time.Day != DateTime.Now.Day) { var time = DateTime.Parse(Class1.Config.clear_msg_time); if (DateTime.Now.Hour >= time.Hour && DateTime.Now.Hour < time.Hour + 1 && DateTime.Now.Minute >= time.Minute) { try { var clients = Chat.Framework.ChatClient.WXClient.Values.Where(f => f.WeixinType == WeixinType.Hook微信 && f.Status == WxStatus.在线).ToList(); for (int i = 0; i < 2; i++) { foreach (var client in clients) { try { client.ClearChatHistoryMsg(); Thread.Sleep(3100); } catch (Exception ex) { } } Thread.Sleep(20000); } } catch (Exception ex) { EventClient.OnEvent(this, " 清理管家:" + ex.Message); } finally { clear_msg_time = DateTime.Now;//最后一次更新时间 } EventClient.OnEvent(this, "清理管家:定时清理微信历史聊天执行完毕!"); } } } if (Class1.Config.switch_delete_friend) { if (DateTime.Now.Hour >= 2 && DateTime.Now.Hour <= 4) { var last_time = DateTime.Parse(Class1.Config.delete_friend_lastTime); //最后一次时间 var next_time = last_time.AddDays(Class1.Config.autocleanday); //下一次时间 if (DateTime.Now > next_time) //如果时间已经过了 { Class1.Config.delete_friend_lastTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 02:00:00")).AddMinutes(new Random().Next(0, 100)).ToString("yyyy-MM-dd HH:mm:ss"); Util.Save(Class1.Config);//更新入库 var wxBases = Chat.Framework.ChatClient.WXClient.Values.ToList().Where(f => f.WeixinType == WeixinType.Grpc微信 && f.Status == WxStatus.在线).ToList(); if (wxBases != null && wxBases.Count > 0) { for (int i = 0; i < wxBases.Count; i++) { wxBases[i].RefreshContact(DeleteFriend); if (IsRunning && wxBases.Count == (i + 1)) Thread.Sleep(2 * 60 * 1000); //3分钟刷新一个机器人 } } } } } } catch (Exception ex) { EventClient.OnEvent(this, $"清理专家:{ex.Message}"); } } private static string[] not_delete = new string[] { "fmessage", "floatbottle", "qmessage", "medianote", "qqmail", "weixin" }; /// /// 获取非好友集合 /// /// /// public static List GetUnFriend(List> clients) { List unFriends = new List();//缓存 try { foreach (var client in clients) { List friends = new List(); if (client.Value.GetType() == typeof(WXClientImpl_IPAD)) { var ipad = client.Value as WXClientImpl_IPAD; if (ipad == null || ipad.Friends == null) continue; friends = ipad.Friends.Values.Where(f => string.IsNullOrEmpty(f.ChatRoomOwner) && !not_delete.Contains(f.UserName) && !string.IsNullOrEmpty(f.Ticket)).ToList(); } else if (client.Value.GetType() == typeof(WXClientImpl_HOOK)) { var hook = client.Value as WXClientImpl_HOOK; if (hook == null || hook.Friends == null) continue; friends = hook.Friends.Values.Where(f => !f.UserName.Contains("@") && !not_delete.Contains(f.UserName) && f.Ticket != "-1" && f.Ticket != "0").ToList(); } bool flag = true; foreach (var item in friends) { unFriends.Add(new UnFriends(client.Value, item)); } } } catch (Exception) { } return unFriends; } /// /// 获取未互动好友集合 /// /// /// public static List GetNoInteractionFriend(List> clients, int day) { List unFriends = new List();//缓存 var session = ApiClient.GetSession(); foreach (var client in clients) { var ipad = client.Value as WXClientImpl_IPAD; if (ipad == null || ipad.Friends == null) continue; var notFriends = ipad.Friends.Values.Where(f => string.IsNullOrEmpty(f.ChatRoomOwner) && !not_delete.Contains(f.UserName) && string.IsNullOrEmpty(f.Ticket)).ToList(); var members = session.Find("upd_time <= @time", new { time = DateTime.Now.AddDays(-day) }); var NoInteractionFriends = notFriends.Where(a => members.Where(t => a.UserName == t.username).Any()).ToList(); // 方法2 foreach (var item in NoInteractionFriends) { unFriends.Add(new UnFriends(ipad, item)); } } return unFriends; } private void DeleteFriend(WXClientImpl_IPAD _ipad) { var ipad = _ipad; Task.Run(() => { try { if (ipad.Status != WxStatus.在线) return; var unFriends = ipad.Friends.Values.Where(f => string.IsNullOrEmpty(f.ChatRoomOwner) && !not_delete.Contains(f.UserName) && !string.IsNullOrEmpty(f.Ticket)).ToList(); int number = 1; StringBuilder sb = new StringBuilder("已自动清理僵尸粉:\r\n"); var flag = true; foreach (var item in unFriends) { if (ipad.Status != WxStatus.在线) break;//在线继续 if (Class1.Config.cleanslimit == number) break;//数量限制 var member = ipad.GetContact(item.UserName, flag); flag = false; if (member != null && !string.IsNullOrEmpty(member.Ticket)) { ipad.DeleteFriend(item.UserName); sb.AppendLine($"{item.GetName()}({item.UserName})-{item.Alias}"); Thread.Sleep(Class1.Config.cleansleep * 1000); number++; } } if (number > 0 && ipad.Status == WxStatus.在线) ipad.SendMessage("filehelper", sb.ToString()); } catch (Exception) { } }); } } }