diff --git a/projects/IoTCenter/Api/NodeController.cs b/projects/IoTCenter/Api/NodeController.cs index f0621fab..3bac4639 100644 --- a/projects/IoTCenter/Api/NodeController.cs +++ b/projects/IoTCenter/Api/NodeController.cs @@ -58,10 +58,9 @@ namespace UserCenter.Controllers try { var model = this._nodeRepo.ReadOnlyTable() - .Include(o => o.Devices) - .ThenInclude(o => o.Product) - .Include(o => o.Devices) - .ThenInclude(o => o.Data) + .Include(o => o.Scenes) + .Include(o => o.Devices).ThenInclude(o => o.Product) + .Include(o => o.Devices).ThenInclude(o => o.Data) .Where(o => o.Number == number) .FirstOrDefault(); return Ok(model); diff --git a/projects/UserCenter/Api/UserController.cs b/projects/UserCenter/Api/UserController.cs index 134c440d..39275a4a 100644 --- a/projects/UserCenter/Api/UserController.cs +++ b/projects/UserCenter/Api/UserController.cs @@ -1,6 +1,8 @@ using Application.Domain.Entities; +using Application.Models; using Infrastructure.Data; using Infrastructure.Extensions; +using Infrastructure.Security; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -15,10 +17,13 @@ namespace UserCenter.Controllers public class UserController : ControllerBase { private readonly IRepository _userRepo; + private readonly IEncryptionService _encryptionService; - public UserController(IRepository userRepo) + public UserController(IRepository userRepo, + IEncryptionService encryptionService) { this._userRepo = userRepo; + this._encryptionService = encryptionService; } [HttpGet] @@ -54,8 +59,7 @@ namespace UserCenter.Controllers .FirstOrDefault(); if (model == null) { - ModelState.AddModelError("", "用户不存在"); - return BadRequest(ModelState); + return BadRequest(ModelState.AddModelError("用户不存在")); } return Ok(model); } @@ -65,5 +69,36 @@ namespace UserCenter.Controllers return Problem(ex.Message); } } + + [HttpPost] + [Authorize] + public ActionResult ChangePassword([FromBody]ChangePasswordModel model) + { + try + { + var userName = User.Identity.Name; + var user = this._userRepo.ReadOnlyTable().FirstOrDefault(o => o.UserName == userName); + if (user == null) + { + return BadRequest(ModelState.AddModelError("用户不存在")); + } + if (this._encryptionService.CreatePasswordHash(model.OldPassword, user.SecurityStamp) != user.PasswordHash) + { + return BadRequest(ModelState.AddModelError(o => model.OldPassword, "当前密码输入错误", 1)); + } + if (model.OldPassword != model.ConfirmNewPassword) + { + return BadRequest(ModelState.AddModelError(o => model.OldPassword, "新密码确认输入错误", 2)); + } + user.PasswordHash = this._encryptionService.CreatePasswordHash(model.NewPassword, user.SecurityStamp); + _userRepo.SaveChanges(); + return Ok("密码修改成功"); + } + catch (Exception ex) + { + ex.PrintStack(); + return Problem(ex.Message); + } + } } } \ No newline at end of file diff --git a/projects/WebApp/wwwroot/js/message.js b/projects/WebApp/wwwroot/js/message.js index bfcf964d..57bbdf4a 100644 --- a/projects/WebApp/wwwroot/js/message.js +++ b/projects/WebApp/wwwroot/js/message.js @@ -15,7 +15,7 @@ connection.on('Connected', function (id) { console.log('signalR 连接Id:' + connectionId); }); connection.on("ServerToClient", function (method, json, to, from) { - console.log(method); + console.log(method+' from:'+from+' to:'+to); console.log(json); var model = JSON.parse(json); if (method === 'NodeEntityUpdated') {