using Competition.Common.Util; using CompetitionAPI.api.unity; using CompetitionAPI.Util; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace CompetitionAPI.Controllers.back { [Route("api/[controller]")] [ApiController] public class ChangePasswordController : Controller { Competition.Mysql.BLL.admin_user bll = new Competition.Mysql.BLL.admin_user(); public ChangePasswordController() { } /// /// 修改自身密码接口 /// /// /// [Authorize] [HttpPost] [APIFilter] public JsonResult Index([FromBody] ChangePasswordRequest req) { try { if (string.IsNullOrWhiteSpace(req.LoginName)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "用户名不能为空")); } if (string.IsNullOrWhiteSpace(req.OldPassword)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "原密码不能为空")); } if (string.IsNullOrWhiteSpace(req.NewPassword)) { return Json(Tool.GetJsonWithCode(APICode.Fail, "新密码不能为空")); } var old_value = RSAHelper.DecryptData(req.OldPassword); var new_value = RSAHelper.DecryptData(req.NewPassword); req.OldPassword = EncryptionAndDecryption.EncryptByLgzn(EncryptionAndDecryption.EncryptByLgzn(old_value)); req.NewPassword = EncryptionAndDecryption.EncryptByLgzn(EncryptionAndDecryption.EncryptByLgzn(new_value)); var user_model = bll.GetCUserModel(req.LoginName); if (user_model != null) { if (user_model.password == req.OldPassword) { user_model.password = req.NewPassword; if (bll.Update(user_model)) { return Json(Tool.GetJsonWithCode(APICode.Success, "修改密码成功")); } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "修改密码失败")); } } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "原密码不对")); } } else { return Json(Tool.GetJsonWithCode(APICode.Fail, "账号不存在")); } } catch (Exception ex) { LogHelper.WriteLog(ex.Message + ",行号:" + ex.StackTrace); return Json(Tool.GetJsonWithCode(APICode.Fail, "发生错误,请联系管理员。")); } } } }