HomeController.cs 6.2 KB

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