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.

189 lines
7.0 KiB

using Elight.Entity;
using Elight.Utility.Code;
using Elight.Utility.Encrypt;
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>
[ApiController]
[Route("api/Login")]
public class LoginController : ControllerBase
{
private readonly IConfiguration _configuration;
private readonly SqlSugarClient _db;//<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;
}
[HttpPost]
[Route("Login")]
public async Task<Result> Login(UserLogin login)
{
var Passmd5 = Md5.Encrypt32(login.Password).ToLower();
var date = await _db.Queryable<App_Sys_UserModel>().Where(q => q.IsDeleted == 0 && q.isdeactivate == 0 && q.phone == login.phone).FirstAsync();
if (date != null)
{
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;
}
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;
}
#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: "https://127.0.0.1:7246",
audience: "https://127.0.0.1:7246",
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
{
name = date.name,
sex = date.sex,
phone = date.phone,
photo = date.photo,
duties = date.duties,
unitCode = "",
department = "",
token = tokenString
};
#endregion
ret.IsSucceed = true;
}
else
{
ret.IsSucceed = false;
ret.Message = "<EFBFBD>˺Ų<EFBFBD><EFBFBD><EFBFBD><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: "https://127.0.0.1:7246",
audience: "https://127.0.0.1:7246",
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
{
name = date.name,
sex = date.sex,
phone = date.phone,
photo = date.photo,
duties = date.duties,
unitCode = "",
department = "",
token = tokenString
};
#endregion
ret.IsSucceed = true;
}
else
{
ret.IsSucceed = false;
ret.Message = <EFBFBD><EFBFBD>δ<EFBFBD><EFBFBD>Ȩ<EFBFBD><EFBFBD>";
}
return ret;
}
}
public class UserLogin
{
[DataMember]
public string phone { get; set; }
/// <summary>
/// <EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
[DataMember]
public string Password { get; set; }
}
}