|
- using Learun.Application.Base.SystemModule;
- using Learun.Application.Organization;
- using Learun.Application.TwoDevelopment.EducationalAdministration;
- using Learun.Util;
- using Learun.Util.Operat;
- using Nancy;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
-
- namespace Learun.Application.WebApi
- {
- /// <summary>
- /// 版 本 Learun-ADMS V7.0.0 数字化智慧校园
- /// Copyright (c) 2013-2018 北京泉江科技有限公司
- /// 创建人:数字化智慧校园-框架开发组
- /// 日 期:2017.05.12
- /// 描 述:用户信息
- /// </summary>
- public class UserApi : BaseApi
- {
- /// <summary>
- /// 注册接口
- /// </summary>
- public UserApi()
- : base("/learun/adms/user")
- {
- Post["/login"] = Login;
- Post["/modifypw"] = ModifyPassword;
- Post["/unbundWeiXin"] = DoUnbundWeiXin;
-
- 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();
-
-
-
- /// <summary>
- /// 登录接口
- /// </summary>
- /// <param name="_"></param>
- /// <returns></returns>
- private Response Login(dynamic _)
- {
- LoginModel loginModel = this.GetReqData<LoginModel>();
-
- #region 内部账户验证
- UserEntity userEntity = userIBLL.CheckLogin(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
- {
- //记录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
- }
-
- /// <summary>
- /// 获取用户信息
- /// </summary>
- /// <param name="_"></param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 修改密码
- /// </summary>
- /// <param name="_"></param>
- /// <returns></returns>
- private Response ModifyPassword(dynamic _)
- {
- ModifyModel modifyModel = this.GetReqData<ModifyModel>();
- if (userInfo.isSystem)
- {
- return Fail("当前账户不能修改密码");
- }
- else
- {
- bool res = userIBLL.RevisePassword(modifyModel.newpassword, modifyModel.oldpassword);
- if (!res)
- {
- return Fail("原密码错误,请重新输入");
- }
- else
- {
- return Success("密码修改成功");
- }
- }
- }
-
- /// <summary>
- /// 解绑微信
- /// </summary>
- /// <param name="_"></param>
- /// <returns></returns>
- private Response DoUnbundWeiXin(dynamic _)
- {
- userIBLL.DoUnbundWeiXin(userInfo.userId);
- return Success("解绑成功");
-
- }
- /// <summary>
- /// 获取所有员工账号列表
- /// </summary>
- /// <param name="_"></param>
- /// <returns></returns>
- 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);
- }
- /// <summary>
- /// 获取用户映射表
- /// </summary>
- /// <param name="_"></param>
- /// <returns></returns>
- 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<UserAccount>().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<string, UserModel>();
- 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);
- }
- /// <summary>
- /// 获取人员头像图标
- /// </summary>
- /// <param name="_"></param>
- /// <returns></returns>
- public Response GetImg(dynamic _)
- {
- string userId = this.GetReqData();// 获取模板请求数据
- userIBLL.GetImg(userId);
- return Success("获取成功");
- }
- /// <summary>
- /// 获取人员头像图标
- /// </summary>
- /// <param name="_"></param>
- /// <returns></returns>
- public Response GetImgForDC(dynamic _)
- {
- string userId = this.GetReqData();// 获取模板请求数据
- userIBLL.GetImgForDC(userId);
- return Success("获取成功");
- }
- /// <summary>
- /// 获取IP
- /// </summary>
- /// <returns></returns>
- 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;
- }
- }
-
- /// <summary>
- /// 登录信息
- /// </summary>
- public class LoginModel
- {
- /// <summary>
- /// 账号
- /// </summary>
- public string username { get; set; }
- /// <summary>
- /// 密码
- /// </summary>
- public string password { get; set; }
- /// <summary>
- /// 是否强密码
- /// </summary>
- public bool up { get; set; }
-
- /// <summary>
- /// 设备号
- /// </summary>
- public string deviceid { get; set; }
- public string openid { get; set; }
- }
- /// <summary>
- /// 修改密码
- /// </summary>
- public class ModifyModel
- {
- /// <summary>
- /// 新密码
- /// </summary>
- public string newpassword { get; set; }
- /// <summary>
- /// 旧密码
- /// </summary>
- public string oldpassword { get; set; }
- }
-
-
- public class UserAccount
- {
- public string account { get; set; }
- }
-
- }
|