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