123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734 |
-
- 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;
- using System.Globalization;
- using Microsoft.AspNetCore.SignalR;
- using OASystem.API.OAMethodLib.Hub.Hubs;
- using OASystem.API.OAMethodLib.Hub.HubClients;
- using static OASystem.API.OAMethodLib.Hub.Hubs.ChatHub;
- using static OASystem.API.OAMethodLib.JWTHelper;
- using OASystem.Domain.Entities.Groups;
- namespace OASystem.API.Controllers
- {
-
-
-
- [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 MessageRepository _messageRep;
- private readonly IQiYeWeChatApiService _qiYeWeChatApiServic;
- private readonly IHubContext<ChatHub, IChatClient> _hubContext;
- public AuthController(IConfiguration config, LoginRepository loginRep, IMapper mapper,MessageRepository message,
- SystemMenuPermissionRepository systemMenuPermissionRepository, IQiYeWeChatApiService qiYeWeChatApiService, MessageRepository messageRep,
- IHubContext<ChatHub, IChatClient> hubContext)
- {
- _config = config;
- _loginRep = loginRep;
- _mapper = mapper;
- _message = message;
- _SystemMenuPermissionRepository = systemMenuPermissionRepository;
- _qiYeWeChatApiServic = qiYeWeChatApiService;
- _messageRep = messageRep;
- _hubContext = hubContext;
- }
-
-
-
-
-
- [Route("login")]
- [HttpPost]
- [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
- public async Task<IActionResult> LoginAsync(LoginDto dto)
- {
- if (string.IsNullOrWhiteSpace(dto.Number) || string.IsNullOrWhiteSpace(dto.Password))
- {
- return Ok(JsonView(false, "账号或密码不能为空!!"));
- }
- #region 校验用户信息
- var userData = _loginRep.Login(dto).Result;
- if (userData.Code != 0) return Ok(JsonView(false, userData.Msg));
- #endregion
- Result authData = null;
- string uName = string.Empty;
- string role = string.Empty;
- int uId = 0;
- int unReadCount = 0;
- int announcementUnReadCount = 0;
- if (userData.Data != null)
- {
- uId = (userData.Data as UserLoginInfoView).UserId;
- uName = (userData.Data as UserLoginInfoView).CnName;
- role = (userData.Data as UserLoginInfoView).JobName;
- authData = _SystemMenuPermissionRepository.QueryMenuLoad(uId, dto.PortType);
- unReadCount = await _messageRep.GetUnReadCount(uId);
- announcementUnReadCount = await _messageRep.GetAnnouncementUnReadCount(uId);
- }
-
- var view = new LoginView
- {
- UserInfo = userData == null ? null : userData.Data,
- AuthData = authData == null ? null : authData.Data,
- UnReadCount = unReadCount,
- AnnouncementUnReadCount = announcementUnReadCount
- };
- DateTime createZebraTime = DateTime.Now;
- string authorId = dto.Number + "Token";
- string authorToken = await RedisRepository.RedisFactory.CreateRedisRepository().StringGetAsync<string>(authorId);
- 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)
- {
-
- authorToken = await JwtHelper.IssueJwtAsync(new TokenModelJwt() { UserId = uId, UserName = uName, Role = role });
- }
- view.Expires = expDt;
- view.Token = authorToken;
- }
- else
- {
- view.Expires = createZebraTime.AddMinutes(30);
-
- view.Token = await JwtHelper.IssueJwtAsync(new TokenModelJwt() { UserId = uId, UserName = uName, Role = role });
- TimeSpan ts = view.Expires.AddMinutes(-1) - createZebraTime;
- await RedisRepository.RedisFactory.CreateRedisRepository().StringSetAsync<string>(authorId, view.Token, ts);
- }
-
-
-
- #region 测试添加系统消息
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion
- return Ok(JsonView(view));
- }
-
-
-
-
-
-
- [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 });
- }
-
-
-
-
-
-
- [HttpPost]
- [Route("register")]
- public async Task<IActionResult> Register(RegisterDto dto)
- {
- #region 企业微信添加员工
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion
- var userData = _loginRep.Register(dto);
- if (userData.Result.Code != 0)
- {
- return Ok(JsonView(false, userData.Result.Msg));
- }
- return Ok(JsonView(true, userData.Result.Msg));
- }
-
-
-
-
-
- [Authorize]
- [HttpPost]
- [Route("UpdPassword")]
- public async Task<IActionResult> UpdateUserPassword(UpdateDto dto)
- {
-
-
-
- var _view = await _loginRep.ChangePassword(dto.UserId,dto.Password);
- if (_view.Code == 0) return Ok(JsonView(true,"操作成功!"));
- return Ok(JsonView(false, _view.Msg));
- }
-
-
-
-
-
- [OASystemAuthentication]
- [HttpPost("TestToken")]
- [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
- public async Task<IActionResult> TestToken(LoginDto dto)
- {
- string authorId = dto.Number + "Token";
-
-
- string userToken = "";
- var view = new LoginView
- {
- Token = authorId + ":" + userToken
- };
- return Ok(JsonView(view));
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [HttpPost("TestCreateUserNumber")]
- [ProducesResponseType(typeof(LoginView), StatusCodes.Status200OK)]
- public async Task<IActionResult> TestCreateUserNumber(int depId)
- {
- try
- {
- var number = await _loginRep.CreateNumber(depId);
- return Ok(JsonView(true, "操作成功!", number));
- }
- catch (Exception ex)
- {
- return Ok(JsonView(false, "操作失败!", ex.Message));
- }
-
- }
- }
- }
|