Просмотр исходного кода

api登录流程重构

娄底高职分支
王晓寒 2 недель назад
Родитель
Сommit
a7e36b46c1
2 измененных файлов: 204 добавлений и 210 удалений
  1. +1
    -1
      Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/BaseApi.cs
  2. +203
    -209
      Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/UserApi.cs

+ 1
- 1
Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/BaseApi.cs Просмотреть файл

@@ -180,7 +180,7 @@ namespace Learun.Application.WebApi
ReqParameter req = this.Bind<ReqParameter>();
loginMark = req.loginMark;
token = req.token;
if (path == "/learun/adms/user/login"|| path== "/learun/adms/user/logincodeverify" || path == "/learun/adms/user/loginbyIdCard" || path == "/" || path == "/bgimg" || path == "/learun/adms/user/img" || path == "/learun/adms/desktop/img"||path== "/learun/adms/user/imgfordc" || path == "/learun/adms/StampPersonal/img"||path== "/learun/adms/user/checkupdate")
if (path== "/learun/adms/user/loginverify"||path== "/learun/adms/user/sendmsg" || path == "/learun/adms/user/login"|| path== "/learun/adms/user/logincodeverify" || path == "/learun/adms/user/loginbyIdCard" || path == "/" || path == "/bgimg" || path == "/learun/adms/user/img" || path == "/learun/adms/desktop/img"||path== "/learun/adms/user/imgfordc" || path == "/learun/adms/StampPersonal/img"||path== "/learun/adms/user/checkupdate")
{// 登录接口,默认页面接口不做权限验证处理
return null;
}


+ 203
- 209
Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/UserApi.cs Просмотреть файл

@@ -12,6 +12,10 @@ using Learun.Application.TwoDevelopment.LR_Desktop;
using Learun.Cache.Base;
using Learun.Cache.Factory;
using Quanjiang.DigitalScholl.SendSms;
using static Learun.Application.WebApi.UserApi;
using static Learun.Util.QRCodeHelper;
using System.Security.Principal;
using static Learun.Application.WebApi.SunshineEducationApi;

namespace Learun.Application.WebApi
{
@@ -31,6 +35,8 @@ namespace Learun.Application.WebApi
: base("/learun/adms/user")
{
Post["/login"] = Login;
Post["/loginverify"] = LoginVerify;
Post["/sendmsg"] = LoginSendMessage;
Post["/logincodeverify"] = LoginCodeVerify;
Post["/modifypw"] = ModifyPassword;
Post["/modifypwiden"] = ModifyPasswordiden;
@@ -56,9 +62,147 @@ namespace Learun.Application.WebApi
private readonly ISms yixintongSms = new YixintongSms();
private ICache redisCache = CacheFactory.CaChe();
private Sys_UpdateRecordIBLL sysUpdateRecordIbll = new Sys_UpdateRecordBLL();
AnnexesFileIBLL annexesFileIbll=new AnnexesFileBLL();
AnnexesFileIBLL annexesFileIbll = new AnnexesFileBLL();
/// <summary>
/// 验证是否需要输入验证码
/// </summary>
/// <returns></returns>
private Response LoginVerify(dynamic _)
{
var account = this.GetReqData<UserAccount>();
var isSend = GetAccountIsSendMsg(account.account);
if (!isSend.HasValue)
{
return Fail("请维护用户手机号信息");
}
return Success(new { sendresult = isSend });
}
/// <summary>
/// 发送短信验证码
/// </summary>
/// <returns></returns>
private Response LoginSendMessage(dynamic _)
{
var account = this.GetReqData<UserAccount>();
var isSend = GetAccountIsSendMsg(account.account);
if (!isSend.HasValue)
return Fail("请维护用户手机号信息");

if (!isSend.Value)
return Fail("该用户不需要短信登录");
var userEntity = userIBLL.GetEntityByMobile(account.account);
LogEntity logEntity;
var redisKey = $"studentuserlogin_{userEntity.F_Mobile}";
var msg = redisCache.Read<string>(redisKey);
if (!string.IsNullOrEmpty(msg)) return Fail("原验证码未过期请使用原验证码。");
try
{
//短信发送验证
string raRndNum = CommonHelper.RndNum(6);
var listStr = new List<string> { $"欢迎使用智慧校园,您本次登录的验证码是 {raRndNum}。" };
var result = yixintongSms.SendSmsToSingle(userEntity.F_Mobile, SmsType.LoginBind, listStr);
if (result.Result.code == "0")
{
redisCache.Write<string>(redisKey, raRndNum, new TimeSpan(0, 5, 0));
return Success(new { sendstate = true, msg = $"短信发送成功:{raRndNum}" });
}
logEntity = new LogEntity
{
F_CategoryId = 4,
F_OperateTypeId = "sms",
F_OperateType = "sms",
F_OperateAccount = "system",
F_ExecuteResult = 400,
F_ExecuteResultJson = "短信发送失败:" + result.Result.message + result.Result.errorType,
F_Description = "短信发送:" + userEntity.F_Mobile
};
logEntity.WriteLog();
return Fail("短信发送失败" + result.Result.message);
}
catch (Exception e)
{
logEntity = new LogEntity
{
F_CategoryId = 4,
F_OperateTypeId = "sms",
F_OperateType = "sms",
F_OperateAccount = "system",
F_ExecuteResult = 400,
F_ExecuteResultJson = "短信发送失败:" + e.Message,
F_Description = "短信发送:" + userEntity.F_Mobile
};
logEntity.WriteLog();
return Fail("短信发送失败" + e.Message);
}
}
/// <summary>
/// 判断是否需要发送验证码
/// </summary>
/// <param name="account"></param>
/// <returns></returns>
private bool? GetAccountIsSendMsg(string account)
{
var userEntity = userIBLL.GetEntityByMobile(account);
//是否强密码验证
bool pwd = !string.IsNullOrEmpty(ConfigurationManager.AppSettings["verifypwd"]) && ConfigurationManager.AppSettings["verifypwd"] == "true";
//是否发送短信
bool whethersendsms = false;
if (!pwd)
{
#region 学生

var studententity = stuInfoBasicIBLL.GetStuInfoBasicEntityByStuNo(userEntity.F_Account);
if (studententity != null)
{
string queryjson = "{\"keywords\":\"" + userEntity.F_Mobile + "\"}";
LogEntity lastsendlog = LogBLL.GetlikeList(queryjson);
if (lastsendlog != null)
{
var aa = ((DateTime)lastsendlog.F_OperateTime - DateTime.Now).TotalMinutes;
//大于30天就要发短信了
if (Math.Abs(aa) > 30 * 24 * 60)
{
whethersendsms = true;
}
}
else
{
whethersendsms = true;
}
}

#endregion

#region 老师

var teacherentity = empInfoIBLL.GetEmpInfoEntityByEmpNo(userEntity.F_Account);
if (teacherentity != null)
{

string queryjson = "{\"keywords\":\"" + userEntity.F_Mobile + "\"}";
LogEntity lastsendlog = LogBLL.GetlikeList(queryjson);
if (lastsendlog != null)
{
var aa = ((DateTime)lastsendlog.F_OperateTime - DateTime.Now).TotalMinutes;
//大于7天就要发短信了
if (Math.Abs(aa) > 7 * 24 * 60)
{
whethersendsms = true;
}
}
else
{
whethersendsms = true;
}
}
#endregion
if (studententity == null && teacherentity == null)
{
return null;
}
}
return whethersendsms;
}
/// <summary>
/// 检查更新
/// </summary>
@@ -66,17 +210,17 @@ namespace Learun.Application.WebApi
/// <returns></returns>
private Response CheckUpdate(dynamic _)
{
var update=sysUpdateRecordIbll.GetNewest();
if (update!=null&&!string.IsNullOrEmpty(update.AppUrl))
var update = sysUpdateRecordIbll.GetNewest();
if (update != null && !string.IsNullOrEmpty(update.AppUrl))
{
var downurl = annexesFileIbll.GetEntityByFolderId(update.AppUrl);
if (downurl.F_FilePath.Contains("Resource"))
{
return Success(new { version = update.VersionNum, content = update.Content, url ="/"+ downurl.F_FilePath.Substring(downurl.F_FilePath.IndexOf("Resource")) });
return Success(new { version = update.VersionNum, content = update.Content, url = "/" + downurl.F_FilePath.Substring(downurl.F_FilePath.IndexOf("Resource")) });
}
return Success(new { version = update.VersionNum, content = update.Content, url = "" });
}
return Success(new {version="1.0.0",content="",url=""});
return Success(new { version = "1.0.0", content = "", url = "" });
}

/// <summary>
@@ -168,12 +312,6 @@ namespace Learun.Application.WebApi

return Success("密码修改成功");
}






/// <summary>
/// 登录接口
/// </summary>
@@ -182,10 +320,9 @@ namespace Learun.Application.WebApi
private Response Login(dynamic _)
{
LoginModel loginModel = this.GetReqData<LoginModel>();

#region 内部账户验证
var isSend = GetAccountIsSendMsg(loginModel.username);
UserEntity userEntity = userIBLL.CheckLogin(loginModel.username, loginModel.password);
#region 写入日志
LogEntity logEntity = new LogEntity();
logEntity.F_CategoryId = 1;
@@ -205,203 +342,59 @@ namespace Learun.Application.WebApi
logEntity.WriteLog();
return Fail(userEntity.LoginMsg);
}
else
if (!isSend.HasValue)
return Fail("请维护用户手机号信息");
if (isSend.Value)
{
//记录ip
userIBLL.UpdateIp(GetIP(), userEntity.F_UserId);
string token = OperatorHelper.Instance.AddLoginUser(userEntity.F_Account, "Learun_ADMS_6.1_App", this.loginMark, false);//写入缓存信息
//写入日志
logEntity.F_ExecuteResult = 1;
logEntity.F_ExecuteResultJson = "登录成功";
logEntity.WriteLog();

//保存用户设备号
userIBLL.UpdateDeviceId(userEntity.F_UserId, loginModel.deviceid);
OperatorResult res = OperatorHelper.Instance.IsOnLine(token, this.loginMark);
res.userInfo.password = null;
res.userInfo.secretkey = null;
string verifyonelogin = Util.Config.GetValue("verifyonelogin");
if (string.IsNullOrEmpty(verifyonelogin) || verifyonelogin != "true")
{
res.userInfo.logTime = null;
}
//是否强密码验证
bool pwd = false;
if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["verifypwd"]) && ConfigurationManager.AppSettings["verifypwd"] == "true" && loginModel.up == false)
{
pwd = true;
}
//是否发送短信了
bool whethersendsms = false;
if (!pwd)
{
//增加短信发送验证
var studententity = stuInfoBasicIBLL.GetStuInfoBasicEntityByStuNo(userEntity.F_Account);
if (studententity != null)
{
res.userInfo.grade = studententity.Grade;
var majorinfo = majorIbll.GetCdMajorEntityByMajorNo(studententity.MajorNo);
if (majorinfo != null)
{
res.userInfo.majorno = majorinfo.ID ?? "";
}
#region 判断是否需要发短信
string queryjson = "{\"keywords\":\"" + userEntity.F_Mobile + "\"}";
LogEntity lastsendlog = LogBLL.GetlikeList(queryjson);
if (lastsendlog != null)
{
var aa = ((DateTime)lastsendlog.F_OperateTime - DateTime.Now).TotalMinutes;
//大于30天就要发短信了
if (Math.Abs(aa) > 30 * 24 * 60 )
{
whethersendsms = true;
}
}
else
{
whethersendsms = true;
}
#endregion
try
{
if (whethersendsms)
{
//学生增加短信发送验证
string raRndNum = Learun.Util.CommonHelper.RndNum(6);
var listStr = new List<string>();
var str1 = $"欢迎使用智慧校园,您本次登录的验证码是 " + raRndNum + "。";
listStr.Add(str1);
var result = yixintongSms.SendSmsToSingle(userEntity.F_Mobile, SmsType.LoginBind, listStr);
if (result.Result.code == "0")
{
redisCache.Write<string>("studentuserlogin_" + userEntity.F_Mobile, raRndNum, new TimeSpan(0, 5, 0));
//日志
logEntity = new LogEntity();
logEntity.F_CategoryId = 3;
logEntity.F_OperateTypeId = "sms";
logEntity.F_OperateType = "sms";
logEntity.F_OperateAccount = "system";
logEntity.F_ExecuteResult = 200;
logEntity.F_ExecuteResultJson = "短信发送成功:" + result.Result.message;
logEntity.F_Description = "短信发送:" + userEntity.F_Mobile;
logEntity.WriteLog();
}
else
{
logEntity = new LogEntity();
logEntity.F_CategoryId = 4;
logEntity.F_OperateTypeId = "sms";
logEntity.F_OperateType = "sms";
logEntity.F_OperateAccount = "system";
logEntity.F_ExecuteResult = 400;
logEntity.F_ExecuteResultJson = "短信发送失败:" + result.Result.message + result.Result.errorType;
logEntity.F_Description = "短信发送:" + userEntity.F_Mobile;
logEntity.WriteLog();
}
}
}
catch (Exception e)
{
logEntity = new LogEntity();
logEntity.F_CategoryId = 4;
logEntity.F_OperateTypeId = "sms";
logEntity.F_OperateType = "sms";
logEntity.F_OperateAccount = "system";
logEntity.F_ExecuteResult = 400;
logEntity.F_ExecuteResultJson = "短信发送失败:" + e.Message;
logEntity.F_Description = "短信发送:" + userEntity.F_Mobile;
logEntity.WriteLog();
}
}
var teacherentity = empInfoIBLL.GetEmpInfoEntityByEmpNo(userEntity.F_Account);
if (teacherentity != null)
{

#region 判断是否需要发短信
string queryjson = "{\"keywords\":\"" + userEntity.F_Mobile + "\"}";
LogEntity lastsendlog = LogBLL.GetlikeList(queryjson);
if (lastsendlog != null)
{
var aa = ((DateTime)lastsendlog.F_OperateTime - DateTime.Now).TotalMinutes;
//大于30天就要发短信了
if (Math.Abs(aa) > 7 * 24 * 60 )
{
whethersendsms = true;
}
}
else
{
whethersendsms = true;
}
#endregion
try
{
if (whethersendsms)
{
//学生增加短信发送验证
string raRndNum = Learun.Util.CommonHelper.RndNum(6);
var listStr = new List<string>();
var str1 = $"欢迎使用智慧校园,您本次登录的验证码是 " + raRndNum + "。";
listStr.Add(str1);
var result = yixintongSms.SendSmsToSingle(userEntity.F_Mobile, SmsType.LoginBind, listStr);
if (result.Result.code == "0")
{
redisCache.Write<string>("studentuserlogin_" + userEntity.F_Mobile, raRndNum, new TimeSpan(0, 5, 0));
//日志
logEntity = new LogEntity();
logEntity.F_CategoryId = 3;
logEntity.F_OperateTypeId = "sms";
logEntity.F_OperateType = "sms";
logEntity.F_OperateAccount = "system";
logEntity.F_ExecuteResult = 200;
logEntity.F_ExecuteResultJson = "短信发送成功:" + result.Result.message;
logEntity.F_Description = "短信发送:" + userEntity.F_Mobile;
logEntity.WriteLog();
}
else
{
logEntity = new LogEntity();
logEntity.F_CategoryId = 4;
logEntity.F_OperateTypeId = "sms";
logEntity.F_OperateType = "sms";
logEntity.F_OperateAccount = "system";
logEntity.F_ExecuteResult = 400;
logEntity.F_ExecuteResultJson = "短信发送失败:" + result.Result.message + result.Result.errorType;
logEntity.F_Description = "短信发送:" + userEntity.F_Mobile;
logEntity.WriteLog();
}
}
}
catch (Exception e)
{
logEntity = new LogEntity();
logEntity.F_CategoryId = 4;
logEntity.F_OperateTypeId = "sms";
logEntity.F_OperateType = "sms";
logEntity.F_OperateAccount = "system";
logEntity.F_ExecuteResult = 400;
logEntity.F_ExecuteResultJson = "短信发送失败:" + e.Message;
logEntity.F_Description = "短信发送:" + userEntity.F_Mobile;
logEntity.WriteLog();
}
}

if (studententity==null &&teacherentity==null)
{
return Fail("请维护手机号信息");
}
}
var jsonData = new
if (string.IsNullOrEmpty(loginModel.verifycode))
return Success(new { sendresult = true, msg = "请输入验证码" });
var vers = redisCache.Read<string>($"studentuserlogin_{userEntity.F_Mobile}");
if (redisCache.Read<string>($"studentuserlogin_{userEntity.F_Mobile}") != loginModel.verifycode)
return Fail("验证码错误或已失效。");
redisCache.Remove($"studentuserlogin_{userEntity.F_Mobile}");
}
//记录ip
userIBLL.UpdateIp(GetIP(), userEntity.F_UserId);
string token = OperatorHelper.Instance.AddLoginUser(userEntity.F_Account, "Learun_ADMS_6.1_App", this.loginMark, false);//写入缓存信息
//写入日志
logEntity.F_ExecuteResult = 1;
logEntity.F_ExecuteResultJson = "登录成功";
logEntity.WriteLog();
if (isSend.Value)
{
logEntity = new LogEntity
{
baseinfo = res.userInfo,
post = postIBLL.GetListByPostIds(res.userInfo.postIds),
role = roleIBLL.GetListByRoleIds(res.userInfo.roleIds),
pwd = pwd,
sendresult=whethersendsms
F_CategoryId = 3,
F_OperateTypeId = "sms",
F_OperateType = "sms",
F_OperateAccount = "system",
F_ExecuteResult = 200,
F_ExecuteResultJson = "短信发送成功:发送短信成功",
F_Description = "短信发送:" + userEntity.F_Mobile
};
return Success(jsonData);
logEntity.WriteLog();
}
#endregion
//配置短信发送日志,为登录时效验是否需要发送短信
//保存用户设备号
userIBLL.UpdateDeviceId(userEntity.F_UserId, loginModel.deviceid);
OperatorResult res = OperatorHelper.Instance.IsOnLine(token, this.loginMark);
res.userInfo.password = null;
res.userInfo.secretkey = null;
string verifyonelogin = Util.Config.GetValue("verifyonelogin");
if (string.IsNullOrEmpty(verifyonelogin) || verifyonelogin != "true")
{
res.userInfo.logTime = null;
}

var jsonData = new
{
baseinfo = res.userInfo,
post = postIBLL.GetListByPostIds(res.userInfo.postIds),
role = roleIBLL.GetListByRoleIds(res.userInfo.roleIds),
pwd = false,
};
return Success(jsonData);
}

/// <summary>
@@ -489,7 +482,8 @@ namespace Learun.Application.WebApi
{
//多客户端登录判断
string logintime = redisCache.Read<string>("logintime" + userInfo.account, CacheId.loginInfo);
return Success(new {
return Success(new
{
logintime
});
}
@@ -775,6 +769,7 @@ namespace Learun.Application.WebApi
/// </summary>
public string deviceid { get; set; }
public string openid { get; set; }
public string verifycode { get; set; }
}
/// <summary>
/// 修改密码
@@ -806,5 +801,4 @@ namespace Learun.Application.WebApi
{
public string account { get; set; }
}

}

Загрузка…
Отмена
Сохранить