HomeController.cs 5.6 KB

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