123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- using Microsoft.AspNetCore.Mvc;
- using ZR.Model.System;
- using ZR.Model.System.Dto;
- namespace ZR.Admin.WebApi.Controllers.System
- {
- /// <summary>
- /// 个人中心
- /// </summary>
- [Route("system/user/profile")]
- [ApiExplorerSettings(GroupName = "sys")]
- public class SysProfileController : BaseController
- {
- private readonly ISysUserService UserService;
- private readonly ISysRoleService RoleService;
- private readonly ISysUserPostService UserPostService;
- private readonly ISysDeptService DeptService;
- private readonly ISysFileService FileService;
- private IWebHostEnvironment hostEnvironment;
- public SysProfileController(
- ISysUserService userService,
- ISysRoleService roleService,
- ISysUserPostService postService,
- ISysDeptService deptService,
- ISysFileService sysFileService,
- IWebHostEnvironment hostEnvironment)
- {
- UserService = userService;
- RoleService = roleService;
- UserPostService = postService;
- DeptService = deptService;
- FileService = sysFileService;
- this.hostEnvironment = hostEnvironment;
- }
- /// <summary>
- /// 个人中心用户信息获取
- /// </summary>
- /// <returns></returns>
- [HttpGet]
- public IActionResult Profile()
- {
- long userId = HttpContext.GetUId();
- var user = UserService.SelectUserById(userId);
- var roles = RoleService.SelectUserRoleNames(userId);
- var postGroup = UserPostService.GetPostsStrByUserId(userId);
- var deptInfo = DeptService.GetFirst(f => f.DeptId == user.DeptId);
- user.DeptName = deptInfo?.DeptName ?? "-";
- return SUCCESS(new { user, roles, postGroup }, TIME_FORMAT_FULL);
- }
- /// <summary>
- /// 修改用户
- /// </summary>
- /// <returns></returns>
- [HttpPut]
- [ActionPermissionFilter(Permission = "common")]
- [Log(Title = "修改信息", BusinessType = BusinessType.UPDATE)]
- public IActionResult UpdateProfile([FromBody] SysUserDto userDto)
- {
- if (userDto == null)
- {
- throw new CustomException(ResultCode.PARAM_ERROR, "请求参数错误");
- }
- var user = userDto.Adapt<SysUser>().ToUpdate(HttpContext);
- int result = UserService.ChangeUser(user);
- return ToResponse(result);
- }
- /// <summary>
- /// 修改密码
- /// </summary>
- /// <returns></returns>
- [HttpPut("updatePwd")]
- [ActionPermissionFilter(Permission = "common")]
- [Log(Title = "修改密码", BusinessType = BusinessType.UPDATE, IsSaveRequestData = false)]
- public IActionResult UpdatePwd(string oldPassword, string newPassword)
- {
- long userId = HttpContext.GetUId();
- SysUser user = UserService.GetFirst(f => f.UserId == userId);
- string oldMd5 = NETCore.Encrypt.EncryptProvider.Md5(oldPassword);
- string newMd5 = NETCore.Encrypt.EncryptProvider.Md5(newPassword);
- if (!user.Password.Equals(oldMd5, StringComparison.OrdinalIgnoreCase))
- {
- return ToResponse(ApiResult.Error("修改密码失败,旧密码错误"));
- }
- if (user.Password.Equals(newMd5, StringComparison.OrdinalIgnoreCase))
- {
- return ToResponse(ApiResult.Error("新密码不能和旧密码相同"));
- }
- if (UserService.ResetPwd(userId, newMd5) > 0)
- {
- //TODO 更新缓存
- return SUCCESS(1);
- }
- return ToResponse(ApiResult.Error("修改密码异常,请联系管理员"));
- }
- /// <summary>
- /// 修改头像
- /// </summary>
- /// <param name="formFile"></param>
- /// <returns></returns>
- [HttpPost("Avatar")]
- [ActionPermissionFilter(Permission = "common")]
- [Log(Title = "修改头像", BusinessType = BusinessType.UPDATE, IsSaveRequestData = false)]
- public async Task<IActionResult> Avatar([FromForm(Name = "picture")] IFormFile formFile)
- {
- long userId = HttpContext.GetUId();
- if (formFile == null) throw new CustomException("请选择文件");
- Model.Dto.UploadDto dto = new()
- {
- FileDir = "avatar",
- ClassifyType = "avatar",
- UserName = HttpContext.GetName()
- };
- SysFile file = await FileService.SaveFileToLocal(hostEnvironment.WebRootPath, dto, dto.UserName, formFile);
- UserService.UpdatePhoto(new SysUser() { Avatar = file.AccessUrl, UserId = userId });
- return SUCCESS(new { imgUrl = file.AccessUrl });
- }
- }
- }
|