HomeController.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Threading.Tasks;
  3. using System.Web.Mvc;
  4. using Abp;
  5. using Abp.Auditing;
  6. using Abp.Domain.Repositories;
  7. using Abp.Notifications;
  8. using Abp.UI;
  9. using Abp.Web.Models;
  10. using Abp.Web.Mvc.Authorization;
  11. using WePlatform.Authorization.Users;
  12. using WePlatform.CommonManager.Notifications;
  13. using WePlatform.Configuration;
  14. using IwbZero.Auditing;
  15. using IwbZero.Runtime.Session;
  16. using IwbZero.ToolCommon.FileHelpers;
  17. namespace WePlatform.Controllers
  18. {
  19. [AbpMvcAuthorize, DisableAuditing]
  20. public class HomeController : IwbControllerBase
  21. {
  22. public HomeController(IRepository<User, long> userRepository)
  23. {
  24. UserRepository = userRepository;
  25. NotificationManager = NullNotificationManager.Instance;
  26. }
  27. public INotificationManager NotificationManager { get; }
  28. private IRepository<User, long> UserRepository { get; }
  29. public ActionResult Index()
  30. {
  31. return View();
  32. }
  33. public async Task<ActionResult> T(string id)
  34. {
  35. await NoticeManager.SendMsgAsync(new UserIdentifier(1, 4), id, NotificationSeverity.Success);
  36. return Content("ok");
  37. }
  38. [AbpMvcAuthorize, AuditLog("个人信息")]
  39. public ActionResult UserProfile()
  40. {
  41. var profile = new UserProfileViewModel(AbpSession);
  42. return View(profile);
  43. }
  44. [AbpMvcAuthorize, AuditLog("修改密码")]
  45. public ActionResult ChangePassword()
  46. {
  47. return View();
  48. }
  49. [AbpMvcAuthorize, AuditLog("修改个人信息")]
  50. [HttpPost]
  51. public async Task<ActionResult> UpdateUserProfile(UserProfileViewModel model)
  52. {
  53. try
  54. {
  55. var sysUser = await UserRepository.FirstOrDefaultAsync(a => a.Id == model.UserId);
  56. sysUser.Name = model.RealName;
  57. sysUser.EmailAddress = model.EmailAddress;
  58. sysUser.PhoneNumber = model.PhoneNumber;
  59. await UserRepository.UpdateAsync(sysUser);
  60. }
  61. catch (Exception e)
  62. {
  63. throw new UserFriendlyException("修改个人信息", e.Message);
  64. }
  65. return AbpJson(new AjaxResponse(true));
  66. }
  67. [AbpMvcAuthorize, AuditLog("修改头像")]
  68. [HttpPost]
  69. public async Task<ActionResult> UpdateAvatar()
  70. {
  71. var fileName = $"{AbpSession.UserId}@{AbpSession.UserName}";
  72. var filePath = $"/{await SettingManager.GetSettingValueAsync(IwbSettingNames.DownloadPath)}/UserAvatar/";
  73. //var url = Request.UploadFile("avatar_file", fileName,filePath,targetExt:"png");
  74. string avatar = Request["Avatar"];
  75. var file = avatar.Replace("data:image/png;base64,", "");
  76. var url = file.Base64ToPng(fileName, filePath);
  77. if (url.StartsWith("error@"))
  78. {
  79. string error = url.Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries)[1];
  80. throw new UserFriendlyException("修改头像", error);
  81. }
  82. var user = await UserRepository.FirstOrDefaultAsync(a => a.Id == AbpSession.UserId);
  83. var oldUrl = user.ImagePath;
  84. var newUrl = url.Replace(fileName, $"{AbpSession.UserId}@@{AbpSession.UserName}");
  85. var path = $"{AppDomain.CurrentDomain.BaseDirectory}{url}";
  86. var newPath = $"{AppDomain.CurrentDomain.BaseDirectory}{newUrl}";
  87. if (path.CompressImage(newPath))
  88. {
  89. user.ImagePath = newUrl;
  90. url.DeleteFile();
  91. url = newUrl;
  92. }
  93. else
  94. {
  95. user.ImagePath = url;
  96. }
  97. await UserRepository.UpdateAsync(user);
  98. if (!string.IsNullOrEmpty(oldUrl))
  99. {
  100. oldUrl.DeleteFile();
  101. }
  102. return AbpJson(new AjaxResponse(url));
  103. //return Content("{\"result\":\"" + url + "\"}");
  104. }
  105. }
  106. public class UserProfileViewModel
  107. {
  108. public UserProfileViewModel()
  109. {
  110. }
  111. public UserProfileViewModel(IIwbSession model)
  112. {
  113. if (model?.UserId == null)
  114. return;
  115. UserId = model.UserId ?? 0;
  116. UserName = model.UserName;
  117. RealName = model.RealName;
  118. EmailAddress = model.EmailAddress;
  119. PhoneNumber = model.PhoneNumber;
  120. ImagePath = model.AvatarImagePath;
  121. }
  122. public long UserId { get; set; }
  123. /// <summary>
  124. /// 系统账号
  125. /// </summary>
  126. public string UserName { get; set; }
  127. /// <summary>
  128. /// 姓名
  129. /// </summary>
  130. public string RealName { get; set; }
  131. /// <summary>
  132. /// 电子邮箱
  133. /// </summary>
  134. public string EmailAddress { get; set; }
  135. /// <summary>
  136. /// 个人简介
  137. /// </summary>
  138. public string Profile { get; set; }
  139. /// <summary>
  140. /// 出生日期
  141. /// </summary>
  142. public DateTime? Birthday { get; set; }
  143. /// <summary>
  144. /// 身份证号
  145. /// </summary>
  146. public string IdCard { get; set; }
  147. /// <summary>
  148. /// 联系号码
  149. /// </summary>
  150. public string PhoneNumber { get; set; }
  151. /// <summary>
  152. /// 备用号码
  153. /// </summary>
  154. public string PhoneNumber2 { get; set; }
  155. /// <summary>
  156. /// 头像图片
  157. /// </summary>
  158. public string ImagePath { get; set; }
  159. }
  160. }