old_flsystem/FLSystem/Forms/robot_manage_control.cs

595 lines
27 KiB
C#

using Api.Framework;
using Api.Framework.Model;
using Chat.Framework;
using Chat.Framework.WXSdk.Implement;
using DevExpress.XtraEditors;
using Api.Framework.Tools;
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using Grant.Framework;
using Chat.Framework.QQSdk;
using System.Threading.Tasks;
using UI.Framework.Forms;
using Api.Framework.Utils;
using System.Collections.Generic;
using Api.Framework.Events;
using CsharpHttpHelper;
using System.Text;
using Robot.Framework.Entities;
using Chat.Framework.QQSdk.QPlus;
using PCRobot.Pack;
namespace FLSystem.Forms
{
public partial class robot_manage_control : DevExpress.XtraEditors.XtraUserControl
{
public robot_manage_control()
{
InitializeComponent();
this.gridView1.CustomDrawRowIndicator += gridView1_CustomDrawRowIndicator;
Refresh();
EventClient.MethodEvent += EventClient_MethodEvent;
this.Disposed += Robot_manage_control_Disposed;
}
private void Robot_manage_control_Disposed(object sender, EventArgs e)
{
EventClient.MethodEvent -= EventClient_MethodEvent;
}
private void EventClient_MethodEvent(object sender, Api.Framework.Events.MethodType e)
{
if (e == Api.Framework.Events.MethodType.)
{
this.Invoke(new Action(delegate
{
Refresh();
}));
}
}
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 new void Refresh()
{
try
{
this.gridControl1.DataSource = null;
var session = ApiClient.GetSession();
//所有机器人列表
var robotList = session.FindRobots(true);
if (robotList != null && robotList.Count != 0)
{
var loginWxs = ChatClient.WXClient.Values.Where(f => f.Status == Chat.Framework.WXSdk.WxStatus.线).ToList();
//在线集合
var robotTemp_online = robotList.Where(f => loginWxs.FirstOrDefault(z => z.WeixinHao == f.name) != null).OrderBy(f => f.type).ToList();
//离线集合
var robotTemp_offline = robotList.Where(f => loginWxs.FirstOrDefault(z => z.WeixinHao == f.name) == null).ToList();
robotTemp_online.AddRange(robotTemp_offline);
robotList = robotTemp_online;
}
this.gridControl1.DataSource = robotList;
}
catch (Exception)
{
}
}
private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
{
var row = e.Row as fl_robot_info;
if (e.Column.Caption == "图标")
{
if (row.type == Api.Framework.SDK.ChatType.)
e.Value = Properties.Resources.__1_;
else if (row.type == Api.Framework.SDK.ChatType.QQ)
e.Value = Properties.Resources.QQ;
if (row.type == Api.Framework.SDK.ChatType.)
e.Value = Properties.Resources.__1_;
}
else if (e.Column.Caption == "状态")
{
if (row.type == Api.Framework.SDK.ChatType. || row.type == Api.Framework.SDK.ChatType.)
{
var wx = ChatClient.WXClient.FirstOrDefault(f => f.Key == row.name).Value;
//if (wx == null)
//{
//var temp = Chat.Framework.ChatClient.PCRobotPool.GetSession(row.name);
//if (temp != null)
//{
// e.Value = "在线";
// return;
//}
//}
e.Value = wx == null ? "未登陆" : wx.Status.ToString();
}
else if (row.type == Api.Framework.SDK.ChatType.QQ)
{
if (ChatClient.QQClients.ContainsKey(row.name))
{
var qBase = ChatClient.QQClients[row.name];
if (qBase == null || qBase == null)
e.Value = "未登陆";
else
{
if (qBase is QQClientImpl_QQPlus)
{
var q = qBase as QQClientImpl_QQPlus;
e.Value = q.QQClient.User.LoginStatus == LoginStatus.Login ? "在线" : "未登陆";
}
}
}
else
e.Value = "未登陆";
}
}
else if (e.Column.Caption == "平台类型")
{
if (row.type == Api.Framework.SDK.ChatType.)
e.Value = row.remark == "PCWechat HOOK" ? "PC微信" : "协议微信";
else if (row.type == Api.Framework.SDK.ChatType.QQ)
e.Value = "QQ";
else if (row.type == Api.Framework.SDK.ChatType.)
e.Value = "企业微信";
else if (row.type == Api.Framework.SDK.ChatType.)
e.Value = "微信公众号";
else if (row.type == Api.Framework.SDK.ChatType.)
e.Value = "微信公众号";
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
Refresh();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
var r = this.gridView1.GetSelectedRows();
if (r.Length == 0) return;
var row = this.gridView1.GetRow(r[0]) as fl_robot_info;
if (row.remark == "PCWechat HOOK")
{
var wx = ChatClient.WXClient.Values.FirstOrDefault(f => f.WeixinHao == row.name);
if (wx != null && wx.Status == Chat.Framework.WXSdk.WxStatus.线)
{
BaseForm.ShowErrorAutoClose("PC微信当前在线状态");
return;
}
if (string.IsNullOrWhiteSpace(row.token))
{
BaseForm.ShowSuccessAutoClose("PC微信未包含设备信息,请在易转发端登录");
}
else
{
Task.Run(() =>
{
ChatClient.LoginPcWeixin(row.token, row.name, row.type == Api.Framework.SDK.ChatType. ? RobotType. : RobotType.);
});
}
return;
}
if (row.is_login) if (MessageBox.Show("确定要重新登录吗?", "重新登录", MessageBoxButtons.OKCancel) != DialogResult.OK) return;
if (row.type == Api.Framework.SDK.ChatType.)
{
if (row.remark != "PCWechat HOOK")
ChatClient.LoginWeixin(row.name);
else
BaseForm.ShowSuccessAutoClose("PC微信请在易转发端登录");
}
else if (row.type == Api.Framework.SDK.ChatType.QQ)
{
ToolStripMenuItem_Click(null, null);
ChatClient.LoginQQ(row.name);
}
Refresh();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (XtraMessageBox.Show(@"您确定要删除机器人吗?
重要提醒:
①、删除后会导致提现找不到机器人
②、删除后订单无法通知到会员信息
③、数据库中、有连锁关联均会异常
除非您不打算用这个微信号,否则请勿删除!", "删除机器人", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
{
var verify = new Grant.Framework.VerifyPassForm();
if (verify.ShowDialog() == DialogResult.OK)
{
var r = this.gridView1.GetSelectedRows();
if (r.Length == 0) return;
var row = this.gridView1.GetRow(r[0]) as fl_robot_info;
var session = ApiClient.GetSession();
if (row.is_login)
{
switch (row.type)
{
case Api.Framework.SDK.ChatType.QQ:
{
if (ChatClient.QQClients.ContainsKey(row.name))
{
var qClient = ChatClient.QQClients[row.name];
#region ,
//不存在表的情况会报错,直接忽略
try { session.ExcuteSQL("delete from fl_plugin_tbcirclepromotion_tbtgw where robot_id in (select robot_id from fl_plugin_tbcirclepromotion_tbtgw where robot_id not in (select id from fl_robot_info))"); } catch { }
try { session.ExcuteSQL("delete from fl_plugin_pddcirclepromotion_pddtgw where robot_id in (select robot_id from fl_plugin_pddcirclepromotion_pddtgw where robot_id not in (select id from fl_robot_info))"); } catch { }
#endregion
try
{
qClient.Logout();
}
catch (Exception ex)
{ }
ChatClient.QQClients.Remove(row.name);
}
}
break;
case Api.Framework.SDK.ChatType.:
case Api.Framework.SDK.ChatType.:
var wx = ChatClient.WXClient.FirstOrDefault(f => f.Key == row.name).Value;
if (wx != null)
{
#region ,
//不存在表的情况会报错,直接忽略
try { session.ExcuteSQL("delete from fl_plugin_tbcirclepromotion_tbtgw where robot_id in (select robot_id from fl_plugin_tbcirclepromotion_tbtgw where robot_id not in (select id from fl_robot_info))"); } catch { }
try { session.ExcuteSQL("delete from fl_plugin_pddcirclepromotion_pddtgw where robot_id in (select robot_id from fl_plugin_pddcirclepromotion_pddtgw where robot_id not in (select id from fl_robot_info))"); } catch { }
#endregion
EventClient.OnEvent("", $"删除机器人:{wx.User.Nick}({wx.WeixinHao})");
wx.LoginOut();
wx.Dispose();
}
break;
case Api.Framework.SDK.ChatType.:
break;
case Api.Framework.SDK.ChatType.:
break;
default:
break;
}
}
session.ExcuteSQL("delete from fl_robot_info where id=" + row.id);
session.ExcuteSQL("delete from fl_weixin_password where robot_id=" + row.id);
Refresh();
}
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (sender == null || MessageBox.Show("确定要注销此机器人吗?", "注销机器人", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
var r = this.gridView1.GetSelectedRows();
if (r.Length == 0) return;
var row = this.gridView1.GetRow(r[0]) as fl_robot_info;
if (row != null)
{
if (row.type == Api.Framework.SDK.ChatType. || row.type == Api.Framework.SDK.ChatType.)
{
row.is_login = false;
ApiClient.GetSession().SaveOrUpdate(row);
EventClient.OnEvent("", $"注销机器人:{row.nick}({row.name})");
ChatClient.LoginOutWeixin(row.name);
}
else if (row.type == Api.Framework.SDK.ChatType.QQ)
ChatClient.LoginOutQQ(row.name);
}
if (sender != null)
Refresh();
}
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
var r = this.gridView1.GetSelectedRows();
ToolStripMenuItem.Visible = ToolStripMenuItem.Enabled = ToolStripMenuItem.Enabled = ToolStripMenuItem.Enabled = ToolStripMenuItem.Enabled = ToolStripMenuItem.Enabled = ToolStripMenuItem.Enabled = ToolStripMenuItem.Enabled = false;
if (r.Length != 0)
{
ToolStripMenuItem.Enabled = ToolStripMenuItem.Enabled = true;
var row = this.gridView1.GetRow(r[0]) as fl_robot_info;
if (row.type == Api.Framework.SDK.ChatType.)
{
ToolStripMenuItem.Enabled = ToolStripMenuItem.Enabled = true;
线ToolStripMenuItem.Enabled = ToolStripMenuItem.Visible = ToolStripMenuItem.Enabled = true;
var wx = ChatClient.WXClient.FirstOrDefault(f => f.Key == row.name).Value;
if (wx != null)
{
var _bool = wx.Status == Chat.Framework.WXSdk.WxStatus.线;
if (_bool) ToolStripMenuItem.Enabled = true;
ToolStripMenuItem.Enabled = _bool;
}
}
else
{
var wx = ChatClient.WXClient.FirstOrDefault(f => f.Key == row.name).Value;
if (wx != null)
{
var _bool = wx.Status == Chat.Framework.WXSdk.WxStatus.线;
ToolStripMenuItem.Enabled = _bool;
}
ToolStripMenuItem.Enabled = true;
}
//if (row.remark == "PCWechat HOOK")
// 账号授权ToolStripMenuItem.Enabled = false;
//else
if (row.type == Api.Framework.SDK.ChatType.QQ)
{
ToolStripMenuItem.Visible = ToolStripMenuItem.Visible = 线ToolStripMenuItem.Enabled = false;
ToolStripMenuItem.Enabled = true;
ToolStripMenuItem.Enabled = true;
if (ChatClient.QQClients.ContainsKey(row.name))
{
var qBase = ChatClient.QQClients[row.name];
if (qBase is QQClientImpl_QQPlus)
{
var q = qBase as QQClientImpl_QQPlus;
var _bool = q.QQClient.User.LoginStatus == LoginStatus.Login;
if (_bool) ToolStripMenuItem.Enabled = true;
ToolStripMenuItem.Enabled = _bool;
}
}
}
}
else
e.Cancel = true;
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
var r = this.gridView1.GetSelectedRows();
if (r.Length == 0) return;
var row = this.gridView1.GetRow(r[0]) as fl_robot_info;
if (row.type == Api.Framework.SDK.ChatType.)
{
var wx = ChatClient.WXClient.FirstOrDefault(f => f.Key == row.name).Value;
if (wx != null && wx.WeixinType == WeixinType.Grpc微信 && wx.Status == Chat.Framework.WXSdk.WxStatus.线)
{
var temp = wx as WXClientImpl_IPAD;
if (temp != null)
new set_weixin_password(temp).Show();
}
else if (wx.WeixinType == WeixinType.Hook微信 || wx.WeixinType == WeixinType.QYHook微信) BaseForm.ShowErrorAutoClose("该协议不支持微信支付");
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
var r = this.gridView1.GetSelectedRows();
var row = this.gridView1.GetRow(r[0]) as fl_robot_info;
if ((row.uin == "-1" || row.uin == "0") && row.remark == "PCWechat HOOK" && string.IsNullOrWhiteSpace(row.token))
throw new Exception("当前为PC微信,目前免费使用,无需授权!");
if (row.uin != "-1" && row.uin != "0")
{
var authorization = new add_authorization(row);
authorization.ShowDialog();
}
else
throw new Exception("账号存在异常,无法进行授权");
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
var r = this.gridView1.GetSelectedRows();
var row = this.gridView1.GetRow(r[0]) as fl_robot_info;
if (row != null) new set_wechat_device(row.name).ShowDialog();
else new set_wechat_device("").ShowDialog();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
var r = this.gridView1.GetSelectedRows();
var row = this.gridView1.GetRow(r[0]) as fl_robot_info;
if (row != null)
{
row.is_receives = (row.is_receives == Api.Framework.Enums.SwitchType.) ? Api.Framework.Enums.SwitchType. : Api.Framework.Enums.SwitchType.;
var session = ApiClient.GetSession();
session.SaveOrUpdate(row);
Refresh();
}
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void 线ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
var r = this.gridView1.GetSelectedRows();
if (r.Length == 0) return;
var row = this.gridView1.GetRow(r[0]) as fl_robot_info;
if (row != null)
{
row.off_line_tip = (row.off_line_tip == Api.Framework.Enums.SwitchType.) ? Api.Framework.Enums.SwitchType. : Api.Framework.Enums.SwitchType.;
var session = ApiClient.GetSession();
session.SaveOrUpdate(row);
Refresh();
}
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
var r = this.gridView1.GetSelectedRows();
if (r.Length == 0) return;
List<fl_robot_info> weixins = new List<fl_robot_info>();
List<fl_robot_info> qqs = new List<fl_robot_info>();
for (int i = 0; i < r.Length; i++)
{
var _row = this.gridView1.GetRow(r[i]) as fl_robot_info;
if (_row != null)
{
if (_row.type == Api.Framework.SDK.ChatType.)
{
BaseForm.ShowErrorAutoClose(@"企业微信不支持自动登录
请在易转发程序端扫码登录");
return;
}
else if (_row.type == Api.Framework.SDK.ChatType.)
{
if (_row.remark != "PCWechat HOOK")
weixins.Add(_row);
else
{
BaseForm.ShowErrorAutoClose(@"PC微信不支持自动登录
请在易转发程序端扫码登录");
return;
}
}
//else if (_row.type == Api.Framework.SDK.ChatType.微信 && (_row.remark != "PCWechat HOOK"))
else if (_row.type == Api.Framework.SDK.ChatType.QQ)
qqs.Add(_row);
}
}
if (weixins.Count == 0 && qqs.Count == 0)
{
BaseForm.ShowErrorAutoClose("当前没有可以自动登陆的账号");
return;
}
TaskTool tasks = new TaskTool();
foreach (var item in weixins)
{
var _item = item;
tasks.AddTask(delegate ()
{
try
{
EventClient.OnEvent(this, $"正在自动微信->{_item.name}({_item.nick})...");
var wx = ChatClient.WXClient.FirstOrDefault(f => f.Key == _item.name).Value;
if (wx != null) wx.ResetConnection();
else ChatClient.LoginWeixin(_item.name, _item.token);
}
catch (Exception ex)
{
EventClient.OnEvent(this, $"登录微信->{_item.name}({_item.nick})异常:{ex.Message}");
}
});
}
if (qqs.Count != 0)
{
for (int i = 0; i < 1; i++)
{
var _item = qqs[i];
tasks.AddTask(delegate ()
{
try
{
EventClient.OnEvent(this, $"正在自动QQ->{_item.name}({_item.nick})...");
if (string.IsNullOrWhiteSpace(_item.token)) throw new Exception("该账号请手动登录!");
var dic = HttpExtend.JsonToDictionary(Encoding.UTF8.GetString(HttpExtend.HexToByte(_item.token)));
if (dic != null)
{
if (dic.ContainsKey("QQ"))
{
var QQ = dic["QQ"]?.ToString();
if (!string.IsNullOrWhiteSpace(QQ) && dic.ContainsKey("Password"))
{
var Password = dic["Password"]?.ToString();
var qq = uint.Parse(QQ);
if (ChatClient.QQClients.ContainsKey(QQ))
{
var qBase = ChatClient.QQClients[QQ];
if (!qBase.IsLogin)
qBase.Login();
}
else
{
var IsEQQ = false;
if (!string.IsNullOrWhiteSpace(Password) && dic.ContainsKey("IsEQQ"))
IsEQQ = (dic["IsEQQ"].ToString().ToLower() == "true");
var qqClient = ChatClient.LoginQQ(QQ, Password);
}
}
}
}
}
catch (Exception ex)
{
EventClient.OnEvent(this, $"登录QQ->{_item.name}({_item.nick})异常:{ex.Message}");
}
});
}
}
tasks.Start(30, AsyncCallbackMethod);
EventClient.OnEvent(this, MethodType.);
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void AsyncCallbackMethod(IAsyncResult ar)
{
EventClient.OnEvent(this, $"手动尝试登录微信执行完毕!");
}
private void 线ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
var r = this.gridView1.GetSelectedRows();
if (r.Length == 0) return;
var row = this.gridView1.GetRow(r[0]) as fl_robot_info;
if (row != null)
{
row.off_line_auto_login = (row.off_line_auto_login == Api.Framework.Enums.SwitchType.) ? Api.Framework.Enums.SwitchType. : Api.Framework.Enums.SwitchType.;
var session = ApiClient.GetSession();
session.SaveOrUpdate(row);
Refresh();
}
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}