24小时一体机服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

491 lines
19 KiB

using _24Hour.Model;
using com.sun.org.apache.bcel.@internal.generic;
using com.sun.xml.@internal.ws.api.model;
using Elight.Entity;
using Elight.Logic;
using Elight.Logic.SystemModel;
using Elight.Utility;
using Elight.Utility.Code;
using Elight.Utility.Encrypt;
using Elight.Utility.Extensions;
using Elight.Utility.logs;
using java.security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using SqlSugar;
using System.IdentityModel.Tokens.Jwt;
using System.Runtime.Serialization;
using System.Security.Claims;
using System.Text;
namespace _24Hour.Controllers
{
/// <summary>
/// <EFBFBD><EFBFBD>¼
/// </summary>
[HiddenApi]
[ApiController]
[Route("api/APP")]
public class LoginController : ControllerBase
{
private readonly IConfiguration _configuration;
private readonly SqlSugarClient _db;//<EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD>
private readonly WriteSysLog _logs;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
private readonly ILogger<LoginController> _logger;//<EFBFBD><EFBFBD>־
Result ret = new Result();
public LoginController(ILogger<LoginController> logger, SqlSugarClient db, IConfiguration configuration)
{
_logger = logger;
_db = db;
_configuration = configuration;
}
/// <summary>
/// ϵͳ<EFBFBD><EFBFBD>¼
/// </summary>
/// <param name="login"></param>
/// <returns></returns>
[HttpPost]
[Route("SystemLogin")]
[CustomCorsActionFilterAttribute]
public async Task<Result> SystemLogin(UserLogin login)
{
try
{
var Passmd5 = Md5.Encrypt32(login.Password).ToLower();
var date = await _db.Queryable<App_Sys_UserModel>().Where(x => x.phone== login.phone && x.IsDeleted==0).FirstAsync();
if (date == null)
{
ret.IsSucceed = false;
ret.Message = "<EFBFBD>˺Ų<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>ᣡ";
return ret;
}
if (Passmd5 != date.Password)
{
ret.IsSucceed = false;
ret.Message = "<EFBFBD>˺Ż<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
return ret;
}
if (!string.IsNullOrEmpty(date.department))
{
var dept = await _db.Queryable<App_Sys_DepartModel>().Where(x => x.Id== date.department && x.IsDelete==0).FirstAsync();
if (dept!= null)
{
date.departmentName= dept.departname;
}
}
#region jwt<EFBFBD><EFBFBD><EFBFBD><EFBFBD>token
var tokenHandler = new JwtSecurityTokenHandler();
var claims = new Claim[]
{
new Claim(ClaimTypes.UserData,JsonConvert.SerializeObject(date)),
};
var key = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_configuration.GetSection("JwtConfiguration:Jwtkey").Value));
var signingCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
//Token
var jwttoken = new JwtSecurityToken(
issuer: _configuration.GetSection("JwtConfiguration:Issuer").Value,
audience: _configuration.GetSection("JwtConfiguration:Audience").Value,
claims: claims,
notBefore: DateTime.Now,
expires: DateTime.Now.AddDays(1),
signingCredentials: signingCredentials
);
//var token = new JwtSecurityTokenHandler().CreateToken(jwttoken);
var tokenString = new JwtSecurityTokenHandler().WriteToken(jwttoken);
ret.IsSucceed=true;
ret.result = new
{
Id = date.Id,
name = date.name,
sex = date.sex,
phone = date.phone,
photo = date.photo,
duties = date.duties,
identity = date.identity,
unitCode = date.unitCode,
department = date.department,
departmentName = date.departmentName,
token = tokenString
};
#endregion
}
catch (Exception ex)
{
ret.IsSucceed = false;
ret.Message = $"{ex.Message}";
LogService.WriteLog(ex, "<EFBFBD><EFBFBD>¼");
}
return ret;
}
/// <summary>
/// app--<EFBFBD><EFBFBD>¼
/// </summary>
/// <param name="login"></param>
/// <returns></returns>
[HttpPost]
[Route("Login")]
[CustomCorsActionFilterAttribute]
public async Task<Result> Login(UserLogin login)
{
try
{
var Passmd5 = Md5.Encrypt32(login.Password).ToLower();
var date = await _db.Queryable<App_Sys_UserModel>().Where(x => x.phone== login.phone && x.IsDeleted==0).FirstAsync();
if (date == null)
{
ret.IsSucceed = false;
ret.Message = "<EFBFBD>˺Ų<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD>ᣡ";
return ret;
}
if (Passmd5 != date.Password)
{
ret.IsSucceed = false;
ret.Message = "<EFBFBD>˺Ż<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
return ret;
}
//else if (date.usertype == 1 && date.audit == null)
//{
// ret.IsSucceed = false;
// ret.Message = "<EFBFBD>˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>";
// return ret;
//}
//else if (date.usertype == 1 && date.audit == 1)
//{
// ret.IsSucceed = false;
// ret.Message = $"<EFBFBD>˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>{date.describe}<EFBFBD><EFBFBD>";
// return ret;
//}
else if (date.usertype == 0 && date.becurrent == 1)
{
ret.IsSucceed = false;
ret.Message = $"<EFBFBD>˺<EFBFBD>δ<EFBFBD><EFBFBD>Ȩ<EFBFBD>޷<EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD>";
return ret;
}
if (!string.IsNullOrEmpty(date.department))
{
var dept = await _db.Queryable<App_Sys_DepartModel>().Where(x => x.Id== date.department && x.IsDelete==0).FirstAsync();
if (dept!= null)
{
date.departmentName= dept.departname;
}
}
#region jwt<EFBFBD><EFBFBD><EFBFBD><EFBFBD>token
var tokenHandler = new JwtSecurityTokenHandler();
var claims = new Claim[]
{
new Claim(ClaimTypes.UserData,JsonConvert.SerializeObject(date)),
};
var key = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_configuration.GetSection("JwtConfiguration:Jwtkey").Value));
var signingCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
//Token
var jwttoken = new JwtSecurityToken(
issuer: _configuration.GetSection("JwtConfiguration:Issuer").Value,
audience: _configuration.GetSection("JwtConfiguration:Audience").Value,
claims: claims,
notBefore: DateTime.Now,
expires: DateTime.Now.AddDays(1),
signingCredentials: signingCredentials
);
//var token = new JwtSecurityTokenHandler().CreateToken(jwttoken);
var tokenString = new JwtSecurityTokenHandler().WriteToken(jwttoken);
ret.result = new
{
Id = date.Id,
name = date.name,
sex = date.sex,
phone = date.phone,
photo = date.photo,
duties = date.duties,
identity = date.identity,
usertype = date.usertype,
unitCode = date.unitCode,
department = date.department,
departmentName = date.departmentName,
token = tokenString
};
#endregion
ret.IsSucceed = true;
}
catch (Exception ex)
{
ret.IsSucceed = false;
ret.Message = $"{ex.Message}";
LogService.WriteLog(ex, "<EFBFBD><EFBFBD>¼");
}
return ret;
}
/// <summary>
/// ΢<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȩ<EFBFBD><EFBFBD>¼
/// </summary>
/// <param name="openId"></param>
/// <returns></returns>
[HttpGet]
[Route("WeChatLogin")]
public async Task<Result> WeChatLogin(string openId)
{
var date = await _db.Queryable<App_Sys_UserModel>().Where(q => q.IsDeleted == 0 && q.isdeactivate == 0 && q.wechatId == openId).FirstAsync();
if (date != null)
{
//if (date.usertype == 1 && date.audit == null)
//{
// ret.IsSucceed = false;
// ret.Message = "<EFBFBD>˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>";
// return ret;
//}
//else if (date.usertype == 1 && date.audit == 1)
//{
// ret.IsSucceed = false;
// ret.Message = $"<EFBFBD>˺<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>{date.describe}<EFBFBD><EFBFBD>";
// return ret;
//}
//else
if (date.usertype == 0 && date.becurrent == 1)
{
ret.IsSucceed = false;
ret.Message = $"<EFBFBD>˺<EFBFBD>δ<EFBFBD><EFBFBD>Ȩ<EFBFBD>޷<EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD>";
return ret;
}
#region jwt<EFBFBD><EFBFBD><EFBFBD><EFBFBD>token
var tokenHandler = new JwtSecurityTokenHandler();
var claims = new Claim[]
{
new Claim(ClaimTypes.UserData,JsonConvert.SerializeObject(date)),
};
var key = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_configuration.GetSection("JwtConfiguration:Jwtkey").Value));
var signingCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
//Token
var jwttoken = new JwtSecurityToken(
issuer: _configuration.GetSection("JwtConfiguration:Issuer").Value,
audience: _configuration.GetSection("JwtConfiguration:Audience").Value,
claims: claims,
notBefore: DateTime.Now,
expires: DateTime.Now.AddDays(1),
signingCredentials: signingCredentials
);
//var token = new JwtSecurityTokenHandler().CreateToken(jwttoken);
var tokenString = new JwtSecurityTokenHandler().WriteToken(jwttoken);
ret.result = new
{
Id = date.Id,
name = date.name,
sex = date.sex,
phone = date.phone,
photo = date.photo,
duties = date.duties,
identity = date.identity,
usertype = date.usertype,
unitCode = date.unitCode,
token = tokenString
};
#endregion
ret.IsSucceed = true;
}
else
{
ret.IsSucceed = false;
ret.Message = <EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD>Ȩ<EFBFBD><EFBFBD>";
}
return ret;
}
/// <summary>
/// APP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
[HttpPost]
[Route("AddUser1")]
public async Task<Result> AddUser1(App_Sys_UserModel UserModel)
{
try
{
var data = await _db.Queryable<App_Sys_UserModel>().Where(q => q.phone == UserModel.phone).FirstAsync();
if (data != null)
{
ret.IsSucceed = false;
ret.Message = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѵ<EFBFBD><EFBFBD>ڣ<EFBFBD>";
return ret;
}
_db.BeginTran();
UserModel.Id = Guid.NewGuid().ToString();
UserModel.usertype = 1;
//Ĭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
UserModel.Password = Elight.Utility.Encrypt.Md5.Encrypt32($"{UserModel.Password}").ToLower();
var num = await _db.Insertable(UserModel).ExecuteCommandAsync();
_db.CommitTran();
if (num > 0)
{
ret.IsSucceed = true;
ret.result = "<EFBFBD><EFBFBD><EFBFBD>ӳɹ<EFBFBD>";
}
}
catch (System.Exception ex)
{
_db.RollbackTran();
ret.IsSucceed = false;
ret.Message = ex.Message;
}
return ret;
}
/// <summary>
/// <EFBFBD><EFBFBD>λlist<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="info"></param>
/// <returns></returns>
[HttpPost]
[Route("QueryUnitlist")]
public async Task<Result> QueryUnitlist(App_Sys_UnitInput Unitdata)
{
var list = await _db.Queryable<App_Sys_UnitModel>()
.WhereIF(!Unitdata.unitCode.IsNull(), q => q.unitCode.Contains(Unitdata.unitCode))
.WhereIF(!Unitdata.unitname.IsNull(), q => q.unitname.Contains(Unitdata.unitname))
.WhereIF(!Unitdata.unitjc.IsNull(), q => q.unitjc.Contains(Unitdata.unitjc))
.Where(q => q.IsDelete == 0).ToListAsync();
ret.IsSucceed = true;
ret.result = list;
return ret;
}
/// <summary>
/// <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
/// </summary>
/// <param name="Sendingdata"></param>
/// <returns></returns>
[HttpGet]
[Route("getrtsp")]
public async Task<Result> getrtsp()
{
ret.IsSucceed = true;
ret.result = $"{_configuration.GetSection("Videoaddress:rtsp").Value}";
return ret;
}
[HttpGet]
[Route("cardIdLogin")]
public async Task<Result> cardIdLogin(string cardId, string name)
{
var date = await _db.Queryable<App_Sys_UserModel>().Where(q => q.IsDeleted == 0 && q.isdeactivate == 0 && q.cardId == cardId).FirstAsync();
if (date != null)
{
//if (date.usertype == 1 && date.audit == null)
// {
// ret.IsSucceed = false;
// ret.Message = "<EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>";
// return ret;
// }
// else if (date.usertype == 1 && date.audit == 1)
// {
// ret.IsSucceed = false;
// ret.Message = $"<EFBFBD>û<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>{date.describe}<EFBFBD><EFBFBD>";
// return ret;
// }
// else
if (date.usertype == 0 && date.becurrent == 1)
{
ret.IsSucceed = false;
ret.Message = $"<EFBFBD>û<EFBFBD>δ<EFBFBD><EFBFBD>Ȩ<EFBFBD>޷<EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD>";
return ret;
}
#region jwt<EFBFBD><EFBFBD><EFBFBD><EFBFBD>token
var tokenHandler = new JwtSecurityTokenHandler();
var claims = new Claim[]
{
new Claim(ClaimTypes.UserData,JsonConvert.SerializeObject(date)),
};
var key = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(_configuration.GetSection("JwtConfiguration:Jwtkey").Value));
var signingCredentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
//Token
var jwttoken = new JwtSecurityToken(
issuer: _configuration.GetSection("JwtConfiguration:Issuer").Value,
audience: _configuration.GetSection("JwtConfiguration:Audience").Value,
claims: claims,
notBefore: DateTime.Now,
expires: DateTime.Now.AddDays(1),
signingCredentials: signingCredentials
);
//var token = new JwtSecurityTokenHandler().CreateToken(jwttoken);
var tokenString = new JwtSecurityTokenHandler().WriteToken(jwttoken);
ret.result = new
{
Id = date.Id,
name = date.name,
sex = date.sex,
phone = date.phone,
photo = date.photo,
duties = date.duties,
identity = date.identity,
usertype = date.usertype,
unitCode = date.unitCode,
token = tokenString
};
#endregion
ret.IsSucceed = true;
}
else
{
var UserModel = new App_Sys_UserModel();
_db.BeginTran();
UserModel.Id = Guid.NewGuid().ToString();
UserModel.cardId = cardId;
UserModel.name = name;
UserModel.usertype = 1;
string cardIdpwa = UserModel.cardId.Substring(UserModel.cardId.Length - 6);
//Ĭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
UserModel.Password = Elight.Utility.Encrypt.Md5.Encrypt32($"{cardIdpwa}").ToLower();
var num = await _db.Insertable(UserModel).ExecuteCommandAsync();
_db.CommitTran();
if (num > 0)
{
ret.IsSucceed = true;
ret.result = "<EFBFBD><EFBFBD><EFBFBD>ӳɹ<EFBFBD>";
}
}
return ret;
}
/// <summary>
/// download file for video repot module
/// </summary>
/// <returns></returns>
[HttpGet, AllowAnonymous]
[Route("download")]
public IActionResult DownloadFile(string filepath)
{
var fileaddr = Path.Combine(Environment.CurrentDirectory,"wwwroot");
fileaddr += filepath;
if (System.IO.File.Exists(fileaddr) == false)
{
return new BadRequestResult();
}
return new FileStreamResult(new FileStream(path: fileaddr, FileMode.Open), "application/octet-stream")
{
FileDownloadName = Path.GetFileName(fileaddr),
};
}
}
public class UserLogin
{
[DataMember]
public string phone { get; set; }
/// <summary>
/// <EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
[DataMember]
public string Password { get; set; }
}
}