use of com.ch999.haha.admin.entity.UserInfo in project haha by hahafreeasair666.
the class UserInfoApi method updateAvatar.
/**
* 修改用户信息
*
* @param type 要修改的类型有{avatar,name,mobile,description,pwd}
* @param userInfoUpdateVO
* @return
*/
@PostMapping("/updateUserInfo/{type}/v1")
public Result<String> updateAvatar(@PathVariable("type") String type, UserInfoUpdateVO userInfoUpdateVO, HttpServletResponse httpResponse) {
// 设置ajax跨域可访问
httpResponse.addHeader("Access-Control-Allow-Origin", "*");
UserInfoBO loginUser = userComponent.getLoginUser();
if (loginUser.getId() == null) {
return Result.error("unLogin", "请登录后再进行操作");
}
UserInfo userInfo = new UserInfo(loginUser.getId());
userInfo.setPicPath(userInfoBORepository.findOne(loginUser.getId()).getUserInfo().getPicPath());
switch(type) {
case "avatar":
if (userInfoUpdateVO.getFile() == null) {
return Result.error("null", "请选择图片");
}
if (userComponent.updateUserAvatar(loginUser.getId(), userInfoUpdateVO.getFile())) {
return Result.success();
}
return Result.error("error", "头像更改失败");
case "name":
if (StringUtils.isBlank(userInfoUpdateVO.getName())) {
return Result.error("null", "用户名不能为空");
} else if (!userInfoUpdateVO.getName().equals(loginUser.getUserInfo().getUsername()) && !userComponent.checkCanUse(type, userInfoUpdateVO.getName())) {
return Result.error("null", "昵称修改失败该昵称已被占用");
}
userInfo.setUsername(userInfoUpdateVO.getName());
break;
case "description":
if (StringUtils.isBlank(userInfoUpdateVO.getDescription())) {
return Result.error("null", "个性签名不能为空");
}
userInfo.setAutograph(userInfoUpdateVO.getDescription());
break;
case "mobile":
if (StringUtils.isBlank(userInfoUpdateVO.getMobile())) {
return Result.error("null", "请输入要修改的手机号码");
} else if (!userInfoUpdateVO.getMobile().matches(Phone.CK)) {
return Result.error("error", "请输入正确的的电话号码");
} else if (!userInfoUpdateVO.getMobile().equals(loginUser.getUserInfo().getMobile()) && !userComponent.checkCanUse(type, userInfoUpdateVO.getMobile())) {
return Result.error("error", "手机号修改失败该号码已被占用");
}
userInfo.setMobile(userInfoUpdateVO.getMobile());
break;
case "pwd":
if (StringUtils.isBlank(userInfoUpdateVO.getOldPwd())) {
return Result.error("null", "请输入原密码");
} else if (!userInfoService.selectById(loginUser.getId()).getPwd().equals(userInfoUpdateVO.getOldPwd())) {
return Result.error("error", "原密码输入错误,密码修改失败");
} else if (StringUtils.isBlank(userInfoUpdateVO.getNewPwd1()) || StringUtils.isBlank(userInfoUpdateVO.getNewPwd2()) || !userInfoUpdateVO.getNewPwd2().equals(userInfoUpdateVO.getNewPwd1())) {
return Result.error("error", "新密码未输入或两次密码输入不一致,密码修改失败");
}
userInfo.setPwd(userInfoUpdateVO.getNewPwd1());
break;
default:
return Result.error("error", "不支持该类型的修改");
}
// 修改数据库
userInfoService.updateById(userInfo);
UserInfo userInfo1 = userInfoService.selectById(loginUser.getId());
// 修改缓存
UserInfoBO one = userInfoBORepository.findOne(loginUser.getId());
if (one != null) {
one.setUserInfo(userInfo1);
} else {
one = new UserInfoBO(loginUser.getId(), userInfo1);
}
userInfoBORepository.save(one);
return Result.success();
}
Aggregations