sunpengfei
2025-11-21 8d9adc809cee12d16caf9854ada2e7d030a5f4b4
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
98
99
100
101
102
103
104
105
106
107
108
109
using ApiTools.Core;
using Baidu.Aip.BodyAnalysis;
using Furion;
using Furion.DatabaseAccessor;
using Furion.HttpRemote;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ApiTools.CommonServer.Application
{
    /// <summary>
    /// 阿里云短信平台回传通知
    /// </summary>
    public class SmsAliyunNotifyCommandHandler(
            IRepository<SmsSetting> repSmsSetting,
            IRepository<SmsLog> repSmsLog,
            ApiTools.Core.SmsUtils smsUtils,
            IHttpRemoteService httpRemoteService
        )
        : IRequestHandler<SmsAliyunNotifyCommand, SmsAliyunNotifyCommandResult>
    {
        private readonly IRepository<SmsSetting> repSmsSetting = repSmsSetting;
        private readonly IRepository<SmsLog> repSmsLog = repSmsLog;
        private readonly Core.SmsUtils smsUtils = smsUtils;
        private readonly IHttpRemoteService httpRemoteService = httpRemoteService;
 
        /// <summary>
        /// 阿里云短信平台回传通知
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<SmsAliyunNotifyCommandResult> Handle(SmsAliyunNotifyCommand 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/sms/smsAliyunNotify",
                        builder => builder.SetJsonContent(json));
                }
                catch
                {
 
                }
            }
 
            var templateCodes = await EnumUtils.GetModel<EnumSmsTemplateCode>();
            var msgIds = request.DistinctSelect(it => it.BizId.ToString());
            var entities = await repSmsLog.AsQueryable()
                .Where(it =>
                    it.Access == EnumSmsAccess.AliyunSms
                    && msgIds.Contains(it.RequestId)
                    && it.Status == EnumSmsStatus.InProcess)
                .ToListAsync();
            var channelIds = entities.DistinctSelect(it => it.ChannelId.HasValue, it => it.ChannelId.Value);
            var settings = await repSmsSetting.AsQueryable().AsNoTracking()
                .Include(it => it.Accesses)
                .Where(it => it.ChannelId.HasValue && channelIds.Contains(it.ChannelId.Value))
                .ToListAsync();
            foreach (var entity in entities)
            {
                var report = request.FirstOrDefault(it => it.BizId.ToString() == entity.RequestId);
                if (report != null)
                {
                    entity.Status = report.Success
                        ? EnumSmsStatus.Success
                        : EnumSmsStatus.Fail;
                    entity.Code = report.ErrCode;
                    entity.Message = report.ErrMsg;
                    await repSmsLog.UpdateNowAsync(entity);
 
                    if (entity.Status == EnumSmsStatus.Fail)
                    {
                        var templateCode = templateCodes.Items.FirstOrDefault(it => it.Name == entity.TemplateCode)?.Enum;
                        if (templateCode.HasValue)
                        {
                            var templateParam = entity.TemplateParam.IsNotNull()
                                ? JObject.Parse(entity.TemplateParam)
                                : null;
                            var setting = settings.FirstOrDefault(it => it.ChannelId == entity.ChannelId);
                            await smsUtils.Send(setting, entity, new SendSmsModel
                            {
                                PhoneNumber = entity.PhoneNumber,
                                TemplateCode = templateCode.Value,
                                Expiry = entity.Expiry,
                            }, templateParam, cancellationToken);
                        }
                    }
                }
            }
            return new SmsAliyunNotifyCommandResult
            {
                Code = 0,
                Msg = " 成功"
            };
        }
    }
}