优化注册服务

This commit is contained in:
不做码农 2023-03-18 07:58:39 +08:00
parent 1993299799
commit 5dc7545c12
8 changed files with 33 additions and 56 deletions

View File

@ -10,10 +10,9 @@ using SqlSugar;
using ZR.Admin.WebApi.Extensions; using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters; using ZR.Admin.WebApi.Filters;
using ZR.Common; using ZR.Common;
using ZR.Model.Dto;
using ZR.Model.System; using ZR.Model.System;
using ZR.Model.System.Dto; using ZR.Model.System.Dto;
using ZR.Service.System; using ZR.Service.System.IService;
namespace ZR.Admin.WebApi.Controllers namespace ZR.Admin.WebApi.Controllers
{ {

View File

@ -93,9 +93,8 @@ namespace ZR.Admin.WebApi.Controllers.System
[Log(Title = "修改密码", BusinessType = BusinessType.UPDATE)] [Log(Title = "修改密码", BusinessType = BusinessType.UPDATE)]
public IActionResult UpdatePwd(string oldPassword, string newPassword) public IActionResult UpdatePwd(string oldPassword, string newPassword)
{ {
LoginUser loginUser = Framework.JwtUtil.GetLoginUser(HttpContext); long userId = HttpContext.GetUId();
SysUser user = UserService.SelectUserById(userId);
SysUser user = UserService.SelectUserById(loginUser.UserId);
string oldMd5 = NETCore.Encrypt.EncryptProvider.Md5(oldPassword); string oldMd5 = NETCore.Encrypt.EncryptProvider.Md5(oldPassword);
string newMd5 = NETCore.Encrypt.EncryptProvider.Md5(newPassword); string newMd5 = NETCore.Encrypt.EncryptProvider.Md5(newPassword);
@ -107,7 +106,7 @@ namespace ZR.Admin.WebApi.Controllers.System
{ {
return ToResponse(ApiResult.Error("新密码不能和旧密码相同")); return ToResponse(ApiResult.Error("新密码不能和旧密码相同"));
} }
if (UserService.ResetPwd(loginUser.UserId, newMd5.ToLower()) > 0) if (UserService.ResetPwd(userId, newMd5) > 0)
{ {
//TODO 更新缓存 //TODO 更新缓存
@ -127,12 +126,12 @@ namespace ZR.Admin.WebApi.Controllers.System
[Log(Title = "修改头像", BusinessType = BusinessType.UPDATE, IsSaveRequestData = false)] [Log(Title = "修改头像", BusinessType = BusinessType.UPDATE, IsSaveRequestData = false)]
public async Task<IActionResult> Avatar([FromForm(Name = "picture")] IFormFile formFile) public async Task<IActionResult> Avatar([FromForm(Name = "picture")] IFormFile formFile)
{ {
LoginUser loginUser = Framework.JwtUtil.GetLoginUser(HttpContext); long userId = HttpContext.GetUId();
if (formFile == null) throw new CustomException("请选择文件"); if (formFile == null) throw new CustomException("请选择文件");
SysFile file = await FileService.SaveFileToLocal(hostEnvironment.WebRootPath, "", "avatar", HttpContext.GetName(), formFile); SysFile file = await FileService.SaveFileToLocal(hostEnvironment.WebRootPath, "", "avatar", HttpContext.GetName(), formFile);
UserService.UpdatePhoto(new SysUser() { Avatar = file.AccessUrl, UserId = loginUser.UserId }); UserService.UpdatePhoto(new SysUser() { Avatar = file.AccessUrl, UserId = userId });
return SUCCESS(new { imgUrl = file.AccessUrl }); return SUCCESS(new { imgUrl = file.AccessUrl });
} }
} }

View File

@ -1,11 +1,13 @@
using Infrastructure.Attribute; using Infrastructure.Attribute;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
namespace Infrastructure.Extensions namespace ZR.Admin.WebApi.Extensions
{ {
/// <summary>
/// App服务注册
/// </summary>
public static class AppServiceExtensions public static class AppServiceExtensions
{ {
/// <summary> /// <summary>
@ -14,18 +16,16 @@ namespace Infrastructure.Extensions
/// <param name="services"></param> /// <param name="services"></param>
public static void AddAppService(this IServiceCollection services) public static void AddAppService(this IServiceCollection services)
{ {
//var assemblies = AppDomain.CurrentDomain.GetAssemblies();
string[] cls = new string[] { "ZR.Repository", "ZR.Service", "ZR.Tasks" }; string[] cls = new string[] { "ZR.Repository", "ZR.Service", "ZR.Tasks" };
foreach (var item in cls) foreach (var item in cls)
{ {
Assembly assembly = Assembly.Load(item); Register(services, item);
Register(services, assembly);
} }
} }
private static void Register(IServiceCollection services, Assembly assembly) private static void Register(IServiceCollection services, string item)
{ {
Assembly assembly = Assembly.Load(item);
foreach (var type in assembly.GetTypes()) foreach (var type in assembly.GetTypes())
{ {
var serviceAttribute = type.GetCustomAttribute<AppServiceAttribute>(); var serviceAttribute = type.GetCustomAttribute<AppServiceAttribute>();
@ -59,11 +59,7 @@ namespace Infrastructure.Extensions
services.AddTransient(serviceType, type); services.AddTransient(serviceType, type);
break; break;
} }
//Console.WriteLine($"注册:{serviceType}"); //System.Console.WriteLine($"注册:{serviceType}");
}
else
{
//Console.WriteLine($"注册:{serviceType}");
} }
} }
} }

View File

@ -11,18 +11,9 @@ namespace ZR.Admin.WebApi.Extensions
var types = source?.GetType(); var types = source?.GetType();
if (types == null) return source; if (types == null) return source;
if (types.GetProperty("CreateTime") != null)
{
types.GetProperty("CreateTime")?.SetValue(source, DateTime.Now, null); types.GetProperty("CreateTime")?.SetValue(source, DateTime.Now, null);
}
if (types.GetProperty("AddTime") != null)
{
types.GetProperty("AddTime")?.SetValue(source, DateTime.Now, null); types.GetProperty("AddTime")?.SetValue(source, DateTime.Now, null);
}
if (types.GetProperty("UpdateTime") != null)
{
types.GetProperty("UpdateTime")?.SetValue(source, DateTime.Now, null); types.GetProperty("UpdateTime")?.SetValue(source, DateTime.Now, null);
}
if (types.GetProperty("Create_by") != null && context != null) if (types.GetProperty("Create_by") != null && context != null)
{ {
types.GetProperty("Create_by")?.SetValue(source, context.GetName(), null); types.GetProperty("Create_by")?.SetValue(source, context.GetName(), null);
@ -47,22 +38,12 @@ namespace ZR.Admin.WebApi.Extensions
var types = source?.GetType(); var types = source?.GetType();
if (types == null) return source; if (types == null) return source;
if (types.GetProperty("UpdateTime") != null)
{
types.GetProperty("UpdateTime")?.SetValue(source, DateTime.Now, null); types.GetProperty("UpdateTime")?.SetValue(source, DateTime.Now, null);
}
if (types.GetProperty("Update_time") != null)
{
types.GetProperty("Update_time")?.SetValue(source, DateTime.Now, null); types.GetProperty("Update_time")?.SetValue(source, DateTime.Now, null);
}
if (types.GetProperty("UpdateBy") != null) types.GetProperty("UpdateBy")?.SetValue(source,context.GetName(), null);
{ types.GetProperty("Update_by")?.SetValue(source, context.GetName(), null);
types.GetProperty("UpdateBy")?.SetValue(source, context?.GetName(), null);
}
if (types.GetProperty("Update_by") != null)
{
types.GetProperty("Update_by")?.SetValue(source, context?.GetName(), null);
}
return source; return source;
} }

View File

@ -1,7 +1,5 @@
using Infrastructure; using Infrastructure;
using Infrastructure.Extensions; using Infrastructure.Extensions;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -10,7 +8,6 @@ using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks;
using UAParser; using UAParser;
using ZR.Model.System; using ZR.Model.System;
@ -73,7 +70,10 @@ namespace ZR.Admin.WebApi.Extensions
public static long GetUId(this HttpContext context) public static long GetUId(this HttpContext context)
{ {
var uid = context.User.FindFirstValue(ClaimTypes.PrimarySid); var uid = context.User.FindFirstValue(ClaimTypes.PrimarySid);
if (uid.IsEmpty() || uid.IsNullOrZero())
{
throw new CustomException(ResultCode.DENY, "未登录");
}
return !string.IsNullOrEmpty(uid) ? long.Parse(uid) : 0; return !string.IsNullOrEmpty(uid) ? long.Parse(uid) : 0;
} }

View File

@ -2,6 +2,7 @@
using Infrastructure.Constant; using Infrastructure.Constant;
using Infrastructure.Model; using Infrastructure.Model;
using IPTools.Core; using IPTools.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using ZR.Admin.WebApi.Extensions; using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Framework; using ZR.Admin.WebApi.Framework;
@ -42,12 +43,12 @@ namespace ZR.Admin.WebApi.Hubs
var ip = HttpContextExtension.GetClientUserIp(App.HttpContext); var ip = HttpContextExtension.GetClientUserIp(App.HttpContext);
var ip_info = IpTool.Search(ip); var ip_info = IpTool.Search(ip);
LoginUser loginUser = JwtUtil.GetLoginUser(App.HttpContext); var userid = HttpContextExtension.GetUId(App.HttpContext);
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId); var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
//判断用户是否存在,否则添加集合 //判断用户是否存在,否则添加集合
if (!user && Context.User.Identity.IsAuthenticated) if (!user && Context.User.Identity.IsAuthenticated)
{ {
OnlineUsers users = new(Context.ConnectionId, name, loginUser?.UserId, ip) OnlineUsers users = new(Context.ConnectionId, name, userid, ip)
{ {
Location = ip_info.City Location = ip_info.City
}; };

View File

@ -1,7 +1,7 @@
using System; using System;
using ZR.Model.System; using ZR.Model.System;
namespace ZR.Service.System namespace ZR.Service.System.IService
{ {
/// <summary> /// <summary>
/// 参数配置service接口 /// 参数配置service接口

View File

@ -1,5 +1,6 @@
using Infrastructure.Attribute; using Infrastructure.Attribute;
using ZR.Model.System; using ZR.Model.System;
using ZR.Service.System.IService;
namespace ZR.Service.System namespace ZR.Service.System
{ {