feat(user): 添加用户信息修改功能及对应验证码校验

- 在管理员页面新增修改用户信息表单,支持姓名、手机号、密码修改
- 实现验证码发送倒计时与发送状态管理
- 新增接口支持用户信息更新,包含密码和手机号校验
- 后端校验验证码有效性,编码密码后更新用户信息
- 修改用户信息后强制登出,确保安全性
- 优化登录状态判断,登出后跳转至登录页
- 取消部分日志打印,调整发送验证码缓存过期时间为5分钟
This commit is contained in:
lbw
2026-01-05 12:07:15 +08:00
parent bf2a80917c
commit 49963bb49c
11 changed files with 256 additions and 10 deletions

View File

@@ -1,16 +1,22 @@
package com.yinlihupo.enlish.service.controller;
import cn.dev33.satoken.stp.StpUtil;
import com.yinlihupo.enlish.service.domain.dataobject.UserDO;
import com.yinlihupo.enlish.service.model.vo.user.FindUserInfoRspVO;
import com.yinlihupo.enlish.service.model.vo.user.UpdateUserInfoReqVO;
import com.yinlihupo.enlish.service.service.UserService;
import com.yinlihupo.framework.biz.operationlog.aspect.ApiOperationLog;
import com.yinlihupo.framework.common.response.Response;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController {
@Resource
@@ -27,4 +33,23 @@ public class UserController {
return Response.success(findUserInfoRspVO);
}
@PostMapping("update-user-info")
@ApiOperationLog(description = "修改密码")
public Response<String> updatePassword(@RequestBody UpdateUserInfoReqVO updateUserInfoReqVO) {
try {
String code = updateUserInfoReqVO.getCode();
String newPassword = updateUserInfoReqVO.getNewPassword();
String phone = updateUserInfoReqVO.getPhone();
String name = updateUserInfoReqVO.getName();
userService.updateUserInfo(newPassword, code, phone, name);
StpUtil.logout();
return Response.success();
} catch (Exception e) {
log.error("修改密码失败 {}", e.getMessage());
return Response.fail(e.getMessage());
}
}
}

View File

@@ -11,6 +11,10 @@ public interface UserDOMapper {
void insert(UserDO userDO);
void updatePassword(@Param("id") Long id, @Param("password") String password);
void updateUserInfo(@Param("id") Long id, @Param("name") String name, @Param("password") String password, @Param("phone") String phone);
UserDO selectById(Long id);
List<UserDO> selectUserDOList(@Param("name") String name, @Param("startIndex") Integer startIndex, @Param("pageSize") Integer pageSize);

View File

@@ -0,0 +1,18 @@
package com.yinlihupo.enlish.service.model.vo.user;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class UpdateUserInfoReqVO {
private String newPassword;
private String name;
private String phone;
private String code;
}

View File

@@ -13,4 +13,6 @@ public interface UserService {
Integer findUserTotal();
void createUser(UserDO userDO);
void updateUserInfo(String password, String reqCode, String phone, String name);
}

View File

@@ -21,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Service
@Slf4j
@@ -83,7 +84,9 @@ public class LoginServiceImpl implements LoginService {
@Override
public void sendVerificationCode(String phone) {
String code = RandomUtil.randomNumbers(6);
redisTemplate.opsForValue().set(UserRedisConstants.buildUserLoginCode(phone), code, Duration.ofSeconds(60));
String key = UserRedisConstants.buildUserLoginCode(phone);
redisTemplate.opsForValue().set(key, code);
redisTemplate.expire(key, 5, TimeUnit.MINUTES);
String signName = "短信测试";
String templateCode = "SMS_154950909";
String templateParam = String.format("{\"code\":\"%s\"}", code);

View File

@@ -1,14 +1,18 @@
package com.yinlihupo.enlish.service.service.user;
import cn.dev33.satoken.stp.StpUtil;
import com.yinlihupo.enlish.service.constant.UserRedisConstants;
import com.yinlihupo.enlish.service.domain.dataobject.UserDO;
import com.yinlihupo.enlish.service.domain.mapper.UserDOMapper;
import com.yinlihupo.enlish.service.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
@Service
@Slf4j
@@ -17,8 +21,10 @@ public class UserServiceImpl implements UserService {
@Resource
private UserDOMapper userDOMapper;
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Resource
private PasswordEncoder passwordEncoder;
@Override
public UserDO findUser() {
@@ -43,4 +49,21 @@ public class UserServiceImpl implements UserService {
userDOMapper.insert(userDO);
}
@Override
public void updateUserInfo(String password, String reqCode, String phone, String name) {
long id = Integer.parseInt(String.valueOf(StpUtil.getLoginId()));
UserDO userDO = userDOMapper.selectById(id);
String key = UserRedisConstants.buildUserLoginCode(userDO.getPhone());
String code = Objects.requireNonNull(redisTemplate.opsForValue().get(key)).toString();
if (code == null || !code.equals(reqCode)) {
throw new RuntimeException("验证码错误");
}
if (password != null) {
password = passwordEncoder.encode(password);
}
userDOMapper.updateUserInfo(id, name, password, phone);
}
}

View File

@@ -28,6 +28,6 @@ sa-token:
is-share: true
# 是否输出操作日志
is-log: true
logging:
level:
com.yinlihupo.enlish.service.domain.mapper: debug
#logging:
# level:
# com.yinlihupo.enlish.service.domain.mapper: debug

View File

@@ -20,6 +20,28 @@
values (#{phone}, #{name}, #{password})
</insert>
<update id="updatePassword">
update user
set password = #{password}
where id = #{id}
</update>
<update id="updateUserInfo">
update user
<set>
<if test="password != null">
password = #{password},
</if>
<if test="name != null">
`name` = #{name},
</if>
<if test="phone != null">
phone = #{phone},
</if>
</set>
where id = #{id}
</update>
<select id="selectByPhone" resultMap="BaseResultMap">
select *
from user