移动系统liao
2025-06-27 6023f73a031bf93797d1cb74e705371abd3990e6
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
 
using Admin.NET.Core.Service;
using Admin.NET.Core;
using Furion.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Dm.util;
using Aop.Api.Domain;
using Furion.FriendlyException;
using Furion.DataValidation;
using static QRCoder.PayloadGenerator;
using System.Net;
using Org.BouncyCastle.Asn1.Ocsp;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Furion.DynamicApiController;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using Yitter.IdGenerator;
using Lazy.Captcha.Core;
using Furion;
using NewLife;
 
namespace FZCZTB.NET.SYSService.MSM;
/// <summary>
/// 政采短信服务
/// </summary>
[AllowAnonymous]
[ApiDescriptionSettings("FZCAPISYS",Order = 149)]
public class ZCSMSService : IDynamicApiController,ITransient
{
    private readonly SMSConfigOptions _smsOptions;
    private readonly SysCacheService _sysCacheService;
    private readonly ICaptcha _captcha;
    public ZCSMSService(IOptions<SMSConfigOptions> smsOptions,
          SysCacheService sysCacheService,
          ICaptcha captcha
          )
    {
      
        _smsOptions = smsOptions.Value;
        _sysCacheService = sysCacheService;
    
         _captcha = captcha;
    }
 
 
    /// <summary>
    /// 根据藏用模版格式化短信内容 会自动贴上标签
    /// </summary>
    /// <param name="Code"> 需要替换的内容,用于替换末班内容的code部分</param>
    /// <param name="Key"></param>
    [NonAction]
    public string FormartMessage(string? Code, string Key = "VCode")
    {
     var Temp=     _smsOptions.lingKai.Templates.Where(x => x.TemplateCode == Key).FirstOrDefault();
        if ( Temp != null )
        {
            if(Code != null)
            {
             
                    return Temp.Content.replace("${code}", Code) + Temp.SignName;
             
            }
           
            else
                return Temp.Content+Temp.SignName;
        }
        if (Code == null)
            throw Oops.Oh("短信内容为空");
        return Code;
    }
 
 
    /// <summary>
    /// 发送短信
    /// </summary>
    /// <param name="Content">要替换的内容</param>   
    [NonAction]
    public async Task SendSMSAsync(string Content,string Phone)
    {
        if (!Phone.TryValidate(ValidationTypes.PhoneNumber).IsValid) throw Oops.Oh("请正确填写手机号码");
 
       if( string.IsNullOrEmpty(Content)) throw Oops.Oh("请填写正确的手机号码");
 
        string postdata1 = "CorpID=" + _smsOptions.lingKai.Number + "&Pwd=" + _smsOptions.lingKai.AccountPassPassword + "&Mobile=" + Phone + "&Content="+ Content + "&SendTime=";
        string code = (string)await HttpPost(_smsOptions.lingKai.Url, postdata1);
 
        var data = Convert.ToInt64(code);
        if (data > 0)
        {
             return ;
        }
        Oops.Oh($"发送短信失败,错误码{data}");
   
 
    }
 
  
 
 
    /// <summary>
    /// 获取验证码 🔖
    /// </summary>
    /// <returns></returns>
    [AllowAnonymous]
    [SuppressMonitor]
    [DisplayName("获取验证码")]
    public dynamic GetCaptcha()
    {
        var codeId = YitIdHelper.NextId().ToString();
        var captcha = _captcha.Generate(codeId);
        var expirySeconds = App.GetOptions<CaptchaOptions>()?.ExpirySeconds ?? 60;
        return new { Id = codeId, Img = captcha.Base64, ExpirySeconds = expirySeconds };
    }
 
    /// <summary>
    /// 校验短信验证码
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [AllowAnonymous]
    [DisplayName("校验短信验证码")]
    public bool VerifyCode(SmsVerifyCodeInput input)
    {
        var verifyCode = _sysCacheService.Get<string>($"{CacheConst.KeyPhoneVerCode}{input.Phone}");
 
        if (string.IsNullOrWhiteSpace(verifyCode)) throw Oops.Oh("验证码不存在或已失效,请重新获取!");
 
        if (verifyCode != input.Code) throw Oops.Oh("验证码错误!");
        _sysCacheService.Remove($"{CacheConst.KeyPhoneVerCode}{input.Phone}");
        return true;
    }
    /// <summary>
    /// 发送验证短信,要要验证码 需要输入校验,避免电脑攻击
    /// </summary>
    /// <param name="phoneNumber"> 电话号码</param>
    /// <param name="VerifyCode">图片校验数据</param>
    ///   <param name="VerifyCodeId">检验的ID</param>
    /// <returns></returns>
    [AllowAnonymous]
    [DisplayName("发送验证码")]
    public async Task SendSMS([Required] string phoneNumber , [Required] string  VerifyCode, [Required] string  VerifyCodeId)
    {
        if (!phoneNumber.TryValidate(ValidationTypes.PhoneNumber).IsValid) throw Oops.Oh("请正确填写手机号码");
 
        // 校验验证码
        if (!_captcha.Validate(VerifyCodeId, VerifyCode)) throw Oops.Oh(ErrorCodeEnum.D0008);
        _captcha.Generate(VerifyCodeId);
 
        // 生成随机验证码
        var random = new Random();
        var verifyCode = random.Next(100000, 999999);
 
        var templateParam = new
        {
            code = verifyCode
        };
       var code=   FormartMessage(verifyCode.toString());
       
       await SendSMSAsync(code, phoneNumber);
   
        _sysCacheService.Set($"{CacheConst.KeyPhoneVerCode}{phoneNumber}", verifyCode, TimeSpan.FromSeconds(_smsOptions.lingKai.VerifyTimeOut.ToInt()));
     
         
 
        
    }
 
    /// <summary>
    /// 发送验证码
    /// </summary>
    /// <param name="Url"> 链接</param>
    /// <param name="postDataStr"> 验证码</param>
    /// <returns></returns>
    private  async Task<string> HttpPost(string Url, string postDataStr)
    {
        //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
        //request.Method = "POST";
        //request.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
 
        //byte[] postData = Encoding.GetEncoding("gb2312").GetBytes(postDataStr);
        //request.ContentLength = postData.Length;
        //Stream myRequestStream = request.GetRequestStream();
        //myRequestStream.Write(postData, 0, postData.Length);
        //myRequestStream.Close();
        //HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
        //Stream myResponseStream = response.GetResponseStream();
        //StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
        //string retString = myStreamReader.ReadToEnd();
        //myStreamReader.Close();
        //myResponseStream.Close();
 
        //return retString;
        // 创建 HttpClient 实例
        using var httpClient = new HttpClient();
        // 准备要发送的内容,设置编码和内容类型
        var content = new StringContent(postDataStr, Encoding.GetEncoding("gb2312"), "application/x-www-form-urlencoded");
        // 发送 POST 请求
        var response = await httpClient.PostAsync(Url, content);
        // 确保请求成功,若失败则抛出异常
        response.EnsureSuccessStatusCode();
        // 读取响应内容
        return await response.Content.ReadAsStringAsync();
    }
 
}