添加修改密码及用户信息

This commit is contained in:
橙子
2022-05-01 18:31:06 +08:00
parent 3871eb3c84
commit d9543ca23c
11 changed files with 189 additions and 55 deletions

View File

@@ -9,12 +9,40 @@
账户管理 账户管理
</summary> </summary>
</member> </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"> <member name="M:Yi.Framework.ApiMicroservice.Controllers.AccountController.GetUserAllInfo">
<summary> <summary>
通过已登录的用户获取用户信息及菜单 通过已登录的用户获取用户信息及菜单
</summary> </summary>
<returns></returns> <returns></returns>
</member> </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"> <member name="T:Yi.Framework.ApiMicroservice.Controllers.BaseCrudController`1">
<summary> <summary>
Json To Sql 类比模式,通用模型 Json To Sql 类比模式,通用模型

View File

@@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yi.Framework.Common.Helper;
using Yi.Framework.Common.Models; using Yi.Framework.Common.Models;
using Yi.Framework.Core; using Yi.Framework.Core;
using Yi.Framework.DTOModel; using Yi.Framework.DTOModel;
@@ -22,9 +23,9 @@ namespace Yi.Framework.ApiMicroservice.Controllers
/// </summary> /// </summary>
[ApiController] [ApiController]
[Route("api/[controller]/[action]")] [Route("api/[controller]/[action]")]
public class AccountController :ControllerBase public class AccountController : ControllerBase
{ {
private IUserService _iUserService; private IUserService _iUserService;
private JwtInvoker _jwtInvoker; private JwtInvoker _jwtInvoker;
public AccountController(ILogger<UserEntity> logger, IUserService iUserService, JwtInvoker jwtInvoker) public AccountController(ILogger<UserEntity> logger, IUserService iUserService, JwtInvoker jwtInvoker)
{ {
@@ -32,18 +33,28 @@ namespace Yi.Framework.ApiMicroservice.Controllers
_jwtInvoker = jwtInvoker; _jwtInvoker = jwtInvoker;
} }
/// <summary>
/// 没啥说,登录
/// </summary>
/// <param name="loginDto"></param>
/// <returns></returns>
[AllowAnonymous] [AllowAnonymous]
[HttpPost] [HttpPost]
public async Task<Result> Login(LoginDto loginDto) public async Task<Result> Login(LoginDto loginDto)
{ {
UserEntity user=new(); UserEntity user = new();
if (await _iUserService.Login(loginDto.UserName, loginDto.Password,o=> user=o)) 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("登录失败!用户名或者密码错误!"); return Result.SuccessError("登录失败!用户名或者密码错误!");
} }
/// <summary>
/// 没啥说,注册
/// </summary>
/// <param name="registerDto"></param>
/// <returns></returns>
[AllowAnonymous] [AllowAnonymous]
[HttpPost] [HttpPost]
public async Task<Result> Register(RegisterDto registerDto) public async Task<Result> Register(RegisterDto registerDto)
@@ -65,10 +76,49 @@ namespace Yi.Framework.ApiMicroservice.Controllers
[HttpGet] [HttpGet]
public async Task<Result> GetUserAllInfo() public async Task<Result> GetUserAllInfo()
{ {
//通过鉴权jwt获取到用户的id //通过鉴权jwt获取到用户的id
var userId=HttpContext.GetCurrentUserEntityInfo(out _).Id; var userId = HttpContext.GetCurrentUserEntityInfo(out _).Id;
return Result.Success().SetData(await _iUserService.GetUserAllInfo(userId)); 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));
}
} }
} }

View File

@@ -41,7 +41,7 @@
"PolicyName": "permission", "PolicyName": "permission",
"DefaultScheme": "Bearer", "DefaultScheme": "Bearer",
"IsHttps": false, "IsHttps": false,
"Expiration": 30, "Expiration": 300,
"ReExpiration": 3000 "ReExpiration": 3000
}, },
"RedisConnOptions": { "RedisConnOptions": {

View File

@@ -36,6 +36,14 @@ namespace Yi.Framework.Common.Models
} }
public Result SetStatus(bool _status) public Result SetStatus(bool _status)
{ {
if (_status)
{
this.message = "操作成功";
}
else
{
this.message = "操作失败";
}
this.status = _status; this.status = _status;
return this; return this;
} }

View 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; }
}
}

View File

@@ -67,5 +67,13 @@ namespace Yi.Framework.Interface
/// <param name="userId"></param> /// <param name="userId"></param>
/// <returns></returns> /// <returns></returns>
Task<UserRoleMenuDto> GetUserAllInfo(long userId); Task<UserRoleMenuDto> GetUserAllInfo(long userId);
/// <summary>
/// 判断用户密码是否和原密码相同
/// </summary>
/// <param name="user"></param>
/// <param name="password"></param>
/// <returns></returns>
bool JudgePassword(UserEntity user, string password);
} }
} }

View File

@@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yi.Framework.Common.Helper;
using Yi.Framework.DTOModel; using Yi.Framework.DTOModel;
using Yi.Framework.Interface; using Yi.Framework.Interface;
using Yi.Framework.Model.Models; using Yi.Framework.Model.Models;
@@ -132,8 +133,15 @@ namespace Yi.Framework.Service
userRoleMenu.User = user; userRoleMenu.User = user;
return userRoleMenu; return userRoleMenu;
}
public bool JudgePassword(UserEntity user,string password)
{
if (user.Password == MD5Helper.SHA2Encode(password, user.Salt))
{
return true;
}
return false;
} }
} }
} }

View File

@@ -37,21 +37,14 @@ namespace Yi.Framework.WebCore
long resId = 0; long resId = 0;
try try
{ {
claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims; claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims;
resId = Convert.ToInt64(claimlist.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Sid).Value); resId = Convert.ToInt64(claimlist.FirstOrDefault(u => u.Type == JwtRegisteredClaimNames.Sid).Value);
} }
catch catch
{ {
throw new Exception("未授权Token鉴权失败"); throw new Exception("未授权Token鉴权失败");
} }
menuIds = claimlist.Where(u => u.Type == "menuIds").ToList().Select(u => new Guid(u.Value)).ToList(); menuIds = claimlist.Where(u => u.Type == "menuIds").ToList().Select(u => new Guid(u.Value)).ToList();
return new UserEntity() return new UserEntity()
{ {
Id = resId, Id = resId,

View File

@@ -35,11 +35,11 @@ export default {
method: 'post', method: 'post',
}) })
}, },
changePassword(user, newPassword) { updatePassword(oldPassword, newPassword) {
return myaxios({ return myaxios({
url: `/Account/changePassword`, url: `/Account/updatePassword`,
method: 'put', method: 'put',
data: { user, newPassword } data: { oldPassword, newPassword }
}) })
}, },
getUserAllInfo() getUserAllInfo()
@@ -48,6 +48,15 @@ export default {
url: `/Account/getUserAllInfo`, url: `/Account/getUserAllInfo`,
method: 'get' method: 'get'
}) })
},
updateUserByHttp(user)
{
return myaxios({
url: `/Account/updateUserByHttp`,
method: 'put',
data:user
})
} }
} }

View File

@@ -3,7 +3,10 @@
<v-row justify="center"> <v-row justify="center">
<v-col cols="12" md="4"> <v-col cols="12" md="4">
<app-card class="mt-4 text-center"> <app-card class="mt-4 text-center">
<ccAvatar :size="128" class="rounded-circle elevation-6 mt-n12 d-inline-block"></ccAvatar> <ccAvatar
:size="128"
class="rounded-circle elevation-6 mt-n12 d-inline-block"
></ccAvatar>
<v-card-text class="text-center"> <v-card-text class="text-center">
<h6 class="text-h6 mb-2 text--secondary"> <h6 class="text-h6 mb-2 text--secondary">
@@ -13,13 +16,19 @@
<h4 class="text-h4 mb-3 text--primary">{{ userInfo.nick }}</h4> <h4 class="text-h4 mb-3 text--primary">{{ userInfo.nick }}</h4>
<p class="text--secondary">{{ userInfo.introduction }}</p> <p class="text--secondary">{{ userInfo.introduction }}</p>
<input <input
type="file" type="file"
ref="imgFile" ref="imgFile"
@change="uploadImage()" @change="uploadImage()"
class="d-none" class="d-none"
/> />
<v-btn class="mr-4" @click="choiceImg" color="primary" min-width="100" rounded> <v-btn
class="mr-4"
@click="choiceImg"
color="primary"
min-width="100"
rounded
>
编辑头像 编辑头像
</v-btn> </v-btn>
<v-btn color="primary" min-width="100" rounded> 绑定QQ </v-btn> <v-btn color="primary" min-width="100" rounded> 绑定QQ </v-btn>
@@ -230,7 +239,7 @@
<v-text-field <v-text-field
style="width: 80%" style="width: 80%"
label="原密码" label="原密码"
v-model="editInfo.password" v-model="oldPassword"
outlined outlined
clearable clearable
></v-text-field> ></v-text-field>
@@ -272,17 +281,18 @@ export default {
userInfo: {}, userInfo: {},
editInfo: {}, editInfo: {},
newPassword: "", newPassword: "",
oldPassword: "",
dis_newPassword: true, dis_newPassword: true,
roleInfo:[], roleInfo: [],
menuInfo: [], menuInfo: [],
}), }),
created() { created() {
this.init(); this.init();
}, },
watch: { watch: {
editInfo: { oldPassword: {
handler(val, oldVal) { handler(val, oldVal) {
if (val.password.length > 0) { if (val != "") {
this.dis_newPassword = false; this.dis_newPassword = false;
} else { } else {
this.dis_newPassword = true; this.dis_newPassword = true;
@@ -294,50 +304,56 @@ export default {
methods: { methods: {
save() { save() {
accountApi if (this.newPassword != "") {
.changePassword(this.editInfo, this.newPassword) accountApi
.then((resp) => { .updatePassword(this.oldPassword, this.newPassword)
if (resp.status) { .then((resp) => {
this.$dialog.notify.error(resp.msg, { if (resp.status) {
position: "top-right", this.$dialog.notify.success(resp.message, {
timeout: 5000, position: "top-right",
}); timeout: 5000,
} else { });
this.$dialog.notify.success(resp.msg, { } else {
position: "top-right", this.$dialog.notify.error(resp.message, {
timeout: 5000, position: "top-right",
}); timeout: 5000,
} });
}
this.init();
});
} else {
accountApi.updateUserByHttp(this.editInfo).then((resp) => {
this.init(); this.init();
}); });
}
}, },
init() { init() {
this.newPassword = ""; this.newPassword = "";
this.oldPassword = "";
accountApi.getUserAllInfo().then((resp) => { accountApi.getUserAllInfo().then((resp) => {
this.userInfo = resp.data.user; this.userInfo = resp.data.user;
this.userInfo.password = ""; this.userInfo.password = "";
this.editInfo = Object.assign({}, this.userInfo); this.editInfo = Object.assign({}, this.userInfo);
this.roleInfo=resp.data.roles; this.roleInfo = resp.data.roles;
this.menuInfo = resp.data.menus; this.menuInfo = resp.data.menus;
this.$store.commit('SET_USER',this.userInfo) this.$store.commit("SET_USER", this.userInfo);
}); });
}, },
choiceImg() { choiceImg() {
this.$refs.imgFile.dispatchEvent(new MouseEvent("click")); this.$refs.imgFile.dispatchEvent(new MouseEvent("click"));
}, },
uploadImage() { uploadImage() {
const file = this.$refs.imgFile.files[0]; const file = this.$refs.imgFile.files[0];
let formData = new FormData(); let formData = new FormData();
formData.append("file", file); formData.append("file", file);
fileApi.EditIcon(formData).then(resp=>{ fileApi.EditIcon(formData).then((resp) => {
this.init(); this.init();
this.$dialog.notify.success(resp.msg, { this.$dialog.notify.success(resp.msg, {
position: "top-right", position: "top-right",
timeout: 5000, timeout: 5000,
}); });
}) });
}, },
}, },
}; };
</script> </script>