old_flsystem/类库/Api.Framework/EventClient.cs

247 lines
9.7 KiB
C#

using Api.Framework.Events;
using Api.Framework.Model;
using Api.Framework.SDK;
using Api.Framework.Tools;
using Chat.Framework;
using Chat.Framework.WXSdk;
using Chat.Framework.WXSdk.Events;
using Chat.Framework.WXSdk.Implement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Api.Framework
{
/// <summary>
/// 事件管理
/// </summary>
public class EventClient
{
static EventClient()
{
ChatClient.Events.WXApplyFriendEvent += OnEvent;
ChatClient.Events.WXApplyGroupEvent += OnEvent;
ChatClient.Events.WXChangeStatusEvent += OnEvent;
ChatClient.Events.WXNewFriendEvent += OnEvent;
ChatClient.Events.WXNewMemerEvent += OnEvent;
ChatClient.Events.WXReceiveGroupMsgEvent += OnEvent;
ChatClient.Events.WXReiceveFriendMsgEvent += OnEvent;
ChatClient.Events.WXSendMessageEvent += OnEvent;
ChatClient.Events.WXWXReicevePayEvent += OnEvent;
ChatClient.Events.WXRefreshContactSuccessEvent += OnEvent;
ChatClient.Events.WXOpenWindowEvent += OnEvent;
ChatClient.QQEvents.QQReceiveFriendMsgEvents += OnEvent;
ChatClient.QQEvents.QQReceiveGroupMsgEvents += OnEvent;
ChatClient.QQEvents.QQAddMeNeedAuthEvents += OnEvent;
ChatClient.QQEvents.QQNewFriendEvents += OnEvent;
ChatClient.QQEvents.QQNewGroupMemberEvents += OnEvent;
ChatClient.QQEvents.QQAddToClusterNeedAuthEvents += OnEvent;
//ChatClient.QQEvents.QQVerifyCodeEvents += QQPool_QQVerifyCodeEvents;
ChatClient.QQEvents.QQLogEvents += QQPool_QQLogEvents;
}
private static void QQPool_QQLogEvents(object sender, Chat.Framework.QQSdk.Events.QQLogEvents e)
{
LogEvent(sender, new LogEvents(e.Message));
}
/// <summary>
/// 是否传递插件
/// </summary>
public static bool StopParsing { get; set; }
/// <summary>
/// 给插件传递消息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void OnEvent(object _sender, object _e)
{
if (StopParsing) return;
object sender = _sender;
object e = _e;
var method = new Action(delegate ()
//Task.Factory.StartNew(delegate ()
{
var msgId = Guid.NewGuid().ToString();
try
{
var plugins = new List<Plugin>(PluginClient.Plugins);
var type = e.GetType();
if (e is Chat.Framework.ChatEvent)
{
//通用消息解析
var b = e as Chat.Framework.ChatEvent;
if (b.IsToString)
EventClient.OnEvent(sender, e.ToString());
if (type == typeof(Chat.Framework.PCRobotSDK.WechatEvents.WechatReceiveMsgEvents) || type == typeof(Chat.Framework.QQSdk.Events.QQReceiveGroupMsgEvents) || type == typeof(Chat.Framework.QQSdk.Events.QQReceiveFriendMsgEvents) || type == typeof(Chat.Framework.WXSdk.Events.WXReceiveGroupMsg) || type == typeof(Chat.Framework.WXSdk.Events.WXReiceveFriendMsg) || type == typeof(DebugEvent))
{
var _time = DateTime.Now;
var c = new ReciveIMEvent(sender, e);
#region
var session = ApiClient.GetSession();
var robots = session.FindRobots();
if (null != robots.FirstOrDefault(f => f.is_receives == Enums.SwitchType. && c.RobotInfo.name == f.name))
return;
#endregion
#region ,
if (c.Sender.GetType() == typeof(WXClientImpl_QYHOOK))
{
var qyhook = c.Sender as WXClientImpl_QYHOOK;
var member = session.FindSingle<fl_member_info>("select * from fl_member_info where robot_type = @robot_type and username = @username", new { username = c.Username, robot_type = c.ChatType });
if (member == null)
{
c.GetMemberinfo();
Friend friend = null;
friend = qyhook.GetContact(c.Username);//这里很大概率获取不到用户的昵称,新用户
if (friend == null)
{
return;
}
friend.NickName = Util.RemoveEmoji(friend.NickName);
EventClient.OnEvent(qyhook, new WXNewFriend(qyhook, friend));
//return;
}
}
#endregion
var m = c.GetMemberinfo();
Dictionary<string, double> time = new Dictionary<string, double>();
foreach (var plugin in plugins)
{
if (!plugin.IsRun) continue;//未运行
var _t = DateTime.Now;
plugin.SDK.OnEvent(sender, c);
time[plugin.Name] = (DateTime.Now - _t).TotalSeconds;
if (c.Cancel)
{
break;
//return;//取消传递
}
}
ApiClient.GetSession().Insertable<fl_debug_log>(new fl_debug_log()
{
crt_time = DateTime.Now,
message = b.ToString(),
token = CsharpHttpHelper.HttpHelper.ObjectToJson(time),
time = (DateTime.Now - _time).TotalSeconds,
userid = m.id
}).ExecuteCommandAsync();
return;
}
//正常事件解析
foreach (var plugin in plugins)
{
if (!plugin.IsRun) continue;//未运行
if (b.Cancel) return;//已取消传递
plugin.SDK.OnEvent(sender, e);
}
}
else if (e is BaseEvents)
{
var b = e as BaseEvents;
//解析API事件
foreach (var plugin in plugins)
{
if (!plugin.IsRun) continue;//未运行
if (b.Cancel) return;//已取消传递
plugin.SDK.OnEvent(sender, e);
}
}
}
catch (Exception ex)
{
//OnEvent(null, $"{msgId} - 异常:"+ex.Message);
}
});
method.BeginInvoke(null, null);
}
/// <summary>
/// 日志事件
/// </summary>
public static event EventHandler<LogEvents> LogEvent;
/// <summary>
/// 输出日志触发
/// </summary>
/// <param name="sender"></param>
/// <param name="log"></param>
public static void OnEvent(object sender, string log)
{
OnEvent(sender, new LogEvents(log));
}
/// <summary>
/// 输出日志触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
internal static void OnEvent(object sender, LogEvents e)
{
if (LogEvent != null)
{
LogEvent.Invoke(sender, e);
}
}
public static event EventHandler<DyyNoticeEvent> DyyNoticeEvent;
/// <summary>
/// 触发代运营通知事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public static void OnDyyNoticeEvent(object sender, DyyNoticeEvent e)
{
if (DyyNoticeEvent != null)
{
DyyNoticeEvent.Invoke(sender, e);
}
}
public static event EventHandler<SharedEvents> SharedEvent;
/// <summary>
/// 触发通用事件
/// </summary>
internal static void OnSharedEvent(object sender, SharedEvents e)
{
if (LogEvent != null)
{
SharedEvent.Invoke(sender, e);
}
}
/// <summary>
/// 刷新刷新传递事件(刷新某些数据或者页面数据)
/// </summary>
public static event EventHandler<MethodType> MethodEvent;
/// <summary>
/// 刷新事件触发
/// </summary>
public static void OnEvent(object sender, MethodType e)
{
if (MethodEvent != null)
{
MethodEvent.Invoke(sender, e);
}
}
/// <summary>
/// 替换短网址链接
/// </summary>
public static Func<int, string, string> ReplaceURL;
}
}