usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;usingSystem.Web;usingSystem.Web.Mvc;usingWolfy.NetDisk.Model;usingWolfy.NetDisk.IBLL;usingWolfy.NetDisk.BLL;usingWolfy.NetDisk.Utilities;usingSystem.IO;usingSystem.Drawing;usingNewtonsoft.Json.Linq;usingSystem.Runtime.Serialization.Formatters.Binary;namespaceWolfy.NetDisk.Site.Controllers
{public classUserInfoController : AsyncController
{private IUserInfoServiceRepository _userInfoServiceRepository = newUserInfoServiceRepository();private IDepartmentServiceRepository _departmentServiceRepository = newDepartmentServiceRepository();///
///用户信息列表///
///
publicActionResult Users()
{var users = _userInfoServiceRepository.FindAll(x => x.UserName != "");returnView(users);
}
[HttpGet]publicActionResult Register()
{
ViewBag.ChildTitle= "用户注册";
IQueryable departments = _departmentServiceRepository.FindAll(x => x.Pid != 0);
List lstItemt = new List();foreach (var item indepartments)
{
lstItemt.Add(new SelectListItem() { Text = item.Name, Value =item.Id.ToString() });
}
ViewData["departments"] =lstItemt;returnView();
}
[HttpPost]public JsonResult Register(stringuser)
{var data = Request.Form["data"];
JObject jobj=JObject.Parse(data);
UserInfo userInfo= newUserInfo()
{
CreateDt=DateTime.Now,
Gender=GenderType.保密,
Header= jobj["header"] != null ? jobj["header"].ToString() : "",
LoginDt=DateTime.Now,
LoginOutDt=DateTime.Now,
Pwd= jobj["pwd"] != null ? jobj["pwd"].ToString() : string.Empty,
UserName= jobj["userName"] != null ? jobj["userName"].ToString() : string.Empty
};string fileName = jobj["fileName"] != null ? jobj["fileName"].ToString() : "";string code = jobj["code"] != null ? jobj["code"].ToString() : "";int intDepartId = Convert.ToInt32(jobj["department"]);
userInfo.Department= _departmentServiceRepository.Find(x => x.Id ==intDepartId);int saveCount = 0;string strFileSavePath = string.Empty;
System.Web.Script.Serialization.JavaScriptSerializer Jss= newSystem.Web.Script.Serialization.JavaScriptSerializer();if (string.IsNullOrEmpty(code))
{var response = new{
_code= 1,
msg= "验证码不能为空"};return new JsonResult() { Data =Jss.Serialize(response) };
}if (code.ToLower() != Session["code"].ToString().ToLower())
{var response = new{
_code= 2,
msg= "验证码不正确,请重新输入"};return new JsonResult() { Data =Jss.Serialize(response) };
}if(_userInfoServiceRepository.Exist(userInfo.UserName))
{var response = new{
_code= 3,
msg= "该用户名已经存在,请重新输入"};return new JsonResult() { Data =Jss.Serialize(response) };
}if (!string.IsNullOrEmpty(fileName) && !string.IsNullOrEmpty(userInfo.Header))
{//说明上传头像
strFileSavePath = Request.MapPath("~/Content/Images");string strFileExtention =Path.GetExtension(fileName);if (!Directory.Exists(strFileSavePath))
{
Directory.CreateDirectory(strFileSavePath);
}
strFileSavePath+= "/" + userInfo.UserName +strFileExtention;string relativePath = "/Content/Headers/" + userInfo.UserName +strFileExtention;//将base64字符串转化为图片
byte[] buffer = Convert.FromBase64String(userInfo.Header.Split(',')[1]);
MemoryStream memStream= newMemoryStream(buffer);
SaveImageByWidthHeight(50, 50, memStream, strFileSavePath);
memStream.Dispose();
userInfo.Header=relativePath;
}//对密码进行处理
userInfo.Pwd =MD5Helper.GetMD5FromString(userInfo.Pwd);
_userInfoServiceRepository.Add(userInfo);
saveCount=_userInfoServiceRepository.SaveChanges();if (saveCount > 0)
{var response = new{
_code= 4,
msg= "添加成功",
img=userInfo.Header
};return new JsonResult() { Data =Jss.Serialize(response) };
}else{//添加失败,将头像删除
if(System.IO.File.Exists(strFileSavePath))
{
System.IO.File.Delete(strFileSavePath);
}var response = new{
_code= 5,
msg= "添加失败"};return new JsonResult() { Data =Jss.Serialize(response) };
}
}///
///等比例压缩图片///
private void SaveImageByWidthHeight(int intImgCompressWidth, int intImgCompressHeight, Stream stream, stringstrFileSavePath)
{//从输入流中获取上传的image对象
using (Image img =Image.FromStream(stream))
{//根据压缩比例求出图片的宽度
int intWidth = intImgCompressWidth / intImgCompressHeight *img.Height;int intHeight = img.Width * intImgCompressHeight /intImgCompressWidth;//画布
using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(img, newSize(intImgCompressWidth, intImgCompressHeight)))
{//在画布上创建画笔对象
using (System.Drawing.Graphics graphics =System.Drawing.Graphics.FromImage(bitmap))
{//将图片使用压缩后的宽高,从0,0位置画在画布上
graphics.DrawImage(img, 0, 0, intImgCompressWidth, intImgCompressHeight);//保存图片
bitmap.Save(strFileSavePath);
}
}
}
}
[HttpGet]publicActionResult VerifyCodeImage()
{string strCode = string.Empty;byte[] buffer = VerifyCode.Create(6, outstrCode);
Session["code"] =strCode;return File(buffer, @"image/jpeg");
}//
//GET: /Home/
[HttpGet]publicActionResult Login()
{
UserInfo userInfo= null;if (Request.Cookies["n"] != null && Request.Cookies["p"] != null)
{string userName = Request.Cookies["n"].Value;string pwd = Request.Cookies["p"].Value;
userInfo= _userInfoServiceRepository.Find(x => x.UserName == userName && x.Pwd ==pwd);
}return RedirectToAction("FileList", "Home");
}///
///登录///
///
///
///
[HttpPost]public JsonResult Login(string userName, string pwd, stringremember)
{if (!string.IsNullOrEmpty(pwd))
{
pwd=MD5Helper.GetMD5FromString(pwd);
}
UserInfo userInfo= _userInfoServiceRepository.Find(x => x.UserName == userName && x.Pwd ==pwd);if (!string.IsNullOrEmpty(remember) && remember.Equals("checked"))
{
HttpCookie nameCookie= new HttpCookie("n", userName);
nameCookie.Expires= DateTime.Now.AddDays(7);//将md5串写入cookie,或者再次进行AES加密写入
HttpCookie pwdCookie = new HttpCookie("p", pwd);
pwdCookie.Expires= DateTime.Now.AddDays(7);
Response.Cookies.Add(nameCookie);
Response.Cookies.Add(pwdCookie);
}if (userInfo != null)
{return new JsonResult() { Data = "200"};
}else{return new JsonResult() { Data = "401"};
}
}
}
}