瀏覽代碼

Merge branch 'fix/sendsms' into release

qinchaoyue 6 月之前
父節點
當前提交
f22a2fe7d5

+ 1 - 1
src/Hotline.Api/Controllers/OrderController.cs

@@ -6374,7 +6374,7 @@ public class OrderController : BaseController
                 i++;
                 try
                 {
-                    var validationResult = item.ValidateObject();
+                    var validationResult = item.ValidateObject(false);
                     if (validationResult.NotNullOrEmpty())
                     {
                         errorMessage.Append($"第{i + 1}行: {validationResult}\r\n");

+ 17 - 6
src/Hotline.Api/Controllers/PushMessageController.cs

@@ -10,8 +10,10 @@ using Hotline.Share.Dtos.Push.FWMessage;
 using Hotline.Share.Enums.Push;
 using Hotline.Share.Mq;
 using Hotline.Share.Tools;
+using Hotline.Tools;
 using Hotline.Users;
 using J2N.Text;
+using JiebaNet.Segmenter.Common;
 using MapsterMapper;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Mvc;
@@ -131,14 +133,23 @@ namespace Hotline.Api.Controllers
         [HttpPost("send")]
         public async Task<string> SendMessage([FromBody] MessageInDto dto)
         {
-            var telNumbers = dto.TelNumbers.Split(",");
+            dto.ValidateObject();
+            if (dto.TelNumbers == null && dto.UserIds == null)
+                throw UserFriendlyException.SameMessage("电话号码和用户Id不能同时为空");
+            var telNumbers = new List<Kv>();
+            if (dto.TelNumbers.NotNullOrEmpty())
+                telNumbers = dto.TelNumbers.Split(",").Select(m => new Kv(m, m)).ToList();
+            if (dto.UserIds.IsNotEmpty())
+                telNumbers = await _userRepository.Queryable()
+                    .Where(m => dto.UserIds.Contains(m.Id))
+                    .Select(m => new Kv { Key = m.PhoneNo, Value = m.Name })
+                    .ToListAsync();
             var count = 0;
             var errorSB = new StringBuilder("失败原因: ");
             var errorCount = 0;
-            for (int i = 0;i < telNumbers.Length;i++)
+            foreach (var telNumber in telNumbers)
             {
-                var telNumber = telNumbers[i];
-                if (telNumber.IsPhoneNumber() == false)
+                if (telNumber.Key.IsPhoneNumber() == false)
                 {
                     errorCount++;
                     errorSB.Append($" {telNumber}不是合法的手机号码.");
@@ -147,10 +158,10 @@ namespace Hotline.Api.Controllers
 
                 var inDto = new MessageDto
                 {
-                    TelNumber = telNumber,
+                    TelNumber = telNumber.Key,
                     Content = dto.Content,
                     PushBusiness = EPushBusiness.ManualSms,
-                    Name = "",
+                    Name = telNumber.Value,
                 };
                 try
                 {

+ 10 - 0
src/Hotline.Application.Tests/Controller/OrderControllerTest.cs

@@ -1,6 +1,7 @@
 using AutoFixture;
 using Hotline.Api.Controllers;
 using Hotline.Share.Dtos.Order;
+using Hotline.Share.Enums.Settings;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.AspNetCore.Mvc.Testing;
@@ -38,4 +39,13 @@ public class OrderControllerTest : IClassFixture<WebApplicationFactory<Startup>>
         var inDto = _fixture.Create<VisitSmsInDto>();
         await _orderController.VisitPushSMSAsync(inDto);
     }
+
+    [Fact]
+    public async Task GetOrderHandleTimeConfigByAcceptType_Test()
+    {
+        var result = await _orderController.GetOrderHandleTimeConfigByAcceptType(Share.Enums.FlowEngine.EFlowDirection.OrgToCenter, "10");
+        result.Count.ShouldBe(1);
+        result.TimeText.ShouldBe("1个工作日");
+        result.TimeType.ShouldBe(ETimeType.WorkDay);
+    }
 }

+ 80 - 0
src/Hotline.Application.Tests/Controller/PushMessageControllerTest.cs

@@ -0,0 +1,80 @@
+using Hotline.Api.Controllers;
+using Hotline.Share.Dtos.Push;
+using Hotline.Users;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Shouldly;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using XF.Domain.Exceptions;
+using XF.Domain.Repository;
+
+namespace Hotline.Application.Tests.Controller;
+
+public class PushMessageControllerTest
+{
+    private readonly PushMessageController _pushMessageController;
+    private readonly IRepository<User> _userRepository;
+
+    public PushMessageControllerTest(PushMessageController pushMessageController, IRepository<User> userRepository)
+    {
+        _pushMessageController = pushMessageController;
+        pushMessageController.ControllerContext = new ControllerContext
+        {
+            HttpContext = new DefaultHttpContext()
+        };
+        _userRepository = userRepository;
+    }
+
+    [Fact]
+    public async Task SendMessage_Test()
+    {
+        try
+        {
+            await _pushMessageController.SendMessage(new MessageInDto { Content = "123"});
+        }
+        catch (UserFriendlyException e)
+        {
+            e.Message.ShouldBe("电话号码和用户Id不能同时为空");
+        }
+
+        try
+        {
+            await _pushMessageController.SendMessage(new MessageInDto());
+        }
+        catch (UserFriendlyException e)
+        {
+            e.Message.ShouldBe("内容不能为空");
+        }
+
+        var phoneNo = await _userRepository.Queryable()
+            .Where(m => m.Name.Contains("测试账号"))
+            .Select(m => m.PhoneNo)
+            .FirstAsync();
+
+        var inDto = new MessageInDto 
+        {
+            TelNumbers = phoneNo,
+            Content = "单元测试, 输入的手机号码发送的短信"
+        };
+        // await _pushMessageController.SendMessage(inDto);
+
+
+        var ids = await _userRepository.Queryable()
+            .Where(m => m.Name.Contains("测试账号"))
+            .Select(m => m.Id)
+            .Take(2)
+            .ToListAsync();
+
+        inDto = new MessageInDto
+        {
+            UserIds = ids,
+            Content = "单元测试, 选择的联系人发送的短信"
+        };
+        await _pushMessageController.SendMessage(inDto);
+    }
+}
+

+ 1 - 0
src/Hotline.Application.Tests/Startup.cs

@@ -157,6 +157,7 @@ public class Startup
             services.AddScoped<ISessionContext, DefaultHttpContextAccessor>();
             services.AddScoped<IExportApplication, ExportApplication>();
             services.AddScoped<OrderController>();
+            services.AddScoped<PushMessageController>();
 
             //ServiceLocator.Instance = services.BuildServiceProvider();
         }

+ 9 - 2
src/Hotline.Share/Dtos/Push/MessageDto.cs

@@ -1,4 +1,5 @@
 using Hotline.Share.Enums.Push;
+using System.ComponentModel.DataAnnotations;
 
 namespace Hotline.Share.Dtos.Push;
 
@@ -66,10 +67,16 @@ public class MessageInDto
     /// <summary>
     /// 接收手机号码(多个逗号分隔)
     /// </summary>
-    public string TelNumbers { get; set; }
+    public string? TelNumbers { get; set; }
+
+    /// <summary>
+    /// 用户Id集合
+    /// </summary>
+    public IList<string>? UserIds { get; set; }
 
     /// <summary>
     /// 内容
     /// </summary>
-    public string? Content { get; set; }
+    [Required(ErrorMessage = "内容不能为空")]
+    public string Content { get; set; }
 }

+ 5 - 23
src/Hotline.Share/Tools/ObjectExtensions.cs

@@ -6,6 +6,7 @@ using System.Linq;
 using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
+using static Lucene.Net.Util.Fst.Util;
 
 namespace Hotline.Share.Tools;
 public static class ObjectExtensions
@@ -29,32 +30,13 @@ public static class ObjectExtensions
         }
     }
 
-    /// <summary>
-    /// 扩展方法用于手动验证对象,并返回验证结果。
-    /// </summary>
-    /// <param name="obj">需要验证的对象</param>
-    /// <returns>如果验证成功返回 string.Empty,否则返回错误信息</returns>
-    public static string ValidateObject(this object obj)
+    public static string ToJson(this object obj)
     {
-        var validationResults = new List<ValidationResult>();
-        var validationContext = new ValidationContext(obj);
-
-        // 验证对象
-        bool isValid = Validator.TryValidateObject(obj, validationContext, validationResults, true);
-
-        if (isValid)
-        {
-            return string.Empty;
-        }
-        else
-        {
-            // 如果验证失败,返回所有错误信息的拼接
-            return string.Join("; ", validationResults.Select(result => result.ErrorMessage));
-        }
+        return obj == null ? default(string) : JsonConvert.SerializeObject(obj);
     }
 
-    public static string ToJson(this object obj)
+    public static T FromJson<T>(this string json)
     {
-        return JsonConvert.SerializeObject(obj);
+        return json.IsNullOrEmpty() ? default(T) : JsonConvert.DeserializeObject<T>(json);
     }
 }

+ 1 - 1
src/Hotline/Settings/TimeLimitDomain/ZiGongExpireTimeLimit.cs

@@ -114,7 +114,7 @@ public class ZiGongExpireTimeLimit : ExpireTimeLimitBase, ICalcExpireTime, IScop
                 return false;
             return true;
         });
-        if (busCode.IsNullOrEmpty() && code.IsNullOrEmpty()) base.GetOrderTimeLimitConfigBase();
+        if (busCode.IsNullOrEmpty() && code.IsNullOrEmpty()) return GetOrderTimeLimitConfigBase();
         var inventory = await _timeLimitSettingInventoryRepository.GetByCode(code);
         if (inventory is not null) return inventory.Adapt<TimeConfig>();
         var timeConfig = await _timeLimitSettingRepository.GetByBusCode(busCode)

+ 38 - 0
src/Hotline/Tools/ObjectExtensions.cs

@@ -0,0 +1,38 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using XF.Domain.Exceptions;
+
+namespace Hotline.Tools;
+public static class ObjectExtensions
+{
+    /// <summary>
+    /// 扩展方法用于手动验证对象,并返回验证结果。
+    /// </summary>
+    /// <param name="obj">需要验证的对象</param>
+    /// <returns>如果验证成功返回 string.Empty,否则返回错误信息</returns>
+    public static string ValidateObject(this object obj, bool throwError = true)
+    {
+        var validationResults = new List<ValidationResult>();
+        var validationContext = new ValidationContext(obj);
+
+        // 验证对象
+        bool isValid = Validator.TryValidateObject(obj, validationContext, validationResults, true);
+
+        if (isValid)
+        {
+            return string.Empty;
+        }
+        else
+        {
+            var msg = string.Join("; ", validationResults.Select(result => result.ErrorMessage));
+            if (throwError)
+                throw UserFriendlyException.SameMessage(msg);
+            // 如果验证失败,返回所有错误信息的拼接
+            return msg;
+        }
+    }
+}