Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

416 рядки
14 KiB

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