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
{
///
/// 版 本 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["/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();
///
/// 登录接口
///
///
///
private Response Login(dynamic _)
{
LoginModel loginModel = this.GetReqData();
#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
}
///
/// 获取用户信息
///
///
///
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 DoUnbundWeiXin(dynamic _)
{
userIBLL.DoUnbundWeiXin(userInfo.userId);
return Success("解绑成功");
}
///
/// 获取所有员工账号列表
///
///
///
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; }
}
///
/// 修改密码
///
public class ModifyModel
{
///
/// 新密码
///
public string newpassword { get; set; }
///
/// 旧密码
///
public string oldpassword { get; set; }
}
public class UserAccount
{
public string account { get; set; }
}
}