shgx_tz_mom/ZR.Admin.WebApi/Controllers/System/SysProfileController.cs

139 lines
4.9 KiB
C#
Raw Normal View History

using Infrastructure;
2021-08-23 16:57:25 +08:00
using Infrastructure.Attribute;
using Infrastructure.Enums;
using Infrastructure.Model;
using Mapster;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
2022-05-24 18:44:11 +08:00
using System;
2022-05-13 22:13:44 +08:00
using System.Threading.Tasks;
2021-08-23 16:57:25 +08:00
using ZR.Admin.WebApi.Extensions;
using ZR.Admin.WebApi.Filters;
using ZR.Model.System;
2022-05-13 22:13:44 +08:00
using ZR.Model.System.Dto;
2021-09-16 19:35:17 +08:00
using ZR.Service.System.IService;
2021-08-23 16:57:25 +08:00
namespace ZR.Admin.WebApi.Controllers.System
{
2022-05-13 22:13:44 +08:00
/// <summary>
/// 个人中心
/// </summary>
2021-08-23 16:57:25 +08:00
[Verify]
[Route("system/user/profile")]
public class SysProfileController : BaseController
{
private readonly ISysUserService UserService;
private readonly ISysRoleService RoleService;
private readonly ISysUserPostService UserPostService;
private readonly ISysDeptService DeptService;
2022-03-29 16:45:11 +08:00
private readonly ISysFileService FileService;
2021-08-23 16:57:25 +08:00
private IWebHostEnvironment hostEnvironment;
public SysProfileController(
ISysUserService userService,
ISysRoleService roleService,
ISysUserPostService postService,
ISysDeptService deptService,
2022-03-29 16:45:11 +08:00
ISysFileService sysFileService,
2021-08-23 16:57:25 +08:00
IWebHostEnvironment hostEnvironment)
{
UserService = userService;
RoleService = roleService;
UserPostService = postService;
DeptService = deptService;
2022-03-29 16:45:11 +08:00
FileService = sysFileService;
2021-08-23 16:57:25 +08:00
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 ?? "-";
2021-08-23 16:57:25 +08:00
return SUCCESS(new { user, roles, postGroup }, TIME_FORMAT_FULL);
}
/// <summary>
/// 修改用户
/// </summary>
/// <returns></returns>
[HttpPut]
[ActionPermissionFilter(Permission = "common")]
2021-08-23 16:57:25 +08:00
[Log(Title = "修改信息", BusinessType = BusinessType.UPDATE)]
public IActionResult UpdateProfile([FromBody] SysUserDto userDto)
{
if (userDto == null)
{
throw new CustomException(ResultCode.PARAM_ERROR, "请求参数错误");
}
2021-09-29 15:57:56 +08:00
var user = userDto.Adapt<SysUser>().ToUpdate(HttpContext);
2021-08-23 16:57:25 +08:00
int result = UserService.ChangeUser(user);
2021-09-27 16:07:55 +08:00
return ToResponse(result);
2021-08-23 16:57:25 +08:00
}
/// <summary>
/// 修改密码
/// </summary>
/// <returns></returns>
[HttpPut("updatePwd")]
[ActionPermissionFilter(Permission = "common")]
2021-08-23 16:57:25 +08:00
[Log(Title = "修改密码", BusinessType = BusinessType.UPDATE)]
public IActionResult UpdatePwd(string oldPassword, string newPassword)
{
2023-03-18 07:58:39 +08:00
long userId = HttpContext.GetUId();
SysUser user = UserService.SelectUserById(userId);
2021-08-23 16:57:25 +08:00
string oldMd5 = NETCore.Encrypt.EncryptProvider.Md5(oldPassword);
string newMd5 = NETCore.Encrypt.EncryptProvider.Md5(newPassword);
2022-05-24 18:44:11 +08:00
if (!user.Password.Equals(oldMd5, StringComparison.OrdinalIgnoreCase))
2021-08-23 16:57:25 +08:00
{
2021-09-27 16:07:55 +08:00
return ToResponse(ApiResult.Error("修改密码失败,旧密码错误"));
2021-08-23 16:57:25 +08:00
}
2022-05-24 18:44:11 +08:00
if (user.Password.Equals(newMd5, StringComparison.OrdinalIgnoreCase))
2021-08-23 16:57:25 +08:00
{
2021-09-27 16:07:55 +08:00
return ToResponse(ApiResult.Error("新密码不能和旧密码相同"));
2021-08-23 16:57:25 +08:00
}
2023-03-18 07:58:39 +08:00
if (UserService.ResetPwd(userId, newMd5) > 0)
2021-08-23 16:57:25 +08:00
{
//TODO 更新缓存
return SUCCESS(1);
}
2021-09-27 16:07:55 +08:00
return ToResponse(ApiResult.Error("修改密码异常,请联系管理员"));
2021-08-23 16:57:25 +08:00
}
/// <summary>
/// 修改头像
/// </summary>
2021-12-04 08:24:38 +08:00
/// <param name="formFile"></param>
2021-08-23 16:57:25 +08:00
/// <returns></returns>
[HttpPost("Avatar")]
[ActionPermissionFilter(Permission = "common")]
2021-12-04 08:24:38 +08:00
[Log(Title = "修改头像", BusinessType = BusinessType.UPDATE, IsSaveRequestData = false)]
2022-03-29 16:45:11 +08:00
public async Task<IActionResult> Avatar([FromForm(Name = "picture")] IFormFile formFile)
2021-08-23 16:57:25 +08:00
{
2023-03-18 07:58:39 +08:00
long userId = HttpContext.GetUId();
2021-08-23 16:57:25 +08:00
if (formFile == null) throw new CustomException("请选择文件");
2022-06-09 08:39:02 +08:00
SysFile file = await FileService.SaveFileToLocal(hostEnvironment.WebRootPath, "", "avatar", HttpContext.GetName(), formFile);
2021-08-23 16:57:25 +08:00
2023-03-18 07:58:39 +08:00
UserService.UpdatePhoto(new SysUser() { Avatar = file.AccessUrl, UserId = userId });
2022-03-29 16:45:11 +08:00
return SUCCESS(new { imgUrl = file.AccessUrl });
2021-08-23 16:57:25 +08:00
}
}
}