Skip to content

Commit

Permalink
fix: 修复查询用户邮箱、手机号时未自动加密导致的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Apr 29, 2024
1 parent 53eaef9 commit faa56d1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,20 @@ public interface UserMapper extends DataPermissionMapper<UserDO> {
*/
@Select("SELECT nickname FROM sys_user WHERE id = #{id}")
String selectNicknameById(@Param("id") Long id);

/**
* 根据邮箱查询数量
*
* @param email 邮箱
* @return 用户数量
*/
Long selectCountByEmail(@FieldEncrypt @Param("email") String email, @Param("id") Long id);

/**
* 根据手机号查询数量
*
* @param phone 手机号
* @return 用户数量
*/
Long selectCountByPhone(@FieldEncrypt @Param("phone") String phone, @Param("id") Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,7 @@ public void updatePassword(String oldPassword, String newPassword, Long id) {
public void updatePhone(String newPhone, String currentPassword, Long id) {
UserDO user = super.getById(id);
CheckUtils.throwIf(!passwordEncoder.matches(currentPassword, user.getPassword()), "当前密码错误");
Long count = baseMapper.lambdaQuery().eq(UserDO::getPhone, newPhone).count();
CheckUtils.throwIf(count > 0, "手机号已绑定其他账号,请更换其他手机号");
CheckUtils.throwIf(this.isPhoneExists(newPhone, id), "手机号已绑定其他账号,请更换其他手机号");
CheckUtils.throwIfEqual(newPhone, user.getPhone(), "新手机号不能与当前手机号相同");
// 更新手机号
baseMapper.lambdaUpdate().set(UserDO::getPhone, newPhone).eq(UserDO::getId, id).update();
Expand All @@ -228,8 +227,7 @@ public void updatePhone(String newPhone, String currentPassword, Long id) {
public void updateEmail(String newEmail, String currentPassword, Long id) {
UserDO user = super.getById(id);
CheckUtils.throwIf(!passwordEncoder.matches(currentPassword, user.getPassword()), "当前密码错误");
Long count = baseMapper.lambdaQuery().eq(UserDO::getEmail, newEmail).count();
CheckUtils.throwIf(count > 0, "邮箱已绑定其他账号,请更换其他邮箱");
CheckUtils.throwIf(this.isEmailExists(newEmail, id), "邮箱已绑定其他账号,请更换其他邮箱");
CheckUtils.throwIfEqual(newEmail, user.getEmail(), "新邮箱不能与当前邮箱相同");
// 更新邮箱
baseMapper.lambdaUpdate().set(UserDO::getEmail, newEmail).eq(UserDO::getId, id).update();
Expand Down Expand Up @@ -296,7 +294,8 @@ private boolean isNameExists(String name, Long id) {
* @return 是否存在
*/
private boolean isEmailExists(String email, Long id) {
return baseMapper.lambdaQuery().eq(UserDO::getEmail, email).ne(null != id, UserDO::getId, id).exists();
Long count = baseMapper.selectCountByEmail(email, id);
return null != count && count > 0;
}

/**
Expand All @@ -307,6 +306,7 @@ private boolean isEmailExists(String email, Long id) {
* @return 是否存在
*/
private boolean isPhoneExists(String phone, Long id) {
return baseMapper.lambdaQuery().eq(UserDO::getPhone, phone).ne(null != id, UserDO::getId, id).exists();
Long count = baseMapper.selectCountByPhone(phone, id);
return null != count && count > 0;
}
}
18 changes: 18 additions & 0 deletions continew-admin-system/src/main/resources/mapper/UserMapper.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="top.continew.admin.system.mapper.UserMapper">

<select id="selectCountByEmail" resultType="java.lang.Long">
SELECT count(*)
FROM sys_user
WHERE email = #{email}
<if test="id != null">
AND id != #{id}
</if>
</select>

<select id="selectCountByPhone" resultType="java.lang.Long">
SELECT count(*)
FROM sys_user
WHERE phone = #{phone}
<if test="id != null">
AND id != #{id}
</if>
</select>
</mapper>

0 comments on commit faa56d1

Please sign in to comment.