diff --git a/Infrastructure/StringConverter.cs b/Infrastructure/StringConverter.cs new file mode 100644 index 0000000..c057b2f --- /dev/null +++ b/Infrastructure/StringConverter.cs @@ -0,0 +1,48 @@ +using System; +using System.Buffers; +using System.Linq; +using System.Text; +using System.Text.Json; + +namespace Infrastructure +{ + /// + /// Json任何类型读取到字符串属性 + /// 因为 System.Text.Json 必须严格遵守类型一致,当非字符串读取到字符属性时报错: + /// The JSON value could not be converted to System.String. + /// + public class StringConverter : System.Text.Json.Serialization.JsonConverter + { + public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType == JsonTokenType.String) + { + return reader.GetString(); + } + else + { + //非字符类型,返回原生内容 + return GetRawPropertyValue(reader); + } + + throw new JsonException(); + } + + public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) + { + writer.WriteStringValue(value); + } + /// + /// 非字符类型,返回原生内容 + /// + /// + /// + private static string GetRawPropertyValue(Utf8JsonReader jsonReader) + { + ReadOnlySpan utf8Bytes = jsonReader.HasValueSequence ? + jsonReader.ValueSequence.ToArray() : + jsonReader.ValueSpan; + return Encoding.UTF8.GetString(utf8Bytes); + } + } +} diff --git a/ZR.Admin.WebApi/Controllers/System/SysUserController.cs b/ZR.Admin.WebApi/Controllers/System/SysUserController.cs index 9b9ce33..2041414 100644 --- a/ZR.Admin.WebApi/Controllers/System/SysUserController.cs +++ b/ZR.Admin.WebApi/Controllers/System/SysUserController.cs @@ -1,7 +1,6 @@ using Microsoft.AspNetCore.Mvc; using MiniExcelLibs; using SqlSugar; -using ZR.Admin.WebApi.Extensions; using ZR.Admin.WebApi.Filters; using ZR.Model; using ZR.Model.System; @@ -146,7 +145,7 @@ namespace ZR.Admin.WebApi.Controllers.System public IActionResult Remove(int userid = 0) { if (userid <= 0) { return ToResponse(ApiResult.Error(101, "请求参数错误")); } - if (userid == 1) return ToResponse(Infrastructure.ResultCode.FAIL, "不能删除管理员账号"); + if (userid == 1) return ToResponse(ResultCode.FAIL, "不能删除管理员账号"); int result = UserService.DeleteUser(userid); return ToResponse(result); @@ -159,7 +158,7 @@ namespace ZR.Admin.WebApi.Controllers.System [HttpPut("resetPwd")] [Log(Title = "重置密码", BusinessType = BusinessType.UPDATE)] [ActionPermissionFilter(Permission = "system:user:resetPwd")] - public IActionResult ResetPwd([FromBody] SysUser sysUser) + public IActionResult ResetPwd([FromBody] SysUserDto sysUser) { //密码md5 sysUser.Password = NETCore.Encrypt.EncryptProvider.Md5(sysUser.Password); diff --git a/ZR.Admin.WebApi/Program.cs b/ZR.Admin.WebApi/Program.cs index 662ce75..985fd4f 100644 --- a/ZR.Admin.WebApi/Program.cs +++ b/ZR.Admin.WebApi/Program.cs @@ -83,6 +83,7 @@ builder.Services.AddMvc(options => options.JsonSerializerOptions.WriteIndented = true; options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter()); options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullConverter()); + options.JsonSerializerOptions.Converters.Add(new Infrastructure.StringConverter()); }); builder.Services.AddSwaggerConfig(); diff --git a/ZR.Model/System/Dto/SysUserDto.cs b/ZR.Model/System/Dto/SysUserDto.cs index 2dc0b95..9d07520 100644 --- a/ZR.Model/System/Dto/SysUserDto.cs +++ b/ZR.Model/System/Dto/SysUserDto.cs @@ -14,6 +14,7 @@ namespace ZR.Model.System.Dto /// 用户性别(0男 1女 2未知) /// public int Sex { get; set; } + public string Password { get; set; } } public class SysUserQueryDto