feat: 添加变更邮箱接口

This commit is contained in:
曹鹏飞 2025-02-23 13:01:18 +08:00
parent 785eb1b9e4
commit b6a0c52313
3 changed files with 143 additions and 63 deletions

View File

@ -135,27 +135,27 @@ public class AdminUserController extends ControllerBase {
@ApiMark(moduleName = "账号管理", apiName = "更新账号")
public ApiResult<Void> updateAccount(@Valid @RequestBody AccountUpdateRequest request) throws MessagingException {
AdminUser user=adminUserService.getById(request.getId());
if (!StrUtil.equals(user.getLoginName(),request.getLoginName())){
VUtils.trueThrowBusinessError(adminUserService.lambdaQuery()
.eq(AdminUser::getLoginName, request.getLoginName())
.ne(AdminUser::getId, request.getId())
.exists())
.throwMessage("已存在相同的登录名");
user.setState(UserState.ToBeActivated.getState());
String code= RandomUtil.randomString(6);
redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + user.getLoginName(), code, Duration.ofHours(72));
redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + code, request.getLoginName(), Duration.ofHours(72));
String subject = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_TITLE_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE);
String content = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_CONTENT_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE)
.replace("${email}", user.getLoginName())
.replace("${url}", activateUrl + "?code=" + Base64.getUrlEncoder().encodeToString((user.getLoginName()+"|1").getBytes()))
.replace("${password}", code);
emailService.sendEmail(request.getLoginName(), subject, content);
} else {
if (Objects.nonNull(request.getEnable())) {
user.setState(request.getEnable() ? UserState.Activated.getState() : UserState.Disabled.getState());
}
}
// if (!StrUtil.equals(user.getLoginName(),request.getLoginName())){
// VUtils.trueThrowBusinessError(adminUserService.lambdaQuery()
// .eq(AdminUser::getLoginName, request.getLoginName())
// .ne(AdminUser::getId, request.getId())
// .exists())
// .throwMessage("已存在相同的登录名");
// user.setState(UserState.ToBeActivated.getState());
// String code= RandomUtil.randomString(6);
// redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + user.getLoginName(), code, Duration.ofHours(72));
// redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + code, request.getLoginName(), Duration.ofHours(72));
// String subject = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_TITLE_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE);
// String content = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_CONTENT_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE)
// .replace("${email}", user.getLoginName())
// .replace("${url}", activateUrl + "?code=" + Base64.getUrlEncoder().encodeToString((user.getLoginName()+"|1").getBytes()))
// .replace("${password}", code);
// emailService.sendEmail(request.getLoginName(), subject, content);
// } else {
// if (Objects.nonNull(request.getEnable())) {
// user.setState(request.getEnable() ? UserState.Activated.getState() : UserState.Disabled.getState());
// }
// }
user.setUserCode(request.getUserCode())
.setUserName(request.getUserName())
.setAvatar(request.getAvatar())
@ -164,6 +164,9 @@ public class AdminUserController extends ControllerBase {
.setTitleId(request.getTitleId())
.setUpdateBy(AdminUserUtil.getUserId())
.setUpdateTime(LocalDateTime.now());
if (!Objects.equals(user.getState(), UserState.ToBeActivated.getState()) && Objects.nonNull(request.getEnable())) {
user.setState(request.getEnable() ? UserState.Activated.getState() : UserState.Disabled.getState());
}
adminUserService.updateById(user);
return ApiResult.success();
}
@ -347,4 +350,29 @@ public class AdminUserController extends ControllerBase {
adminUserService.deleteAccount(request.getId());
return ApiResult.success();
}
/**
* 更改邮箱
* @param request 请求参数
**/
@PostMapping("changeEmail")
@ApiMark(moduleName = "账号管理", apiName = "更改邮箱")
public ApiResult<Void> changeEmail(@Valid @RequestBody EmailChangeRequest request) throws MessagingException {
AdminUser user=adminUserService.getById(request.getUserId());
VUtils.trueThrowBusinessError(adminUserService.lambdaQuery()
.eq(AdminUser::getLoginName, request.getEmail())
.ne(AdminUser::getId, request.getUserId())
.exists())
.throwMessage("已存在相同的登录名");
String code= RandomUtil.randomString(6);
redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + user.getLoginName(), code, Duration.ofHours(72));
redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + code, request.getEmail(), Duration.ofHours(72));
String subject = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_TITLE_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE);
String content = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_CONTENT_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE)
.replace("${email}", user.getLoginName())
.replace("${url}", activateUrl + "?code=" + Base64.getUrlEncoder().encodeToString((user.getLoginName()+"|1").getBytes()))
.replace("${password}", code);
emailService.sendEmail(request.getEmail(), subject, content);
return ApiResult.success();
}
}

View File

@ -140,33 +140,40 @@ public class AppUserController extends ControllerBase {
}
public void updateAppUser(AppUser user,AppUserUpdateRequest request) throws MessagingException {
if (!StrUtil.equals(user.getLoginName(), request.getLoginName())){
VUtils.trueThrowBusinessError(appUserService.lambdaQuery()
.eq(AppUser::getLoginName, request.getLoginName())
.ne(AppUser::getId, request.getId())
.exists())
.throwMessage("登录名已存在");
user.setState(UserState.ToBeActivated.getState());
String code= RandomUtil.randomString(6);
redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + user.getId(), code, Duration.ofHours(72));
redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + code, request.getLoginName(), Duration.ofHours(72));
String subject = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_TITLE_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE);
String content = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_CONTENT_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE)
.replace("${email}", user.getLoginName())
.replace("${url}", activateUrl + "?code=" + Base64.getUrlEncoder().encodeToString((user.getLoginName()+"|1").getBytes()))
.replace("${password}", code);
emailService.sendEmail(request.getLoginName(), subject, content);
}else {
if (Objects.nonNull(request.getEnable())) {
user.setState(request.getEnable() ? UserState.Activated.getState() : UserState.Disabled.getState());
}
}
// if (!StrUtil.equals(user.getLoginName(), request.getLoginName())){
// VUtils.trueThrowBusinessError(appUserService.lambdaQuery()
// .eq(AppUser::getLoginName, request.getLoginName())
// .ne(AppUser::getId, request.getId())
// .exists())
// .throwMessage("登录名已存在");
// user.setState(UserState.ToBeActivated.getState());
// String code= RandomUtil.randomString(6);
// redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + user.getId(), code, Duration.ofHours(72));
// redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + code, request.getLoginName(), Duration.ofHours(72));
// String subject = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_TITLE_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE);
// String content = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_CONTENT_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE)
// .replace("${email}", user.getLoginName())
// .replace("${url}", activateUrl + "?code=" + Base64.getUrlEncoder().encodeToString((user.getLoginName()+"|1").getBytes()))
// .replace("${password}", code);
// emailService.sendEmail(request.getLoginName(), subject, content);
// }else {
// if (Objects.nonNull(request.getEnable())) {
// user.setState(request.getEnable() ? UserState.Activated.getState() : UserState.Disabled.getState());
// }
// }
user.setName(request.getUserName())
.setPhone(request.getPhone())
.setAvatar(request.getAvatar())
.setSalesUserName(request.getSalesUserName())
.setPhone(request.getPhone())
.setLanguageCode(request.getLanguageCode())
.setAreaId(request.getAreaId())
.setTitleId(request.getTitleId())
.setCompanyId(StrUtil.join(",", request.getCompanyIds()))
.setUpdateTime(LocalDateTime.now());
if (!Objects.equals(user.getState(), UserState.ToBeActivated.getState()) && Objects.nonNull(request.getEnable())) {
user.setState(request.getEnable() ? UserState.Activated.getState() : UserState.Disabled.getState());
}
appUserService.updateById(user);
}
@ -189,33 +196,40 @@ public class AppUserController extends ControllerBase {
.throwMessage("以下公司已设置主账号:" + StrUtil.join(",", customerService.listByIds(cIds).stream()
.map(TBaseCustomer::getAgencyCompanyName).collect(Collectors.toList())));
}
if (!StrUtil.equals(user.getLoginName(), request.getLoginName())){
VUtils.trueThrowBusinessError(appUserService.lambdaQuery()
.eq(AppUser::getLoginName, request.getLoginName())
.ne(AppUser::getId, request.getId())
.exists())
.throwMessage("登录名已存在");
user.setState(UserState.ToBeActivated.getState());
String code= RandomUtil.randomString(6);
redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + user.getLoginName(), code, Duration.ofHours(72));
redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + code, request.getLoginName(), Duration.ofHours(72));
String subject = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_TITLE_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE);
String content = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_CONTENT_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE)
.replace("${email}", user.getLoginName())
.replace("${url}", activateUrl + "?code=" + Base64.getUrlEncoder().encodeToString((user.getLoginName()+"|1").getBytes()))
.replace("${password}", code);
emailService.sendEmail(request.getLoginName(), subject, content);
}else {
if (Objects.nonNull(request.getEnable())) {
user.setState(request.getEnable() ? UserState.Activated.getState() : UserState.Disabled.getState());
}
}
// if (!StrUtil.equals(user.getLoginName(), request.getLoginName())){
// VUtils.trueThrowBusinessError(appUserService.lambdaQuery()
// .eq(AppUser::getLoginName, request.getLoginName())
// .ne(AppUser::getId, request.getId())
// .exists())
// .throwMessage("登录名已存在");
// user.setState(UserState.ToBeActivated.getState());
// String code= RandomUtil.randomString(6);
// redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + user.getLoginName(), code, Duration.ofHours(72));
// redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + code, request.getLoginName(), Duration.ofHours(72));
// String subject = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_TITLE_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE);
// String content = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_CONTENT_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE)
// .replace("${email}", user.getLoginName())
// .replace("${url}", activateUrl + "?code=" + Base64.getUrlEncoder().encodeToString((user.getLoginName()+"|1").getBytes()))
// .replace("${password}", code);
// emailService.sendEmail(request.getLoginName(), subject, content);
// }else {
// if (Objects.nonNull(request.getEnable())) {
// user.setState(request.getEnable() ? UserState.Activated.getState() : UserState.Disabled.getState());
// }
// }
user.setName(request.getUserName())
.setPhone(request.getPhone())
.setAvatar(request.getAvatar())
.setSalesUserName(request.getSalesUserName())
.setPhone(request.getPhone())
.setLanguageCode(request.getLanguageCode())
.setAreaId(request.getAreaId())
.setTitleId(request.getTitleId())
.setCompanyId(StrUtil.join(",", request.getCompanyIds()))
.setUpdateTime(LocalDateTime.now());
if (!Objects.equals(user.getState(), UserState.ToBeActivated.getState()) && Objects.nonNull(request.getEnable())) {
user.setState(request.getEnable() ? UserState.Activated.getState() : UserState.Disabled.getState());
}
appUserService.updateById(user);
}
@ -366,4 +380,29 @@ public class AppUserController extends ControllerBase {
appUserService.deleteAppUser(request.getId());
return ApiResult.success();
}
/**
* 更改邮箱
* @param request 请求参数
**/
@PostMapping("changeEmail")
@ApiMark(moduleName = "账号管理", apiName = "更改邮箱")
public ApiResult<Void> changeEmail(@Valid @RequestBody EmailChangeRequest request) throws MessagingException {
AppUser user=appUserService.getById(request.getUserId());
VUtils.trueThrowBusinessError(appUserService.lambdaQuery()
.eq(AppUser::getLoginName, request.getEmail())
.ne(AppUser::getId, request.getUserId())
.exists())
.throwMessage("已存在相同的登录名");
String code= RandomUtil.randomString(6);
redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + user.getLoginName(), code, Duration.ofHours(72));
redisTemplate.opsForValue().set(Constant.REDIS_KEY_CHANGE_EMAIL_CODE + code, request.getEmail(), Duration.ofHours(72));
String subject = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_TITLE_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE);
String content = dictionaryItemTranslateService.getValueByCode(Constant.DICTIONARY_EMAIL_NOTIFY, Constant.DICTIONARY_ITEM_EMAIL_CONTENT_CHANGE_EMAIL, Constant.DEFAULT_LANGUAGE_CODE)
.replace("${email}", user.getLoginName())
.replace("${url}", activateUrl + "?code=" + Base64.getUrlEncoder().encodeToString((user.getLoginName()+"|1").getBytes()))
.replace("${password}", code);
emailService.sendEmail(request.getEmail(), subject, content);
return ApiResult.success();
}
}

View File

@ -0,0 +1,13 @@
package com.nflg.mobilebroken.common.pojo.request;
import lombok.Data;
@Data
public class EmailChangeRequest {
// 用户ID
private Integer userId;
// 新邮箱
private String email;
}