123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Security.Claims;
- using System.Text;
- using System.Threading.Tasks;
- using Datory.Utils;
- using GxPress.Api.Tools;
- using GxPress.Auth;
- using GxPress.Common.Exceptions;
- using GxPress.Common.Validation;
- using GxPress.Entity;
- using GxPress.EnumConst;
- using GxPress.Repository.Interface;
- using GxPress.Request.App.User;
- using GxPress.Request.User;
- using GxPress.Result.App.FileLibrary;
- using GxPress.Result.App.User;
- using GxPress.Result.User;
- using GxPress.Service.Interface;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Caching.Distributed;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- namespace GxPress.Api.AppControllers
- {
- /// <summary>
- /// 用户
- /// </summary>
- [Route("/api/app/user")]
- [ApiController]
- [Authorize]
- public class UserController : ControllerBase
- {
- private readonly JwtOptions _jwtOptions;
- private readonly ILogger<UserController> _logger;
- private readonly IUserRepository _userRepository;
- private readonly IDepartmentRepository _departmentRepository;
- private readonly ILoginContext _loginContext;
- private readonly IUserService _userService;
- private readonly IFileLibraryRepository fileLibraryRepository;
- private readonly IDistributedCache _cache;
- public UserController(IUserRepository userRepository, IOptions<JwtOptions> jwtOptions,
- ILogger<UserController> logger, IDepartmentRepository departmentRepository, ILoginContext loginContext,
- IUserService userService, IFileLibraryRepository fileLibraryRepository, IDistributedCache cache)
- {
- _userRepository = userRepository;
- _departmentRepository = departmentRepository;
- _userService = userService;
- _jwtOptions = jwtOptions.Value;
- _logger = logger;
- _loginContext = loginContext;
- this.fileLibraryRepository = fileLibraryRepository;
- _cache = cache;
- }
- ///// <summary>
- ///// 添加
- ///// </summary>
- ///// <param name="request"></param>
- ///// <returns></returns>
- //[HttpPost]
- //public async Task<User> Add([FromBody] User request)
- //{
- // request.Id = await _userRepository.InsertAsync(request);
- // return request;
- //}
- /// <summary>
- /// 登录
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("signin")]
- [AllowAnonymous]
- public async Task<UserSignInResult> SignIn(UserSignInRequest request)
- {
- var result = await _userRepository.SignInAsync(request);
- var claims = new[]
- {
- new Claim(ClaimTypes.NameIdentifier, result.UserId.ToString()),
- new Claim(ClaimTypes.Role, AccountTypeConst.User.ToString())
- };
- result.Token = TokenHelper.BuildToken(_jwtOptions, claims);
- return result;
- }
- /// <summary>
- /// 绑定opendId
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("set-opend-Id")]
- [AllowAnonymous]
- public async Task<UserSignInResult> SetOpenId(UserSignInRequest request)
- {
- var success = await _userRepository.UpdateByOpendIdAsync(request);
- if (success)
- {
- var result = await _userRepository.SignInAsync(request);
- var claims = new[]
- {
- new Claim(ClaimTypes.NameIdentifier, result.UserId.ToString()),
- new Claim(ClaimTypes.Role, AccountTypeConst.User.ToString())
- };
- result.Token = TokenHelper.BuildToken(_jwtOptions, claims);
- return result;
- }
- return new UserSignInResult();
- }
- /// <summary>
- /// 查询opendId是否存在
- /// </summary>
- /// <param name="opendId"></param>
- /// <returns></returns>
- [HttpGet("find-opend-Id/{opendId}")]
- [AllowAnonymous]
- public async Task<bool> FindOpenId(string opendId)
- {
- var user = await _userRepository.GetByOpenIdAsync(opendId);
- if (user == null)
- return false;
- return true;
- }
- /////// <summary>
- /////// 登录验证码发送
- /////// </summary>
- /////// <param name="phone"></param>
- /////// <returns></returns>
- ////[HttpGet("sendSmsCode")]
- ////[AllowAnonymous]
- ////public async Task<bool> SendSmsCode([FromQuery] [Required] [Mobile] string phone)
- ////{
- //// var user = await _userRepository.GetByPhoneAsync(phone);
- //// //用户不存在
- //// if (user == null)
- //// {
- //// throw new BusinessException("该用户不存在");
- //// }
- //// //TODO 短信验证码发送
- //// //return await _smsService.
- //// //SendValidationCodeAsync(phone);
- //// var key = $"login:{phone}";
- //// if (await RedisHelper.ExistsAsync(key)) throw new BusinessException("发送太频繁");
- //// var code = RandomGenerator.GetNumberString(6);
- //// _logger.LogInformation("{phone}验证码:{code}", phone, code);
- //// //发送验证码阿里云
- //// IClientProfile profile =
- //// DefaultProfile.GetProfile("cn-hangzhou", "LTAI2E47R4DlcYfo", "5epQRUGRrDSoF7yukyYf4HX6dUlvF3");
- //// DefaultAcsClient client = new DefaultAcsClient(profile);
- //// CommonRequest request = new CommonRequest
- //// {
- //// Method = MethodType.POST,
- //// Domain = "dysmsapi.aliyuncs.com",
- //// Version = "2017-05-25",
- //// Action = "SendSms"
- //// };
- //// request.AddQueryParameters("PhoneNumbers", $"{phone}");
- //// request.AddQueryParameters("SignName", "泰德合众");
- //// request.AddQueryParameters("TemplateCode", "SMS_168126117");
- //// request.AddQueryParameters("TemplateParam", "{\"code\":\"" + code + "\"}");
- //// try
- //// {
- //// CommonResponse response = client.GetCommonResponse(request);
- //// _logger.LogInformation(Encoding.Default.GetString(response.HttpResponse.Content));
- //// }
- //// catch (ServerException e)
- //// {
- //// throw new BusinessException(e.Message);
- //// }
- //return await RedisHelper.SetAsync(key, code, 300);
- ////}
- /// <summary>
- /// 登录验证码发送
- /// </summary>
- /// <param name="phone"></param>
- /// <returns></returns>
- [HttpGet("sendSmsCode")]
- [AllowAnonymous]
- public async Task<bool> SendSmsCode([FromQuery] [Required] [Mobile] string phone)
- {
- var user = await _userRepository.GetByPhoneAsync(phone);
- //用户不存在
- if (user == null)
- {
- throw new BusinessException("该用户不存在");
- }
- //TODO 短信验证码发送
- //return await _smsService.
- //SendValidationCodeAsync(phone);
- var key = $"login:{phone}";
- //if (await RedisHelper.ExistsAsync(key)) throw new BusinessException("发送太频繁");
- var code = "180606";
- _logger.LogInformation($"{key}", phone, code);
- var codeByte = Encoding.UTF8.GetBytes(Utilities.JsonSerialize(code));
- await _cache.SetAsync($"{key}", codeByte, new DistributedCacheEntryOptions
- {
- AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(300)
- });
- // var result = await RedisHelper.SetAsync(key, code, 300);
- // if (result == false)
- // throw new BusinessException("发送失败");
- return true;
- }
- /// <summary>
- /// 更换手机号码验证码发送
- /// </summary>
- /// <param name="phone"></param>
- /// <returns></returns>
- [HttpGet("send-sms-code")]
- public async Task<bool> SendSmsCodeReplace([FromQuery] [Required] [Mobile] string phone)
- {
- var user = await _userRepository.GetByPhoneAsync(phone);
- if (user != null)
- throw new BusinessException("号码以被使用");
- //TODO 短信验证码发送
- //return await _smsService.
- //SendValidationCodeAsync(phone);
- var key = $"login:{phone}";
- // if (await RedisHelper.ExistsAsync(key)) throw new BusinessException("发送太频繁");
- var code = "180606";
- _logger.LogInformation("{phone}验证码:{code}", phone, code);
- //var result = await RedisHelper.SetAsync(key, code, 300);
- //if (result == false)
- // throw new BusinessException("发送失败");
- return true;
- }
- /// <summary>
- /// app查询用户详情
- /// </summary>
- /// <returns></returns>
- [HttpGet("detail")]
- public async Task<UserDetail> GetDetail()
- {
- var id = _loginContext.AccountId;
- var user = await _userRepository.GetAsync(id);
- if (user == null)
- throw new BusinessException("用户id有误");
- return await _userRepository.GetDetailAsync(id);
- }
- /// <summary>
- /// app查询他人用户详情
- /// </summary>
- /// <returns></returns>
- [HttpGet("detail/{id}")]
- public async Task<UserDetail> GetDetail(int id)
- {
- if (id <= 0)
- throw new BusinessException("用户id有误");
- return await _userService.GetUserByIdAsync(_loginContext.AccountId, id);
- }
- /// <summary>
- /// app更新用户信息
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPut("update")]
- public async Task<bool> Update([FromBody] User request)
- {
- var id = _loginContext.AccountId;
- var result = await _userRepository.UpdateAsync(id, request);
- if (result == false)
- throw new BusinessException("更新失败");
- return true;
- }
- /// <summary>
- /// 私信
- /// </summary>
- /// <returns></returns>
- [HttpPost("update-user-private-letter")]
- public async Task<bool> UpdateUserPrivateLetter()
- {
- UserPrivateLetterRequest request = new UserPrivateLetterRequest { Id = _loginContext.AccountId };
- var result = await _userRepository.UpdateUserPrivateLetterAsync(request);
- if (result == false)
- throw new BusinessException("更新失败");
- return true;
- }
- /// <summary>
- /// 通知
- /// </summary>
- /// <returns></returns>
- [HttpPost("update-user-notice")]
- public async Task<bool> UpdateUserNotice()
- {
- var request = new UserNoticeRequest { Id = _loginContext.AccountId };
- var result = await _userRepository.UpdateUserNoticeAsync(request);
- if (result == false)
- throw new BusinessException("更新失败");
- return true;
- }
- /// <summary>
- /// 回复
- /// </summary>
- /// <returns></returns>
- [HttpPost("update-user-reply")]
- public async Task<bool> UpdateUserReply()
- {
- var request = new UserReplyRequest { Id = _loginContext.AccountId };
- var result = await _userRepository.UpdateUserReplyAsync(request);
- if (result == false)
- throw new BusinessException("更新失败");
- return true;
- }
- /// <summary>
- /// 静音
- /// </summary>
- /// <returns></returns>
- [HttpPost("update-user-mute")]
- public async Task<bool> UpdateUserMute()
- {
- var request = new UserMuteRequest { Id = _loginContext.AccountId };
- var result = await _userRepository.UpdateUserMuteAsync(request);
- if (result == false)
- throw new BusinessException("更新失败");
- return true;
- }
- /// <summary>
- /// 震动
- /// </summary>
- /// <returns></returns>
- [HttpPost("update-user-shake")]
- public async Task<bool> UpdateUserShake()
- {
- var request = new UserShakeRequest { Id = _loginContext.AccountId };
- var result = await _userRepository.UpdateUserShakeAsync(request);
- if (result == false)
- throw new BusinessException("更新失败");
- return true;
- }
- /// <summary>
- /// 用户修改手机号码
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("update-user-phone")]
- public async Task<bool> UpdateUserPhone(UserUpdatePhoneRequest request)
- {
- request.UserId = _loginContext.AccountId;
- var result = await _userRepository.UpdateUserPhoneAsync(request);
- if (result == false)
- throw new BusinessException("更新失败");
- return true;
- }
- /// <summary>
- /// 邮箱验证码
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("send-email-verify-code")]
- public async Task<bool> SendEmailVerifyCode(UserEmailVerifyCodeRequest request)
- {
- request.UserId = _loginContext.AccountId;
- var result = await _userRepository.SendEmailVerifyCodeAsync(request);
- if (result == false)
- throw new BusinessException("更新失败");
- return true;
- }
- /// <summary>
- /// 修改邮箱
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("update-user-email")]
- public async Task<bool> UpdateUserEmail(UserUpdateEmailRequest request)
- {
- request.UserId = _loginContext.AccountId;
- var result = await _userRepository.UpdateUserEmailAsync(request);
- if (result == false)
- throw new BusinessException("更新失败");
- return true;
- }
- /// <summary>
- /// 查询联系人
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("search")]
- public async Task<IEnumerable<UserInfoResult>> SearchUserName(SearchUserNameRequest request)
- {
- return await _userRepository.SearchUserNameAsync(request);
- }
- /// <summary>
- /// 根据部门ID获取自建ID获取用户列表
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("find")]
- public async Task<IEnumerable<UserInfoResult>> FindUser(FindUserRequest request)
- {
- request.UserId = _loginContext.AccountId;
- return await _userService.FindUser(request);
- }
- /// <summary>
- /// 根据部门ID获取自建ID获取用户列表
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- [HttpGet("find-name")]
- public async Task<IEnumerable<UserInfoResult>> FindUserByName([FromQuery] string name)
- {
- return await _userRepository.UserByNameAsync(name);
- }
- /// <summary>
- /// 根据GUID查询用户
- /// </summary>
- /// <returns></returns>
- [HttpPost("guid")]
- public async Task<UserDetail> FindUserByGuid(FindUserByGuidRequest request)
- {
- var user = await _userRepository.GetGuidAsync(request.Guid);
- return user;
- }
- /// <summary>
- /// 获取用户工作模块未读数据
- /// </summary>
- /// <returns></returns>
- [HttpGet("user-uread-count")]
- public async Task<UserCountResult> GetUserCountAsync()
- {
- return await _userService.GetUserCountAsync(_loginContext.AccountId);
- }
- /// <summary>
- /// 根据用户名获取电脑上传的数据
- /// </summary>
- /// <returns></returns>
- [HttpGet("user-file-library")]
- public async Task<IEnumerable<FileLibraryResult>> GetFileLibraryByUserIdAsync()
- {
- return await fileLibraryRepository.GetFileLibraryByUserIdAsync(_loginContext.AccountId);
- }
- }
- }
|