UserController.cs 16 KB

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