Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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