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
{
///
/// 创 建 人:王海洋
/// 创建日期:2018-12-10
/// 功能描述:账户管理控制类
///
[CheckLogin(false)]
public class AccountController : BaseController
{
IAccount account_obj = InterfaceFactory.CreateBusinessInstance();
// 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();
}
///
/// 验证当前用户的旧密码是否正确
///
///
///
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";
}
}
///
/// 修改用户密码
///
///
///
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();
}
///
/// 修改个人信息
///
///
public int UpdatePersonInfo(PersonBusinessModel model)
{
return account_obj.UpdatePersonInfo(model);
}
///
/// 获取信息
///
///
///
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 内网判断
///
/// 判断IP地址是否为内网IP地址
///
/// IP地址字符串
///
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;
}
///
/// 把IP地址转换为Long型数字
///
/// IP地址字符串
///
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;
}
///
/// 判断用户IP地址转换为Long型后是否在内网IP地址所在范围
///
///
///
///
///
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
}
}