sunpengfei
2025-09-30 a6e5a2e630a4a1dcfd9def94c20bde6bec89f96f
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
using Furion.DatabaseAccessor;
using Furion.DataEncryption;
using Furion.DependencyInjection;
using Furion.HttpRemote;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace ApiTools.Core
{
    public class ChengLiYeSmsService(
        IRepository<ThreeResourceLog, LogDbContextLocator> repThreeResourceLog,
        IOptions<ChengLiYeSmsOptions> options,
        IHttpRemoteService httpRemoteService
        ) : ISmsService, ITransient
    {
        private readonly IRepository<ThreeResourceLog, LogDbContextLocator> repThreeResourceLog = repThreeResourceLog;
        private readonly IOptions<ChengLiYeSmsOptions> options = options;
        private readonly IHttpRemoteService httpRemoteService = httpRemoteService;
 
        public async Task<SmsResponse> SendAsync(string phoneNumber, EnumSmsTemplateCode templateCode, object templateParam, CancellationToken cancellationToken)
        {
            var content = options.Value.TemplateCodes[templateCode.ToString()];
            if (templateParam != null)
            {
                var jObject = JObject.FromObject(templateParam);
                var props = jObject.Properties();
                foreach (var prop in props)
                {
                    content = content.Replace($"${{{prop.Name}}}", prop.Value.ToString());
                }
            }
 
            var body = new ChengLiYeSmsSubmitRequest
            {
                UserName = options.Value.UserName,
                Sign = MD5Encryption.Encrypt($"{options.Value.UserName}{options.Value.Password}{phoneNumber}【{options.Value.SignName}】{content}"),
                Mobile = phoneNumber,
                Content = $"【{options.Value.SignName}】{content}",
            };
            var log = new ThreeResourceLog
            {
                Method = EnumResourceMethod.Get,
                Domain = "https://www.sms-cly.cn",
                Path = "/v7/msg/submit.json",
                Request = body.ToJson(),
            };
            await repThreeResourceLog.InsertNowAsync(log);
            var stopwatch = Stopwatch.StartNew();
            var result = await httpRemoteService.PostAsAsync<ChengLiYeSmsSubmitResponse>($"{log.Domain}{log.Path}",
                build => build.SetJsonContent(body));
            log.UpdatedTime = DateTimeOffset.Now;
            stopwatch.Stop();
            log.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds;
            log.Response = result.ToJson();
            log.IsSuccess = result.ResultCode == "1";
            await repThreeResourceLog.UpdateNowAsync(log);
 
            return new SmsResponse
            {
                Status = log.IsSuccess
                    ? EnumSmsStatus.InProcess
                    : EnumSmsStatus.Fail,
                Code = result.ResultCode,
                Message = result.ResultMsg,
                RequestId = result.Msgid
            };
        }
    }
}