123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
-
- using OASystem.Infrastructure.Repositories.Login;
- using System.IdentityModel.Tokens.Jwt;
- using System.Security.Claims;
- using OASystem.Domain.Dtos.UserDto;
- using OASystem.API.OAMethodLib;
- using Serilog.Parsing;
- using OASystem.Domain.Dtos.System;
- using System.Drawing.Drawing2D;
- using System.Collections;
- using OASystem.API.OAMethodLib.JuHeAPI;
- using OASystem.API.OAMethodLib.QiYeWeChatAPI;
- using OASystem.Domain.Dtos.QiYeWeChat;
- using OASystem.Domain.Entities.System;
- using TinyPinyin;
- namespace OASystem.API.Controllers
- {
- /// <summary>
- /// 鉴权相关
- /// </summary>
- [Route("api/")]
- public class AuthController : ControllerBase
- {
- private readonly IMapper _mapper;
- private readonly IConfiguration _config;
- private readonly LoginRepository _loginRep;
- private readonly MessageRepository _message;
- private readonly SystemMenuPermissionRepository _SystemMenuPermissionRepository;
- private readonly IQiYeWeChatApiService _qiYeWeChatApiServic;
- public AuthController(IConfiguration config, LoginRepository loginRep, IMapper mapper,MessageRepository message,
- SystemMenuPermissionRepository systemMenuPermissionRepository, IQiYeWeChatApiService qiYeWeChatApiService)
- {
- _config = config;
- _loginRep = loginRep;
- _mapper = mapper;
- _message = message;
- _SystemMenuPermissionRepository = systemMenuPermissionRepository;
- _qiYeWeChatApiServic = qiYeWeChatApiService;
- }
- /// <summary>
- /// 用户登录
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- [Route("login")]
- [HttpPost]
- [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
- public async Task<IActionResult> LoginAsync(LoginDto dto)
- {
- #region 校验用户信息
- var userData = _loginRep.Login(dto).Result;
- if (userData.Code != 0)
- {
- if (userData.Code != 0) { return Ok(JsonView(false, userData.Msg)); }
- return Ok(JsonView(false, "暂无该员工信息!"));
- }
- #endregion
- Result authData = null;
- if (userData.Data != null)
- {
- var uid = (userData.Data as UserLoginInfoView).UserId;
- authData = _SystemMenuPermissionRepository.QueryMenuLoad(uid, dto.PortType);
- }
- var view = new LoginView
- {
- UserInfo = userData == null ? null : userData.Data,
- AuthData = authData == null ? null : authData.Data,
- };
- DateTime createZebraTime = DateTime.Now;
- string authorId = dto.Number + "Token";
- string authorToken = await RedisRepository.RedisFactory.CreateRedisRepository().StringGetAsync<string>(authorId);//string 取
- if (authorToken != null)
- {
- #region 解析出过期时间
- var jwtHandler = new JwtSecurityTokenHandler();
- JwtSecurityToken securityToken = jwtHandler.ReadJwtToken(authorToken);
- DateTime expDt = (securityToken.Payload[JwtRegisteredClaimNames.Exp] ?? 0).GetInt().GetTimeSpmpToDate();
- #endregion
- if (expDt >= createZebraTime) //超时重新获取token
- {
- authorToken = GeneralMethod.GetToken(_config, dto.Number, createZebraTime);
- }
- view.Expires = expDt;
- view.Token = authorToken;
- }
- else
- {
- view.Expires = createZebraTime.AddMinutes(30);
- view.Token = GeneralMethod.GetToken(_config, dto.Number, createZebraTime);
- TimeSpan ts = view.Expires.AddMinutes(-1) - createZebraTime; //设置redis 过期时间 比 jwt 时间 快一分钟
- await RedisRepository.RedisFactory.CreateRedisRepository().StringSetAsync<string>(authorId, view.Token, ts);//string 存
- }
- #region 测试添加系统消息
- //await _message.AddMsg(new MessageDto()
- //{
- // Type = 1,
- // IssuerId = 208,
- // Title = "测试添加消息标题",
- // Content = "消息体测试",
- // ReleaseTime = DateTime.Now,
- // UIdList = new List<int> {
- // 5,
- // 208,
- // 219
- // }
- //});
- #endregion
- return Ok(JsonView(view));
- }
- /// <summary>
- /// 申请注册 数据Data
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- //[Authorize]
- [HttpPost]
- [Route("register/daraSource")]
- public async Task<IActionResult> RegisterDataSource()
- {
- string sql = string.Format(@"Select sc.Id CompanyId,sc.CompanyName,sd.Id DepId,sd.DepName,sjp.Id JobId,sjp.JobName From Sys_Company sc
- Left Join Sys_Department sd On sd.IsDel = 0 And sc.Id = sd.CompanyId
- Left Join Sys_JobPost sjp On sjp.IsDel = 0 And sjp.DepId = sd.Id
- Where sc.IsDel = 0");
- var companyDetails = _loginRep._sqlSugar.SqlQueryable<CompanyDetailsView>(sql).ToList();
- List<CompanyDetailsView1> detailsView1 = new List<CompanyDetailsView1>();
- if (companyDetails.Count > 0)
- {
- var companyDetails1 = companyDetails.GroupBy(it => it.CompanyId).Select(it => it.First()).ToList();
- detailsView1 = companyDetails1.Select(it =>
- {
- CompanyDetailsView1 itemCompany = new CompanyDetailsView1();
- List<DepDetailsView> depDetailsView = new List<DepDetailsView>();
- var companyDetails2 = companyDetails.GroupBy(it => it.DepId).Select(it => it.First()).ToList();
- //部门
- depDetailsView = companyDetails2.Where(depIt => depIt.CompanyId == it.CompanyId).Select(depIt => {
- DepDetailsView depDetails = new DepDetailsView();
- List<JobDetailsView> jobDetails = new List<JobDetailsView>();
- //岗位
- jobDetails = companyDetails.Where(jobIt => jobIt.DepId == depIt.DepId).Select(jobIt => {
- JobDetailsView jobDetail = new JobDetailsView() {
- JobId = jobIt.JobId,
- JobName = jobIt.JobName,
- };
- return jobDetail;
- }).ToList();
- depDetails.DepId = depIt.DepId;
- depDetails.DepName = depIt.DepName;
- depDetails.SubJob = jobDetails;
- return depDetails;
- }).ToList();
- itemCompany.CompanyId = it.CompanyId;
- itemCompany.CompanyName = it.CompanyName;
- itemCompany.SubDep = depDetailsView;
- return itemCompany;
- }).ToList();
- }
- return Ok(new { Code = 200, Msg = "查询成功!", Data = detailsView1 });
- }
- /// <summary>
- /// 申请注册
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- //[Authorize]
- [HttpPost]
- [Route("register")]
- public async Task<IActionResult> Register(RegisterDto dto)
- {
- #region 企业微信添加员工
- //string lastName = dto.CnName.Substring(0, 1);
- //string lastNamePy = string.Empty;
- //if (PinyinHelper.IsChinese(Convert.ToChar(lastName)))
- //{
- // lastNamePy = PinyinHelper.GetPinyin(lastName);
- //}
- //string userId = string.Format("{0}.{1}", dto.EnName, lastNamePy.ToLower());
- //Create_Request request = new Create_Request()
- //{
- // userid = userId,
- // name = dto.CnName,
- // mobile = dto.Phone,
- // department = new List<long>() { dto.DepId },
- // position = dto.JobPostId.ToString(),
- // gender = dto.Sex == 0 ? 1 : dto.Sex == 1 ? 2 : 1,
- // biz_mail = dto.Email
- //};
- //var qiYeWeChatCreateData = await _qiYeWeChatApiServic.CreateAsync(request);
- #endregion
- var userData = _loginRep.Register(dto);
- if (userData.Result.Code != 0)
- {
- return Ok(JsonView(false, "注册失败!"));
- }
- return Ok(JsonView(true, userData.Result.Msg));
- }
- /// <summary>
- /// 修改密码
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- [Authorize]
- [HttpPost]
- [Route("UpdPassword")]
- public async Task<IActionResult> UpdateUserPassword(UpdateDto dto)
- {
- Result result = new Result();
- Sys_Users sys_Users = _mapper.Map<Sys_Users>(dto);
- var _UpdateState = await _loginRep.UpdateAsync(s => s.Id == dto.UserId, ss => sys_Users);
- if (_UpdateState)
- {
- result.Code = 0;
- result.Msg = "申请成功!人事主管审核后且信息部经理分配了登录账号,可登录OA!";
- }
- else
- {
- result.Code = -2;
- result.Msg = "用户修改失败!";
- }
- return Ok(JsonView(result));
- }
- /// <summary>
- /// 测试auth
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- [OASystemAuthentication]
- [HttpPost("TestToken")]
- [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
- public async Task<IActionResult> TestToken(LoginDto dto)
- {
- string authorId = dto.Number + "Token";
- // 从Redis里面取数据
- //string userToken = _redis.StringGet(authorId);
- string userToken = "";
- var view = new LoginView
- {
- Token = authorId + ":" + userToken
- };
- return Ok(JsonView(view));
- }
- }
- }
|