UserController.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Security.Claims;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Datory.Utils;
  8. using GxPress.Api.Tools;
  9. using GxPress.Auth;
  10. using GxPress.Common.Exceptions;
  11. using GxPress.Common.Validation;
  12. using GxPress.Entity;
  13. using GxPress.EnumConst;
  14. using GxPress.Repository.Interface;
  15. using GxPress.Request.App.User;
  16. using GxPress.Request.User;
  17. using GxPress.Result.App.FileLibrary;
  18. using GxPress.Result.App.User;
  19. using GxPress.Result.User;
  20. using GxPress.Service.Interface;
  21. using Microsoft.AspNetCore.Authorization;
  22. using Microsoft.AspNetCore.Mvc;
  23. using Microsoft.Extensions.Caching.Distributed;
  24. using Microsoft.Extensions.Logging;
  25. using Microsoft.Extensions.Options;
  26. namespace GxPress.Api.AppControllers
  27. {
  28. /// <summary>
  29. /// 用户
  30. /// </summary>
  31. [Route("/api/app/user")]
  32. [ApiController]
  33. [Authorize]
  34. public class UserController : ControllerBase
  35. {
  36. private readonly JwtOptions _jwtOptions;
  37. private readonly ILogger<UserController> _logger;
  38. private readonly IUserRepository _userRepository;
  39. private readonly IDepartmentRepository _departmentRepository;
  40. private readonly ILoginContext _loginContext;
  41. private readonly IUserService _userService;
  42. private readonly IFileLibraryRepository fileLibraryRepository;
  43. private readonly IDistributedCache _cache;
  44. public UserController(IUserRepository userRepository, IOptions<JwtOptions> jwtOptions,
  45. ILogger<UserController> logger, IDepartmentRepository departmentRepository, ILoginContext loginContext,
  46. IUserService userService, IFileLibraryRepository fileLibraryRepository, IDistributedCache cache)
  47. {
  48. _userRepository = userRepository;
  49. _departmentRepository = departmentRepository;
  50. _userService = userService;
  51. _jwtOptions = jwtOptions.Value;
  52. _logger = logger;
  53. _loginContext = loginContext;
  54. this.fileLibraryRepository = fileLibraryRepository;
  55. _cache = cache;
  56. }
  57. ///// <summary>
  58. ///// 添加
  59. ///// </summary>
  60. ///// <param name="request"></param>
  61. ///// <returns></returns>
  62. //[HttpPost]
  63. //public async Task<User> Add([FromBody] User request)
  64. //{
  65. // request.Id = await _userRepository.InsertAsync(request);
  66. // return request;
  67. //}
  68. /// <summary>
  69. /// 登录
  70. /// </summary>
  71. /// <param name="request"></param>
  72. /// <returns></returns>
  73. [HttpPost("signin")]
  74. [AllowAnonymous]
  75. public async Task<UserSignInResult> SignIn(UserSignInRequest request)
  76. {
  77. var result = await _userRepository.SignInAsync(request);
  78. var claims = new[]
  79. {
  80. new Claim(ClaimTypes.NameIdentifier, result.UserId.ToString()),
  81. new Claim(ClaimTypes.Role, AccountTypeConst.User.ToString())
  82. };
  83. result.Token = TokenHelper.BuildToken(_jwtOptions, claims);
  84. return result;
  85. }
  86. /// <summary>
  87. /// 绑定opendId
  88. /// </summary>
  89. /// <param name="request"></param>
  90. /// <returns></returns>
  91. [HttpPost("set-opend-Id")]
  92. [AllowAnonymous]
  93. public async Task<UserSignInResult> SetOpenId(UserSignInRequest request)
  94. {
  95. var success = await _userRepository.UpdateByOpendIdAsync(request);
  96. if (success)
  97. {
  98. var result = await _userRepository.SignInAsync(request);
  99. var claims = new[]
  100. {
  101. new Claim(ClaimTypes.NameIdentifier, result.UserId.ToString()),
  102. new Claim(ClaimTypes.Role, AccountTypeConst.User.ToString())
  103. };
  104. result.Token = TokenHelper.BuildToken(_jwtOptions, claims);
  105. return result;
  106. }
  107. return new UserSignInResult();
  108. }
  109. /// <summary>
  110. /// 查询opendId是否存在
  111. /// </summary>
  112. /// <param name="opendId"></param>
  113. /// <returns></returns>
  114. [HttpGet("find-opend-Id/{opendId}")]
  115. [AllowAnonymous]
  116. public async Task<bool> FindOpenId(string opendId)
  117. {
  118. var user = await _userRepository.GetByOpenIdAsync(opendId);
  119. if (user == null)
  120. return false;
  121. return true;
  122. }
  123. /////// <summary>
  124. /////// 登录验证码发送
  125. /////// </summary>
  126. /////// <param name="phone"></param>
  127. /////// <returns></returns>
  128. ////[HttpGet("sendSmsCode")]
  129. ////[AllowAnonymous]
  130. ////public async Task<bool> SendSmsCode([FromQuery] [Required] [Mobile] string phone)
  131. ////{
  132. //// var user = await _userRepository.GetByPhoneAsync(phone);
  133. //// //用户不存在
  134. //// if (user == null)
  135. //// {
  136. //// throw new BusinessException("该用户不存在");
  137. //// }
  138. //// //TODO 短信验证码发送
  139. //// //return await _smsService.
  140. //// //SendValidationCodeAsync(phone);
  141. //// var key = $"login:{phone}";
  142. //// if (await RedisHelper.ExistsAsync(key)) throw new BusinessException("发送太频繁");
  143. //// var code = RandomGenerator.GetNumberString(6);
  144. //// _logger.LogInformation("{phone}验证码:{code}", phone, code);
  145. //// //发送验证码阿里云
  146. //// IClientProfile profile =
  147. //// DefaultProfile.GetProfile("cn-hangzhou", "LTAI2E47R4DlcYfo", "5epQRUGRrDSoF7yukyYf4HX6dUlvF3");
  148. //// DefaultAcsClient client = new DefaultAcsClient(profile);
  149. //// CommonRequest request = new CommonRequest
  150. //// {
  151. //// Method = MethodType.POST,
  152. //// Domain = "dysmsapi.aliyuncs.com",
  153. //// Version = "2017-05-25",
  154. //// Action = "SendSms"
  155. //// };
  156. //// request.AddQueryParameters("PhoneNumbers", $"{phone}");
  157. //// request.AddQueryParameters("SignName", "泰德合众");
  158. //// request.AddQueryParameters("TemplateCode", "SMS_168126117");
  159. //// request.AddQueryParameters("TemplateParam", "{\"code\":\"" + code + "\"}");
  160. //// try
  161. //// {
  162. //// CommonResponse response = client.GetCommonResponse(request);
  163. //// _logger.LogInformation(Encoding.Default.GetString(response.HttpResponse.Content));
  164. //// }
  165. //// catch (ServerException e)
  166. //// {
  167. //// throw new BusinessException(e.Message);
  168. //// }
  169. //return await RedisHelper.SetAsync(key, code, 300);
  170. ////}
  171. /// <summary>
  172. /// 登录验证码发送
  173. /// </summary>
  174. /// <param name="phone"></param>
  175. /// <returns></returns>
  176. [HttpGet("sendSmsCode")]
  177. [AllowAnonymous]
  178. public async Task<bool> SendSmsCode([FromQuery] [Required] [Mobile] string phone)
  179. {
  180. var user = await _userRepository.GetByPhoneAsync(phone);
  181. //用户不存在
  182. if (user == null)
  183. {
  184. throw new BusinessException("该用户不存在");
  185. }
  186. //TODO 短信验证码发送
  187. //return await _smsService.
  188. //SendValidationCodeAsync(phone);
  189. var key = $"login:{phone}";
  190. //if (await RedisHelper.ExistsAsync(key)) throw new BusinessException("发送太频繁");
  191. var code = "180606";
  192. _logger.LogInformation($"{key}", phone, code);
  193. var codeByte = Encoding.UTF8.GetBytes(Utilities.JsonSerialize(code));
  194. await _cache.SetAsync($"{key}", codeByte, new DistributedCacheEntryOptions
  195. {
  196. AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(300)
  197. });
  198. // var result = await RedisHelper.SetAsync(key, code, 300);
  199. // if (result == false)
  200. // throw new BusinessException("发送失败");
  201. return true;
  202. }
  203. /// <summary>
  204. /// 更换手机号码验证码发送
  205. /// </summary>
  206. /// <param name="phone"></param>
  207. /// <returns></returns>
  208. [HttpGet("send-sms-code")]
  209. public async Task<bool> SendSmsCodeReplace([FromQuery] [Required] [Mobile] string phone)
  210. {
  211. var user = await _userRepository.GetByPhoneAsync(phone);
  212. if (user != null)
  213. throw new BusinessException("号码以被使用");
  214. //TODO 短信验证码发送
  215. //return await _smsService.
  216. //SendValidationCodeAsync(phone);
  217. var key = $"login:{phone}";
  218. // if (await RedisHelper.ExistsAsync(key)) throw new BusinessException("发送太频繁");
  219. var code = "180606";
  220. _logger.LogInformation("{phone}验证码:{code}", phone, code);
  221. //var result = await RedisHelper.SetAsync(key, code, 300);
  222. //if (result == false)
  223. // throw new BusinessException("发送失败");
  224. return true;
  225. }
  226. /// <summary>
  227. /// app查询用户详情
  228. /// </summary>
  229. /// <returns></returns>
  230. [HttpGet("detail")]
  231. public async Task<UserDetail> GetDetail()
  232. {
  233. var id = _loginContext.AccountId;
  234. var user = await _userRepository.GetAsync(id);
  235. if (user == null)
  236. throw new BusinessException("用户id有误");
  237. return await _userRepository.GetDetailAsync(id);
  238. }
  239. /// <summary>
  240. /// app查询他人用户详情
  241. /// </summary>
  242. /// <returns></returns>
  243. [HttpGet("detail/{id}")]
  244. public async Task<UserDetail> GetDetail(int id)
  245. {
  246. if (id <= 0)
  247. throw new BusinessException("用户id有误");
  248. return await _userService.GetUserByIdAsync(_loginContext.AccountId, id);
  249. }
  250. /// <summary>
  251. /// app更新用户信息
  252. /// </summary>
  253. /// <param name="request"></param>
  254. /// <returns></returns>
  255. [HttpPut("update")]
  256. public async Task<bool> Update([FromBody] User request)
  257. {
  258. var id = _loginContext.AccountId;
  259. var result = await _userRepository.UpdateAsync(id, request);
  260. if (result == false)
  261. throw new BusinessException("更新失败");
  262. return true;
  263. }
  264. /// <summary>
  265. /// 私信
  266. /// </summary>
  267. /// <returns></returns>
  268. [HttpPost("update-user-private-letter")]
  269. public async Task<bool> UpdateUserPrivateLetter()
  270. {
  271. UserPrivateLetterRequest request = new UserPrivateLetterRequest { Id = _loginContext.AccountId };
  272. var result = await _userRepository.UpdateUserPrivateLetterAsync(request);
  273. if (result == false)
  274. throw new BusinessException("更新失败");
  275. return true;
  276. }
  277. /// <summary>
  278. /// 通知
  279. /// </summary>
  280. /// <returns></returns>
  281. [HttpPost("update-user-notice")]
  282. public async Task<bool> UpdateUserNotice()
  283. {
  284. var request = new UserNoticeRequest { Id = _loginContext.AccountId };
  285. var result = await _userRepository.UpdateUserNoticeAsync(request);
  286. if (result == false)
  287. throw new BusinessException("更新失败");
  288. return true;
  289. }
  290. /// <summary>
  291. /// 回复
  292. /// </summary>
  293. /// <returns></returns>
  294. [HttpPost("update-user-reply")]
  295. public async Task<bool> UpdateUserReply()
  296. {
  297. var request = new UserReplyRequest { Id = _loginContext.AccountId };
  298. var result = await _userRepository.UpdateUserReplyAsync(request);
  299. if (result == false)
  300. throw new BusinessException("更新失败");
  301. return true;
  302. }
  303. /// <summary>
  304. /// 静音
  305. /// </summary>
  306. /// <returns></returns>
  307. [HttpPost("update-user-mute")]
  308. public async Task<bool> UpdateUserMute()
  309. {
  310. var request = new UserMuteRequest { Id = _loginContext.AccountId };
  311. var result = await _userRepository.UpdateUserMuteAsync(request);
  312. if (result == false)
  313. throw new BusinessException("更新失败");
  314. return true;
  315. }
  316. /// <summary>
  317. /// 震动
  318. /// </summary>
  319. /// <returns></returns>
  320. [HttpPost("update-user-shake")]
  321. public async Task<bool> UpdateUserShake()
  322. {
  323. var request = new UserShakeRequest { Id = _loginContext.AccountId };
  324. var result = await _userRepository.UpdateUserShakeAsync(request);
  325. if (result == false)
  326. throw new BusinessException("更新失败");
  327. return true;
  328. }
  329. /// <summary>
  330. /// 用户修改手机号码
  331. /// </summary>
  332. /// <param name="request"></param>
  333. /// <returns></returns>
  334. [HttpPost("update-user-phone")]
  335. public async Task<bool> UpdateUserPhone(UserUpdatePhoneRequest request)
  336. {
  337. request.UserId = _loginContext.AccountId;
  338. var result = await _userRepository.UpdateUserPhoneAsync(request);
  339. if (result == false)
  340. throw new BusinessException("更新失败");
  341. return true;
  342. }
  343. /// <summary>
  344. /// 邮箱验证码
  345. /// </summary>
  346. /// <param name="request"></param>
  347. /// <returns></returns>
  348. [HttpPost("send-email-verify-code")]
  349. public async Task<bool> SendEmailVerifyCode(UserEmailVerifyCodeRequest request)
  350. {
  351. request.UserId = _loginContext.AccountId;
  352. var result = await _userRepository.SendEmailVerifyCodeAsync(request);
  353. if (result == false)
  354. throw new BusinessException("更新失败");
  355. return true;
  356. }
  357. /// <summary>
  358. /// 修改邮箱
  359. /// </summary>
  360. /// <param name="request"></param>
  361. /// <returns></returns>
  362. [HttpPost("update-user-email")]
  363. public async Task<bool> UpdateUserEmail(UserUpdateEmailRequest request)
  364. {
  365. request.UserId = _loginContext.AccountId;
  366. var result = await _userRepository.UpdateUserEmailAsync(request);
  367. if (result == false)
  368. throw new BusinessException("更新失败");
  369. return true;
  370. }
  371. /// <summary>
  372. /// 查询联系人
  373. /// </summary>
  374. /// <param name="request"></param>
  375. /// <returns></returns>
  376. [HttpPost("search")]
  377. public async Task<IEnumerable<UserInfoResult>> SearchUserName(SearchUserNameRequest request)
  378. {
  379. return await _userRepository.SearchUserNameAsync(request);
  380. }
  381. /// <summary>
  382. /// 根据部门ID获取自建ID获取用户列表
  383. /// </summary>
  384. /// <param name="request"></param>
  385. /// <returns></returns>
  386. [HttpPost("find")]
  387. public async Task<IEnumerable<UserInfoResult>> FindUser(FindUserRequest request)
  388. {
  389. request.UserId = _loginContext.AccountId;
  390. return await _userService.FindUser(request);
  391. }
  392. /// <summary>
  393. /// 根据部门ID获取自建ID获取用户列表
  394. /// </summary>
  395. /// <param name="name"></param>
  396. /// <returns></returns>
  397. [HttpGet("find-name")]
  398. public async Task<IEnumerable<UserInfoResult>> FindUserByName([FromQuery] string name)
  399. {
  400. return await _userRepository.UserByNameAsync(name);
  401. }
  402. /// <summary>
  403. /// 根据GUID查询用户
  404. /// </summary>
  405. /// <returns></returns>
  406. [HttpPost("guid")]
  407. public async Task<UserDetail> FindUserByGuid(FindUserByGuidRequest request)
  408. {
  409. var user = await _userRepository.GetGuidAsync(request.Guid);
  410. return user;
  411. }
  412. /// <summary>
  413. /// 获取用户工作模块未读数据
  414. /// </summary>
  415. /// <returns></returns>
  416. [HttpGet("user-uread-count")]
  417. public async Task<UserCountResult> GetUserCountAsync()
  418. {
  419. return await _userService.GetUserCountAsync(_loginContext.AccountId);
  420. }
  421. /// <summary>
  422. /// 根据用户名获取电脑上传的数据
  423. /// </summary>
  424. /// <returns></returns>
  425. [HttpGet("user-file-library")]
  426. public async Task<IEnumerable<FileLibraryResult>> GetFileLibraryByUserIdAsync()
  427. {
  428. return await fileLibraryRepository.GetFileLibraryByUserIdAsync(_loginContext.AccountId);
  429. }
  430. }
  431. }