12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Security.Claims;
- using System.Threading.Tasks;
- using Wms.Screen.Service.IService;
- namespace Wms.Screen.Api.Controllers
- {
- [Route("api/[controller]/[action]")]
- [ApiController]
- public class BaseApiController: ControllerBase
- {
- public IDtoConvertService DtoConvertService { get; set; }
- private IHttpContextAccessor _httpContextAccessor;
- public BaseApiController(IHttpContextAccessor httpContextAccessor)
- {
- _httpContextAccessor = httpContextAccessor;
- }
- public BaseApiController()
- { }
- private object _lockObj1 = new object();
- private long _currentUserId { get; set; }
- public long CurrentUserId
- {
- get
- {
- if (_currentUserId > 0)
- {
- return _currentUserId;
- }
- else
- {
- lock (_lockObj1)
- {
- if (_currentUserId <= 0)
- {
- if (_httpContextAccessor != null && _httpContextAccessor.HttpContext != null && _httpContextAccessor.HttpContext.User != null)
- {
- _currentUserId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier) != null ? long.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value) : 0;
- }
- else
- {
- _currentUserId = 0;
- }
- }
- }
- }
- return _currentUserId;
- }
- }
- protected T AddOrUpdateSign<T>(T reqEntity)
- {
- var userId = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier) != null ? _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value : "0";
- //var userName = _httpContextAccessor.HttpContext.User.FindFirst("UserName") != null ? _httpContextAccessor.HttpContext.User.FindFirst("UserName").Value : "";
- Type entityType = reqEntity.GetType();
- object Id = entityType.GetProperty("Id").GetValue(reqEntity);
- long id = Convert.ToInt64(Id);
- if (id > 0)//update
- {
- PropertyInfo updatedUserIdPropertyInfo = entityType.GetProperty("UpdatedUserId");
- updatedUserIdPropertyInfo.SetValue(reqEntity, userId, null);
- //PropertyInfo updatedUserNamePropertyInfo = entityType.GetProperty("UpdatedUserName");
- //updatedUserNamePropertyInfo.SetValue(reqEntity, userName, null);
- PropertyInfo updatedTimePropertyInfo = entityType.GetProperty("UpdatedTime");
- updatedTimePropertyInfo.SetValue(reqEntity, DateTime.Now, null);
- }
- else //add
- {
- //PropertyInfo propid = entityType.GetProperty("Id");
- //propid.SetValue(reqEntity, IdFactory.NewId().ToString(), null);
- PropertyInfo createdUserIdPropertyInfo = entityType.GetProperty("CreatedUserId");
- createdUserIdPropertyInfo.SetValue(reqEntity, userId, null);
- //PropertyInfo createdUserNamePropertyInfo = entityType.GetProperty("CreatedUserName");
- //createdUserNamePropertyInfo.SetValue(reqEntity, userName, null);
- PropertyInfo createdTimePropertyInfo = entityType.GetProperty("CreatedTime");
- createdTimePropertyInfo.SetValue(reqEntity, DateTime.Now, null);
- PropertyInfo updatedUserIdPropertyInfo = entityType.GetProperty("UpdatedUserId");
- updatedUserIdPropertyInfo.SetValue(reqEntity, userId, null);
- //PropertyInfo updatedUserNamePropertyInfo = entityType.GetProperty("UpdatedUserName");
- //updatedUserNamePropertyInfo.SetValue(reqEntity, userName, null);
- PropertyInfo updatedTimePropertyInfo = entityType.GetProperty("UpdatedTime");
- updatedTimePropertyInfo.SetValue(reqEntity, DateTime.Now, null);
- }
- return reqEntity;
- }
- }
- }
|