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
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> |
|
/// 登录 |
|
/// </summary> |
|
[HiddenApi] |
|
[ApiController] |
|
[Route("api/APP")] |
|
public class LoginController : ControllerBase |
|
{ |
|
private readonly IConfiguration _configuration; |
|
private readonly SqlSugarClient _db;//数据库 |
|
private readonly WriteSysLog _logs;//操作日志 |
|
private readonly ILogger<LoginController> _logger;//日志 |
|
Result ret = new Result(); |
|
public LoginController(ILogger<LoginController> logger, SqlSugarClient db, IConfiguration configuration) |
|
{ |
|
_logger = logger; |
|
_db = db; |
|
_configuration = configuration; |
|
} |
|
/// <summary> |
|
/// 系统登录 |
|
/// </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 = "账号不存在,请先注册!"; |
|
return ret; |
|
} |
|
if (Passmd5 != date.Password) |
|
{ |
|
ret.IsSucceed = false; |
|
ret.Message = "账号或者密码错误!"; |
|
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生成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, "登录"); |
|
} |
|
return ret; |
|
} |
|
|
|
/// <summary> |
|
/// app--登录 |
|
/// </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 = "账号不存在,请先注册!"; |
|
return ret; |
|
} |
|
if (Passmd5 != date.Password) |
|
{ |
|
ret.IsSucceed = false; |
|
ret.Message = "账号或密码错误!"; |
|
return ret; |
|
} |
|
//else if (date.usertype == 1 && date.audit == null) |
|
//{ |
|
// ret.IsSucceed = false; |
|
// ret.Message = "账号审核中!"; |
|
// return ret; |
|
//} |
|
//else if (date.usertype == 1 && date.audit == 1) |
|
//{ |
|
// ret.IsSucceed = false; |
|
// ret.Message = $"账号审核未通过,原因:{date.describe}!"; |
|
// return ret; |
|
//} |
|
else if (date.usertype == 0 && date.becurrent == 1) |
|
{ |
|
ret.IsSucceed = false; |
|
ret.Message = $"账号未授权无法登录,请联系管理员!"; |
|
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生成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, "登录"); |
|
} |
|
return ret; |
|
} |
|
|
|
/// <summary> |
|
/// 微信授权登录 |
|
/// </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 = "账号审核中!"; |
|
// return ret; |
|
//} |
|
//else if (date.usertype == 1 && date.audit == 1) |
|
//{ |
|
// ret.IsSucceed = false; |
|
// ret.Message = $"账号审核未通过,原因:{date.describe}!"; |
|
// return ret; |
|
//} |
|
//else |
|
if (date.usertype == 0 && date.becurrent == 1) |
|
{ |
|
ret.IsSucceed = false; |
|
ret.Message = $"账号未授权无法登录,请联系管理员!"; |
|
return ret; |
|
} |
|
#region jwt生成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 = "微信未授权!"; |
|
} |
|
return ret; |
|
} |
|
|
|
/// <summary> |
|
/// APP添加用户 |
|
/// </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 = "电话号码已存在!"; |
|
return ret; |
|
} |
|
_db.BeginTran(); |
|
UserModel.Id = Guid.NewGuid().ToString(); |
|
UserModel.usertype = 1; |
|
//默认密码 |
|
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 = "添加成功"; |
|
} |
|
} |
|
catch (System.Exception ex) |
|
{ |
|
_db.RollbackTran(); |
|
ret.IsSucceed = false; |
|
ret.Message = ex.Message; |
|
} |
|
return ret; |
|
} |
|
|
|
/// <summary> |
|
/// 单位list集合 |
|
/// </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> |
|
/// 获取设备视频流地址 |
|
/// </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 = "用户审核中!"; |
|
// return ret; |
|
// } |
|
// else if (date.usertype == 1 && date.audit == 1) |
|
// { |
|
// ret.IsSucceed = false; |
|
// ret.Message = $"用户审核未通过,原因:{date.describe}!"; |
|
// return ret; |
|
// } |
|
// else |
|
if (date.usertype == 0 && date.becurrent == 1) |
|
{ |
|
ret.IsSucceed = false; |
|
ret.Message = $"用户未授权无法登录,请联系管理员!"; |
|
return ret; |
|
} |
|
#region jwt生成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); |
|
//默认密码身份证后六位 |
|
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 = "添加成功"; |
|
} |
|
} |
|
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> |
|
/// 登录密码 |
|
/// </summary> |
|
[DataMember] |
|
public string Password { get; set; } |
|
} |
|
} |