public ServerResponse<User> login(String username,String password)
{
int resultCount = userMapper.checkUsername(username);
if(resultCount==0)
{
return ServerResponse.createByErrorMessage("用户名不存在");
}
//密码*-z错误
String md5password=MD5Util.MD5EncodeUtf8(password);
User user= userMapper.selectLogin(username,md5password);
if(user==null)
{
return ServerResponse.createByErrorMessage("密码错误");
}
user.setPassword(null);
//user.setPassword(StringUtils.EMPTY);
return ServerResponse.createBySuccess("登录成功",user);
}
public ServerResponse<String> register(User user)
{
int resultcount=userMapper.checkUsername(user.getUsername());
if(resultcount>0)
{
return ServerResponse.createByErrorMessage("用户名已经存在");
}
resultcount=userMapper.checkEmail(user.getEmail());
if(resultcount>0)
{
return ServerResponse.createByErrorMessage("邮件已经存在");
}
//设置为普通用户
user.setRole(Const.Role.ROLE_CUSTOMER);
// MD5加密
user.setPassword(MD5Util.MD5EncodeUtf8(user.getPassword()));
resultcount=userMapper.insert(user);
if (resultcount==0)
{
return ServerResponse.createByErrorMessage("注册失败");
}
return ServerResponse.createBySuccess("注册成功");
}
public ServerResponse<String> checkVaild(String str,String type)
{
// 过滤了空字符串 比如“ ”
if(org.apache.commons.lang3.StringUtils.isNoneBlank(type))
{
//开始校验用户名
if(Const.USERNAME.equals(type))
{
int resultcount=userMapper.checkUsername(str);
if(resultcount>0)
{
return ServerResponse.createByErrorMessage("用户名已经存在");
}
}
// 开始校验邮件
if(Const.EMAIL.equals(type))
{
// 注意 视频中 传入是type
int resultcount=userMapper.checkEmail(str);
if(resultcount>0)
{
return ServerResponse.createByErrorMessage("邮件已经存在");
}
}
}
else
{
return ServerResponse.createByErrorMessage("注册参数错误");
}
return ServerResponse.createBySuccessMessage("校验成功");
}
//选择问题
public ServerResponse selectQuestion(String username)
{
//判断是否有该用户
// 校验成功 代表用户名不存在
ServerResponse vaildResponse=this.checkVaild(username,Const.USERNAME);
if ( vaildResponse.isSuccess())
{
return ServerResponse.createByErrorMessage("用户不存在");
}
String question=userMapper.selectQuestionByUsername(username);
if (StringUtils.isNoneBlank(question))
{
return ServerResponse.createBySuccess(question);
}
return ServerResponse.createByErrorMessage("找回密码问题失败");
}
// 检查答案是否正确 生成token 值 存放到客户端
public ServerResponse<String> checkAnswer(String username,String question,String answer){
int resultCount = userMapper.checkAnswer(username, question, answer);
if(resultCount>0){
//说明问题及问题答案是这个用户的,并且是正确的
String forgetToken = UUID.randomUUID().toString();
TokenCache.setKey(TokenCache.TOKEN_PREFIX + username, forgetToken);
return ServerResponse.createBySuccess(forgetToken);
}
return ServerResponse.createByErrorMessage("问题的答案错误");
}
// 重置密码
public ServerResponse<String> forgetResetPassword(String username,String passwordNew,String forgetToken) {
if (org.apache.commons.lang3.StringUtils.isBlank(forgetToken)) {
return ServerResponse.createByErrorMessage("参数错误,token需要传递");
}
ServerResponse validResponse = this.checkVaild(username, Const.USERNAME);
if (validResponse.isSuccess()) {
//用户不存在
return ServerResponse.createByErrorMessage("用户不存在");
}
// 获取token值
String token = TokenCache.getKey(TokenCache.TOKEN_PREFIX + username);
if (org.apache.commons.lang3.StringUtils.isBlank(token)) {
return ServerResponse.createByErrorMessage("token无效或者过期");
}
if (org.apache.commons.lang3.StringUtils.equals(forgetToken, token)) {
String md5Password = MD5Util.MD5EncodeUtf8(passwordNew);
int rowCount = userMapper.updatePasswordByUsername(username, md5Password);
if (rowCount > 0) {
return ServerResponse.createBySuccessMessage("修改密码成功");
}
} else {
return ServerResponse.createByErrorMessage("token错误,请重新获取重置密码的token");
}
return ServerResponse.createByErrorMessage("修改密码失败");
}
public ServerResponse<String> resetPassword(String passwordOld,String passwordNew,User user){
//防止横向越权,要校验一下这个用户的旧密码,一定要指定是这个用户.因为我们会查询一个count(1),如果不指定id,那么结果就是true啦count>0;
int resultCount = userMapper.checkPassword(MD5Util.MD5EncodeUtf8(passwordOld), user.getId());
if(resultCount == 0){
return ServerResponse.createByErrorMessage("旧密码错误");
}
user.setPassword(MD5Util.MD5EncodeUtf8(passwordNew));
// 修改新密码
int updateCount = userMapper.updateByPrimaryKeySelective(user);
if(updateCount > 0){
return ServerResponse.createBySuccessMessage("密码更新成功");
}
return ServerResponse.createByErrorMessage("密码更新失败");
}
public ServerResponse<User> updateInformation(User user){
//username是不能被更新的
//email也要进行一个校验,校验新的email是不是已经存在,并且存在的email如果相同的话,不能是我们当前的这个用户的.
int resultCount = userMapper.checkEmailByUserId(user.getEmail(),user.getId());
if(resultCount > 0){
return ServerResponse.createByErrorMessage("email已存在,请更换email再尝试更新");
}
User updateUser = new User();
updateUser.setId(user.getId());
updateUser.setEmail(user.getEmail());
updateUser.setPhone(user.getPhone());
updateUser.setQuestion(user.getQuestion());
updateUser.setAnswer(user.getAnswer());
int updateCount = userMapper.updateByPrimaryKeySelective(updateUser);
if(updateCount > 0){
return ServerResponse.createBySuccess("更新个人信息成功", updateUser);
}
return ServerResponse.createByErrorMessage("更新个人信息失败");
}
public ServerResponse<User> getInformation(Integer userId){
User user = userMapper.selectByPrimaryKey(userId);
if(user == null){
return ServerResponse.createByErrorMessage("找不到当前用户");
}
user.setPassword(org.apache.commons.lang3.StringUtils.EMPTY);
return ServerResponse.createBySuccess(user);
}
//检测是否是管理员
public ServerResponse checkAdminRole(User user){
if(user != null && user.getRole().intValue() == Const.Role.RO