123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using PMS.Interface;
- using PMS.Interface.SysManager;
- using QWPlatform.SystemLibrary.ValidateCode;
- using QWPlatform.SystemLibrary.Web;
- using PMS.BusinessModels.Person;
- using System.Drawing;
- using QWPlatform.SystemLibrary.LogManager;
- using System.Data;
- using PMS.Interface.ProManager;
- using QWPlatform.SystemLibrary;
- using System.Web.Security;
- using PMS.BusinessModels.Account;
- namespace PMS.WebUI.Controllers
- {
- /// <summary>
- /// 创 建 人:王海洋
- /// 创建日期:2018-12-10
- /// 功能描述:账户管理控制类
- /// </summary>
- [CheckLogin(false)]
- public class AccountController : BaseController
- {
- IAccount account_obj = InterfaceFactory.CreateBusinessInstance<IAccount>();
- // GET: Account
- public ActionResult Login()
- {
- return View();
- }
- //退出
- [HttpGet]
- public ActionResult LogOut()
- {
- //todo:需要更新数据库在线状态
- //var user = this.;
- //account_obj.Logout();
- //获取当前用户信息
- var user = SysCom.Instance.GetCurrentAccount();
- if (user != null)
- { //清除本地缓存
- SysCom.Instance.ClearAccountCache(user.ID);
- }
- //退出登录 ,清除本地cookie,
- HttpCookie hc = Request.Cookies["UserID"];
- hc.Expires = DateTime.Now.AddDays(-1);
- hc.Path = "/";
- hc.Value = "";
- Response.AppendCookie(hc);
- return Content("OK");
- }
- //获取验证码
- [HttpGet]
- public ActionResult VCode()
- {
- VcodeImageCreator2 vcode = new VcodeImageCreator2();
- vcode.CodeType = "3";
- vcode.Chaos = true;
- vcode.IsTwist = true;
- var code = vcode.CreateVerifyCode(4);
- //记录到Session中
- this.Session["vcode"] = code;
- var bmp = vcode.CreateImageCode(code);
- using (var ms = new MemoryStream())
- {
- bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
- return File(ms.ToArray(), "image/jpeg");
- }
- }
- //执行登录
- [HttpPost]
- public ActionResult CheckLogin(string account, string pwd, string vcode)
- {
- var code = this.Session["vcode"];
- var json = new PmsJsonResoult(System.Net.HttpStatusCode.OK, "登录成功", null);
- if (code == null || code.ToString().ToLower() != vcode.ToLower())
- {//验证码不正确
- json.msg = "验证码不正确,请重新录入";
- json.code = System.Net.HttpStatusCode.PreconditionFailed;
- return Content(json.ToString(), "application/json");
- }
- //清空验证码
- this.Session["vcode"] = null;
- //读取IP
- var ip = QWPlatform.SystemLibrary.Utils.Strings.GetWebClientIP();
- //到数据库中验证是否正确
- var r = account_obj.Login(account, pwd, ip);
- if (r.Success)
- {//登录成功
- return Content(new PmsJsonResoult(System.Net.HttpStatusCode.OK, r.Message, null).ToString(), "application/json");
- }
- else
- {//登录失败
- return Content(new PmsJsonResoult(System.Net.HttpStatusCode.Forbidden, r.Message, null).ToString(), "application/json");
- }
- }
- //修改密码页面
- public ActionResult Password()
- {
- return View();
- }
- /// <summary>
- /// 验证当前用户的旧密码是否正确
- /// </summary>
- /// <param name="pwd"></param>
- /// <returns></returns>
- public string CheckOldPwd(string pwd)
- {
- var u = SysCom.Instance.GetCurrentAccount();
- if (u == null)
- {//用户未登录
- return bool.FalseString;
- }
- else
- {
- //获取用户ID
- var id = u.ID;
- bool r = account_obj.CheckOldPwd(id, pwd);
- return r ? "true" : "false";
- }
- }
- /// <summary>
- /// 修改用户密码
- /// </summary>
- /// <param name="newpwd"></param>
- /// <returns></returns>
- public ActionResult ChangePassword(string newpwd)
- {
- var u = SysCom.Instance.GetCurrentAccount();
- if (u == null)
- {//用户未登录
- return new JsonContent(false, "用户未登录");
- }
- else
- {//获取当前用户
- var id = u.ID;
- bool r = account_obj.ChangePassword(id, newpwd);
- return new JsonContent(r, "完成修改");
- }
- }
- //修改个人信息页面
- public ActionResult PersonInfo()
- {
- var user = GetCurrentUser().PersonID;
- ViewBag.id = user;
- return View();
- }
- /// <summary>
- /// 修改个人信息
- /// </summary>
- /// <returns></returns>
- public int UpdatePersonInfo(PersonBusinessModel model)
- {
- return account_obj.UpdatePersonInfo(model);
- }
- /// <summary>
- /// 获取信息
- /// </summary>
- /// <param name="tel"></param>
- /// <returns></returns>
- public ActionResult GetTelAccountInfo(string tel)
- {
- string json = "";
- var dt= account_obj.GetTelAccountInfo(tel);
- UserInfo _userInfo = new UserInfo(); ;
- if (dt.Rows.Count>0)
- {
- if(dt.Rows[0]["密码变更时间"].ToString()=="")
- {
- json = "系统检测到您长时间未修改密码,建议您尽快修改密码,否则无法继续使用!";
- }
- else if ((dt.Rows[0]["密码变更时间"].ToDateTime()- DateTime.Now).Days<4)
- {
- json = "您的密码有效期剩余" + (dt.Rows[0]["密码变更时间"].ToDateTime() - DateTime.Now).Days + "天,请您尽快修改密码,到期后账号将自动停用!";
- }
- _userInfo = account_obj.GetAccountInfo(dt.Rows[0]["ID"].ToInt32());
- }
- return Content(new PmsJsonResoult(true, json, _userInfo.Roles).ToString(), "text/json");
- }
-
- #region 内网判断
- /// <summary>
- /// 判断IP地址是否为内网IP地址
- /// </summary>
- /// <param name="ipAddress">IP地址字符串</param>
- /// <returns></returns>
- public static bool IsInnerIP(String ipAddress)
- {
- if (ipAddress == "::1")
- {
- return true;
- }
- bool isInnerIp = false;
- long ipNum = GetIpNum(ipAddress);
- /**
- 私有IP:A类 10.0.0.0-10.255.255.255
- B类 172.16.0.0-172.31.255.255
- C类 192.168.0.0-192.168.255.255
- 当然,还有127这个网段是环回地址
- **/
- long aBegin = GetIpNum("10.0.0.0");
- long aEnd = GetIpNum("10.255.255.255");
- long bBegin = GetIpNum("172.16.0.0");
- long bEnd = GetIpNum("172.31.255.255");
- long cBegin = GetIpNum("192.168.0.0");
- long cEnd = GetIpNum("192.168.255.255");
- isInnerIp = IsInner(ipNum, cBegin, cEnd) || ipAddress.Equals("127.0.0.1");
- return isInnerIp;
- }
- /// <summary>
- /// 把IP地址转换为Long型数字
- /// </summary>
- /// <param name="ipAddress">IP地址字符串</param>
- /// <returns></returns>
- private static long GetIpNum(String ipAddress)
- {
- String[] ip = ipAddress.Split('.');
- long a = int.Parse(ip[0]);
- long b = int.Parse(ip[1]);
- long c = int.Parse(ip[2]);
- long d = int.Parse(ip[3]);
- long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;
- return ipNum;
- }
- /// <summary>
- /// 判断用户IP地址转换为Long型后是否在内网IP地址所在范围
- /// </summary>
- /// <param name="userIp"></param>
- /// <param name="begin"></param>
- /// <param name="end"></param>
- /// <returns></returns>
- private static bool IsInner(long userIp, long begin, long end)
- {
- return (userIp >= begin) && (userIp <= end);
- }
- public ActionResult IpJudgment()
- {
- //读取IP
- var i = Request.UserHostAddress;
- //Logger.Instance.Info("发出请求的远程主机的IP地址"+ i);
- var ip = QWPlatform.SystemLibrary.Utils.Strings.GetWebClientIP();
- var t = IsInnerIP(ip);
- if (t&&this.Session["vcode"]!=null)
- {
- var code = this.Session["vcode"];
- return Content(code.ToString());
- }
- else
- {
- return Content("");
- }
- }
- #endregion
- }
- }
|