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.
 
 
 
 
 
 

662 regels
24 KiB

  1. using Learun.Application.Base.SystemModule;
  2. using Learun.Application.Organization;
  3. using Learun.Application.TwoDevelopment.EducationalAdministration;
  4. using Learun.Cache.Base;
  5. using Learun.Cache.Factory;
  6. using Learun.Util;
  7. using Learun.Util.Operat;
  8. using Nancy;
  9. using Quanjiang.DigitalScholl.SendSms;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Configuration;
  13. using System.Linq;
  14. namespace Learun.Application.WebApi
  15. {
  16. /// <summary>
  17. /// 版 本 Learun-ADMS V7.0.0 数字化智慧校园
  18. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  19. /// 创建人:数字化智慧校园-框架开发组
  20. /// 日 期:2017.05.12
  21. /// 描 述:用户信息
  22. /// </summary>
  23. public class UserApi : BaseApi
  24. {
  25. /// <summary>
  26. /// 注册接口
  27. /// </summary>
  28. public UserApi()
  29. : base("/learun/adms/user")
  30. {
  31. Post["/login"] = Login;
  32. Post["/modifypw"] = ModifyPassword;
  33. Post["/modifypwiden"] = ModifyPasswordiden;
  34. Post["/unbundWeiXin"] = DoUnbundWeiXin;
  35. Post["/loginbyIdCard"] = LoginByIdCard;
  36. Post["/updateMobile"] = UpdateMobile;
  37. Get["/info"] = Info;
  38. Get["/map"] = GetMap;
  39. Get["/img"] = GetImg;
  40. Get["/imgfordc"] = GetImgForDC;
  41. Get["/saveMap"] = GetSaveClassMap;
  42. }
  43. private UserIBLL userIBLL = new UserBLL();
  44. private PostIBLL postIBLL = new PostBLL();
  45. private RoleIBLL roleIBLL = new RoleBLL();
  46. private StuInfoBasicIBLL stuInfoBasicIBLL = new StuInfoBasicBLL();
  47. CdMajorIBLL majorIbll = new CdMajorBLL();
  48. private readonly ISms aliyunSms = new AliyunSms();
  49. private ICache redisCache = CacheFactory.CaChe();
  50. /// <summary>
  51. /// 短信验证码校验
  52. /// </summary>
  53. /// <param name="_"></param>
  54. /// <returns></returns>
  55. private Response LoginCodeVerify(dynamic _)
  56. {
  57. MobileVerify mobileVerify = this.GetReqData<MobileVerify>();
  58. if (string.IsNullOrEmpty(mobileVerify.codeType))
  59. {
  60. return Fail("未指定短信类型。");
  61. }
  62. if (string.IsNullOrEmpty(mobileVerify.mobile))
  63. {
  64. return Fail("手机号不能为空。");
  65. }
  66. var code = redisCache.Read<string>("sendcodeinapp_" + mobileVerify.codeType + "_" + mobileVerify.mobile, CacheId.sms);
  67. if (!string.IsNullOrEmpty(code) && code == mobileVerify.verifycode)
  68. {
  69. return Success("验证成功。");
  70. }
  71. else
  72. {
  73. return Fail("验证失败,验证码错误或已失效。");
  74. }
  75. }
  76. /// <summary>
  77. /// 发送短信验证码
  78. /// </summary>
  79. /// <param name="_"></param>
  80. /// <returns></returns>
  81. private Response Sendcode(dynamic _)
  82. {
  83. LoginModel loginModel = this.GetReqData<LoginModel>();
  84. if (string.IsNullOrEmpty(loginModel.codeType))
  85. {
  86. return Fail("未指定短信类型。");
  87. }
  88. UserEntity userEntity = null;
  89. userEntity = userIBLL.GetEntityByUserId(userInfo.userId);
  90. if (userEntity == null)
  91. {
  92. return Fail("用户不存在!");
  93. }
  94. if (loginModel.codeType == "unbindwx")
  95. {
  96. if (string.IsNullOrEmpty(userEntity.F_Mobile))
  97. {
  98. return Fail("用户手机号不存在!");
  99. }
  100. loginModel.username = userEntity.F_Mobile;
  101. }
  102. if (string.IsNullOrEmpty(loginModel.username))
  103. {
  104. return Fail("手机号不能为空。");
  105. }
  106. if (!CommonHelper.IsValidMobile(loginModel.username))
  107. {
  108. return Fail("手机号格式不正确!");
  109. }
  110. var listStr = new List<string>();
  111. var result = aliyunSms.SendSmsToSingle(loginModel.username, SmsType.LoginBind, listStr);
  112. if (result.Result.code == "OK")
  113. {
  114. redisCache.Write<string>("sendcodeinapp_" + loginModel.codeType + "_" + loginModel.username, result.Result.randomNum, new TimeSpan(0, 5, 0), CacheId.sms);
  115. //日志
  116. LogEntity logEntity = new LogEntity();
  117. logEntity.F_CategoryId = 3;
  118. logEntity.F_SourceObjectId = loginModel.codeType;
  119. logEntity.F_OperateTypeId = "sms";
  120. logEntity.F_OperateType = "sms";
  121. logEntity.F_OperateAccount = "system";
  122. logEntity.F_ExecuteResult = 200;
  123. logEntity.F_ExecuteResultJson = "短信发送成功:" + result.Result.message;
  124. logEntity.F_Description = "短信发送:" + loginModel.username + " 验证码:" + result.Result.randomNum;
  125. logEntity.WriteLog();
  126. return Success("短信发送成功:" + result.Result.message);
  127. }
  128. else
  129. {
  130. LogEntity logEntity = new LogEntity();
  131. logEntity.F_CategoryId = 4;
  132. logEntity.F_SourceObjectId = loginModel.codeType;
  133. logEntity.F_OperateTypeId = "sms";
  134. logEntity.F_OperateType = "sms";
  135. logEntity.F_OperateAccount = "system";
  136. logEntity.F_ExecuteResult = 400;
  137. logEntity.F_ExecuteResultJson = "短信发送失败:" + result.Result.message + result.Result.errorType;
  138. logEntity.F_Description = "短信发送:" + loginModel.username;
  139. logEntity.WriteLog();
  140. return Fail("短信发送失败:" + result.Result.message + result.Result.errorType);
  141. }
  142. }
  143. /// <summary>
  144. /// 登录接口
  145. /// </summary>
  146. /// <param name="_"></param>
  147. /// <returns></returns>
  148. private Response Login(dynamic _)
  149. {
  150. LoginModel loginModel = this.GetReqData<LoginModel>();
  151. #region 内部账户验证
  152. UserEntity userEntity = userIBLL.CheckLogin(loginModel.username, loginModel.password);
  153. userEntity.F_Password = null;
  154. userEntity.F_IdentityCardNo = null;
  155. userEntity.F_Secretkey = null;
  156. #region 写入日志
  157. LogEntity logEntity = new LogEntity();
  158. logEntity.F_CategoryId = 1;
  159. logEntity.F_OperateTypeId = ((int)OperationType.Login).ToString();
  160. logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.Login);
  161. logEntity.F_OperateAccount = loginModel.username + "(" + userEntity.F_RealName + ")";
  162. logEntity.F_OperateUserId = !string.IsNullOrEmpty(userEntity.F_UserId) ? userEntity.F_UserId : loginModel.username;
  163. logEntity.F_Module = Config.GetValue("SoftName");
  164. logEntity.F_Description = "移动端";
  165. #endregion
  166. if (!userEntity.LoginOk)//登录失败
  167. {
  168. //写入日志
  169. logEntity.F_ExecuteResult = 0;
  170. logEntity.F_ExecuteResultJson = "登录失败:" + userEntity.LoginMsg;
  171. logEntity.WriteLog();
  172. return Fail(userEntity.LoginMsg);
  173. }
  174. else
  175. {
  176. //记录ip
  177. userIBLL.UpdateIp(GetIP(), userEntity.F_UserId);
  178. string token = OperatorHelper.Instance.AddLoginUser(userEntity.F_Account, "Learun_ADMS_6.1_App", this.loginMark, false);//写入缓存信息
  179. //写入日志
  180. logEntity.F_ExecuteResult = 1;
  181. logEntity.F_ExecuteResultJson = "登录成功";
  182. logEntity.WriteLog();
  183. //保存用户设备号
  184. userIBLL.UpdateDeviceId(userEntity.F_UserId, loginModel.deviceid);
  185. OperatorResult res = OperatorHelper.Instance.IsOnLine(token, this.loginMark);
  186. res.userInfo.password = null;
  187. res.userInfo.secretkey = null;
  188. var studententity = stuInfoBasicIBLL.GetStuInfoBasicEntityByStuNo(userEntity.F_Account);
  189. if (studententity != null)
  190. {
  191. res.userInfo.grade = studententity.Grade;
  192. var majorinfo = majorIbll.GetCdMajorEntityByMajorNo(studententity.MajorNo);
  193. if (majorinfo != null)
  194. {
  195. res.userInfo.majorno = majorinfo.ID ?? "";
  196. }
  197. }
  198. //是否强密码验证
  199. bool pwd = false;
  200. if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["verifypwd"]) && ConfigurationManager.AppSettings["verifypwd"] == "true" && loginModel.up == false)
  201. {
  202. pwd = true;
  203. }
  204. var jsonData = new
  205. {
  206. baseinfo = res.userInfo,
  207. post = postIBLL.GetListByPostIds(res.userInfo.postIds),
  208. role = roleIBLL.GetListByRoleIds(res.userInfo.roleIds),
  209. pwd= pwd
  210. };
  211. return Success(jsonData);
  212. }
  213. #endregion
  214. }
  215. /// <summary>
  216. /// 身份验证-登录接口
  217. /// </summary>
  218. /// <param name="_"></param>
  219. /// <returns></returns>
  220. private Response LoginByIdCard(dynamic _)
  221. {
  222. LoginModel loginModel = this.GetReqData<LoginModel>();
  223. #region 内部账户验证
  224. UserEntity userEntity = userIBLL.CheckLoginByIdCard(loginModel.username, loginModel.password);
  225. #region 写入日志
  226. LogEntity logEntity = new LogEntity();
  227. logEntity.F_CategoryId = 1;
  228. logEntity.F_OperateTypeId = ((int)OperationType.Login).ToString();
  229. logEntity.F_OperateType = EnumAttribute.GetDescription(OperationType.Login);
  230. logEntity.F_OperateAccount = loginModel.username + "(" + userEntity.F_RealName + ")";
  231. logEntity.F_OperateUserId = !string.IsNullOrEmpty(userEntity.F_UserId) ? userEntity.F_UserId : loginModel.username;
  232. logEntity.F_Module = Config.GetValue("SoftName");
  233. logEntity.F_Description = "移动端";
  234. #endregion
  235. if (!userEntity.LoginOk)//登录失败
  236. {
  237. //写入日志
  238. logEntity.F_ExecuteResult = 0;
  239. logEntity.F_ExecuteResultJson = "登录失败:" + userEntity.LoginMsg;
  240. logEntity.WriteLog();
  241. return Fail(userEntity.LoginMsg);
  242. }
  243. else
  244. {
  245. //新增新生判断
  246. var stuinfobasic = stuInfoBasicIBLL.GetStuInfoBasicEntityByStuNo(userEntity.F_EnCode);
  247. if (stuinfobasic!=null&&stuinfobasic.Grade!="21")
  248. {
  249. userEntity.LoginMsg = "只有新生支持身份证方式登录";
  250. return Fail(userEntity.LoginMsg);
  251. }
  252. //记录ip
  253. userIBLL.UpdateIp(GetIP(), userEntity.F_UserId);
  254. string token = OperatorHelper.Instance.AddLoginUser(userEntity.F_Account, "Learun_ADMS_6.1_App", this.loginMark, false);//写入缓存信息
  255. //写入日志
  256. logEntity.F_ExecuteResult = 1;
  257. logEntity.F_ExecuteResultJson = "登录成功";
  258. logEntity.WriteLog();
  259. //保存用户设备号
  260. userIBLL.UpdateDeviceId(userEntity.F_UserId, loginModel.deviceid);
  261. OperatorResult res = OperatorHelper.Instance.IsOnLine(token, this.loginMark);
  262. res.userInfo.password = null;
  263. res.userInfo.secretkey = null;
  264. var studententity = stuInfoBasicIBLL.GetStuInfoBasicEntityByStuNo(userEntity.F_Account);
  265. if (studententity != null)
  266. {
  267. res.userInfo.grade = studententity.Grade;
  268. var majorinfo = majorIbll.GetCdMajorEntityByMajorNo(studententity.MajorNo);
  269. if (majorinfo != null)
  270. {
  271. res.userInfo.majorno = majorinfo.ID ?? "";
  272. }
  273. }
  274. //是否强密码验证
  275. bool pwd = false;
  276. if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["verifypwd"]) && ConfigurationManager.AppSettings["verifypwd"] == "true" && loginModel.up == false)
  277. {
  278. pwd = true;
  279. }
  280. var jsonData = new
  281. {
  282. baseinfo = res.userInfo,
  283. post = postIBLL.GetListByPostIds(res.userInfo.postIds),
  284. role = roleIBLL.GetListByRoleIds(res.userInfo.roleIds),
  285. pwd = pwd
  286. };
  287. return Success(jsonData);
  288. }
  289. #endregion
  290. }
  291. /// <summary>
  292. /// 获取用户信息
  293. /// </summary>
  294. /// <param name="_"></param>
  295. /// <returns></returns>
  296. private Response Info(dynamic _)
  297. {
  298. var data = userInfo;
  299. data.password = null;
  300. data.secretkey = null;
  301. var jsonData = new
  302. {
  303. baseinfo = data,
  304. post = postIBLL.GetListByPostIds(data.postIds),
  305. role = roleIBLL.GetListByRoleIds(data.roleIds)
  306. };
  307. return Success(jsonData);
  308. }
  309. /// <summary>
  310. /// 修改密码
  311. /// </summary>
  312. /// <param name="_"></param>
  313. /// <returns></returns>
  314. private Response ModifyPassword(dynamic _)
  315. {
  316. ModifyModel modifyModel = this.GetReqData<ModifyModel>();
  317. if (userInfo.isSystem)
  318. {
  319. return Fail("当前账户不能修改密码");
  320. }
  321. else
  322. {
  323. bool res = userIBLL.RevisePassword(modifyModel.newpassword, modifyModel.oldpassword);
  324. if (!res)
  325. {
  326. return Fail("原密码错误,请重新输入");
  327. }
  328. else
  329. {
  330. return Success("密码修改成功");
  331. }
  332. }
  333. }
  334. private Response ModifyPasswordiden(dynamic _)
  335. {
  336. ModifyModel modifyModel = this.GetReqData<ModifyModel>();
  337. if (userInfo.isSystem)
  338. {
  339. return Fail("当前账户不能修改密码");
  340. }
  341. else
  342. {
  343. bool res = userIBLL.RevisePasswordiden(modifyModel.newpassword, modifyModel.oldpassword);
  344. if (!res)
  345. {
  346. return Fail("原密码错误,请重新输入");
  347. }
  348. else
  349. {
  350. return Success("密码修改成功");
  351. }
  352. }
  353. }
  354. /// <summary>
  355. /// 解绑微信
  356. /// </summary>
  357. /// <param name="_"></param>
  358. /// <returns></returns>
  359. private Response DoUnbundWeiXin(dynamic _)
  360. {
  361. userIBLL.DoUnbundWeiXin(userInfo.userId);
  362. return Success("解绑成功");
  363. }
  364. /// <summary>
  365. /// 修改手机号
  366. /// </summary>
  367. /// <param name="_"></param>
  368. /// <returns></returns>
  369. private Response UpdateMobile(dynamic _)
  370. {
  371. MobileVerify mobileVerify = this.GetReqData<MobileVerify>();
  372. if (string.IsNullOrEmpty(mobileVerify.codeType))
  373. {
  374. return Fail("未指定短信类型。");
  375. }
  376. if (string.IsNullOrEmpty(mobileVerify.mobile))
  377. {
  378. return Fail("手机号不能为空。");
  379. }
  380. var code = redisCache.Read<string>("sendcodeinapp_" + mobileVerify.codeType + "_" + mobileVerify.mobile, CacheId.sms);
  381. if (!string.IsNullOrEmpty(code) && code == mobileVerify.verifycode)
  382. {
  383. //return Success("验证成功。");
  384. userIBLL.UpdateMobile(userInfo.userId, mobileVerify.mobile);
  385. return Success("修改成功");
  386. }
  387. else
  388. {
  389. return Fail("验证失败,验证码错误或已失效。");
  390. }
  391. }
  392. /// <summary>
  393. /// 获取所有员工账号列表
  394. /// </summary>
  395. /// <param name="_"></param>
  396. /// <returns></returns>
  397. private Response GetList(dynamic _)
  398. {
  399. var data = userInfo;
  400. data.password = null;
  401. data.secretkey = null;
  402. var jsonData = new
  403. {
  404. baseinfo = data,
  405. post = postIBLL.GetListByPostIds(data.postIds),
  406. role = roleIBLL.GetListByRoleIds(data.roleIds)
  407. };
  408. return Success(jsonData);
  409. }
  410. /// <summary>
  411. /// 获取用户映射表
  412. /// </summary>
  413. /// <param name="_"></param>
  414. /// <returns></returns>
  415. public Response GetMap(dynamic _)
  416. {
  417. string ver = this.GetReqData();// 获取模板请求数据
  418. var data = userIBLL.GetModelMap();
  419. string md5 = Md5Helper.Encrypt(data.ToJson(), 32);
  420. if (md5 == ver)
  421. {
  422. return Success("no update");
  423. }
  424. else
  425. {
  426. var jsondata = new
  427. {
  428. data = data,
  429. ver = md5
  430. };
  431. return Success(jsondata);
  432. }
  433. }
  434. public Response GetSaveClassMap(dynamic _)
  435. {
  436. string account = this.GetReqData<UserAccount>().account;
  437. var data = userIBLL.GetSaveClassMap();
  438. var students = stuInfoBasicIBLL.GetSaveClassStudents(account);
  439. data = data.Where(a => students.Contains(a.F_Account) && a.F_Account != account).ToList();
  440. var dic = new Dictionary<string, UserModel>();
  441. foreach (var item in data)
  442. {
  443. UserModel model = new UserModel()
  444. {
  445. companyId = item.F_CompanyId,
  446. departmentId = item.F_DepartmentId,
  447. name = item.F_RealName,
  448. id = item.F_UserId,
  449. };
  450. string img = "";
  451. if (!string.IsNullOrEmpty(item.F_HeadIcon))
  452. {
  453. string fileHeadImg = Config.GetValue("fileHeadImg");
  454. string fileImg = string.Format("{0}/{1}{2}", fileHeadImg, item.F_UserId, item.F_HeadIcon);
  455. if (DirFileHelper.IsExistFile(fileImg))
  456. {
  457. img = item.F_HeadIcon;
  458. }
  459. }
  460. if (string.IsNullOrEmpty(img))
  461. {
  462. if (item.F_Gender == 0)
  463. {
  464. img = "0";
  465. }
  466. else
  467. {
  468. img = "1";
  469. }
  470. }
  471. model.img = img;
  472. dic.Add(item.F_UserId, model);
  473. }
  474. string md5 = Md5Helper.Encrypt(dic.ToJson(), 32);
  475. var jsondata = new
  476. {
  477. data = dic,
  478. var = md5
  479. };
  480. return Success(jsondata);
  481. }
  482. /// <summary>
  483. /// 获取人员头像图标
  484. /// </summary>
  485. /// <param name="_"></param>
  486. /// <returns></returns>
  487. public Response GetImg(dynamic _)
  488. {
  489. string userId = this.GetReqData();// 获取模板请求数据
  490. userIBLL.GetImg(userId);
  491. return Success("获取成功");
  492. }
  493. /// <summary>
  494. /// 获取人员头像图标
  495. /// </summary>
  496. /// <param name="_"></param>
  497. /// <returns></returns>
  498. public Response GetImgForDC(dynamic _)
  499. {
  500. string userId = this.GetReqData();// 获取模板请求数据
  501. userIBLL.GetImgForDC(userId);
  502. return Success("获取成功");
  503. }
  504. /// <summary>
  505. /// 获取IP
  506. /// </summary>
  507. /// <returns></returns>
  508. private string GetIP()
  509. {
  510. //string ip = string.Empty;
  511. //if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
  512. // ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
  513. //if (string.IsNullOrEmpty(ip))
  514. // ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
  515. //return ip;
  516. string userIP = "未获取用户IP";
  517. try
  518. {
  519. if (System.Web.HttpContext.Current == null
  520. || System.Web.HttpContext.Current.Request == null
  521. || System.Web.HttpContext.Current.Request.ServerVariables == null)
  522. {
  523. return "";
  524. }
  525. string CustomerIP = "";
  526. //CDN加速后取到的IP simone 090805
  527. CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
  528. if (!string.IsNullOrEmpty(CustomerIP))
  529. {
  530. return CustomerIP;
  531. }
  532. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  533. if (!string.IsNullOrEmpty(CustomerIP))
  534. {
  535. return CustomerIP;
  536. }
  537. if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
  538. {
  539. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  540. if (CustomerIP == null)
  541. {
  542. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  543. }
  544. }
  545. else
  546. {
  547. CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  548. }
  549. if (string.Compare(CustomerIP, "unknown", true) == 0 || string.IsNullOrEmpty(CustomerIP))
  550. {
  551. return System.Web.HttpContext.Current.Request.UserHostAddress;
  552. }
  553. return CustomerIP;
  554. }
  555. catch { }
  556. return userIP;
  557. }
  558. }
  559. /// <summary>
  560. /// 登录信息
  561. /// </summary>
  562. public class LoginModel
  563. {
  564. /// <summary>
  565. /// 账号
  566. /// </summary>
  567. public string username { get; set; }
  568. /// <summary>
  569. /// 密码
  570. /// </summary>
  571. public string password { get; set; }
  572. /// <summary>
  573. /// 是否强密码
  574. /// </summary>
  575. public bool up { get; set; }
  576. /// <summary>
  577. /// 设备号
  578. /// </summary>
  579. public string deviceid { get; set; }
  580. public string openid { get; set; }
  581. /// <summary>
  582. /// 发送短信类型:忘记密码forgetpwd,首次登录firstlogin,绑定微信bindwx,解绑微信unbindwx,修改手机号modifymobile,;
  583. /// </summary>
  584. public string codeType { get; set; }
  585. /// <summary>
  586. /// 短信验证码
  587. /// </summary>
  588. public string verifycode { get; set; }
  589. }
  590. /// <summary>
  591. /// 修改密码
  592. /// </summary>
  593. public class ModifyModel
  594. {
  595. public string phone { set; get; }
  596. /// <summary>
  597. /// 新密码
  598. /// </summary>
  599. public string newpassword { get; set; }
  600. /// <summary>
  601. /// 旧密码
  602. /// </summary>
  603. public string oldpassword { get; set; }
  604. }
  605. public class MobileVerify
  606. {
  607. public string mobile { get; set; }
  608. public string verifycode { get; set; }
  609. /// <summary>
  610. /// 发送短信类型
  611. /// </summary>
  612. public string codeType { get; set; }
  613. }
  614. public class UserAccount
  615. {
  616. public string account { get; set; }
  617. }
  618. }