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.
 
 
 
 
 
 

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