using Learun.Application.Base.SystemModule; using Learun.Application.Organization; using Learun.Application.TwoDevelopment.EducationalAdministration; using Learun.Cache.Base; using Learun.Cache.Factory; using Learun.Util; using Learun.Util.Operat; using Nancy; using Quanjiang.DigitalScholl.SendSms; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; namespace Learun.Application.WebApi { /// /// 版 本 Learun-ADMS V7.0.0 数字化智慧校园 /// Copyright (c) 2013-2018 北京泉江科技有限公司 /// 创建人:数字化智慧校园-框架开发组 /// 日 期:2017.05.12 /// 描 述:用户信息 /// public class UserApi : BaseApi { /// /// 注册接口 /// public UserApi() : base("/learun/adms/user") { Post["/login"] = Login; Post["/modifypw"] = ModifyPassword; Post["/modifypwiden"] = ModifyPasswordiden; Post["/unbundWeiXin"] = DoUnbundWeiXin; Post["/loginbyIdCard"] = LoginByIdCard; Post["/updateMobile"] = UpdateMobile; Get["/info"] = Info; Get["/map"] = GetMap; Get["/img"] = GetImg; Get["/imgfordc"] = GetImgForDC; Get["/saveMap"] = GetSaveClassMap; } private UserIBLL userIBLL = new UserBLL(); private PostIBLL postIBLL = new PostBLL(); private RoleIBLL roleIBLL = new RoleBLL(); private StuInfoBasicIBLL stuInfoBasicIBLL = new StuInfoBasicBLL(); CdMajorIBLL majorIbll = new CdMajorBLL(); private readonly ISms aliyunSms = new AliyunSms(); private ICache redisCache = CacheFactory.CaChe(); /// /// 短信验证码校验 /// /// /// private Response LoginCodeVerify(dynamic _) { MobileVerify mobileVerify = this.GetReqData(); if (string.IsNullOrEmpty(mobileVerify.codeType)) { return Fail("未指定短信类型。"); } if (string.IsNullOrEmpty(mobileVerify.mobile)) { return Fail("手机号不能为空。"); } var code = redisCache.Read("sendcodeinapp_" + mobileVerify.codeType + "_" + mobileVerify.mobile, CacheId.sms); if (!string.IsNullOrEmpty(code) && code == mobileVerify.verifycode) { return Success("验证成功。"); } else { return Fail("验证失败,验证码错误或已失效。"); } } /// /// 发送短信验证码 /// /// /// private Response Sendcode(dynamic _) { LoginModel loginModel = this.GetReqData(); if (string.IsNullOrEmpty(loginModel.codeType)) { return Fail("未指定短信类型。"); } UserEntity userEntity = null; userEntity = userIBLL.GetEntityByUserId(userInfo.userId); if (userEntity == null) { return Fail("用户不存在!"); } if (loginModel.codeType == "unbindwx") { if (string.IsNullOrEmpty(userEntity.F_Mobile)) { return Fail("用户手机号不存在!"); } loginModel.username = userEntity.F_Mobile; } if (string.IsNullOrEmpty(loginModel.username)) { return Fail("手机号不能为空。"); } if (!CommonHelper.IsValidMobile(loginModel.username)) { return Fail("手机号格式不正确!"); } var listStr = new List(); var result = aliyunSms.SendSmsToSingle(loginModel.username, SmsType.LoginBind, listStr); if (result.Result.code == "OK") { redisCache.Write("sendcodeinapp_" + loginModel.codeType + "_" + loginModel.username, result.Result.randomNum, new TimeSpan(0, 5, 0), CacheId.sms); //日志 LogEntity logEntity = new LogEntity(); logEntity.F_CategoryId = 3; logEntity.F_SourceObjectId = loginModel.codeType; 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 = "短信发送:" + loginModel.username + " 验证码:" + result.Result.randomNum; logEntity.WriteLog(); return Success("短信发送成功:" + result.Result.message); } else { LogEntity logEntity = new LogEntity(); logEntity.F_CategoryId = 4; logEntity.F_SourceObjectId = loginModel.codeType; 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 = "短信发送:" + loginModel.username; logEntity.WriteLog(); return Fail("短信发送失败:" + result.Result.message + result.Result.errorType); } } /// /// 登录接口 /// /// /// private Response Login(dynamic _) { LoginModel loginModel = this.GetReqData(); #region 内部账户验证 UserEntity userEntity = userIBLL.CheckLogin(loginModel.username, loginModel.password); userEntity.F_Password = null; userEntity.F_IdentityCardNo = null; userEntity.F_Secretkey = null; #region 写入日志 LogEntity logEntity = new LogEntity(); logEntity.F_CategoryId = 1; logEntity.F_OperateTypeId = ((int)OperationType.Login).ToString(); logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.Login); logEntity.F_OperateAccount = loginModel.username + "(" + userEntity.F_RealName + ")"; logEntity.F_OperateUserId = !string.IsNullOrEmpty(userEntity.F_UserId) ? userEntity.F_UserId : loginModel.username; logEntity.F_Module = Config.GetValue("SoftName"); logEntity.F_Description = "移动端"; #endregion if (!userEntity.LoginOk)//登录失败 { //写入日志 logEntity.F_ExecuteResult = 0; logEntity.F_ExecuteResultJson = "登录失败:" + userEntity.LoginMsg; logEntity.WriteLog(); return Fail(userEntity.LoginMsg); } else { //记录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; 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 ?? ""; } } //是否强密码验证 bool pwd = false; if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["verifypwd"]) && ConfigurationManager.AppSettings["verifypwd"] == "true" && loginModel.up == false) { pwd = true; } var jsonData = new { baseinfo = res.userInfo, post = postIBLL.GetListByPostIds(res.userInfo.postIds), role = roleIBLL.GetListByRoleIds(res.userInfo.roleIds), pwd= pwd }; return Success(jsonData); } #endregion } /// /// 身份验证-登录接口 /// /// /// private Response LoginByIdCard(dynamic _) { LoginModel loginModel = this.GetReqData(); #region 内部账户验证 UserEntity userEntity = userIBLL.CheckLoginByIdCard(loginModel.username, loginModel.password); #region 写入日志 LogEntity logEntity = new LogEntity(); logEntity.F_CategoryId = 1; logEntity.F_OperateTypeId = ((int)OperationType.Login).ToString(); logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.Login); logEntity.F_OperateAccount = loginModel.username + "(" + userEntity.F_RealName + ")"; logEntity.F_OperateUserId = !string.IsNullOrEmpty(userEntity.F_UserId) ? userEntity.F_UserId : loginModel.username; logEntity.F_Module = Config.GetValue("SoftName"); logEntity.F_Description = "移动端"; #endregion if (!userEntity.LoginOk)//登录失败 { //写入日志 logEntity.F_ExecuteResult = 0; logEntity.F_ExecuteResultJson = "登录失败:" + userEntity.LoginMsg; logEntity.WriteLog(); return Fail(userEntity.LoginMsg); } else { //新增新生判断 var stuinfobasic = stuInfoBasicIBLL.GetStuInfoBasicEntityByStuNo(userEntity.F_EnCode); if (stuinfobasic!=null&&stuinfobasic.Grade!="21") { userEntity.LoginMsg = "只有新生支持身份证方式登录"; return Fail(userEntity.LoginMsg); } //记录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; 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 ?? ""; } } //是否强密码验证 bool pwd = false; if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["verifypwd"]) && ConfigurationManager.AppSettings["verifypwd"] == "true" && loginModel.up == false) { pwd = true; } var jsonData = new { baseinfo = res.userInfo, post = postIBLL.GetListByPostIds(res.userInfo.postIds), role = roleIBLL.GetListByRoleIds(res.userInfo.roleIds), pwd = pwd }; return Success(jsonData); } #endregion } /// /// 获取用户信息 /// /// /// private Response Info(dynamic _) { var data = userInfo; data.password = null; data.secretkey = null; var jsonData = new { baseinfo = data, post = postIBLL.GetListByPostIds(data.postIds), role = roleIBLL.GetListByRoleIds(data.roleIds) }; return Success(jsonData); } /// /// 修改密码 /// /// /// private Response ModifyPassword(dynamic _) { ModifyModel modifyModel = this.GetReqData(); if (userInfo.isSystem) { return Fail("当前账户不能修改密码"); } else { bool res = userIBLL.RevisePassword(modifyModel.newpassword, modifyModel.oldpassword); if (!res) { return Fail("原密码错误,请重新输入"); } else { return Success("密码修改成功"); } } } private Response ModifyPasswordiden(dynamic _) { ModifyModel modifyModel = this.GetReqData(); if (userInfo.isSystem) { return Fail("当前账户不能修改密码"); } else { bool res = userIBLL.RevisePasswordiden(modifyModel.newpassword, modifyModel.oldpassword); if (!res) { return Fail("原密码错误,请重新输入"); } else { return Success("密码修改成功"); } } } /// /// 解绑微信 /// /// /// private Response DoUnbundWeiXin(dynamic _) { userIBLL.DoUnbundWeiXin(userInfo.userId); return Success("解绑成功"); } /// /// 修改手机号 /// /// /// private Response UpdateMobile(dynamic _) { MobileVerify mobileVerify = this.GetReqData(); if (string.IsNullOrEmpty(mobileVerify.codeType)) { return Fail("未指定短信类型。"); } if (string.IsNullOrEmpty(mobileVerify.mobile)) { return Fail("手机号不能为空。"); } var code = redisCache.Read("sendcodeinapp_" + mobileVerify.codeType + "_" + mobileVerify.mobile, CacheId.sms); if (!string.IsNullOrEmpty(code) && code == mobileVerify.verifycode) { //return Success("验证成功。"); userIBLL.UpdateMobile(userInfo.userId, mobileVerify.mobile); return Success("修改成功"); } else { return Fail("验证失败,验证码错误或已失效。"); } } /// /// 获取所有员工账号列表 /// /// /// private Response GetList(dynamic _) { var data = userInfo; data.password = null; data.secretkey = null; var jsonData = new { baseinfo = data, post = postIBLL.GetListByPostIds(data.postIds), role = roleIBLL.GetListByRoleIds(data.roleIds) }; return Success(jsonData); } /// /// 获取用户映射表 /// /// /// public Response GetMap(dynamic _) { string ver = this.GetReqData();// 获取模板请求数据 var data = userIBLL.GetModelMap(); string md5 = Md5Helper.Encrypt(data.ToJson(), 32); if (md5 == ver) { return Success("no update"); } else { var jsondata = new { data = data, ver = md5 }; return Success(jsondata); } } public Response GetSaveClassMap(dynamic _) { string account = this.GetReqData().account; var data = userIBLL.GetSaveClassMap(); var students = stuInfoBasicIBLL.GetSaveClassStudents(account); data = data.Where(a => students.Contains(a.F_Account) && a.F_Account != account).ToList(); var dic = new Dictionary(); foreach (var item in data) { UserModel model = new UserModel() { companyId = item.F_CompanyId, departmentId = item.F_DepartmentId, name = item.F_RealName, id = item.F_UserId, }; string img = ""; if (!string.IsNullOrEmpty(item.F_HeadIcon)) { string fileHeadImg = Config.GetValue("fileHeadImg"); string fileImg = string.Format("{0}/{1}{2}", fileHeadImg, item.F_UserId, item.F_HeadIcon); if (DirFileHelper.IsExistFile(fileImg)) { img = item.F_HeadIcon; } } if (string.IsNullOrEmpty(img)) { if (item.F_Gender == 0) { img = "0"; } else { img = "1"; } } model.img = img; dic.Add(item.F_UserId, model); } string md5 = Md5Helper.Encrypt(dic.ToJson(), 32); var jsondata = new { data = dic, var = md5 }; return Success(jsondata); } /// /// 获取人员头像图标 /// /// /// public Response GetImg(dynamic _) { string userId = this.GetReqData();// 获取模板请求数据 userIBLL.GetImg(userId); return Success("获取成功"); } /// /// 获取人员头像图标 /// /// /// public Response GetImgForDC(dynamic _) { string userId = this.GetReqData();// 获取模板请求数据 userIBLL.GetImgForDC(userId); return Success("获取成功"); } /// /// 获取IP /// /// private string GetIP() { //string ip = string.Empty; //if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"])) // ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]); //if (string.IsNullOrEmpty(ip)) // ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]); //return ip; string userIP = "未获取用户IP"; try { if (System.Web.HttpContext.Current == null || System.Web.HttpContext.Current.Request == null || System.Web.HttpContext.Current.Request.ServerVariables == null) { return ""; } string CustomerIP = ""; //CDN加速后取到的IP simone 090805 CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"]; if (!string.IsNullOrEmpty(CustomerIP)) { return CustomerIP; } CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (!string.IsNullOrEmpty(CustomerIP)) { return CustomerIP; } if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null) { CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (CustomerIP == null) { CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } } else { CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } if (string.Compare(CustomerIP, "unknown", true) == 0 || string.IsNullOrEmpty(CustomerIP)) { return System.Web.HttpContext.Current.Request.UserHostAddress; } return CustomerIP; } catch { } return userIP; } } /// /// 登录信息 /// public class LoginModel { /// /// 账号 /// public string username { get; set; } /// /// 密码 /// public string password { get; set; } /// /// 是否强密码 /// public bool up { get; set; } /// /// 设备号 /// public string deviceid { get; set; } public string openid { get; set; } /// /// 发送短信类型:忘记密码forgetpwd,首次登录firstlogin,绑定微信bindwx,解绑微信unbindwx,修改手机号modifymobile,; /// public string codeType { get; set; } /// /// 短信验证码 /// public string verifycode { get; set; } } /// /// 修改密码 /// public class ModifyModel { public string phone { set; get; } /// /// 新密码 /// public string newpassword { get; set; } /// /// 旧密码 /// public string oldpassword { get; set; } } public class MobileVerify { public string mobile { get; set; } public string verifycode { get; set; } /// /// 发送短信类型 /// public string codeType { get; set; } } public class UserAccount { public string account { get; set; } } }