sunpengfei
2025-12-01 6396dac27ca99e84a2e3c772fb079bceddf67ff8
ApiTools.Application/WxUtils/Commands/WxmpSubscribMessageCommandHandler.cs
@@ -1,5 +1,9 @@
using Aop.Api.Domain;
using ApiTools.Core;
using ApiTools.Core.Entities.LogRecords;
using Furion;
using Furion.DatabaseAccessor;
using Furion.HttpRemote;
using log4net.Core;
using MediatR;
using Microsoft.AspNetCore.Http;
@@ -13,16 +17,18 @@
namespace ApiTools.Application
{
    public class WxmpSubscribMessageCommandHandler(
            IRepository<WxmpSubscribMessageLog> repWxmpSubscribMessageLog,
            ILogger<WxmpSubscribMessageCommandHandler> logger,
            WxmpUtils utils,
            IHttpContextAccessor httpContextAccessor
            IHttpRemoteService httpRemoteService
        ) :
        IRequestHandler<WxmpSendSubscribMessageCommand, Guid>,
        IRequestHandler<WxmpSubscribMessageNotifyCommand, bool>
        IRequestHandler<SendWxmpSubscribMessageCommand, Guid>,
        IRequestHandler<WxmpSubscribMessageNotifyCommand, Guid>
    {
        private readonly IRepository<WxmpSubscribMessageLog> repWxmpSubscribMessageLog = repWxmpSubscribMessageLog;
        private readonly ILogger<WxmpSubscribMessageCommandHandler> logger = logger;
        private readonly WxmpUtils utils = utils;
        private readonly IHttpContextAccessor httpContextAccessor = httpContextAccessor;
        private readonly IHttpRemoteService httpRemoteService = httpRemoteService;
        /// <summary>
        /// 微信小程序发送订阅消息
@@ -30,17 +36,27 @@
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Guid> Handle(WxmpSendSubscribMessageCommand request, CancellationToken cancellationToken)
        public async Task<Guid> Handle(SendWxmpSubscribMessageCommand request, CancellationToken cancellationToken)
        {
            await utils.WxmpSendSubscribMessage(new WxmpSendSubscribMessageRequest
            {
                Data = request.Data,
                TemplateId = request.Template.ToString(),
                TemplateId = request.TemplateId,
                Page = request.Page,
                WxmpCode = request.WxmpCode,
                Touser = request.Touser,
            });
            return Guid.Empty;
            var log = new WxmpSubscribMessageLog
            {
                Code = request.WxmpCode,
                OpenId = request.Touser,
                SubscribeStatusString = "send",
                TemplateId = request.TemplateId,
                Page = request.Page,
                Data = request.Data.ToJson()
            };
            await repWxmpSubscribMessageLog.InsertNowAsync(log);
            return log.Id;
        }
        /// <summary>
@@ -49,18 +65,33 @@
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<bool> Handle(WxmpSubscribMessageNotifyCommand request, CancellationToken cancellationToken)
        public async Task<Guid> Handle(WxmpSubscribMessageNotifyCommand request, CancellationToken cancellationToken)
        {
            var req = httpContextAccessor.HttpContext.Request;
            logger.LogInformation($"微信小程序订阅消息通知query:{req.QueryString.Value}");
            var env = App.GetConfig<string>("Environment");
            if (env == "Product")
            {
                try
                {
                    var json = request.ToJson();
                    await httpRemoteService.PostAsStringAsync("http://118.178.252.28:8780/api/common/wxmp/wxmpSubscribMessageNotify",
                        builder => builder.SetJsonContent(json));
                }
                catch
                {
            req.EnableBuffering();
            req.Body.Position = 0;
            using var reader = new StreamReader(req.Body, Encoding.UTF8, leaveOpen: true);
            var body = await reader.ReadToEndAsync();
            logger.LogInformation($"微信小程序订阅消息通知body:{body}");
            req.Body.Position = 0;
            return true;
                }
            }
            var log = new WxmpSubscribMessageLog
            {
                Code = request.Code,
                OpenId = request.OpenId,
                PopupScene = request.Content.PopupScene,
                SubscribeStatusString = request.Content.SubscribeStatusString,
                TemplateId = request.Content.TemplateId
            };
            await repWxmpSubscribMessageLog.InsertNowAsync(log);
            return log.Id;
        }
    }
}