You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

482 lines
16 KiB
C#

10 months ago
using BookingJieFeng.DB;
using BookingJieFeng.DB.Model;
using BookingJieFeng.Models;
using log4net;
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 BookingJieFeng.Controllers
{
public class UserController : BaseController
{
private BookingDB bookingDB = new BookingDB();
private JiefengDB jiefengDB = new JiefengDB();
private ILog log = LogManager.GetLogger("UserController");
#region 登录
[AllowAnonymous]
[HttpGet]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
[AllowAnonymous]
[HttpPost]
public ActionResult Login(string mobile, string password)
{
RespCommon resp = new RespCommon();
var user = bookingDB.Users.FirstOrDefault(u => u.MOBILE == mobile);
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;
resp.Message = "登录成功";
}
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;
resp.Message = "登录成功";
}
else
{
resp.Success = false;
resp.Message = "账号已被禁用";
}
}
}
else
{
resp.Success = false;
resp.Message = "账号已被禁用";
}
}
else
{
resp.Success = false;
resp.Message = "登录失败";
}
}
else
{
resp.Success = false;
resp.Message = "登录失败";
}
return Json(resp);
}
[AllowAnonymous]
[HttpGet]
public ActionResult Logout()
{
Session["UserInfo"] = null;
return RedirectToAction("login");
}
#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);
if (dbUser != null)
{
resp.Success = false;
resp.Message = "该手机已被使用";
}
else
{
//校验验证码
if (!ValidCaptcha(viewModel.Captcha))
{
resp.Success = false;
resp.Message = "验证码错误";
return Json(resp);
}
//手机验证码
if (!ValidMobileCode(viewModel.MOBILE, viewModel.MobileCode))
{
resp.Success = false;
resp.Message = "手机验证码错误";
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.STATUS = UserStatus.Active.ToString();
bookingDB.Users.Add(user);
bookingDB.SaveChanges();
resp.Success = true;
resp.Message = "注册成功";
}
}
else
{
resp.Success = success;
resp.Message = message;
}
return Json(resp);
}
#endregion
#region 认证
[HttpGet]
public ActionResult Identification()
{
var user = bookingDB.Users.FirstOrDefault(u => u.GID == CurrentCompany.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();
//图片同时存储到数据库供DS6读取
SysUserImg usrImg = bookingDB.UserImgs.FirstOrDefault(ui => ui.UserId == user.GID);
if (usrImg == null)
{
usrImg = new SysUserImg();
usrImg.UserId = user.GID;
bookingDB.UserImgs.Add(usrImg);
}
usrImg.PicData = System.IO.File.ReadAllBytes(realStorePathName);
bookingDB.SaveChanges();
//DS6任务
var sqlDs6 = $@"insert into t_op_task(任务编号,任务类型,任务来源,任务状态,任务说明,发起人,录入日期,任务开始时间,提单号,是否公共,任务相关人员,SEA编号,是否完成,备注)
values(newid(), '', '', '', '{user.COMPANY_SHORT_NAME}',
'DEMO-SA', GETDATE(), GETDATE(), '{user.COMPANY_SHORT_NAME}', 0, 'DEMO-SA', '{user.GID}', 0, '')";
log.Debug(sqlDs6);
jiefengDB.Database.ExecuteSqlCommand(sqlDs6);
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.REG_TIME = DateTime.Now;
model.STATUS = UserStatus.Active.ToString();
model.REC_BC_MAIL = CurrentCompany.REC_BC_MAIL;
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 = "手机验证码错误";
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 srcPwd, string newPwd)
{
RespCommon resp = new RespCommon();
var user = bookingDB.Users.First(u => u.GID == CurrentUser.GID);
if (user.PASSWORD != srcPwd)
{
resp.Success = false;
resp.Message = "原始密码不匹配";
}
else
{
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, string captcha)
{
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 (!ValidCaptcha(captcha))
{
resp.Success = false;
resp.Message = "验证码错误";
return Json(resp);
}
//手机验证码
if (!ValidMobileCode(mobile, mobileCode))
{
resp.Success = false;
resp.Message = "手机验证码错误";
return Json(resp);
}
user.PASSWORD = password;
bookingDB.SaveChanges();
resp.Success = true;
resp.Message = LangAll.MsgOptSuccess;
return Json(resp);
}
#endregion
}
}