sunpengfei
2025-12-01 6396dac27ca99e84a2e3c772fb079bceddf67ff8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ApiTools.Application
{
    public class WxmpSubscribMessageCommandHandler(
            IRepository<WxmpSubscribMessageLog> repWxmpSubscribMessageLog,
            ILogger<WxmpSubscribMessageCommandHandler> logger,
            WxmpUtils utils,
            IHttpRemoteService httpRemoteService
        ) :
        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 IHttpRemoteService httpRemoteService = httpRemoteService;
 
        /// <summary>
        /// 微信小程序发送订阅消息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Guid> Handle(SendWxmpSubscribMessageCommand request, CancellationToken cancellationToken)
        {
            await utils.WxmpSendSubscribMessage(new WxmpSendSubscribMessageRequest
            {
                Data = request.Data,
                TemplateId = request.TemplateId,
                Page = request.Page,
                WxmpCode = request.WxmpCode,
                Touser = request.Touser,
            });
            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>
        /// 微信小程序订阅消息通知
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Guid> Handle(WxmpSubscribMessageNotifyCommand request, CancellationToken cancellationToken)
        {
            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
                {
 
                }
            }
 
            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;
        }
    }
}