You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

409 lines
14 KiB

  1. using Learun.Application.Base.SystemModule;
  2. using Learun.Application.Organization;
  3. using Learun.Application.TwoDevelopment.EducationalAdministration;
  4. using Learun.Util;
  5. using Learun.Util.Operat;
  6. using Nancy;
  7. using System.Collections.Generic;
  8. using System.Configuration;
  9. using System.Linq;
  10. namespace Learun.Application.WebApi
  11. {
  12. /// <summary>
  13. /// 版 本 Learun-ADMS V7.0.0 数字化智慧校园
  14. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  15. /// 创建人:数字化智慧校园-框架开发组
  16. /// 日 期:2017.05.12
  17. /// 描 述:用户信息
  18. /// </summary>
  19. public class UserApi : BaseApi
  20. {
  21. /// <summary>
  22. /// 注册接口
  23. /// </summary>
  24. public UserApi()
  25. : base("/learun/adms/user")
  26. {
  27. Post["/login"] = Login;
  28. Post["/modifypw"] = ModifyPassword;
  29. Post["/unbundWeiXin"] = DoUnbundWeiXin;
  30. Get["/info"] = Info;
  31. Get["/map"] = GetMap;
  32. Get["/img"] = GetImg;
  33. Get["/imgfordc"] = GetImgForDC;
  34. Get["/saveMap"] = GetSaveClassMap;
  35. }
  36. private UserIBLL userIBLL = new UserBLL();
  37. private PostIBLL postIBLL = new PostBLL();
  38. private RoleIBLL roleIBLL = new RoleBLL();
  39. private StuInfoBasicIBLL stuInfoBasicIBLL = new StuInfoBasicBLL();
  40. CdMajorIBLL majorIbll = new CdMajorBLL();
  41. /// <summary>
  42. /// 登录接口
  43. /// </summary>
  44. /// <param name="_"></param>
  45. /// <returns></returns>
  46. private Response Login(dynamic _)
  47. {
  48. LoginModel loginModel = this.GetReqData<LoginModel>();
  49. #region 内部账户验证
  50. UserEntity userEntity = userIBLL.CheckLogin(loginModel.username, loginModel.password);
  51. userEntity.F_Password = null;
  52. userEntity.F_IdentityCardNo = null;
  53. userEntity.F_Secretkey = null;
  54. #region 写入日志
  55. LogEntity logEntity = new LogEntity();
  56. logEntity.F_CategoryId = 1;
  57. logEntity.F_OperateTypeId = ((int)OperationType.Login).ToString();
  58. logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.Login);
  59. logEntity.F_OperateAccount = loginModel.username + "(" + userEntity.F_RealName + ")";
  60. logEntity.F_OperateUserId = !string.IsNullOrEmpty(userEntity.F_UserId) ? userEntity.F_UserId : loginModel.username;
  61. logEntity.F_Module = Config.GetValue("SoftName");
  62. logEntity.F_Description = "移动端";
  63. #endregion
  64. if (!userEntity.LoginOk)//登录失败
  65. {
  66. //写入日志
  67. logEntity.F_ExecuteResult = 0;
  68. logEntity.F_ExecuteResultJson = "登录失败:" + userEntity.LoginMsg;
  69. logEntity.WriteLog();
  70. return Fail(userEntity.LoginMsg);
  71. }
  72. else
  73. {
  74. //记录ip
  75. userIBLL.UpdateIp(GetIP(), userEntity.F_UserId);
  76. string token = OperatorHelper.Instance.AddLoginUser(userEntity.F_Account, "Learun_ADMS_6.1_App", this.loginMark, false);//写入缓存信息
  77. //写入日志
  78. logEntity.F_ExecuteResult = 1;
  79. logEntity.F_ExecuteResultJson = "登录成功";
  80. logEntity.WriteLog();
  81. //保存用户设备号
  82. userIBLL.UpdateDeviceId(userEntity.F_UserId, loginModel.deviceid);
  83. OperatorResult res = OperatorHelper.Instance.IsOnLine(token, this.loginMark);
  84. res.userInfo.password = null;
  85. res.userInfo.secretkey = null;
  86. var studententity = stuInfoBasicIBLL.GetStuInfoBasicEntityByStuNo(userEntity.F_Account);
  87. if (studententity != null)
  88. {
  89. res.userInfo.grade = studententity.Grade;
  90. var majorinfo = majorIbll.GetCdMajorEntityByMajorNo(studententity.MajorNo);
  91. if (majorinfo != null)
  92. {
  93. res.userInfo.majorno = majorinfo.ID ?? "";
  94. }
  95. }
  96. //是否强密码验证
  97. bool pwd = false;
  98. if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["verifypwd"]) && ConfigurationManager.AppSettings["verifypwd"] == "true" && loginModel.up == false)
  99. {
  100. pwd = true;
  101. }
  102. var jsonData = new
  103. {
  104. baseinfo = res.userInfo,
  105. post = postIBLL.GetListByPostIds(res.userInfo.postIds),
  106. role = roleIBLL.GetListByRoleIds(res.userInfo.roleIds),
  107. pwd= pwd
  108. };
  109. return Success(jsonData);
  110. }
  111. #endregion
  112. }
  113. /// <summary>
  114. /// 获取用户信息
  115. /// </summary>
  116. /// <param name="_"></param>
  117. /// <returns></returns>
  118. private Response Info(dynamic _)
  119. {
  120. var data = userInfo;
  121. data.password = null;
  122. data.secretkey = null;
  123. var jsonData = new
  124. {
  125. baseinfo = data,
  126. post = postIBLL.GetListByPostIds(data.postIds),
  127. role = roleIBLL.GetListByRoleIds(data.roleIds)
  128. };
  129. return Success(jsonData);
  130. }
  131. /// <summary>
  132. /// 修改密码
  133. /// </summary>
  134. /// <param name="_"></param>
  135. /// <returns></returns>
  136. private Response ModifyPassword(dynamic _)
  137. {
  138. ModifyModel modifyModel = this.GetReqData<ModifyModel>();
  139. if (userInfo.isSystem)
  140. {
  141. return Fail("当前账户不能修改密码");
  142. }
  143. else
  144. {
  145. bool res = userIBLL.RevisePassword(modifyModel.newpassword, modifyModel.oldpassword);
  146. if (!res)
  147. {
  148. return Fail("原密码错误,请重新输入");
  149. }
  150. else
  151. {
  152. return Success("密码修改成功");
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// 解绑微信
  158. /// </summary>
  159. /// <param name="_"></param>
  160. /// <returns></returns>
  161. private Response DoUnbundWeiXin(dynamic _)
  162. {
  163. userIBLL.DoUnbundWeiXin(userInfo.userId);
  164. return Success("解绑成功");
  165. }
  166. /// <summary>
  167. /// 获取所有员工账号列表
  168. /// </summary>
  169. /// <param name="_"></param>
  170. /// <returns></returns>
  171. private Response GetList(dynamic _)
  172. {
  173. var data = userInfo;
  174. data.password = null;
  175. data.secretkey = null;
  176. var jsonData = new
  177. {
  178. baseinfo = data,
  179. post = postIBLL.GetListByPostIds(data.postIds),
  180. role = roleIBLL.GetListByRoleIds(data.roleIds)
  181. };
  182. return Success(jsonData);
  183. }
  184. /// <summary>
  185. /// 获取用户映射表
  186. /// </summary>
  187. /// <param name="_"></param>
  188. /// <returns></returns>
  189. public Response GetMap(dynamic _)
  190. {
  191. string ver = this.GetReqData();// 获取模板请求数据
  192. var data = userIBLL.GetModelMap();
  193. string md5 = Md5Helper.Encrypt(data.ToJson(), 32);
  194. if (md5 == ver)
  195. {
  196. return Success("no update");
  197. }
  198. else
  199. {
  200. var jsondata = new
  201. {
  202. data = data,
  203. ver = md5
  204. };
  205. return Success(jsondata);
  206. }
  207. }
  208. public Response GetSaveClassMap(dynamic _)
  209. {
  210. string account = this.GetReqData<UserAccount>().account;
  211. var data = userIBLL.GetSaveClassMap();
  212. var students = stuInfoBasicIBLL.GetSaveClassStudents(account);
  213. data = data.Where(a => students.Contains(a.F_Account) && a.F_Account != account).ToList();
  214. var dic = new Dictionary<string, UserModel>();
  215. foreach (var item in data)
  216. {
  217. UserModel model = new UserModel()
  218. {
  219. companyId = item.F_CompanyId,
  220. departmentId = item.F_DepartmentId,
  221. name = item.F_RealName,
  222. id = item.F_UserId,
  223. };
  224. string img = "";
  225. if (!string.IsNullOrEmpty(item.F_HeadIcon))
  226. {
  227. string fileHeadImg = Config.GetValue("fileHeadImg");
  228. string fileImg = string.Format("{0}/{1}{2}", fileHeadImg, item.F_UserId, item.F_HeadIcon);
  229. if (DirFileHelper.IsExistFile(fileImg))
  230. {
  231. img = item.F_HeadIcon;
  232. }
  233. }
  234. if (string.IsNullOrEmpty(img))
  235. {
  236. if (item.F_Gender == 0)
  237. {
  238. img = "0";
  239. }
  240. else
  241. {
  242. img = "1";
  243. }
  244. }
  245. model.img = img;
  246. dic.Add(item.F_UserId, model);
  247. }
  248. string md5 = Md5Helper.Encrypt(dic.ToJson(), 32);
  249. var jsondata = new
  250. {
  251. data = dic,
  252. var = md5
  253. };
  254. return Success(jsondata);
  255. }
  256. /// <summary>
  257. /// 获取人员头像图标
  258. /// </summary>
  259. /// <param name="_"></param>
  260. /// <returns></returns>
  261. public Response GetImg(dynamic _)
  262. {
  263. string userId = this.GetReqData();// 获取模板请求数据
  264. userIBLL.GetImg(userId);
  265. return Success("获取成功");
  266. }
  267. /// <summary>
  268. /// 获取人员头像图标
  269. /// </summary>
  270. /// <param name="_"></param>
  271. /// <returns></returns>
  272. public Response GetImgForDC(dynamic _)
  273. {
  274. string userId = this.GetReqData();// 获取模板请求数据
  275. userIBLL.GetImgForDC(userId);
  276. return Success("获取成功");
  277. }
  278. /// <summary>
  279. /// 获取IP
  280. /// </summary>
  281. /// <returns></returns>
  282. private string GetIP()
  283. {
  284. //string ip = string.Empty;
  285. //if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
  286. // ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
  287. //if (string.IsNullOrEmpty(ip))
  288. // ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
  289. //return ip;
  290. string userIP = "未获取用户IP";
  291. try
  292. {
  293. if (System.Web.HttpContext.Current == null
  294. || System.Web.HttpContext.Current.Request == null
  295. || System.Web.HttpContext.Current.Request.ServerVariables == null)
  296. {
  297. return "";
  298. }
  299. string CustomerIP = "";
  300. //CDN加速后取到的IP simone 090805
  301. CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
  302. if (!string.IsNullOrEmpty(CustomerIP))
  303. {
  304. return CustomerIP;
  305. }
  306. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  307. if (!string.IsNullOrEmpty(CustomerIP))
  308. {
  309. return CustomerIP;
  310. }
  311. if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
  312. {
  313. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  314. if (CustomerIP == null)
  315. {
  316. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  317. }
  318. }
  319. else
  320. {
  321. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  322. }
  323. if (string.Compare(CustomerIP, "unknown", true) == 0 || string.IsNullOrEmpty(CustomerIP))
  324. {
  325. return System.Web.HttpContext.Current.Request.UserHostAddress;
  326. }
  327. return CustomerIP;
  328. }
  329. catch { }
  330. return userIP;
  331. }
  332. }
  333. /// <summary>
  334. /// 登录信息
  335. /// </summary>
  336. public class LoginModel
  337. {
  338. /// <summary>
  339. /// 账号
  340. /// </summary>
  341. public string username { get; set; }
  342. /// <summary>
  343. /// 密码
  344. /// </summary>
  345. public string password { get; set; }
  346. /// <summary>
  347. /// 是否强密码
  348. /// </summary>
  349. public bool up { get; set; }
  350. /// <summary>
  351. /// 设备号
  352. /// </summary>
  353. public string deviceid { get; set; }
  354. public string openid { get; set; }
  355. }
  356. /// <summary>
  357. /// 修改密码
  358. /// </summary>
  359. public class ModifyModel
  360. {
  361. /// <summary>
  362. /// 新密码
  363. /// </summary>
  364. public string newpassword { get; set; }
  365. /// <summary>
  366. /// 旧密码
  367. /// </summary>
  368. public string oldpassword { get; set; }
  369. }
  370. public class UserAccount
  371. {
  372. public string account { get; set; }
  373. }
  374. }