public IActionResult Login([FromBody] LoginViewModel model) { if (!ModelState.IsValid) { return(Json(ReturnJson.ServerError("数据格式错误"))); } try { var user = _db.Users.SingleOrDefault(c => c.UserName == model.UserName); if (user == null) { return(Json(ReturnJson.NotFound("该用户不存在"))); } if (user.Password != Crypto.DesEncrypt(model.Password)) { return(Json(ReturnJson.ServerError("用户名密码错误"))); } var userRole = Enum.GetName(typeof(CustomerEnum.UserRole), user.UserRole); var returnToken = getToken(model.UserName, userRole); return(Json(ReturnJson.Success(returnToken))); } catch (Exception ex) { return(Json(ReturnJson.ServerError(ex.ToString()))); } }
public IActionResult Register([FromBody] RegisterViewModel model) { if (!ModelState.IsValid) { return(Json(ReturnJson.ServerError("数据格式错误"))); } try { var user = new User { UserName = model.UserName, NickName = model.NickName, Phone = model.Phone, IsVerifyPhone = CustomerEnum.Verification.False, Email = model.Email, IsVerifyEmail = CustomerEnum.Verification.False, Password = Crypto.DesEncrypt(model.Password), UserRole = CustomerEnum.UserRole.Admin }; _db.Users.Add(user); _db.SaveChanges(); var userRole = Enum.GetName(typeof(CustomerEnum.UserRole), user.UserRole); var returnToken = getToken(user.UserName, userRole); return(Json(ReturnJson.Success(returnToken))); } catch (Exception ex) { return(Json(ReturnJson.ServerError(ex.ToString()))); } }