添加修改密码及用户信息
This commit is contained in:
@@ -9,12 +9,40 @@
|
||||
账户管理
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.AccountController.Login(Yi.Framework.DTOModel.LoginDto)">
|
||||
<summary>
|
||||
没啥说,登录
|
||||
</summary>
|
||||
<param name="loginDto"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.AccountController.Register(Yi.Framework.DTOModel.RegisterDto)">
|
||||
<summary>
|
||||
没啥说,注册
|
||||
</summary>
|
||||
<param name="registerDto"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.AccountController.GetUserAllInfo">
|
||||
<summary>
|
||||
通过已登录的用户获取用户信息及菜单
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.AccountController.UpdatePassword(Yi.Framework.DTOModel.UpdatePasswordDto)">
|
||||
<summary>
|
||||
更新登录的用户密码
|
||||
</summary>
|
||||
<param name="updatePasswordDto"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.AccountController.UpdateUserByHttp(Yi.Framework.Model.Models.UserEntity)">
|
||||
<summary>
|
||||
更新已登录用户的用户信息
|
||||
</summary>
|
||||
<param name="user"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="T:Yi.Framework.ApiMicroservice.Controllers.BaseCrudController`1">
|
||||
<summary>
|
||||
Json To Sql 类比模式,通用模型
|
||||
|
||||
@@ -5,6 +5,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Helper;
|
||||
using Yi.Framework.Common.Models;
|
||||
using Yi.Framework.Core;
|
||||
using Yi.Framework.DTOModel;
|
||||
@@ -22,9 +23,9 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]/[action]")]
|
||||
public class AccountController :ControllerBase
|
||||
public class AccountController : ControllerBase
|
||||
{
|
||||
private IUserService _iUserService;
|
||||
private IUserService _iUserService;
|
||||
private JwtInvoker _jwtInvoker;
|
||||
public AccountController(ILogger<UserEntity> logger, IUserService iUserService, JwtInvoker jwtInvoker)
|
||||
{
|
||||
@@ -32,18 +33,28 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
_jwtInvoker = jwtInvoker;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 没啥说,登录
|
||||
/// </summary>
|
||||
/// <param name="loginDto"></param>
|
||||
/// <returns></returns>
|
||||
[AllowAnonymous]
|
||||
[HttpPost]
|
||||
public async Task<Result> Login(LoginDto loginDto)
|
||||
{
|
||||
UserEntity user=new();
|
||||
if (await _iUserService.Login(loginDto.UserName, loginDto.Password,o=> user=o))
|
||||
UserEntity user = new();
|
||||
if (await _iUserService.Login(loginDto.UserName, loginDto.Password, o => user = o))
|
||||
{
|
||||
return Result.Success("登录成功!").SetData(new { user, token = _jwtInvoker.GetAccessToken(user)});
|
||||
return Result.Success("登录成功!").SetData(new { user, token = _jwtInvoker.GetAccessToken(user) });
|
||||
}
|
||||
return Result.SuccessError("登录失败!用户名或者密码错误!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 没啥说,注册
|
||||
/// </summary>
|
||||
/// <param name="registerDto"></param>
|
||||
/// <returns></returns>
|
||||
[AllowAnonymous]
|
||||
[HttpPost]
|
||||
public async Task<Result> Register(RegisterDto registerDto)
|
||||
@@ -65,10 +76,49 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
[HttpGet]
|
||||
public async Task<Result> GetUserAllInfo()
|
||||
{
|
||||
//通过鉴权jwt获取到用户的id
|
||||
var userId=HttpContext.GetCurrentUserEntityInfo(out _).Id;
|
||||
//通过鉴权jwt获取到用户的id
|
||||
var userId = HttpContext.GetCurrentUserEntityInfo(out _).Id;
|
||||
|
||||
return Result.Success().SetData(await _iUserService.GetUserAllInfo(userId));
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 更新登录的用户密码
|
||||
/// </summary>
|
||||
/// <param name="updatePasswordDto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut]
|
||||
public async Task<Result> UpdatePassword(UpdatePasswordDto updatePasswordDto)
|
||||
{
|
||||
var userId = HttpContext.GetCurrentUserEntityInfo(out _).Id;
|
||||
var userEntiy = await _iUserService._repository.GetByIdAsync(userId);
|
||||
|
||||
//判断输入的老密码是否和原密码相同
|
||||
if (_iUserService.JudgePassword(userEntiy, updatePasswordDto.OldPassword))
|
||||
{
|
||||
userEntiy.Password = updatePasswordDto.NewPassword;
|
||||
userEntiy.BuildPassword();
|
||||
return Result.Success().SetStatus(await _iUserService._repository.UpdateAsync(userEntiy));
|
||||
}
|
||||
return Result.SuccessError("原密码错误!");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新已登录用户的用户信息
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut]
|
||||
public async Task<Result> UpdateUserByHttp(UserEntity user)
|
||||
{
|
||||
//当然,密码是不能给他修改的
|
||||
user.Password = null;
|
||||
user.Salt = null;
|
||||
|
||||
//修改需要赋值上主键哦
|
||||
user.Id = HttpContext.GetCurrentUserEntityInfo(out _).Id;
|
||||
return Result.Success().SetStatus(await _iUserService._repository.UpdateIgnoreNullAsync(user));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
"PolicyName": "permission",
|
||||
"DefaultScheme": "Bearer",
|
||||
"IsHttps": false,
|
||||
"Expiration": 30,
|
||||
"Expiration": 300,
|
||||
"ReExpiration": 3000
|
||||
},
|
||||
"RedisConnOptions": {
|
||||
|
||||
Binary file not shown.
@@ -36,6 +36,14 @@ namespace Yi.Framework.Common.Models
|
||||
}
|
||||
public Result SetStatus(bool _status)
|
||||
{
|
||||
if (_status)
|
||||
{
|
||||
this.message = "操作成功";
|
||||
}
|
||||
else
|
||||
{
|
||||
this.message = "操作失败";
|
||||
}
|
||||
this.status = _status;
|
||||
return this;
|
||||
}
|
||||
|
||||
14
Yi.Framework.Net6/Yi.Framework.DTOModel/UpdatePasswordDto.cs
Normal file
14
Yi.Framework.Net6/Yi.Framework.DTOModel/UpdatePasswordDto.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.DTOModel
|
||||
{
|
||||
public class UpdatePasswordDto
|
||||
{
|
||||
public string NewPassword { get; set; }
|
||||
public string OldPassword { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -67,5 +67,13 @@ namespace Yi.Framework.Interface
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
Task<UserRoleMenuDto> GetUserAllInfo(long userId);
|
||||
|
||||
/// <summary>
|
||||
/// 判断用户密码是否和原密码相同
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="password"></param>
|
||||
/// <returns></returns>
|
||||
bool JudgePassword(UserEntity user, string password);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Common.Helper;
|
||||
using Yi.Framework.DTOModel;
|
||||
using Yi.Framework.Interface;
|
||||
using Yi.Framework.Model.Models;
|
||||
@@ -132,8 +133,15 @@ namespace Yi.Framework.Service
|
||||
userRoleMenu.User = user;
|
||||
|
||||
return userRoleMenu;
|
||||
}
|
||||
|
||||
|
||||
public bool JudgePassword(UserEntity user,string password)
|
||||
{
|
||||
if (user.Password == MD5Helper.SHA2Encode(password, user.Salt))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,21 +37,14 @@ namespace Yi.Framework.WebCore
|
||||
long resId = 0;
|
||||
try
|
||||
{
|
||||
|
||||
claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims;
|
||||
resId = Convert.ToInt64(claimlist.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Sid).Value);
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw new Exception("未授权,Token鉴权失败!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
menuIds = claimlist.Where(u => u.Type == "menuIds").ToList().Select(u => new Guid(u.Value)).ToList();
|
||||
|
||||
|
||||
return new UserEntity()
|
||||
{
|
||||
Id = resId,
|
||||
|
||||
Reference in New Issue
Block a user