using CommonUtil;
|
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Mvc;
|
using Operater.DAL;
|
using Operater.DbModel;
|
using Operater.DTO.System;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Threading.Tasks;
|
|
namespace Api.Operater.Controllers
|
{
|
[Route(TopConstants.API_ROUTE)]
|
public class LoginController : BaseController
|
{
|
/// <summary>
|
/// 登录
|
/// </summary>
|
/// <param name="request"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public IActionResult LoginCheck([FromBody] LoginCheckRequest request)
|
{
|
// string pwdEn = SecurityUtil.MD5Encrypt(request.Pwd, false);
|
var userInfo = new UserInfoDAL().ListGet(t => t.UserName == request.UserName && t.Pwd == request.Pwd);
|
if (userInfo.IsNull() || userInfo.Count == 0)
|
{
|
return Error("账号或密码错误!");
|
}
|
LoginCheckResponse loginCheckResponse = new LoginCheckResponse()
|
{
|
UserId = userInfo[0].Id
|
};
|
//生成token
|
string enKey = "ophwuv2E1qyy6R2r";
|
TokenUserInfo tokenUserInfo = new TokenUserInfo()
|
{
|
Id = loginCheckResponse.UserId,
|
ExpirationTime = DateTime.Now.AddHours(5)
|
};
|
string EncryStr = SecurityUtil.AesEncrypt(JSONUtil.ObjectToJson(tokenUserInfo), enKey, "0102030405060708");
|
loginCheckResponse.LoginToken = EncryStr;
|
return Success(loginCheckResponse);
|
}
|
|
|
|
public IActionResult AddUser([FromBody] AddUserRequest request)
|
{
|
string userId = string.Empty;
|
while (true)
|
{
|
userId = GetRandomString(6);
|
if (userId.StartsWith("0"))
|
{
|
continue;
|
}
|
var hasExit = new UserInfoDAL().GetById(userId);
|
if (!hasExit.IsNull() && !hasExit.Id.IsNullOrEmpty())
|
{
|
continue;
|
}
|
break;
|
}
|
string enPwd = SecurityUtil.MD5Encrypt(request.Pwd, false);
|
UserInfo userInfo = new UserInfo()
|
{
|
Id = userId,
|
UserName = request.UserName,
|
Pwd = enPwd,
|
Mobile = request.Mobile
|
};
|
var res = new UserInfoDAL().Insert(userInfo);
|
return Success(res);
|
}
|
|
public string GetRandomString(int len)
|
{
|
string s = "1234567890";
|
string reValue = string.Empty;
|
Random rd = new Random();
|
while (reValue.Length < len)
|
{
|
string s1 = s[rd.Next(0, s.Length)].ToString();
|
if (reValue.IndexOf(s1) == -1)
|
reValue += s1;
|
}
|
return reValue;
|
}
|
}
|
}
|