UserController.cs 17 KB

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