用户操作
- 已经登录了账号
- 填写新的邮箱地址
- 填写验证码
- 提交数据
代码逻辑
- 检查邮箱是否已经注册过了
- 判断用户是否已经登录
- 判断验证码是否正确
- 修改邮箱/返回内容
接口
/**
* 1、必须已经登录了
* 2、新的邮箱没有注册过
* <p>
* 用户的步骤:
* 1、已经登录
* 2、输入新的邮箱地址
* 3、获取验证码 type=update
* 4、输入验证码
* 5、提交数据
* <p>
* 需要提交的数据
* 1、新的邮箱地址
* 2、验证码
* 3、其他信息我们可以token里获取
*
* @return
*/
@PutMapping("/email")
public ResponseResult updateEmail(@RequestParam("email") String email,
@RequestParam("verify_code") String verifyCode) {
return userService.updateEmail(email, verifyCode);
}
实现代码
/**
* 更新邮箱地址
*
* @param email
* @param verifyCode
* @return
*/
@Override
public ResponseResult updateEmail(String email, String verifyCode) {
//1、确保用户已经登录了
SobUser sobUser = this.checkSobUser();
//没有登录
if (sobUser == null) {
return ResponseResult.ACCOUNT_NOT_LOGIN();
}
//2、对比验证码,确保新的邮箱地址是属于当前用户的
String redisVerifyCode = (String) redisUtils.get(Constants.User.KEY_EMAIL_CODE_CONTENT + email);
if (TextUtils.isEmpty(redisVerifyCode) || !redisVerifyCode.equals(verifyCode)) {
return ResponseResult.FAILED("验证码错误");
}
//可以修改邮箱
int result = userDao.updateEmailById(email, sobUser.getId());
return result > 0 ? ResponseResult.SUCCESS("邮箱修改成功") : ResponseResult.FAILED("邮箱修改失败");
}
DAO的代码
@Modifying
@Query(nativeQuery = true, value = "update `tb_user` set `email` = ? where `id` = ?")
int updateEmailById(String email, String id);