AccountController.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Mvc;
  7. using PMS.Interface;
  8. using PMS.Interface.SysManager;
  9. using QWPlatform.SystemLibrary.ValidateCode;
  10. using QWPlatform.SystemLibrary.Web;
  11. using PMS.BusinessModels.Person;
  12. using System.Drawing;
  13. using QWPlatform.SystemLibrary.LogManager;
  14. using System.Data;
  15. using PMS.Interface.ProManager;
  16. using QWPlatform.SystemLibrary;
  17. using System.Web.Security;
  18. using PMS.BusinessModels.Account;
  19. namespace PMS.WebUI.Controllers
  20. {
  21. /// <summary>
  22. /// 创 建 人:王海洋
  23. /// 创建日期:2018-12-10
  24. /// 功能描述:账户管理控制类
  25. /// </summary>
  26. [CheckLogin(false)]
  27. public class AccountController : BaseController
  28. {
  29. IAccount account_obj = InterfaceFactory.CreateBusinessInstance<IAccount>();
  30. // GET: Account
  31. public ActionResult Login()
  32. {
  33. return View();
  34. }
  35. //退出
  36. [HttpGet]
  37. public ActionResult LogOut()
  38. {
  39. //todo:需要更新数据库在线状态
  40. //var user = this.;
  41. //account_obj.Logout();
  42. //获取当前用户信息
  43. var user = SysCom.Instance.GetCurrentAccount();
  44. if (user != null)
  45. { //清除本地缓存
  46. SysCom.Instance.ClearAccountCache(user.ID);
  47. }
  48. //退出登录 ,清除本地cookie,
  49. HttpCookie hc = Request.Cookies["UserID"];
  50. hc.Expires = DateTime.Now.AddDays(-1);
  51. hc.Path = "/";
  52. hc.Value = "";
  53. Response.AppendCookie(hc);
  54. return Content("OK");
  55. }
  56. //获取验证码
  57. [HttpGet]
  58. public ActionResult VCode()
  59. {
  60. VcodeImageCreator2 vcode = new VcodeImageCreator2();
  61. vcode.CodeType = "3";
  62. vcode.Chaos = true;
  63. vcode.IsTwist = true;
  64. var code = vcode.CreateVerifyCode(4);
  65. //记录到Session中
  66. this.Session["vcode"] = code;
  67. var bmp = vcode.CreateImageCode(code);
  68. using (var ms = new MemoryStream())
  69. {
  70. bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  71. return File(ms.ToArray(), "image/jpeg");
  72. }
  73. }
  74. //执行登录
  75. [HttpPost]
  76. public ActionResult CheckLogin(string account, string pwd, string vcode)
  77. {
  78. var code = this.Session["vcode"];
  79. var json = new PmsJsonResoult(System.Net.HttpStatusCode.OK, "登录成功", null);
  80. if (code == null || code.ToString().ToLower() != vcode.ToLower())
  81. {//验证码不正确
  82. json.msg = "验证码不正确,请重新录入";
  83. json.code = System.Net.HttpStatusCode.PreconditionFailed;
  84. return Content(json.ToString(), "application/json");
  85. }
  86. //清空验证码
  87. this.Session["vcode"] = null;
  88. //读取IP
  89. var ip = QWPlatform.SystemLibrary.Utils.Strings.GetWebClientIP();
  90. //到数据库中验证是否正确
  91. var r = account_obj.Login(account, pwd, ip);
  92. if (r.Success)
  93. {//登录成功
  94. return Content(new PmsJsonResoult(System.Net.HttpStatusCode.OK, r.Message, null).ToString(), "application/json");
  95. }
  96. else
  97. {//登录失败
  98. return Content(new PmsJsonResoult(System.Net.HttpStatusCode.Forbidden, r.Message, null).ToString(), "application/json");
  99. }
  100. }
  101. //修改密码页面
  102. public ActionResult Password()
  103. {
  104. return View();
  105. }
  106. /// <summary>
  107. /// 验证当前用户的旧密码是否正确
  108. /// </summary>
  109. /// <param name="pwd"></param>
  110. /// <returns></returns>
  111. public string CheckOldPwd(string pwd)
  112. {
  113. var u = SysCom.Instance.GetCurrentAccount();
  114. if (u == null)
  115. {//用户未登录
  116. return bool.FalseString;
  117. }
  118. else
  119. {
  120. //获取用户ID
  121. var id = u.ID;
  122. bool r = account_obj.CheckOldPwd(id, pwd);
  123. return r ? "true" : "false";
  124. }
  125. }
  126. /// <summary>
  127. /// 修改用户密码
  128. /// </summary>
  129. /// <param name="newpwd"></param>
  130. /// <returns></returns>
  131. public ActionResult ChangePassword(string newpwd)
  132. {
  133. var u = SysCom.Instance.GetCurrentAccount();
  134. if (u == null)
  135. {//用户未登录
  136. return new JsonContent(false, "用户未登录");
  137. }
  138. else
  139. {//获取当前用户
  140. var id = u.ID;
  141. bool r = account_obj.ChangePassword(id, newpwd);
  142. return new JsonContent(r, "完成修改");
  143. }
  144. }
  145. //修改个人信息页面
  146. public ActionResult PersonInfo()
  147. {
  148. var user = GetCurrentUser().PersonID;
  149. ViewBag.id = user;
  150. return View();
  151. }
  152. /// <summary>
  153. /// 修改个人信息
  154. /// </summary>
  155. /// <returns></returns>
  156. public int UpdatePersonInfo(PersonBusinessModel model)
  157. {
  158. return account_obj.UpdatePersonInfo(model);
  159. }
  160. /// <summary>
  161. /// 获取信息
  162. /// </summary>
  163. /// <param name="tel"></param>
  164. /// <returns></returns>
  165. public ActionResult GetTelAccountInfo(string tel)
  166. {
  167. string json = "";
  168. var dt= account_obj.GetTelAccountInfo(tel);
  169. UserInfo _userInfo = new UserInfo(); ;
  170. if (dt.Rows.Count>0)
  171. {
  172. if(dt.Rows[0]["密码变更时间"].ToString()=="")
  173. {
  174. json = "系统检测到您长时间未修改密码,建议您尽快修改密码,否则无法继续使用!";
  175. }
  176. else if ((dt.Rows[0]["密码变更时间"].ToDateTime()- DateTime.Now).Days<4)
  177. {
  178. json = "您的密码有效期剩余" + (dt.Rows[0]["密码变更时间"].ToDateTime() - DateTime.Now).Days + "天,请您尽快修改密码,到期后账号将自动停用!";
  179. }
  180. _userInfo = account_obj.GetAccountInfo(dt.Rows[0]["ID"].ToInt32());
  181. }
  182. return Content(new PmsJsonResoult(true, json, _userInfo.Roles).ToString(), "text/json");
  183. }
  184. #region 内网判断
  185. /// <summary>
  186. /// 判断IP地址是否为内网IP地址
  187. /// </summary>
  188. /// <param name="ipAddress">IP地址字符串</param>
  189. /// <returns></returns>
  190. public static bool IsInnerIP(String ipAddress)
  191. {
  192. if (ipAddress == "::1")
  193. {
  194. return true;
  195. }
  196. bool isInnerIp = false;
  197. long ipNum = GetIpNum(ipAddress);
  198. /**
  199. 私有IP:A类 10.0.0.0-10.255.255.255
  200. B类 172.16.0.0-172.31.255.255
  201. C类 192.168.0.0-192.168.255.255
  202. 当然,还有127这个网段是环回地址
  203. **/
  204. long aBegin = GetIpNum("10.0.0.0");
  205. long aEnd = GetIpNum("10.255.255.255");
  206. long bBegin = GetIpNum("172.16.0.0");
  207. long bEnd = GetIpNum("172.31.255.255");
  208. long cBegin = GetIpNum("192.168.0.0");
  209. long cEnd = GetIpNum("192.168.255.255");
  210. isInnerIp = IsInner(ipNum, cBegin, cEnd) || ipAddress.Equals("127.0.0.1");
  211. return isInnerIp;
  212. }
  213. /// <summary>
  214. /// 把IP地址转换为Long型数字
  215. /// </summary>
  216. /// <param name="ipAddress">IP地址字符串</param>
  217. /// <returns></returns>
  218. private static long GetIpNum(String ipAddress)
  219. {
  220. String[] ip = ipAddress.Split('.');
  221. long a = int.Parse(ip[0]);
  222. long b = int.Parse(ip[1]);
  223. long c = int.Parse(ip[2]);
  224. long d = int.Parse(ip[3]);
  225. long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
  226. return ipNum;
  227. }
  228. /// <summary>
  229. /// 判断用户IP地址转换为Long型后是否在内网IP地址所在范围
  230. /// </summary>
  231. /// <param name="userIp"></param>
  232. /// <param name="begin"></param>
  233. /// <param name="end"></param>
  234. /// <returns></returns>
  235. private static bool IsInner(long userIp, long begin, long end)
  236. {
  237. return (userIp >= begin) && (userIp <= end);
  238. }
  239. public ActionResult IpJudgment()
  240. {
  241. //读取IP
  242. var i = Request.UserHostAddress;
  243. //Logger.Instance.Info("发出请求的远程主机的IP地址"+ i);
  244. var ip = QWPlatform.SystemLibrary.Utils.Strings.GetWebClientIP();
  245. var t = IsInnerIP(ip);
  246. if (t&&this.Session["vcode"]!=null)
  247. {
  248. var code = this.Session["vcode"];
  249. return Content(code.ToString());
  250. }
  251. else
  252. {
  253. return Content("");
  254. }
  255. }
  256. #endregion
  257. }
  258. }