diff --git a/CoreCms.Net.WeChat.Service/Mediator/ImageMessageEventCommandHandler.cs b/CoreCms.Net.WeChat.Service/Mediator/ImageMessageEventCommandHandler.cs new file mode 100644 index 0000000..a2c504b --- /dev/null +++ b/CoreCms.Net.WeChat.Service/Mediator/ImageMessageEventCommandHandler.cs @@ -0,0 +1,67 @@ +/*********************************************************************** + * Project: CoreCms.Net * + * Web: https://CoreCms.Net * + * ProjectName: 核心内容管理系统 * + * Author: 大灰灰 * + * Email: JianWeie@163.com * + * CreateTime: 2020-08-13 23:57:23 + * Description: 暂无 + ***********************************************************************/ + + +using System.Threading; +using System.Threading.Tasks; +using CoreCms.Net.Utility.Helper; +using CoreCms.Net.WeChat.Service.HttpClients; +using CoreCms.Net.WeChat.Service.Models; +using MediatR; +using SKIT.FlurlHttpClient.Wechat.Api; +using SKIT.FlurlHttpClient.Wechat.Api.Events; + +namespace CoreCms.Net.WeChat.Service.Mediator +{ + /// + /// 表示 TEXT 事件的数据 + /// + public class ImageMessageEventCommand : IRequest + { + public ImageMessageEvent EventObj { get; set; } + } + + /// + /// 处理TEXT 事件的数据-以被动回复文本消息为例 + /// + public class ImageMessageEventCommandHandler : IRequestHandler + { + private readonly WeChat.Service.HttpClients.IWeChatApiHttpClientFactory _weChatApiHttpClientFactory; + + + + public ImageMessageEventCommandHandler(IWeChatApiHttpClientFactory weChatApiHttpClientFactory) + { + _weChatApiHttpClientFactory = weChatApiHttpClientFactory; + } + + public async Task Handle(ImageMessageEventCommand request, CancellationToken cancellationToken) + { + + var jm = new WeChatApiCallBack() { Status = true }; + + if (request.EventObj != null) + { + var client = _weChatApiHttpClientFactory.CreateWxOpenClient(); + var replyModel = new SKIT.FlurlHttpClient.Wechat.Api.Events.TransferCustomerServiceReply() + { + ToUserName = request.EventObj.FromUserName, + FromUserName = request.EventObj.ToUserName, + CreateTimestamp = CommonHelper.GetTimeStampByTotalSeconds() + }; + var replyXml = client.SerializeEventToXml(replyModel); + jm.Data = replyXml; + } + + return await Task.FromResult(jm); + } + } + +} diff --git a/CoreCms.Net.WeChat.Service/Mediator/VoiceMessageEventCommandHandler.cs b/CoreCms.Net.WeChat.Service/Mediator/VoiceMessageEventCommandHandler.cs new file mode 100644 index 0000000..c0141e0 --- /dev/null +++ b/CoreCms.Net.WeChat.Service/Mediator/VoiceMessageEventCommandHandler.cs @@ -0,0 +1,67 @@ +/*********************************************************************** + * Project: CoreCms.Net * + * Web: https://CoreCms.Net * + * ProjectName: 核心内容管理系统 * + * Author: 大灰灰 * + * Email: JianWeie@163.com * + * CreateTime: 2020-08-13 23:57:23 + * Description: 暂无 + ***********************************************************************/ + + +using System.Threading; +using System.Threading.Tasks; +using CoreCms.Net.Utility.Helper; +using CoreCms.Net.WeChat.Service.HttpClients; +using CoreCms.Net.WeChat.Service.Models; +using MediatR; +using SKIT.FlurlHttpClient.Wechat.Api; +using SKIT.FlurlHttpClient.Wechat.Api.Events; + +namespace CoreCms.Net.WeChat.Service.Mediator +{ + /// + /// 表示 TEXT 事件的数据 + /// + public class VoiceMessageEventCommand : IRequest + { + public VoiceMessageEvent EventObj { get; set; } + } + + /// + /// 处理TEXT 事件的数据-以被动回复文本消息为例 + /// + public class VoiceMessageEventCommandHandler : IRequestHandler + { + private readonly WeChat.Service.HttpClients.IWeChatApiHttpClientFactory _weChatApiHttpClientFactory; + + + + public VoiceMessageEventCommandHandler(IWeChatApiHttpClientFactory weChatApiHttpClientFactory) + { + _weChatApiHttpClientFactory = weChatApiHttpClientFactory; + } + + public async Task Handle(VoiceMessageEventCommand request, CancellationToken cancellationToken) + { + + var jm = new WeChatApiCallBack() { Status = true }; + + if (request.EventObj != null) + { + var client = _weChatApiHttpClientFactory.CreateWxOpenClient(); + var replyModel = new SKIT.FlurlHttpClient.Wechat.Api.Events.TransferCustomerServiceReply() + { + ToUserName = request.EventObj.FromUserName, + FromUserName = request.EventObj.ToUserName, + CreateTimestamp = CommonHelper.GetTimeStampByTotalSeconds() + }; + var replyXml = client.SerializeEventToXml(replyModel); + jm.Data = replyXml; + } + + return await Task.FromResult(jm); + } + } + +} diff --git a/CoreCms.Net.Web.WebApi/Controllers/WeChatOAuth/WxOpenController.cs b/CoreCms.Net.Web.WebApi/Controllers/WeChatOAuth/WxOpenController.cs index ae12d59..4d93cd1 100644 --- a/CoreCms.Net.Web.WebApi/Controllers/WeChatOAuth/WxOpenController.cs +++ b/CoreCms.Net.Web.WebApi/Controllers/WeChatOAuth/WxOpenController.cs @@ -165,18 +165,21 @@ namespace CoreCms.Net.Web.WebApi.Controllers.WeChatOAuth switch (requestType) { + case RequestMsgType.Text: - var callbackModel = client.DeserializeEventFromXml(msgXml); - callBack = await _mediator.Send(new TextMessageEventCommand() { EventObj = callbackModel }); + var textMessageEvent = client.DeserializeEventFromXml(msgXml); + callBack = await _mediator.Send(new TextMessageEventCommand() { EventObj = textMessageEvent }); break; case RequestMsgType.Location: break; case RequestMsgType.Image: - + var imageMessageEvent = client.DeserializeEventFromXml(msgXml); + callBack = await _mediator.Send(new ImageMessageEventCommand() { EventObj = imageMessageEvent }); break; case RequestMsgType.Voice: - + var voiceMessageEvent = client.DeserializeEventFromXml(msgXml); + callBack = await _mediator.Send(new VoiceMessageEventCommand() { EventObj = voiceMessageEvent }); break; case RequestMsgType.Video: diff --git a/README.md b/README.md index bb0e825..6d13174 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,8 @@

支持可视化布局的.Net小程序商城

[![star](https://gitee.com/CoreUnion/CoreShop/badge/star.svg)](https://gitee.com/CoreUnion/CoreShop.git) [![fork](https://gitee.com/CoreUnion/CoreShop/badge/fork.svg)](https://gitee.com/CoreUnion/CoreShop.git) -[![GitHub stars](https://img.shields.io/github/stars/CoreUnion/CoreShop?logo=github)](https://gihub.com/CoreUnion/CoreShop.git) -[![GitHub forks](https://img.shields.io/github/forks/CoreUnion/CoreShop?logo=github)](https://gihub.com/CoreUnion/CoreShop.git) +[![GitHub stars](https://img.shields.io/github/stars/CoreUnion/CoreShop?logo=github)](https://github.com/CoreUnion/CoreShop) +[![GitHub forks](https://img.shields.io/github/forks/CoreUnion/CoreShop?logo=github)](https://github.com/CoreUnion/CoreShop)