UserController.cs 16 KB

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