using BookingWeb.DB; using BookingWeb.DB.Model; using BookingWeb.Models; using Resources; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Caching; using System.Text; using System.Web; using System.Web.Mvc; using System.Web.Security; namespace BookingWeb.Controllers { public class UserController : BaseController { private BookingDB bookingDB = new BookingDB(); #region 登录 [AllowAnonymous] [HttpGet] public ActionResult Login(string returnUrl) { ViewBag.ReturnUrl = returnUrl; return View(); } [AllowAnonymous] [HttpPost] public ActionResult Login(string mobile, string password, bool? withCode) { RespCommon resp = new RespCommon(); var user = bookingDB.Users.FirstOrDefault(u => u.MOBILE == mobile && u.CLIENT_ID == ClientId); if (user != null) { if (user.PASSWORD == password) { if (user.STATUS == UserStatus.Active.ToString()) //判断账号状态 { //FormsAuthentication.SetAuthCookie(mobile, false); //HttpContext.Response.Cookies[FormsAuthentication.FormsCookieName].Expires = DateTime.Now.AddDays(1); if (user.IS_ADMIN) { InitCurrentUser(user); InitCurrentCompany(user); resp.Success = true; if (withCode.HasValue && withCode.Value) { string code = Guid.NewGuid().ToString().Replace("-", ""); resp.Message = code; string origin = Request.Headers["origin"]; MemoryCache.Default.Add(new CacheItem(code, new { mobile = mobile, clientId = ClientId, origin }), new CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.AddSeconds(5) }); } else { resp.Message = LangLogin.MsgSuccess; } } else { var comp = bookingDB.Users.First(u => u.GID == user.PARENT_ID); if (comp.STATUS == UserStatus.Active.ToString()) //子账号,判断管理员账号状态 { InitCurrentUser(user); InitCurrentCompany(comp); resp.Success = true; if (withCode.HasValue && withCode.Value) { string code = Guid.NewGuid().ToString().Replace("-", ""); resp.Message = code; string origin = Request.Headers["origin"]; MemoryCache.Default.Add(new CacheItem(code, new { mobile = mobile, clientId = ClientId, origin }), new CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.AddSeconds(5) }); } else { resp.Message = LangLogin.MsgSuccess; } } else { resp.Success = false; resp.Message = LangLogin.MsgAccountDisabled; } } } else { resp.Success = false; resp.Message = LangLogin.MsgAccountDisabled; } } else { resp.Success = false; resp.Message = LangLogin.MsgFail; } } else { resp.Success = false; resp.Message = LangLogin.MsgFail; } return Json(resp); } [AllowAnonymous] [HttpGet] public ActionResult Logout() { Session["UserInfo"] = null; var origin = Session["Origin"]; if (origin != null) { return Redirect(origin.ToString()); } else { return RedirectToAction("login"); } } [AllowAnonymous] [HttpGet] public ActionResult LoginRedirect(string code) { if (MemoryCache.Default.Contains(code)) { dynamic obj = MemoryCache.Default[code]; string mobile = obj.mobile; string clientId = obj.clientId; var user = bookingDB.Users.FirstOrDefault(u => u.MOBILE == mobile && u.CLIENT_ID == clientId); InitCurrentUser(user); if (user.IS_ADMIN) { InitCurrentCompany(user); } else { var comp = bookingDB.Users.First(u => u.GID == user.PARENT_ID); InitCurrentCompany(comp); } if (!string.IsNullOrEmpty(obj.origin)) { Session["Origin"] = obj.origin; } return RedirectToAction("Index", "Home"); } return RedirectToAction("Login", "User"); } #endregion #region 注册 [AllowAnonymous] [HttpGet] public ActionResult Regist() { return View(); } [AllowAnonymous] [HttpPost] public JsonResult Regist(UserRegistViewModel viewModel) { RespCommon resp = new RespCommon(); string message = string.Empty; var success = ValidData(out message); if (success) { var dbUser = bookingDB.Users.FirstOrDefault(u => u.MOBILE == viewModel.MOBILE && u.CLIENT_ID == ClientId); if (dbUser != null) { resp.Success = false; resp.Message = LangReg.MsgUserMobileExist; } else { //校验验证码 if (!ValidCaptcha(viewModel.Captcha)) { resp.Success = false; resp.Message = LangReg.MsgCaptchaInvalid; return Json(resp); } //手机验证码 if (!ValidMobileCode(viewModel.MOBILE, viewModel.MobileCode)) { resp.Success = false; resp.Message = LangReg.MsgMobileCodeInvalid; return Json(resp); } SysUser user = viewModel.AsModel(); user.GID = Guid.NewGuid().ToString(); user.REG_TIME = DateTime.Now; user.IS_ADMIN = true; user.IDENTIFICATION_STATE = UserIdentiState.NotIndent.ToString(); user.CLIENT_ID = ClientId; user.STATUS = UserStatus.Active.ToString(); bookingDB.Users.Add(user); bookingDB.SaveChanges(); resp.Success = true; resp.Message = LangReg.MsgRegistSuccess; } } else { resp.Success = success; resp.Message = message; } return Json(resp); } #endregion #region 认证 [HttpGet] public ActionResult Identification() { var user = bookingDB.Users.FirstOrDefault(u => u.GID == CurrentUser.GID); ViewBag.Indentified = user.IDENTIFICATION_STATE == UserIdentiState.Indentified.ToString(); ViewBag.Indentifing = user.IDENTIFICATION_STATE == UserIdentiState.Identifying.ToString(); ViewBag.Reject = user.IDENTIFICATION_STATE == UserIdentiState.Reject.ToString(); ViewBag.CompanyCode = user.COMPANY_CODE; ViewBag.Address = user.ADDRESS; return View(); } //[HttpPost] //public ActionResult UpIdentificationImg() //{ // RespCommon resp = new RespCommon(); // return Json(resp); //} [HttpPost] public ActionResult IdentiSubmit(string companyCode, string address) { RespCommon resp = new RespCommon(); if (Request.Files.Count > 0 && !string.IsNullOrEmpty(companyCode) && !string.IsNullOrEmpty(address)) { string name = Request.Files[0].FileName; string ext = Path.GetExtension(name).ToLower(); string[] allowExt = new string[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp" }; if (allowExt.Contains(ext)) { string storeName = $"{CurrentUser.GID}{ext}"; string storePath = $"~/User/Indentification"; string realStorePath = Server.MapPath(storePath); if (!Directory.Exists(realStorePath)) { Directory.CreateDirectory(realStorePath); } string storePathName = $"{storePath}/{storeName}"; string realStorePathName = $"{Server.MapPath(storePath)}\\{storeName}"; Request.Files[0].SaveAs(realStorePathName); var user = bookingDB.Users.First(u => u.GID == CurrentUser.GID); user.COMPANY_CODE = companyCode; user.ADDRESS = address; user.PIC_PATH = storePathName; user.IDENTIFICATION_STATE = UserIdentiState.Identifying.ToString(); bookingDB.SaveChanges(); InitCurrentUser(user); //更新CurrentUser状态 resp.Message = LangIdentification.MsgSubmitIndentSuccess; } else { resp.Success = false; resp.Message = LangIdentification.MsgInvalidImageExt; } } else { resp.Success = false; resp.Message = LangIdentification.MsgInvalidParam; } return Json(resp); } [AllowAnonymous] [HttpGet] public ActionResult ViewIdentifyImg(string uid) { if (string.IsNullOrEmpty(uid)) { uid = CurrentUser.GID; } var user = bookingDB.Users.FirstOrDefault(u => u.GID == uid); string realStorePath = Server.MapPath(user.PIC_PATH); return File(realStorePath, "image/*"); } #endregion #region 子账号 [HttpGet] public ActionResult AccountList() { return View(); } [HttpPost] public ActionResult AccountList(int offset, int limit, string sort = "", string order = "") { RespListUser resp = new RespListUser(); var query = bookingDB.Users.Where(u => u.PARENT_ID == CurrentCompany.GID); int total = query.Count(); var list = query.OrderBy(u => u.REG_TIME).Skip(offset).Take(limit).ToList(); resp.Total = total; resp.Data = list.AsListViewModelList(); return Json(resp); } [HttpPost] public ActionResult AccountSave(SubAccountEditViewModel viewModel) { RespCommon resp = new RespCommon(); string msg = string.Empty; if (!ValidData(out msg)) { resp.Success = false; resp.Message = msg; return Json(resp); } if (!string.IsNullOrWhiteSpace(viewModel.GID)) { var model = bookingDB.Users.First(u => u.GID == viewModel.GID); //viewModel.PASSWORD = model.PASSWORD;//修改信息,不改密码 viewModel.AsModel(model); bookingDB.SaveChanges(); resp.Success = true; resp.Message = LangSubAccount.MsgSaveSuccess; } else { var model = viewModel.AsModel(); model.GID = Guid.NewGuid().ToString(); model.IS_ADMIN = false; model.PARENT_ID = CurrentCompany.GID; model.CLIENT_ID = CurrentCompany.CLIENT_ID; model.REG_TIME = DateTime.Now; model.STATUS = UserStatus.Active.ToString(); model.INFO_CLIENT = CurrentCompany.INFO_CLIENT; bookingDB.Users.Add(model); bookingDB.SaveChanges(); resp.Success = true; resp.Message = LangSubAccount.MsgAddSuccess; } return Json(resp); } #endregion #region 个人信息修改 [HttpGet] public ActionResult EditInfo() { var user = bookingDB.Users.First(u => u.GID == CurrentUser.GID); return View(user.AsUserEditInfoViewModel()); } [HttpPost] public ActionResult ChangeMobile(string newMobile, string mobileCode) { RespCommon resp = new RespCommon(); //手机验证码 if (!ValidMobileCode(newMobile, mobileCode)) { resp.Success = false; resp.Message = LangReg.MsgMobileCodeInvalid; return Json(resp); } var user = bookingDB.Users.First(u => u.GID == CurrentUser.GID); user.MOBILE = newMobile; bookingDB.SaveChanges(); resp.Success = true; resp.Message = LangAll.MsgOptSuccess; return Json(resp); } [HttpPost] public ActionResult ChangePassword(string newPwd) { RespCommon resp = new RespCommon(); var user = bookingDB.Users.First(u => u.GID == CurrentUser.GID); user.PASSWORD = newPwd; bookingDB.SaveChanges(); resp.Success = true; resp.Message = LangAll.MsgOptSuccess; return Json(resp); } [HttpPost] public ActionResult SaveInfo(string name, string email, string tel) { RespCommon resp = new RespCommon(); var user = bookingDB.Users.First(u => u.GID == CurrentUser.GID); user.NAME = name; user.EMAIL = email; user.TEL = tel; bookingDB.SaveChanges(); resp.Success = true; resp.Message = LangAll.MsgOptSuccess; return Json(resp); } #endregion #region 其他 private bool ValidCaptcha(string captcha) { if (Session["ValidateCode"] != null) { string c = Session["ValidateCode"].ToString(); return c.ToLower() == captcha.ToLower(); //return c == captcha; } return false; } private bool ValidMobileCode(string mobile, string code) { string key = $"MC_{mobile}"; if (MemoryCache.Default.Contains(key)) { var storeCode = MemoryCache.Default[key].ToString(); if (storeCode == code) { return true; } } return false; } #endregion #region 忘记密码 [AllowAnonymous] [HttpGet] public ActionResult Retrieve() { return View(); } [AllowAnonymous] [HttpPost] public ActionResult Retrieve(string mobile, string mobileCode, string password) { RespCommon resp = new RespCommon(); var user = bookingDB.Users.FirstOrDefault(u => u.MOBILE == mobile); if (user == null) { resp.Success = false; resp.Message = LangAll.MsgUserNotExist; return Json(resp); } //手机验证码 if (!ValidMobileCode(mobile, mobileCode)) { resp.Success = false; resp.Message = LangReg.MsgMobileCodeInvalid; return Json(resp); } user.PASSWORD = password; bookingDB.SaveChanges(); resp.Success = true; resp.Message = LangAll.MsgOptSuccess; return Json(resp); } #endregion } }