UserController.cs 16 KB

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