diff --git a/src/main/kotlin/com/frontleaves/fantasticeditor/controllers/v1/UserController.java b/src/main/kotlin/com/frontleaves/fantasticeditor/controllers/v1/UserController.java index 215b857..1c2d727 100644 --- a/src/main/kotlin/com/frontleaves/fantasticeditor/controllers/v1/UserController.java +++ b/src/main/kotlin/com/frontleaves/fantasticeditor/controllers/v1/UserController.java @@ -16,6 +16,7 @@ import com.frontleaves.fantasticeditor.exceptions.BusinessException; import com.frontleaves.fantasticeditor.models.vo.api.user.UserMailVerifyVO; +import com.frontleaves.fantasticeditor.models.vo.api.user.UserPublicInfoVO; import com.frontleaves.fantasticeditor.services.interfaces.UserService; import com.frontleaves.fantasticeditor.utility.BaseResponse; import com.frontleaves.fantasticeditor.utility.ErrorCode; @@ -31,50 +32,72 @@ *
+ * 用于存储用户基本信息的实体 + * @since v1.0.0 + * @version v1.0.0 + * @author zrx + * @date 2024/6/1 8:45 + */ +@Data +@NoArgsConstructor +@Accessors(chain = true) +public class RedisUserPublicInfoDO { + + public String uuid; + + public String username; + + public String email; + + public String phone; + + public String basicInformation; + + public String roleId; + + public String roleName; + + public String vipId; + + public String vipName; + +} + + diff --git a/src/main/kotlin/com/frontleaves/fantasticeditor/models/vo/api/user/UserPublicInfoVO.java b/src/main/kotlin/com/frontleaves/fantasticeditor/models/vo/api/user/UserPublicInfoVO.java new file mode 100644 index 0000000..8e49c60 --- /dev/null +++ b/src/main/kotlin/com/frontleaves/fantasticeditor/models/vo/api/user/UserPublicInfoVO.java @@ -0,0 +1,52 @@ +/* + * ******************************************************************************* + * Copyright (C) 2024-NOW(至今) 妙笔智编 + * Author: 锋楪技术团队 + * + * 本文件包含 妙笔智编「FantasticEditor」 的源代码,该项目的所有源代码均遵循MIT开源许可证协议。 + * 本代码仅允许在十三届软件杯比赛授权比赛方可直接使用 + * ******************************************************************************* + * 免责声明: + * 使用本软件的风险由用户自担。作者或版权持有人在法律允许的最大范围内, + * 对因使用本软件内容而导致的任何直接或间接的损失不承担任何责任。 + * ******************************************************************************* + */ + +package com.frontleaves.fantasticeditor.models.vo.api.user; + +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.Accessors; + +/** + * 用户基本信息 + *
+ * + * @author zrx + * @date 2024/5/31 21:33 + * @since v1.0.0 + */ +@Data +@NoArgsConstructor +@Accessors(chain = true) +public class UserPublicInfoVO { + public String uuid; + + public String username; + + public String email; + + public String phone; + + public String basicInformation; + + public String roleId; + + public String roleName; + + public String vipId; + + public String vipName; +} + + diff --git a/src/main/kotlin/com/frontleaves/fantasticeditor/services/UserServiceImpl.java b/src/main/kotlin/com/frontleaves/fantasticeditor/services/UserServiceImpl.java index af0aba8..1799d83 100644 --- a/src/main/kotlin/com/frontleaves/fantasticeditor/services/UserServiceImpl.java +++ b/src/main/kotlin/com/frontleaves/fantasticeditor/services/UserServiceImpl.java @@ -19,14 +19,18 @@ import com.frontleaves.fantasticeditor.constant.SmsControl; import com.frontleaves.fantasticeditor.dao.RoleDAO; import com.frontleaves.fantasticeditor.dao.UserDAO; +import com.frontleaves.fantasticeditor.dao.VipDAO; import com.frontleaves.fantasticeditor.exceptions.BusinessException; import com.frontleaves.fantasticeditor.models.dto.UserCurrentDTO; import com.frontleaves.fantasticeditor.models.entity.redis.RedisMailCodeDO; import com.frontleaves.fantasticeditor.models.entity.redis.RedisSmsCodeDO; +import com.frontleaves.fantasticeditor.models.entity.redis.RedisUserPublicInfoDO; import com.frontleaves.fantasticeditor.models.entity.sql.SqlRoleDO; import com.frontleaves.fantasticeditor.models.entity.sql.SqlUserDO; +import com.frontleaves.fantasticeditor.models.entity.sql.SqlVipDO; import com.frontleaves.fantasticeditor.models.vo.api.auth.AuthUserLoginVO; import com.frontleaves.fantasticeditor.models.vo.api.auth.AuthUserRegisterVO; +import com.frontleaves.fantasticeditor.models.vo.api.user.UserPublicInfoVO; import com.frontleaves.fantasticeditor.services.interfaces.MailService; import com.frontleaves.fantasticeditor.services.interfaces.SmsService; import com.frontleaves.fantasticeditor.services.interfaces.UserService; @@ -59,200 +63,249 @@ @Service @RequiredArgsConstructor public class UserServiceImpl implements UserService { - private final UserDAO userDAO; - private final RoleDAO roleDAO; - private final RedisUtil redisUtil; - private final OperateUtil operateUtil; - private final SmsService smsService; - private final MailService mailService; + private final UserDAO userDAO; + private final RoleDAO roleDAO; + private final VipDAO vipDAO; + private final RedisUtil redisUtil; + private final OperateUtil operateUtil; + private final SmsService smsService; + private final MailService mailService; - /** - * 用户注册 - *
- * 用户注册,使用用户名和密码注册 - * - * @param authUserRegisterVO 用户注册信息 - * @return 注册结果 - */ - @NotNull - @Override - public UserCurrentDTO userRegister(final @NotNull AuthUserRegisterVO authUserRegisterVO) { - assert authUserRegisterVO.getUsername() != null; - assert authUserRegisterVO.getEmail() != null; - assert authUserRegisterVO.getPhone() != null; - assert authUserRegisterVO.getPassword() != null; - // 获取信息查询用户 - SqlUserDO getUser = userDAO.getUserByUsername(authUserRegisterVO.getUsername()); - if (getUser == null) { - getUser = userDAO.getUserByEmail(authUserRegisterVO.getEmail()); - } - if (getUser == null) { - getUser = userDAO.getUserByEmail(authUserRegisterVO.getPhone()); - } - if (getUser != null) { - throw new BusinessException("用户已存在", ErrorCode.USER_EXIST); - } - // 获取基本用户组 - SqlRoleDO getRole = roleDAO.getRoleByRoleName("user"); - assert getRole != null; - assert getRole.getRuuid() != null; - // 创建用户 - SqlUserDO user = new SqlUserDO() - .setUuid(Util.INSTANCE.makeNoDashUUID()) - .setUsername(authUserRegisterVO.getUsername()) - .setEmail(authUserRegisterVO.getEmail()) - .setPhone(authUserRegisterVO.getPhone()) - .setPassword(Util.INSTANCE.encryptPassword(authUserRegisterVO.getPassword())) - .setOtpAuth(Util.INSTANCE.makeNoDashUUID()) - .setMailVerify(false) - .setPhoneVerify(true) - .setBasicInformation("{1=1}") - .setRole(getRole.getRuuid()); - if (userDAO.save(user)) { - UserCurrentDTO getUserCurrent = new UserCurrentDTO(); - BeanUtils.copyProperties(user, getUserCurrent); - return getUserCurrent; - } else { - throw new BusinessException("用户注册失败", ErrorCode.OPERATION_FAILED); - } - } + /** + * 用户注册 + *
+ * 用户注册,使用用户名和密码注册
+ *
+ * @param authUserRegisterVO 用户注册信息
+ * @return 注册结果
+ */
+ @NotNull
+ @Override
+ public UserCurrentDTO userRegister(final @NotNull AuthUserRegisterVO authUserRegisterVO) {
+ assert authUserRegisterVO.getUsername() != null;
+ assert authUserRegisterVO.getEmail() != null;
+ assert authUserRegisterVO.getPhone() != null;
+ assert authUserRegisterVO.getPassword() != null;
+ // 获取信息查询用户
+ SqlUserDO getUser = userDAO.getUserByUsername(authUserRegisterVO.getUsername());
+ if (getUser == null) {
+ getUser = userDAO.getUserByEmail(authUserRegisterVO.getEmail());
+ }
+ if (getUser == null) {
+ getUser = userDAO.getUserByEmail(authUserRegisterVO.getPhone());
+ }
+ if (getUser != null) {
+ throw new BusinessException("用户已存在", ErrorCode.USER_EXIST);
+ }
+ // 获取基本用户组
+ SqlRoleDO getRole = roleDAO.getRoleByRoleName("user");
+ assert getRole != null;
+ assert getRole.getRuuid() != null;
+ // 创建用户
+ SqlUserDO user = new SqlUserDO()
+ .setUuid(Util.INSTANCE.makeNoDashUUID())
+ .setUsername(authUserRegisterVO.getUsername())
+ .setEmail(authUserRegisterVO.getEmail())
+ .setPhone(authUserRegisterVO.getPhone())
+ .setPassword(Util.INSTANCE.encryptPassword(authUserRegisterVO.getPassword()))
+ .setOtpAuth(Util.INSTANCE.makeNoDashUUID())
+ .setMailVerify(false)
+ .setPhoneVerify(true)
+ .setBasicInformation("{1=1}")
+ .setRole(getRole.getRuuid());
+ if (userDAO.save(user)) {
+ UserCurrentDTO getUserCurrent = new UserCurrentDTO();
+ BeanUtils.copyProperties(user, getUserCurrent);
+ return getUserCurrent;
+ } else {
+ throw new BusinessException("用户注册失败", ErrorCode.OPERATION_FAILED);
+ }
+ }
- @Override
- public void sendRegisterPhoneCode(final @NotNull String phone) {
- // 对手机号内容进行检查,检查缓存中是否存在
- operateUtil.checkSmsResendAble(phone);
- // 检查此手机是否已被注册
- SqlUserDO getUser = userDAO.getUserByPhone(phone);
- if (getUser != null) {
- throw new BusinessException("手机号已被注册", ErrorCode.USER_EXIST);
- }
- // 新建验证码并存入缓存
- String getNumberCode = Util.INSTANCE.generateNumber(6);
- RedisSmsCodeDO smsPhoneDO = new RedisSmsCodeDO()
- .setPhone(phone)
- .setCode(getNumberCode)
- .setSendAt(String.valueOf(System.currentTimeMillis()))
- .setFrequency("1");
- RedisSmsCodeDO getPhoneCode = Util.INSTANCE.mapToObject(
- redisUtil.hashGet("sms:code:" + phone),
- RedisSmsCodeDO.class
- );
- if (getPhoneCode != null) {
- smsPhoneDO.setFrequency(String.valueOf(Long.parseLong(getPhoneCode.getFrequency()) + 1));
- }
- if (redisUtil.hashSet("sms:code:" + phone, smsPhoneDO, 15 * 60)) {
- // 发送验证码
- if (!smsService.sendCode(phone, getNumberCode, SmsControl.USER_REGISTER)) {
- throw new BusinessException("发送失败", ErrorCode.VERIFY_CODE_ERROR);
- }
- } else {
- throw new BusinessException("发送失败", ErrorCode.VERIFY_CODE_ERROR);
- }
- }
+ @Override
+ public void sendRegisterPhoneCode(final @NotNull String phone) {
+ // 对手机号内容进行检查,检查缓存中是否存在
+ operateUtil.checkSmsResendAble(phone);
+ // 检查此手机是否已被注册
+ SqlUserDO getUser = userDAO.getUserByPhone(phone);
+ if (getUser != null) {
+ throw new BusinessException("手机号已被注册", ErrorCode.USER_EXIST);
+ }
+ // 新建验证码并存入缓存
+ String getNumberCode = Util.INSTANCE.generateNumber(6);
+ RedisSmsCodeDO smsPhoneDO = new RedisSmsCodeDO()
+ .setPhone(phone)
+ .setCode(getNumberCode)
+ .setSendAt(String.valueOf(System.currentTimeMillis()))
+ .setFrequency("1");
+ RedisSmsCodeDO getPhoneCode = Util.INSTANCE.mapToObject(
+ redisUtil.hashGet("sms:code:" + phone),
+ RedisSmsCodeDO.class
+ );
+ if (getPhoneCode != null) {
+ smsPhoneDO.setFrequency(String.valueOf(Long.parseLong(getPhoneCode.getFrequency()) + 1));
+ }
+ if (redisUtil.hashSet("sms:code:" + phone, smsPhoneDO, 15 * 60)) {
+ // 发送验证码
+ if (!smsService.sendCode(phone, getNumberCode, SmsControl.USER_REGISTER)) {
+ throw new BusinessException("发送失败", ErrorCode.VERIFY_CODE_ERROR);
+ }
+ } else {
+ throw new BusinessException("发送失败", ErrorCode.VERIFY_CODE_ERROR);
+ }
+ }
- @Override
- public boolean userLogin(@NotNull final AuthUserLoginVO authUserLoginVO) {
- // 定义邮箱的正则表达式
- String emailRegex =
- "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
- Pattern emailPattern = Pattern.compile(emailRegex);
- // 定义手机号的正则表达式
- String phoneRegex = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
- Pattern phonePattern = Pattern.compile(phoneRegex);
- // 先判断传入的username为什么类型
- String username = authUserLoginVO.getUsername();
- Matcher emailMatcher = emailPattern.matcher(username);
- Matcher phoneMatcher = phonePattern.matcher(username);
- if (emailMatcher.matches()) {
- SqlUserDO sqlUserDO = userDAO.getUserByEmail(username);
- if (sqlUserDO == null) {
- return false;
- }
- if (!sqlUserDO.getMailVerify()) {
- throw new BusinessException("邮箱未验证", ErrorCode.OPERATION_FAILED);
- }
- if (!Util.INSTANCE.verifyPassword(authUserLoginVO.getPassword(), sqlUserDO.getPassword())) {
- throw new BusinessException("邮箱或密码错误", ErrorCode.OPERATION_FAILED);
- }
- return true;
- } else if (phoneMatcher.matches()) {
- SqlUserDO sqlUserDO = userDAO.getUserByPhone(username);
- if (sqlUserDO == null) {
- return false;
- }
- if (!Util.INSTANCE.verifyPassword(authUserLoginVO.getPassword(), sqlUserDO.getPassword())) {
- throw new BusinessException("手机号或密码错误", ErrorCode.OPERATION_FAILED);
- }
- return true;
- } else {
- SqlUserDO sqlUserDO = userDAO.getUserByUsername(username);
- if (sqlUserDO == null) {
- return false;
- }
- if (!Util.INSTANCE.verifyPassword(authUserLoginVO.getPassword(), sqlUserDO.getPassword())) {
- throw new BusinessException("用户名或密码错误", ErrorCode.OPERATION_FAILED);
- }
- return true;
- }
- }
+ @Override
+ public boolean userLogin(@NotNull final AuthUserLoginVO authUserLoginVO) {
+ // 定义邮箱的正则表达式
+ String emailRegex =
+ "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
+ Pattern emailPattern = Pattern.compile(emailRegex);
+ // 定义手机号的正则表达式
+ String phoneRegex = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
+ Pattern phonePattern = Pattern.compile(phoneRegex);
+ // 先判断传入的username为什么类型
+ String username = authUserLoginVO.getUsername();
+ Matcher emailMatcher = emailPattern.matcher(username);
+ Matcher phoneMatcher = phonePattern.matcher(username);
+ if (emailMatcher.matches()) {
+ SqlUserDO sqlUserDO = userDAO.getUserByEmail(username);
+ if (sqlUserDO == null) {
+ return false;
+ }
+ if (!sqlUserDO.getMailVerify()) {
+ throw new BusinessException("邮箱未验证", ErrorCode.OPERATION_FAILED);
+ }
+ if (!Util.INSTANCE.verifyPassword(authUserLoginVO.getPassword(), sqlUserDO.getPassword())) {
+ throw new BusinessException("邮箱或密码错误", ErrorCode.OPERATION_FAILED);
+ }
+ return true;
+ } else if (phoneMatcher.matches()) {
+ SqlUserDO sqlUserDO = userDAO.getUserByPhone(username);
+ if (sqlUserDO == null) {
+ return false;
+ }
+ if (!Util.INSTANCE.verifyPassword(authUserLoginVO.getPassword(), sqlUserDO.getPassword())) {
+ throw new BusinessException("手机号或密码错误", ErrorCode.OPERATION_FAILED);
+ }
+ return true;
+ } else {
+ SqlUserDO sqlUserDO = userDAO.getUserByUsername(username);
+ if (sqlUserDO == null) {
+ return false;
+ }
+ if (!Util.INSTANCE.verifyPassword(authUserLoginVO.getPassword(), sqlUserDO.getPassword())) {
+ throw new BusinessException("用户名或密码错误", ErrorCode.OPERATION_FAILED);
+ }
+ return true;
+ }
+ }
- @Override
- @Transactional
- public void checkMailVerify(@NotNull final String email, @NotNull final String verifyCode) {
- // 获取验证码
- RedisMailCodeDO getMailCode = Util.INSTANCE.mapToObject(
- redisUtil.hashGet("mail:code:" + email),
- RedisMailCodeDO.class
- );
- if (getMailCode == null) {
- throw new BusinessException("验证码不存在", ErrorCode.VERIFY_CODE_ERROR);
- }
- // 检查验证码
- if (!getMailCode.getCode().equals(verifyCode)) {
- throw new BusinessException("验证码错误", ErrorCode.VERIFY_CODE_ERROR);
- }
- // 删除验证码
- redisUtil.delete("mail:code:" + email);
- // 检查用户是否已验证
- SqlUserDO getUser = userDAO.getUserByEmail(email);
- if (getUser == null) {
- throw new BusinessException("用户不存在", ErrorCode.USER_NOT_EXIST);
- }
- if (getUser.getMailVerify()) {
- throw new BusinessException("用户已验证", ErrorCode.OPERATION_FAILED);
- }
- // 验证成功
- getUser.setMailVerify(true)
- .setUpdatedAt(new Timestamp(System.currentTimeMillis()));
- if (!userDAO.update(getUser, new UpdateWrapper copyPropertiesData(source: S, target: T) {
+ val sourceClass: Class<*> = source.javaClass
+ val targetClass: Class<*> = target.javaClass
+
+ try {
+ val sourceFields: Array