BaseApiController.cs 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Security.Claims;
  8. using System.Threading.Tasks;
  9. using Wms.Screen.Service.IService;
  10. namespace Wms.Screen.Api.Controllers
  11. {
  12. [Route("api/[controller]/[action]")]
  13. [ApiController]
  14. public class BaseApiController: ControllerBase
  15. {
  16. public IDtoConvertService DtoConvertService { get; set; }
  17. private IHttpContextAccessor _httpContextAccessor;
  18. public BaseApiController(IHttpContextAccessor httpContextAccessor)
  19. {
  20. _httpContextAccessor = httpContextAccessor;
  21. }
  22. public BaseApiController()
  23. { }
  24. private object _lockObj1 = new object();
  25. private long _currentUserId { get; set; }
  26. public long CurrentUserId
  27. {
  28. get
  29. {
  30. if (_currentUserId > 0)
  31. {
  32. return _currentUserId;
  33. }
  34. else
  35. {
  36. lock (_lockObj1)
  37. {
  38. if (_currentUserId <= 0)
  39. {
  40. if (_httpContextAccessor != null && _httpContextAccessor.HttpContext != null && _httpContextAccessor.HttpContext.User != null)
  41. {
  42. _currentUserId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier) != null ? long.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value) : 0;
  43. }
  44. else
  45. {
  46. _currentUserId = 0;
  47. }
  48. }
  49. }
  50. }
  51. return _currentUserId;
  52. }
  53. }
  54. protected T AddOrUpdateSign<T>(T reqEntity)
  55. {
  56. var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier) != null ? _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value : "0";
  57. //var userName = _httpContextAccessor.HttpContext.User.FindFirst("UserName") != null ? _httpContextAccessor.HttpContext.User.FindFirst("UserName").Value : "";
  58. Type entityType = reqEntity.GetType();
  59. object Id = entityType.GetProperty("Id").GetValue(reqEntity);
  60. long id = Convert.ToInt64(Id);
  61. if (id > 0)//update
  62. {
  63. PropertyInfo updatedUserIdPropertyInfo = entityType.GetProperty("UpdatedUserId");
  64. updatedUserIdPropertyInfo.SetValue(reqEntity, userId, null);
  65. //PropertyInfo updatedUserNamePropertyInfo = entityType.GetProperty("UpdatedUserName");
  66. //updatedUserNamePropertyInfo.SetValue(reqEntity, userName, null);
  67. PropertyInfo updatedTimePropertyInfo = entityType.GetProperty("UpdatedTime");
  68. updatedTimePropertyInfo.SetValue(reqEntity, DateTime.Now, null);
  69. }
  70. else //add
  71. {
  72. //PropertyInfo propid = entityType.GetProperty("Id");
  73. //propid.SetValue(reqEntity, IdFactory.NewId().ToString(), null);
  74. PropertyInfo createdUserIdPropertyInfo = entityType.GetProperty("CreatedUserId");
  75. createdUserIdPropertyInfo.SetValue(reqEntity, userId, null);
  76. //PropertyInfo createdUserNamePropertyInfo = entityType.GetProperty("CreatedUserName");
  77. //createdUserNamePropertyInfo.SetValue(reqEntity, userName, null);
  78. PropertyInfo createdTimePropertyInfo = entityType.GetProperty("CreatedTime");
  79. createdTimePropertyInfo.SetValue(reqEntity, DateTime.Now, null);
  80. PropertyInfo updatedUserIdPropertyInfo = entityType.GetProperty("UpdatedUserId");
  81. updatedUserIdPropertyInfo.SetValue(reqEntity, userId, null);
  82. //PropertyInfo updatedUserNamePropertyInfo = entityType.GetProperty("UpdatedUserName");
  83. //updatedUserNamePropertyInfo.SetValue(reqEntity, userName, null);
  84. PropertyInfo updatedTimePropertyInfo = entityType.GetProperty("UpdatedTime");
  85. updatedTimePropertyInfo.SetValue(reqEntity, DateTime.Now, null);
  86. }
  87. return reqEntity;
  88. }
  89. }
  90. }