From a7e36b46c1510bb109978b16ca08b6b420521127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=99=93=E5=AF=92?= <823756726@qq.com> Date: Mon, 11 Nov 2024 14:18:00 +0800 Subject: [PATCH] =?UTF-8?q?api=E7=99=BB=E5=BD=95=E6=B5=81=E7=A8=8B?= =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Modules/BaseApi.cs | 2 +- .../Modules/UserApi.cs | 412 +++++++++--------- 2 files changed, 204 insertions(+), 210 deletions(-) diff --git a/Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/BaseApi.cs b/Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/BaseApi.cs index dc4dc9257..c20c9ab02 100644 --- a/Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/BaseApi.cs +++ b/Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/BaseApi.cs @@ -180,7 +180,7 @@ namespace Learun.Application.WebApi ReqParameter req = this.Bind(); 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; } diff --git a/Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/UserApi.cs b/Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/UserApi.cs index 0a60f238c..de2b9707d 100644 --- a/Learun.Framework.Ultimate V7/Learun.Application.WebApi/Modules/UserApi.cs +++ b/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(); + /// + /// 验证是否需要输入验证码 + /// + /// + private Response LoginVerify(dynamic _) + { + var account = this.GetReqData(); + var isSend = GetAccountIsSendMsg(account.account); + if (!isSend.HasValue) + { + return Fail("请维护用户手机号信息"); + } + return Success(new { sendresult = isSend }); + } + /// + /// 发送短信验证码 + /// + /// + private Response LoginSendMessage(dynamic _) + { + var account = this.GetReqData(); + 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(redisKey); + if (!string.IsNullOrEmpty(msg)) return Fail("原验证码未过期请使用原验证码。"); + try + { + //短信发送验证 + string raRndNum = CommonHelper.RndNum(6); + var listStr = new List { $"欢迎使用智慧校园,您本次登录的验证码是 {raRndNum}。" }; + var result = yixintongSms.SendSmsToSingle(userEntity.F_Mobile, SmsType.LoginBind, listStr); + if (result.Result.code == "0") + { + redisCache.Write(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); + } + } + /// + /// 判断是否需要发送验证码 + /// + /// + /// + 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; + } /// /// 检查更新 /// @@ -66,17 +210,17 @@ namespace Learun.Application.WebApi /// 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 = "" }); } /// @@ -168,12 +312,6 @@ namespace Learun.Application.WebApi return Success("密码修改成功"); } - - - - - - /// /// 登录接口 /// @@ -182,10 +320,9 @@ namespace Learun.Application.WebApi private Response Login(dynamic _) { LoginModel loginModel = this.GetReqData(); - - #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(); - var str1 = $"欢迎使用智慧校园,您本次登录的验证码是 " + raRndNum + "。"; - listStr.Add(str1); - var result = yixintongSms.SendSmsToSingle(userEntity.F_Mobile, SmsType.LoginBind, listStr); - if (result.Result.code == "0") - { - redisCache.Write("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(); - var str1 = $"欢迎使用智慧校园,您本次登录的验证码是 " + raRndNum + "。"; - listStr.Add(str1); - var result = yixintongSms.SendSmsToSingle(userEntity.F_Mobile, SmsType.LoginBind, listStr); - if (result.Result.code == "0") - { - redisCache.Write("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($"studentuserlogin_{userEntity.F_Mobile}"); + if (redisCache.Read($"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); } /// @@ -489,7 +482,8 @@ namespace Learun.Application.WebApi { //多客户端登录判断 string logintime = redisCache.Read("logintime" + userInfo.account, CacheId.loginInfo); - return Success(new { + return Success(new + { logintime }); } @@ -775,6 +769,7 @@ namespace Learun.Application.WebApi /// public string deviceid { get; set; } public string openid { get; set; } + public string verifycode { get; set; } } /// /// 修改密码 @@ -806,5 +801,4 @@ namespace Learun.Application.WebApi { public string account { get; set; } } - } \ No newline at end of file