| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using System;
- using System.Threading.Tasks;
- using System.Web.Mvc;
- using Abp;
- using Abp.Auditing;
- using Abp.Domain.Repositories;
- using Abp.Notifications;
- using Abp.UI;
- using Abp.Web.Models;
- using Abp.Web.Mvc.Authorization;
- using WePlatform.Authorization.Users;
- using WePlatform.CommonManager.Notifications;
- using WePlatform.Configuration;
- using IwbZero.Auditing;
- using IwbZero.Runtime.Session;
- using IwbZero.ToolCommon.FileHelpers;
- namespace WePlatform.Controllers
- {
- [AbpMvcAuthorize, DisableAuditing]
- public class HomeController : IwbControllerBase
- {
- public HomeController(IRepository<User, long> userRepository)
- {
- UserRepository = userRepository;
- NotificationManager = NullNotificationManager.Instance;
- }
- public INotificationManager NotificationManager { get; }
- private IRepository<User, long> UserRepository { get; }
- public ActionResult Index()
- {
- return View();
- }
- public async Task<ActionResult> T(string id)
- {
- await NoticeManager.SendMsgAsync(new UserIdentifier(1, 4), id, NotificationSeverity.Success);
- return Content("ok");
- }
- [AbpMvcAuthorize, AuditLog("个人信息")]
- public ActionResult UserProfile()
- {
- var profile = new UserProfileViewModel(AbpSession);
- return View(profile);
- }
- [AbpMvcAuthorize, AuditLog("修改密码")]
- public ActionResult ChangePassword()
- {
- return View();
- }
- [AbpMvcAuthorize, AuditLog("修改个人信息")]
- [HttpPost]
- public async Task<ActionResult> UpdateUserProfile(UserProfileViewModel model)
- {
- try
- {
- var sysUser = await UserRepository.FirstOrDefaultAsync(a => a.Id == model.UserId);
- sysUser.Name = model.RealName;
- sysUser.EmailAddress = model.EmailAddress;
- sysUser.PhoneNumber = model.PhoneNumber;
- await UserRepository.UpdateAsync(sysUser);
- }
- catch (Exception e)
- {
- throw new UserFriendlyException("修改个人信息", e.Message);
- }
- return AbpJson(new AjaxResponse(true));
- }
- [AbpMvcAuthorize, AuditLog("修改头像")]
- [HttpPost]
- public async Task<ActionResult> UpdateAvatar()
- {
- var fileName = $"{AbpSession.UserId}@{AbpSession.UserName}";
- var filePath = $"/{await SettingManager.GetSettingValueAsync(IwbSettingNames.DownloadPath)}/UserAvatar/";
- //var url = Request.UploadFile("avatar_file", fileName,filePath,targetExt:"png");
- string avatar = Request["Avatar"];
- var file = avatar.Replace("data:image/png;base64,", "");
- var url = file.Base64ToPng(fileName, filePath);
- if (url.StartsWith("error@"))
- {
- string error = url.Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries)[1];
- throw new UserFriendlyException("修改头像", error);
- }
- var user = await UserRepository.FirstOrDefaultAsync(a => a.Id == AbpSession.UserId);
- var oldUrl = user.ImagePath;
- var newUrl = url.Replace(fileName, $"{AbpSession.UserId}@@{AbpSession.UserName}");
- var path = $"{AppDomain.CurrentDomain.BaseDirectory}{url}";
- var newPath = $"{AppDomain.CurrentDomain.BaseDirectory}{newUrl}";
- if (path.CompressImage(newPath))
- {
- user.ImagePath = newUrl;
- url.DeleteFile();
- url = newUrl;
- }
- else
- {
- user.ImagePath = url;
- }
- await UserRepository.UpdateAsync(user);
- if (!string.IsNullOrEmpty(oldUrl))
- {
- oldUrl.DeleteFile();
- }
- return AbpJson(new AjaxResponse(url));
- //return Content("{\"result\":\"" + url + "\"}");
- }
- }
- public class UserProfileViewModel
- {
- public UserProfileViewModel()
- {
- }
- public UserProfileViewModel(IIwbSession model)
- {
- if (model?.UserId == null)
- return;
- UserId = model.UserId ?? 0;
- UserName = model.UserName;
- RealName = model.RealName;
- EmailAddress = model.EmailAddress;
- PhoneNumber = model.PhoneNumber;
- ImagePath = model.AvatarImagePath;
- }
- public long UserId { get; set; }
- /// <summary>
- /// 系统账号
- /// </summary>
- public string UserName { get; set; }
- /// <summary>
- /// 姓名
- /// </summary>
- public string RealName { get; set; }
- /// <summary>
- /// 电子邮箱
- /// </summary>
- public string EmailAddress { get; set; }
- /// <summary>
- /// 个人简介
- /// </summary>
- public string Profile { get; set; }
- /// <summary>
- /// 出生日期
- /// </summary>
- public DateTime? Birthday { get; set; }
- /// <summary>
- /// 身份证号
- /// </summary>
- public string IdCard { get; set; }
- /// <summary>
- /// 联系号码
- /// </summary>
- public string PhoneNumber { get; set; }
- /// <summary>
- /// 备用号码
- /// </summary>
- public string PhoneNumber2 { get; set; }
- /// <summary>
- /// 头像图片
- /// </summary>
- public string ImagePath { get; set; }
- }
- }
|