|
|
@@ -3,6 +3,7 @@ package shop.alien.store.service.impl;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
@@ -14,13 +15,17 @@ import shop.alien.entity.store.dto.SystemUserStatusDto;
|
|
|
import shop.alien.entity.store.vo.SystemLoginVo;
|
|
|
import shop.alien.store.config.BaseRedisService;
|
|
|
import shop.alien.mapper.LifeSysMapper;
|
|
|
+import shop.alien.store.service.LifeSysUserRoleService;
|
|
|
import shop.alien.store.service.SystemService;
|
|
|
import shop.alien.util.common.JwtUtil;
|
|
|
|
|
|
import java.security.MessageDigest;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
|
@@ -30,6 +35,7 @@ import java.util.Map;
|
|
|
* @author ssk
|
|
|
* @since 2025-02-20
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
@RequiredArgsConstructor
|
|
|
public class SystemServiceImpl implements SystemService {
|
|
|
@@ -38,6 +44,8 @@ public class SystemServiceImpl implements SystemService {
|
|
|
|
|
|
private final BaseRedisService baseRedisService;
|
|
|
|
|
|
+ private final LifeSysUserRoleService lifeSysUserRoleService;
|
|
|
+
|
|
|
@Value("${jwt.expiration-time}")
|
|
|
private String effectiveTime;
|
|
|
|
|
|
@@ -143,12 +151,12 @@ public class SystemServiceImpl implements SystemService {
|
|
|
}
|
|
|
|
|
|
// 校验邮箱是否已存在
|
|
|
- LambdaQueryWrapper<LifeSys> emailQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
+/* LambdaQueryWrapper<LifeSys> emailQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
emailQueryWrapper.eq(LifeSys::getEmail, addDto.getEmail());
|
|
|
LifeSys existingEmail = lifeSysMapper.selectOne(emailQueryWrapper);
|
|
|
if (existingEmail != null) {
|
|
|
return R.fail("该邮箱已被使用,请更换其他邮箱");
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
/* // 校验手机号是否已存在
|
|
|
LambdaQueryWrapper<LifeSys> phoneQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
@@ -166,10 +174,10 @@ public class SystemServiceImpl implements SystemService {
|
|
|
lifeSys.setEmail(addDto.getEmail());
|
|
|
// 密码使用MD5加密后存储
|
|
|
// lifeSys.setUserPassword(encryptToMD5(addDto.getPassword()));
|
|
|
- lifeSys.setUserPassword(addDto.getPassword());
|
|
|
+ lifeSys.setUserPassword("123456");
|
|
|
|
|
|
- // 处理多角色:将角色ID列表转换为逗号分隔的字符串
|
|
|
- lifeSys.setRoleId(addDto.getRoleIds());
|
|
|
+ // 不再将角色ID保存到life_sys表的role_id字段,而是保存到中间关系表
|
|
|
+ // lifeSys.setRoleId(addDto.getRoleIds());
|
|
|
|
|
|
lifeSys.setDepartmentId(addDto.getDepartmentId());
|
|
|
lifeSys.setRemark(addDto.getRemark());
|
|
|
@@ -179,6 +187,26 @@ public class SystemServiceImpl implements SystemService {
|
|
|
// 保存用户
|
|
|
int result = lifeSysMapper.insert(lifeSys);
|
|
|
if (result > 0) {
|
|
|
+ // 保存用户与角色的关系到中间表
|
|
|
+ if (StringUtils.hasText(addDto.getRoleIds())) {
|
|
|
+ try {
|
|
|
+ // 解析角色ID字符串(支持逗号分隔)
|
|
|
+ List<Long> roleIdList = Arrays.stream(addDto.getRoleIds().split(","))
|
|
|
+ .map(String::trim)
|
|
|
+ .filter(id -> !id.isEmpty())
|
|
|
+ .map(Long::parseLong)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 批量保存到中间关系表
|
|
|
+ if (!roleIdList.isEmpty()) {
|
|
|
+ lifeSysUserRoleService.assignRoles(lifeSys.getId(), roleIdList);
|
|
|
+ }
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ log.error("角色ID格式错误: {}", addDto.getRoleIds(), e);
|
|
|
+ // 角色ID解析失败不影响用户创建,只记录日志
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 清除密码信息,避免返回到前端
|
|
|
lifeSys.setUserPassword(null);
|
|
|
return R.data(lifeSys, "新增账号成功");
|