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.
 
 
 
 
 
 

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