From d9543ca23cd3ca9b14bb6b61ccaaedfc51b54b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Sun, 1 May 2022 18:31:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BF=AE=E6=94=B9=E5=AF=86?= =?UTF-8?q?=E7=A0=81=E5=8F=8A=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Config/SwaggerDoc.xml | 28 ++++++ .../Controllers/AccountController.cs | 64 +++++++++++-- .../appsettings.json | 2 +- .../yi-sqlsugar-dev.db | Bin 102400 -> 102400 bytes .../Yi.Framework.Common/Models/Result.cs | 8 ++ .../UpdatePasswordDto.cs | 14 +++ .../Yi.Framework.Interface/IUserService.cs | 8 ++ .../Yi.Framework.Service/UserService.cs | 10 +- .../Yi.Framework.WebCore/CommonExtend.cs | 7 -- Yi.Vue2.x/src/api/accountApi.js | 15 ++- Yi.Vue2.x/src/views/userInfo.vue | 88 +++++++++++------- 11 files changed, 189 insertions(+), 55 deletions(-) create mode 100644 Yi.Framework.Net6/Yi.Framework.DTOModel/UpdatePasswordDto.cs diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml index 42b057aa..b05d0639 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml @@ -9,12 +9,40 @@ 账户管理 + + + 没啥说,登录 + + + + + + + 没啥说,注册 + + + + 通过已登录的用户获取用户信息及菜单 + + + 更新登录的用户密码 + + + + + + + 更新已登录用户的用户信息 + + + + Json To Sql 类比模式,通用模型 diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs index b71d5d22..c4938b56 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs @@ -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 /// [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 logger, IUserService iUserService, JwtInvoker jwtInvoker) { @@ -32,18 +33,28 @@ namespace Yi.Framework.ApiMicroservice.Controllers _jwtInvoker = jwtInvoker; } + /// + /// 没啥说,登录 + /// + /// + /// [AllowAnonymous] [HttpPost] public async Task 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("登录失败!用户名或者密码错误!"); } + /// + /// 没啥说,注册 + /// + /// + /// [AllowAnonymous] [HttpPost] public async Task Register(RegisterDto registerDto) @@ -65,10 +76,49 @@ namespace Yi.Framework.ApiMicroservice.Controllers [HttpGet] public async Task 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)); } + + + /// + /// 更新登录的用户密码 + /// + /// + /// + [HttpPut] + public async Task 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("原密码错误!"); + } + + /// + /// 更新已登录用户的用户信息 + /// + /// + /// + [HttpPut] + public async Task UpdateUserByHttp(UserEntity user) + { + //当然,密码是不能给他修改的 + user.Password = null; + user.Salt = null; + + //修改需要赋值上主键哦 + user.Id = HttpContext.GetCurrentUserEntityInfo(out _).Id; + return Result.Success().SetStatus(await _iUserService._repository.UpdateIgnoreNullAsync(user)); + } } } diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.json b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.json index d7e71ae1..94d17102 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.json +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.json @@ -41,7 +41,7 @@ "PolicyName": "permission", "DefaultScheme": "Bearer", "IsHttps": false, - "Expiration": 30, + "Expiration": 300, "ReExpiration": 3000 }, "RedisConnOptions": { diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db index 4979279147c88b5e0a9ead4c2f32391fa5f7980c..f9c19a9a6b340f8fbca1300cf296ba608326fc6e 100644 GIT binary patch delta 404 zcmZozz}B#UZGtpo!bBNo)&vGU_Z1sc7V@)lIWurMZ{`xP1Y=3m6+=i9vT|%W13Z( z7vk>hms#)RQflrRo}FIe9-kgooL+9~VG&szoKuzM;bdT1n4E555a^X=lAdW{Y8H}T zLusrKHa(h$&NYC=QKXs zzVYdt%};kU85$Xzn3|beSQ;1_J=?J3#r`=!^nBTFHcLyh_j?*QyGw6kWZ`#YI6V1) ztk`B@Sv@8eV4&>Z>?(iIfa4tx<|Nl{41m|Gc|TbY{Z0WCGRG&gLvj^Az_&v?`T0Ep3pWdHyG delta 143 zcmV;A0C4|+pay`T29O&8SCJe;1y=wrEKspzp$`QMM*t5&vkDM73bQZ}1Oc;08?pfe z0002PlfWGovlAUI0tA6P0S>c8AHgsP^Z*Y#53mmOvmqen4jDxQ2?iMeGeZaf02LGA x-9Z8n0OzE>=arn34PO}*G%YeYATcmHGB-LgFfKSYIWaacHG?}}w>w_}#W2b(CkFrk diff --git a/Yi.Framework.Net6/Yi.Framework.Common/Models/Result.cs b/Yi.Framework.Net6/Yi.Framework.Common/Models/Result.cs index 3e2e1cac..12184c4f 100644 --- a/Yi.Framework.Net6/Yi.Framework.Common/Models/Result.cs +++ b/Yi.Framework.Net6/Yi.Framework.Common/Models/Result.cs @@ -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; } diff --git a/Yi.Framework.Net6/Yi.Framework.DTOModel/UpdatePasswordDto.cs b/Yi.Framework.Net6/Yi.Framework.DTOModel/UpdatePasswordDto.cs new file mode 100644 index 00000000..fe7788a1 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.DTOModel/UpdatePasswordDto.cs @@ -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; } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Interface/IUserService.cs b/Yi.Framework.Net6/Yi.Framework.Interface/IUserService.cs index 1017b157..bda0620d 100644 --- a/Yi.Framework.Net6/Yi.Framework.Interface/IUserService.cs +++ b/Yi.Framework.Net6/Yi.Framework.Interface/IUserService.cs @@ -67,5 +67,13 @@ namespace Yi.Framework.Interface /// /// Task GetUserAllInfo(long userId); + + /// + /// 判断用户密码是否和原密码相同 + /// + /// + /// + /// + bool JudgePassword(UserEntity user, string password); } } diff --git a/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs b/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs index a7c9e0f1..a2275c85 100644 --- a/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs +++ b/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs @@ -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; } } } diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/CommonExtend.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/CommonExtend.cs index 6e3af2a7..25aff44f 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/CommonExtend.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/CommonExtend.cs @@ -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, diff --git a/Yi.Vue2.x/src/api/accountApi.js b/Yi.Vue2.x/src/api/accountApi.js index fdd6f285..c8553e44 100644 --- a/Yi.Vue2.x/src/api/accountApi.js +++ b/Yi.Vue2.x/src/api/accountApi.js @@ -35,11 +35,11 @@ export default { method: 'post', }) }, - changePassword(user, newPassword) { + updatePassword(oldPassword, newPassword) { return myaxios({ - url: `/Account/changePassword`, + url: `/Account/updatePassword`, method: 'put', - data: { user, newPassword } + data: { oldPassword, newPassword } }) }, getUserAllInfo() @@ -48,6 +48,15 @@ export default { url: `/Account/getUserAllInfo`, method: 'get' }) + }, + updateUserByHttp(user) + { + + return myaxios({ + url: `/Account/updateUserByHttp`, + method: 'put', + data:user + }) } } \ No newline at end of file diff --git a/Yi.Vue2.x/src/views/userInfo.vue b/Yi.Vue2.x/src/views/userInfo.vue index 3d271a5c..5dd17dc2 100644 --- a/Yi.Vue2.x/src/views/userInfo.vue +++ b/Yi.Vue2.x/src/views/userInfo.vue @@ -3,7 +3,10 @@ - +
@@ -13,13 +16,19 @@

{{ userInfo.nick }}

{{ userInfo.introduction }}

- - + + 编辑头像 绑定QQ @@ -230,7 +239,7 @@ @@ -272,17 +281,18 @@ export default { userInfo: {}, editInfo: {}, newPassword: "", + oldPassword: "", dis_newPassword: true, - roleInfo:[], + roleInfo: [], menuInfo: [], }), created() { this.init(); }, watch: { - editInfo: { + oldPassword: { handler(val, oldVal) { - if (val.password.length > 0) { + if (val != "") { this.dis_newPassword = false; } else { this.dis_newPassword = true; @@ -294,50 +304,56 @@ export default { methods: { save() { - accountApi - .changePassword(this.editInfo, this.newPassword) - .then((resp) => { - if (resp.status) { - this.$dialog.notify.error(resp.msg, { - position: "top-right", - timeout: 5000, - }); - } else { - this.$dialog.notify.success(resp.msg, { - position: "top-right", - timeout: 5000, - }); - } + if (this.newPassword != "") { + accountApi + .updatePassword(this.oldPassword, this.newPassword) + .then((resp) => { + if (resp.status) { + this.$dialog.notify.success(resp.message, { + position: "top-right", + timeout: 5000, + }); + } else { + this.$dialog.notify.error(resp.message, { + position: "top-right", + timeout: 5000, + }); + } + this.init(); + }); + } else { + accountApi.updateUserByHttp(this.editInfo).then((resp) => { this.init(); }); + } }, init() { this.newPassword = ""; + this.oldPassword = ""; accountApi.getUserAllInfo().then((resp) => { this.userInfo = resp.data.user; this.userInfo.password = ""; this.editInfo = Object.assign({}, this.userInfo); - this.roleInfo=resp.data.roles; + this.roleInfo = resp.data.roles; 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")); }, uploadImage() { const file = this.$refs.imgFile.files[0]; let formData = new FormData(); formData.append("file", file); - fileApi.EditIcon(formData).then(resp=>{ + fileApi.EditIcon(formData).then((resp) => { this.init(); - this.$dialog.notify.success(resp.msg, { - position: "top-right", - timeout: 5000, - }); - }) + this.$dialog.notify.success(resp.msg, { + position: "top-right", + timeout: 5000, + }); + }); }, - }, };