Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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