李昊 5 lat temu
rodzic
commit
f40bc6b323

BIN
GxPress/.DS_Store


BIN
GxPress/Api/.DS_Store


+ 18 - 13
GxPress/Api/GxPress.Api/AppControllers/UserController.cs

@@ -8,6 +8,7 @@ using Datory.Utils;
 using GxPress.Api.Tools;
 using GxPress.Auth;
 using GxPress.Common.Exceptions;
+using GxPress.Common.Tools;
 using GxPress.Common.Validation;
 using GxPress.Entity;
 using GxPress.EnumConst;
@@ -183,7 +184,7 @@ namespace GxPress.Api.AppControllers
         /// <returns></returns>
         [HttpGet("sendSmsCode")]
         [AllowAnonymous]
-        public async Task<bool> SendSmsCode([FromQuery] [Required] [Mobile] string phone)
+        public async Task<bool> SendSmsCode([FromQuery][Required][Mobile] string phone)
         {
             var user = await _userRepository.GetByPhoneAsync(phone);
 
@@ -196,19 +197,23 @@ namespace GxPress.Api.AppControllers
             //TODO 短信验证码发送
             //return await _smsService.
             //SendValidationCodeAsync(phone);
+            //发送短信
             var key = $"login:{phone}";
-            //if (await RedisHelper.ExistsAsync(key)) throw new BusinessException("发送太频繁");
-            var code = "180606";
-            _logger.LogInformation($"{key}", phone, code);
-            var codeByte = Encoding.UTF8.GetBytes(Utilities.JsonSerialize(code));
-            await _cache.SetAsync($"{key}", codeByte, new DistributedCacheEntryOptions
+            var code = await _cache.GetStringAsync(key);
+            if (!string.IsNullOrEmpty(code))
+                throw new BusinessException("请求太频繁!");
+            code = RandomGenerator.GetNumberString(6);
+            if (Common.Sms.MasSms.SendSms(phone, code))
             {
-                AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(300)
-            });
-            // var result = await RedisHelper.SetAsync(key, code, 300);
-            // if (result == false)
-            //     throw new BusinessException("发送失败");
-            return true;
+                _logger.LogInformation("{phone}验证码:{code}", phone, code);
+                var codeByte = Encoding.UTF8.GetBytes(Utilities.JsonSerialize(code));
+                await _cache.SetAsync($"{key}", codeByte, new DistributedCacheEntryOptions
+                {
+                    AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(60)
+                });
+                return true;
+            }
+            return false;
         }
 
         /// <summary>
@@ -217,7 +222,7 @@ namespace GxPress.Api.AppControllers
         /// <param name="phone"></param>
         /// <returns></returns>
         [HttpGet("send-sms-code")]
-        public async Task<bool> SendSmsCodeReplace([FromQuery] [Required] [Mobile] string phone)
+        public async Task<bool> SendSmsCodeReplace([FromQuery][Required][Mobile] string phone)
         {
             var user = await _userRepository.GetByPhoneAsync(phone);
             if (user != null)

+ 10 - 0
GxPress/Infrastructure/GxPress.Common/Encrypt/EncryptProvider.cs

@@ -31,5 +31,15 @@ namespace GxPress.Common.Encrypt
             strMd5Out = strMd5Out.Replace("-", "");
             return strMd5Out;
         }
+        /// <summary>
+        /// 将字符串转换成base64格式,使用UTF8字符集
+        /// </summary>
+        /// <param name="content">加密内容</param>
+        /// <returns></returns>
+        public static string Base64Encode(string content)
+        {
+            byte[] bytes = Encoding.UTF8.GetBytes(content);
+            return Convert.ToBase64String(bytes);
+        }
     }
 }

+ 61 - 0
GxPress/Infrastructure/GxPress.Common/Sms/MasSms.cs

@@ -0,0 +1,61 @@
+using System;
+using System.IO;
+using System.Net;
+using System.Text;
+using GxPress.Common.Encrypt;
+using Newtonsoft.Json;
+namespace GxPress.Common.Sms
+{
+    public static class MasSms
+    {
+        /// <summary>
+        /// 广西短信
+        /// </summary>
+        /// <param name="phone"></param>
+        /// <param name="code"></param>
+        /// <returns></returns>
+        public static bool SendSms(string phone, string code)
+        {
+            var url = "http://112.35.1.155:1992/sms/norsubmit";
+            try
+            {
+                var content = $"打死也不能告诉别人,短信验证码:{code}";
+                var md5 = EncryptProvider.Md5($"接口联调账号impactpasswd@1342{phone}{content}bM16CfN2B").ToLower();
+                var json = "{\"ecName\":\"接口联调账号\", \"apId\":\"impact\", \"mobiles\":\"" + phone + "\", \"content\":\"" + content + "\", \"sign\":\"bM16CfN2B\", \"addSerial\":\"\", \"mac\":\""+md5+"\"}";
+                var base64Encode = EncryptProvider.Base64Encode(json);
+                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(base64Encode); //转化
+                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
+                webReq.Method = "POST";
+                webReq.ContentType = "application/x-www-form-urlencoded";
+                //webReq.ContentType = "application/json";
+                webReq.ContentLength = byteArray.Length;
+                Stream newStream = webReq.GetRequestStream();
+                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
+                newStream.Close();
+                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
+                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
+                var ret = sr.ReadToEnd();
+                sr.Close();
+                response.Close();
+                newStream.Close();
+                var success = JsonConvert.DeserializeObject<SmsSuccess>(ret);
+                return success.Success;
+            }
+            catch (Exception ex)
+            {
+                throw new Common.Exceptions.BusinessException(ex.Message);
+            }
+
+        }
+
+    }
+
+    public class SmsSuccess
+    {
+        public string MsgGroup { get; set; }
+
+        public string Rspcod { get; set; }
+
+        public bool Success { get; set; }
+    }
+}