修复:修复用户管理手机号和所属店铺问题
This commit is contained in:
+42
-4
@@ -45,6 +45,8 @@ import java.util.stream.Collectors;
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements UserService {
|
||||
|
||||
private static final String VIP_LEVEL_NORMAL = "普通";
|
||||
private static final String PHONE_REGEX = "^1[3-9]\\d{9}$";
|
||||
private static final String DELETED_PHONE_MARKER = "_deleted_";
|
||||
private static final List<String> ADMIN_DEFAULT_PERMISSION_CODES = List.of(
|
||||
"home:view",
|
||||
"dashboard:view",
|
||||
@@ -110,13 +112,16 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
throw new RuntimeException("账号已存在");
|
||||
}
|
||||
|
||||
Long validatedShopId = validateAndNormalizeShopId(userDTO.getShopId());
|
||||
String validatedPhone = validateUserPhone(userDTO.getPhone(), null, true);
|
||||
|
||||
// 创建用户实体并设置属性
|
||||
UserDO userDO = new UserDO();
|
||||
userDO.setShopId(userDTO.getShopId());
|
||||
userDO.setShopId(validatedShopId);
|
||||
userDO.setNickname(userDTO.getNickname());
|
||||
userDO.setAccount(userDTO.getAccount());
|
||||
userDO.setPassword(passwordEncoder.encode(userDTO.getPassword()));
|
||||
userDO.setPhone(userDTO.getPhone());
|
||||
userDO.setPhone(validatedPhone);
|
||||
userDO.setSignature(userDTO.getSignature());
|
||||
userDO.setLevel(userDTO.getLevel());
|
||||
userDO.setType(userDTO.getType());
|
||||
@@ -229,7 +234,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
|
||||
// 更新其他字段(如果提供)
|
||||
if (userDTO.getShopId() != null) {
|
||||
userDO.setShopId(userDTO.getShopId());
|
||||
userDO.setShopId(validateAndNormalizeShopId(userDTO.getShopId()));
|
||||
}
|
||||
if (userDTO.getNickname() != null) {
|
||||
userDO.setNickname(userDTO.getNickname());
|
||||
@@ -238,7 +243,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
userDO.setPassword(passwordEncoder.encode(userDTO.getPassword()));
|
||||
}
|
||||
if (userDTO.getPhone() != null) {
|
||||
userDO.setPhone(userDTO.getPhone());
|
||||
userDO.setPhone(validateUserPhone(userDTO.getPhone(), id, true));
|
||||
}
|
||||
if (userDTO.getAvatar() != null) {
|
||||
if (userDTO.getAvatar().isEmpty()) {
|
||||
@@ -893,6 +898,39 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
}
|
||||
}
|
||||
|
||||
private Long validateAndNormalizeShopId(Long shopId) {
|
||||
Long normalizedShopId = normalizeShopId(shopId);
|
||||
if (normalizedShopId == null) {
|
||||
return null;
|
||||
}
|
||||
if (shopService.getShopById(normalizedShopId) == null) {
|
||||
throw new RuntimeException("所属店铺不存在或已失效");
|
||||
}
|
||||
return normalizedShopId;
|
||||
}
|
||||
|
||||
private String validateUserPhone(String phone, Long currentUserId, boolean required) {
|
||||
String normalizedPhone = phone == null ? null : phone.trim();
|
||||
if (!StringUtils.hasText(normalizedPhone)) {
|
||||
if (required) {
|
||||
throw new RuntimeException("手机号不能为空");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (isDeletedPlaceholderPhone(normalizedPhone)) {
|
||||
return normalizedPhone;
|
||||
}
|
||||
if (!normalizedPhone.matches(PHONE_REGEX)) {
|
||||
throw new RuntimeException("手机号格式不正确");
|
||||
}
|
||||
ensurePhoneNotUsed(normalizedPhone, currentUserId);
|
||||
return normalizedPhone;
|
||||
}
|
||||
|
||||
private boolean isDeletedPlaceholderPhone(String phone) {
|
||||
return StringUtils.hasText(phone) && phone.contains(DELETED_PHONE_MARKER);
|
||||
}
|
||||
|
||||
private void ensurePhoneNotUsed(String phone, Long currentUserId) {
|
||||
LambdaQueryWrapper<UserDO> wrapper = Wrappers.lambdaQuery(UserDO.class)
|
||||
.eq(UserDO::getPhone, phone)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.xiaowang.yuminailcore.vo;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
@@ -23,12 +25,14 @@ public class LoginResponseVO {
|
||||
|
||||
@Data
|
||||
public static class UserInfo {
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long id;
|
||||
private String account;
|
||||
private String nickname;
|
||||
private String avatar;
|
||||
private Integer type;
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long shopId;
|
||||
private LocalDateTime createTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.xiaowang.yuminailcore.vo;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
@@ -18,6 +20,7 @@ public class ShopVO {
|
||||
/**
|
||||
* 店铺唯一主键ID
|
||||
*/
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.xiaowang.yuminailcore.vo;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
@@ -17,11 +19,13 @@ public class UserVO {
|
||||
/**
|
||||
* 用户唯一主键ID(雪花ID)
|
||||
*/
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 所属店铺ID,关联店铺表主键(0表示无所属店铺)
|
||||
*/
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long shopId;
|
||||
|
||||
/**
|
||||
@@ -67,6 +71,7 @@ public class UserVO {
|
||||
/**
|
||||
* 关联VIP信息表主键(0表示非VIP)
|
||||
*/
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long vipId;
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user