using Api.Framework; using Api.Framework.Model; using Api.Framework.Tools; using Chat.Framework.WXSdk; using Chat.Framework.WXSdk.Implement; using CleaningTools.Entity; using CleaningTools.Properties; using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using DevExpress.Utils.Extensions; using UI.Framework.Forms; namespace CleaningTools { public partial class MainForm : BaseForm { public MainForm() { InitializeComponent(); this.Text = Resources.MainFormTitle; } public void CloseForm() { try { if (!this.IsDisposed) { this.Invoke(new Action(delegate { this.Close(); this.Dispose(); })); } } catch (Exception ex) { ShowError(ex); } } private void MainForm_Load(object sender, EventArgs e) { try { toolStripComboBox1.Items.Clear(); dateTimePicker1.Value = DateTime.Parse(Class1.Config.timing_time); #region 加载下拉框 toolStripComboBox1.Items.Add($"选择机器人"); toolStripComboBox1.Items.Add($"全部机器人"); toolStripComboBox2.Items.Add($"选择机器人"); toolStripComboBox2.Items.Add($"全部机器人"); toolStripComboBox1.SelectedIndex = toolStripComboBox2.SelectedIndex = 0; Dictionary dicstr = new Dictionary(); var clients = Chat.Framework.ChatClient.WXClient; foreach (var item in clients) { if ((item.Value.WeixinType == WeixinType.Grpc微信 || item.Value.WeixinType == WeixinType.Hook微信) && item.Value.Status == WxStatus.在线) { dicstr.Add(toolStripComboBox1.Items.Count, item.Value.WeixinHao); toolStripComboBox1.Items.Add($"{item.Value.User.Nick}({item.Value.WeixinHao})"); toolStripComboBox2.Items.Add($"{item.Value.User.Nick}({item.Value.WeixinHao})"); } } toolStripComboBox1.Tag = toolStripComboBox2.Tag = dicstr; #endregion this.checkBox3.Checked = Class1.Config.switch_delete_friend; this.checkBox1.Checked = Class1.Config.timing; this.numericUpDown1.Value = Class1.Config.autocleanday; this.numericUpDown2.Value = Class1.Config.cleansleep; this.numericUpDown3.Value = Class1.Config.cleanslimit; this.dateTimePicker3.Value = DateTime.Parse(Class1.Config.clear_msg_time); this.checkBox6.Checked = Class1.Config.isAutoClearChatMsg; this.dateTimePicker2.Value = DateTime.Now.Date.AddMonths(-10); simpleButton2.Enabled = !isRunFlag; } catch (Exception ex) { ShowError(ex); } } /// /// 缓存目录文件删除 /// /// /// private void simpleButton1_Click(object sender, EventArgs e) { Loding.ShowWaitForm(); ThreadPool.QueueUserWorkItem(f => { 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(-30).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) }); ShowSuccessAutoClose("清理完成!"); } catch (Exception ex) { this.Invoke(new Action(delegate { ShowErrorAutoClose(ex); })); } finally { try { this.Invoke(new Action(delegate { Loding.CloseWaitForm(); })); } catch (Exception) { } } }); } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { try { Class1.Config.switch_delete_friend = this.checkBox3.Checked; Class1.Config.timing = this.checkBox1.Checked; Class1.Config.autocleanday = (int)this.numericUpDown1.Value; Class1.Config.cleansleep = (int)this.numericUpDown2.Value; Class1.Config.cleanslimit = (int)this.numericUpDown3.Value; Class1.Config.clear_msg_time = dateTimePicker3.Value.ToString("HH:mm"); Class1.Config.isAutoClearChatMsg = checkBox6.Checked; Util.Save(Class1.Config); } catch (Exception) { } } #region /// /// 清粉之选机器人账号改变按钮状态 /// /// /// private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (toolStripComboBox1.SelectedIndex == 0) { 点击更新ToolStripMenuItem.Enabled = false; 点击更新ToolStripMenuItem.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F); 点击更新ToolStripMenuItem.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64); this.gridControl1.DataSource = null;//清空数据 } else { 点击更新ToolStripMenuItem.Enabled = true; 点击更新ToolStripMenuItem.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F, System.Drawing.FontStyle.Underline); 点击更新ToolStripMenuItem.ForeColor = System.Drawing.Color.Blue; List> clients = null; if (toolStripComboBox1.SelectedIndex == 1) clients = Chat.Framework.ChatClient.WXClient.Where(f => (f.Value.WeixinType == WeixinType.Grpc微信 || f.Value.WeixinType == WeixinType.Hook微信) && f.Value.Status == WxStatus.在线).ToList(); else { var dicstr = toolStripComboBox1.Tag as Dictionary; var weixinHao = dicstr[toolStripComboBox1.SelectedIndex]; clients = Chat.Framework.ChatClient.WXClient.Where(f => f.Value.WeixinHao == weixinHao && (f.Value.WeixinType == WeixinType.Grpc微信 || f.Value.WeixinType == WeixinType.Hook微信) && f.Value.Status == WxStatus.在线).ToList(); } if (clients != null && clients.Count > 0) gridControl1.DataSource = CleaningThread.GetUnFriend(clients);//展示数据 } } /// /// 机器人好友僵尸粉列表(展示) /// /// /// private void 点击更新ToolStripMenuItem_Click(object sender, EventArgs e) { try { Loding.ShowWaitForm();//开启等待 _SetControlEnabled(false); List> clients = null; if (toolStripComboBox1.SelectedIndex == 0)//还未选择 throw new Exception("请选择要清理的机器人账号"); else if (toolStripComboBox1.SelectedIndex == 1)//全部机器人的僵尸粉 clients = Chat.Framework.ChatClient.WXClient.Where(f => (f.Value.WeixinType == WeixinType.Grpc微信 || f.Value.WeixinType == WeixinType.Hook微信) && f.Value.Status == WxStatus.在线).ToList(); else//个体 { var dicstr = toolStripComboBox1.Tag as Dictionary; var weixinHao = dicstr[toolStripComboBox1.SelectedIndex]; clients = Chat.Framework.ChatClient.WXClient.Where(f => f.Value.WeixinHao == weixinHao && (f.Value.WeixinType == WeixinType.Grpc微信 || f.Value.WeixinType == WeixinType.Hook微信) && f.Value.Status == WxStatus.在线).ToList(); } if (clients != null && clients.Count != 0) { foreach (var item in clients) { if (item.Value.GetType() == typeof(WXClientImpl_IPAD)) { var wx = item.Value as WXClientImpl_IPAD; if (item.Value.Status == WxStatus.在线 && !wx.IsRefreshContact) item.Value.RefreshContact(); } else if (item.Value.GetType() == typeof(WXClientImpl_HOOK)) { var wx = item.Value as WXClientImpl_HOOK; wx.GetZombie(); } } ShowSuccess("已启动任务缓慢刷新通讯录,耗时可能较长!"); } else throw new Exception("没有在线的微信号,或者没有符合的微信号"); } catch (Exception ex) { ShowError(ex); } finally { _SetControlEnabled(true); Loding.CloseWaitForm();//关闭等待 } } /// /// 设置控件的交互状态 /// /// private void _SetControlEnabled(bool flag = false) { foreach (var item in menuStrip1.Items) { if (item is ToolStripTextBox) (item as ToolStripTextBox).Enabled = flag; else if (item is ToolStripMenuItem) (item as ToolStripMenuItem).Enabled = flag; else if (item is ToolStripComboBox) (item as ToolStripComboBox).Enabled = flag; } } #endregion private string[] not_delete = new string[] { "fmessage", "floatbottle" }; private bool delete_ing = false; private void DeleteIng(int count = 0) { if (count == 0) { delete_ing = false; toolStripComboBox1.Enabled = 点击更新ToolStripMenuItem.Enabled = 一键清理ToolStripMenuItem2.Enabled = true; label2.Visible = this.label1.Visible = this.progressBar1.Visible = false; gridControl1.DataSource = new List(); } else { toolStripComboBox1.Enabled = 点击更新ToolStripMenuItem.Enabled = 一键清理ToolStripMenuItem2.Enabled = false; delete_ing = true; this.label1.Text = "准备就绪"; this.progressBar1.Maximum = count; this.progressBar1.Value = 0; label2.Visible = this.label1.Visible = this.progressBar1.Visible = true; } } Thread delete_thread = null; /// /// 一键清理僵尸粉 /// /// /// private void 一键清理ToolStripMenuItem2_Click(object sender, EventArgs e) { try { if (delete_thread != null && delete_thread.IsAlive) throw new Exception("当前已经有线程正在执行清理操作,请稍后!"); var unFriends = this.gridView1.DataSource as List; if (unFriends == null || unFriends.Count == 0) throw new Exception("当前列表中,没有可清理数据!"); //初始化删除操作 DeleteIng(unFriends.Count); //启动线程去执行删除 delete_thread = new Thread(new ThreadStart(delegate { try { int number = 0; do { if (!delete_ing) return;//便于做终止操作,防止卡死 var item = unFriends.FirstOrDefault();//取出默认数据 if (item == null) break;//没有数据了 try { var wxBase = item.Client; //获取对应的微信对象 if (wxBase.Status == WxStatus.在线) { number++; var result = wxBase.DeleteFriend(item.Friend.UserName);//删除 this.Invoke(new Action(delegate { this.label1.Text = string.Format("删除第{0}位:{1}={2}", number, item.weixinnice, string.IsNullOrEmpty(result) ? "成功" : result); this.progressBar1.Value++; Application.DoEvents();//不卡界面刷新 })); //更新进度条 Thread.Sleep(Class1.Config.cleansleep * 1000); } if (wxBase.GetType() == typeof(WXClientImpl_HOOK)) { var hook = wxBase as WXClientImpl_HOOK; hook.Friends.Remove(item.Friend.UserName); } } catch { } unFriends.Remove(item);//不管是否删除成功,移除列表数据 } while (true); } catch (Exception) { } finally { this.Invoke(new Action(delegate { ShowSuccess("删除好友完成!"); DeleteIng(); })); } })); delete_thread.IsBackground = true; delete_thread.Start(); } catch (Exception ex) { ShowError(ex); } } /// /// gridviwe 增加标识列 /// /// /// private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) { if (e.Info.IsRowIndicator && e.RowHandle >= 0) e.Info.DisplayText = (e.RowHandle + 1).ToString(); } /// /// 僵尸粉清理之休眠时间,自动清理天数,输入限制(只能为数字) /// /// /// private void toolStripTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != '\b')//这是允许输入退格键 { if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字 { e.Handled = true; } } } private void label2_Click(object sender, EventArgs e) { try { //传0表示终止 DeleteIng(); //强制销毁 if (delete_thread != null) delete_thread.Abort(); } catch (Exception) { } delete_thread = null; } /// /// 清粉之选机器人账号改变按钮状态 - 未交互的用户 /// /// /// private void toolStripComboBox2_SelectedIndexChanged(object sender, EventArgs e) { if (toolStripComboBox2.SelectedIndex == 0) { toolStripMenuItem3.Enabled = false; toolStripMenuItem3.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F); toolStripMenuItem3.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64); this.gridControl2.DataSource = null;//清空数据 } else { toolStripMenuItem3.Enabled = true; toolStripMenuItem3.Font = new System.Drawing.Font("Microsoft YaHei UI", 9F, System.Drawing.FontStyle.Underline); toolStripMenuItem3.ForeColor = System.Drawing.Color.Blue; List> clients = null; if (toolStripComboBox2.SelectedIndex == 1) clients = Chat.Framework.ChatClient.WXClient.Where(f => f.Value.WeixinType == WeixinType.Grpc微信 && f.Value.Status == WxStatus.在线).ToList(); else { var dicstr = toolStripComboBox2.Tag as Dictionary; var weixinHao = dicstr[toolStripComboBox2.SelectedIndex]; clients = Chat.Framework.ChatClient.WXClient.Where(f => f.Value.WeixinHao == weixinHao && f.Value.WeixinType == WeixinType.Grpc微信 && f.Value.Status == WxStatus.在线).ToList(); } if (clients != null && clients.Count > 0) gridControl2.DataSource = CleaningThread.GetNoInteractionFriend(clients, (int)numericUpDown4.Value);//展示数据 } } //?? private void label5_Click(object sender, EventArgs e) { try { //传0表示终止 DeleteIng(); //强制销毁 if (delete_thread != null) delete_thread.Abort(); } catch (Exception) { } delete_thread = null; } private void gridView2_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e) { if (e.Info.IsRowIndicator && e.RowHandle >= 0) e.Info.DisplayText = (e.RowHandle + 1).ToString(); } private void numericUpDown4_ValueChanged(object sender, EventArgs e) { toolStripComboBox2_SelectedIndexChanged(null, null); } private void 点击更新ToolStripMenuItem1_Click(object sender, EventArgs e) { try { Loding.ShowWaitForm();//开启等待 _SetControlEnabled(false); List> clients = null; if (toolStripComboBox2.SelectedIndex == 0)//还未选择 throw new Exception("请选择要清理的机器人账号"); else if (toolStripComboBox2.SelectedIndex == 1)//全部机器人的僵尸粉 { clients = Chat.Framework.ChatClient.WXClient.Where(f => f.Value.WeixinType == WeixinType.Grpc微信 && f.Value.Status == WxStatus.在线).ToList(); } else//个体 { var dicstr = toolStripComboBox2.Tag as Dictionary; var weixinHao = dicstr[toolStripComboBox2.SelectedIndex]; clients = Chat.Framework.ChatClient.WXClient.Where(f => f.Value.WeixinHao == weixinHao && f.Value.WeixinType == WeixinType.Grpc微信 && f.Value.Status == WxStatus.在线).ToList(); } if (clients != null && clients.Count != 0) { foreach (var item in clients) { var ipad = item.Value as WXClientImpl_IPAD; if (ipad.Status == WxStatus.在线 && !ipad.IsRefreshContact) { ipad.RefreshContact(); } } ShowSuccess("已启动任务缓慢刷新通讯录,耗时可能较长!"); } else throw new Exception("没有在线的微信号,或者没有符合的微信号"); } catch (Exception ex) { ShowError(ex); } finally { _SetControlEnabled(true); Loding.CloseWaitForm();//关闭等待 } } private bool delete_ing2 = false; private void DeleteIng2(int count = 0) { if (count == 0) { delete_ing2 = false; toolStripComboBox2.Enabled = toolStripMenuItem3.Enabled = toolStripMenuItem5.Enabled = true; label5.Visible = this.label4.Visible = this.progressBar5.Visible = false; gridControl2.DataSource = new List(); } else { toolStripComboBox2.Enabled = toolStripMenuItem3.Enabled = toolStripMenuItem5.Enabled = false; delete_ing2 = true; this.label4.Text = "准备就绪"; this.progressBar5.Maximum = count; this.progressBar5.Value = 0; label5.Visible = this.label4.Visible = this.progressBar5.Visible = true; } } private void toolStripMenuItem5_Click(object sender, EventArgs e) { try { if (delete_thread != null && delete_thread.IsAlive) throw new Exception("当前已经有线程正在执行清理操作,请稍后!"); var unFriends = this.gridView2.DataSource as List; if (unFriends == null || unFriends.Count == 0) throw new Exception("当前列表中,没有可清理数据!"); //初始化删除操作 DeleteIng(unFriends.Count); //启动线程去执行删除 delete_thread = new Thread(new ThreadStart(delegate { try { int number = 0; do { if (!delete_ing) return;//便于做终止操作,防止卡死 var item = unFriends.FirstOrDefault();//取出默认数据 if (item == null) break;//没有数据了 try { var wxBase = item.Client; //获取对应的微信对象 if (wxBase.Status == WxStatus.在线) { number++; var result = wxBase.DeleteFriend(item.Friend.UserName);//删除 this.Invoke(new Action(delegate { this.label4.Text = string.Format("删除第{0}位:{1}={2}", number, item.weixinnice, string.IsNullOrEmpty(result) ? "成功" : result); this.progressBar5.Value++; System.Windows.Forms.Application.DoEvents();//不卡界面刷新 })); //更新进度条 Thread.Sleep(Class1.Config.cleansleep * 1000); } } catch { } unFriends.Remove(item);//不管是否删除成功,移除列表数据 } while (true); } catch (Exception) { } finally { this.Invoke(new Action(delegate { ShowSuccess("删除好友完成!"); DeleteIng(); })); } })); delete_thread.IsBackground = true; delete_thread.Start(); } catch (Exception ex) { ShowError(ex); } } private void button1_Click(object sender, EventArgs e) { var tb = checkBox2.Checked; var jd = checkBox4.Checked; var pdd = checkBox5.Checked; var wph = checkBox7.Checked; var datetime = dateTimePicker2.Value; try { if (!(tb || jd || pdd || wph)) throw new Exception("请选择要清理的联盟"); if (XtraMessageBox.Show($@"确定要清理{datetime.ToString("yyyy-MM-dd HH:mm:ss")}之前的订单吗? 清理后{datetime.ToString("yyyy-MM-dd HH:mm:ss")}之前的订单将不存在", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel) { return; } } catch (Exception ex) { ShowError(ex); return; } button1.Enabled = false; Task.Run(() => { try { var session = ApiClient.GetSession(); if (tb) { //session.Deleteable(f => f.create_time < datetime).ExecuteCommand(); session.ExcuteSQL("DELETE FROM fl_order_alimama where create_time < @datetime", new { datetime = datetime }); } if (jd) { //session.Deleteable(f => f.orderTime < datetime).ExecuteCommand(); session.ExcuteSQL("DELETE FROM fl_order_jingdong where orderTime < @datetime", new { datetime = datetime }); } if (pdd) { //session.Deleteable(f => f.order_create_time < datetime).ExecuteCommand(); session.ExcuteSQL("DELETE FROM fl_order_pinduoduo where order_create_time < @datetime", new { datetime = datetime }); } if (wph) { var timeSpan = Util.GetTimeSpan(datetime); //session.Deleteable(f => f.orderTime < timeSpan).ExecuteCommand(); session.ExcuteSQL("DELETE FROM fl_order_weipinhui where orderTime < @datetime", new { datetime = timeSpan }); } ShowSuccessAutoClose("历史订单清理完成"); } catch (Exception ex) { ShowError(ex); } finally { this.Invoke(new Action(() => { button1.Enabled = true; })); } }); ShowSuccessAutoClose("已成功创建历史订单清理任务,后台运行中"); } private static bool isRunFlag = false; private void simpleButton2_Click(object sender, EventArgs e) { var msg = "清理任务执行完成,一共执行了{0}个机器人"; //try //{ simpleButton2.Enabled = false; var task = Task.Run(() => { try { isRunFlag = true; var clients = Chat.Framework.ChatClient.WXClient.Values.Where(f => f.WeixinType == WeixinType.Hook微信 && f.Status == WxStatus.在线).ToList(); msg = string.Format(msg, clients.Count); foreach (var client in clients) { try { client.ClearChatHistoryMsg(); Thread.Sleep(3300); } catch (Exception ex) { } } if (!this.IsDisposed) { this.simpleButton2.InvokeSafe(() => { simpleButton2.Enabled = true; }); } isRunFlag = false; } catch (Exception ex) { } }); //task.Wait(); //} //catch (Exception ex) //{ // msg = "清理任务异常:" + ex.Message; //} //finally //{ // simpleButton2.Enabled = true; //} //ShowSuccessAutoClose(msg); } } }