|
|
@@ -35,12 +35,7 @@ import com.sckw.system.model.LoginLogs;
|
|
|
import com.sckw.system.api.model.dto.res.RoleInfoDto;
|
|
|
import com.sckw.system.model.report.KwsUserExcel;
|
|
|
import com.sckw.system.model.vo.req.*;
|
|
|
-import com.sckw.system.model.vo.res.KwsUserResVo;
|
|
|
-import com.sckw.system.model.vo.res.KwsUserSystemTypeVo;
|
|
|
-import com.sckw.system.model.vo.res.LoginResVo;
|
|
|
-import com.sckw.system.model.vo.res.SalesResp;
|
|
|
-import com.sckw.system.model.vo.res.SwitchAccountResVo;
|
|
|
-import com.sckw.system.model.vo.res.UserInfoResVo;
|
|
|
+import com.sckw.system.model.vo.res.*;
|
|
|
import com.sckw.system.repository.KwsRoleRepository;
|
|
|
import com.sckw.system.repository.KwsUserRepository;
|
|
|
import com.sckw.system.repository.KwsUserRoleRepository;
|
|
|
@@ -51,6 +46,7 @@ import jakarta.servlet.http.HttpServletResponse;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
@@ -913,29 +909,29 @@ public class KwsUserService {
|
|
|
*/
|
|
|
public LoginResVo login(String account, String password, Integer systemType, HttpServletRequest request) {
|
|
|
log.info("用户登录,账号: {}, 系统类型: {}", account, systemType);
|
|
|
-
|
|
|
+
|
|
|
// 1. 查询用户信息
|
|
|
- KwsUser kwsUser = getUserByAccount(account, systemType);
|
|
|
- if (Objects.isNull(kwsUser)) {
|
|
|
- throw new SystemException(HttpStatus.QUERY_FAIL_CODE, "账号不存在");
|
|
|
- }
|
|
|
- if (kwsUser.getStatus() != null && kwsUser.getStatus() == Global.YES) {
|
|
|
- throw new SystemException(HttpStatus.CODE_10301, "账号已被锁定");
|
|
|
- }
|
|
|
+ KwsUser kwsUser = getKwsUser(account, systemType);
|
|
|
password= PasswordUtils.md5(password);
|
|
|
// 3. 校验密码
|
|
|
checkPassword(account, password, kwsUser.getPassword(), kwsUser.getSalt());
|
|
|
|
|
|
// 4. 查询用户角色
|
|
|
List<RoleInfoDto> roleInfoList = kwsUserRoleDao.queryRoleList(kwsUser.getId());
|
|
|
- String roleName = "";
|
|
|
- Long roleId = null;
|
|
|
- if (CollUtil.isNotEmpty(roleInfoList)) {
|
|
|
- roleName = roleInfoList.stream()
|
|
|
- .map(RoleInfoDto::getRoleName)
|
|
|
- .collect(Collectors.joining(","));
|
|
|
- roleId = roleInfoList.get(0).getRoleId();
|
|
|
+ if (CollUtil.isEmpty(roleInfoList)){
|
|
|
+ throw new SystemException(HttpStatus.CODE_60603, "该用户没有分配角色");
|
|
|
}
|
|
|
+ List<String> roleNames = Arrays.asList("铲车司机", "门卫");
|
|
|
+ roleInfoList = roleInfoList.stream().filter(x -> roleNames.contains(x.getRoleName()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (CollUtil.isEmpty(roleInfoList)) {
|
|
|
+ throw new SystemException(HttpStatus.CODE_60603, "你没有登录该系统权限");
|
|
|
+ }
|
|
|
+
|
|
|
+ String roleName = roleInfoList.stream()
|
|
|
+ .map(RoleInfoDto::getRoleName)
|
|
|
+ .collect(Collectors.joining(","));
|
|
|
+ Long roleId = roleInfoList.get(0).getRoleId();
|
|
|
|
|
|
// 5. 获取客户端类型
|
|
|
String clientType = request != null ? request.getHeader(RequestConstant.CLIENT_TYPE) : null;
|
|
|
@@ -944,24 +940,20 @@ public class KwsUserService {
|
|
|
}
|
|
|
|
|
|
// 6. 生成token并缓存
|
|
|
- if (request != null) {
|
|
|
- generateToken(kwsUser.getId(), account, clientType, systemType);
|
|
|
- }
|
|
|
|
|
|
+ String token = generateToken(kwsUser.getId(), account, clientType, systemType);
|
|
|
+ log.info("生成token成功,用户ID: {}, token: {}", kwsUser.getId(), token);
|
|
|
// 7. 缓存用户登录信息
|
|
|
- if (request != null) {
|
|
|
- saveUserToCache(kwsUser, systemType, clientType, roleId);
|
|
|
- }
|
|
|
+ saveUserToCache(kwsUser, systemType, clientType, roleId);
|
|
|
|
|
|
// 8. 缓存企业信息
|
|
|
- if (request != null && kwsUser.getEntId() != null) {
|
|
|
- saveEntToCache(kwsUser.getEntId());
|
|
|
+ KwsEnterprise kwsEnterprise = null;
|
|
|
+ if ( kwsUser.getEntId() != null) {
|
|
|
+ kwsEnterprise = saveEntToCache(kwsUser.getEntId());
|
|
|
}
|
|
|
|
|
|
// 9. 缓存菜单权限
|
|
|
- if (request != null && roleId != null) {
|
|
|
- saveMenusToCache(kwsUser.getId(), systemType, roleId);
|
|
|
- }
|
|
|
+ List<KwsMenuResVo> kwsMenuResVos = saveMenusToCache(kwsUser.getId(), systemType, roleId);
|
|
|
|
|
|
// 10. 组装返回结果
|
|
|
LoginResVo result = LoginResVo.builder()
|
|
|
@@ -972,6 +964,10 @@ public class KwsUserService {
|
|
|
.photo(kwsUser.getPhoto())
|
|
|
.roleId(roleId)
|
|
|
.roleName(roleName)
|
|
|
+ .token(token)
|
|
|
+ .entId(kwsUser.getEntId())
|
|
|
+ .enterpriseName(Optional.ofNullable(kwsEnterprise).map(KwsEnterprise::getFirmName).orElse(""))
|
|
|
+ .menuList(kwsMenuResVos)
|
|
|
.systemType(kwsUser.getSystemType())
|
|
|
.build();
|
|
|
|
|
|
@@ -997,43 +993,70 @@ public class KwsUserService {
|
|
|
log.info("切换账号,账号: {}, 系统类型: {}", account, systemType);
|
|
|
|
|
|
// 1. 查询用户信息
|
|
|
- KwsUser kwsUser = getUserByAccount(account, systemType);
|
|
|
- if (Objects.isNull(kwsUser)) {
|
|
|
- throw new SystemException(HttpStatus.QUERY_FAIL_CODE, "账号不存在");
|
|
|
- }
|
|
|
-
|
|
|
- // 2. 检查用户状态
|
|
|
- if (kwsUser.getDelFlag() != Global.NO) {
|
|
|
- throw new SystemException(HttpStatus.QUERY_FAIL_CODE, "账号已删除");
|
|
|
- }
|
|
|
- if (kwsUser.getStatus() != null && kwsUser.getStatus() == Global.YES) {
|
|
|
- throw new SystemException(HttpStatus.CODE_10301, "账号已被锁定");
|
|
|
- }
|
|
|
+ KwsUser kwsUser = getKwsUser(account, systemType);
|
|
|
|
|
|
// 3. 校验密码
|
|
|
+ password = PasswordUtils.md5(password);
|
|
|
checkPassword(account, password, kwsUser.getPassword(), kwsUser.getSalt());
|
|
|
|
|
|
// 4. 查询用户角色
|
|
|
List<RoleInfoDto> roleInfoList = kwsUserRoleDao.queryRoleList(kwsUser.getId());
|
|
|
- String roleName = "";
|
|
|
- if (CollUtil.isNotEmpty(roleInfoList)) {
|
|
|
- roleName = roleInfoList.stream()
|
|
|
- .map(RoleInfoDto::getRoleName)
|
|
|
- .collect(Collectors.joining(","));
|
|
|
+ if (CollUtil.isEmpty(roleInfoList)){
|
|
|
+ throw new SystemException(HttpStatus.CODE_60603, "该用户没有分配角色");
|
|
|
+ }
|
|
|
+ List<String> roleNames = Arrays.asList("铲车司机", "门卫");
|
|
|
+ roleInfoList = roleInfoList.stream().filter(x -> roleNames.contains(x.getRoleName()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (CollUtil.isEmpty(roleInfoList)) {
|
|
|
+ throw new SystemException(HttpStatus.CODE_60603, "你没有登录该系统权限");
|
|
|
+ }
|
|
|
+
|
|
|
+ String roleName = roleInfoList.stream()
|
|
|
+ .map(RoleInfoDto::getRoleName)
|
|
|
+ .collect(Collectors.joining(","));
|
|
|
+ Long roleId = roleInfoList.get(0).getRoleId();
|
|
|
+
|
|
|
+
|
|
|
+ // 5. 获取客户端类型
|
|
|
+ String clientType = request != null ? request.getHeader(RequestConstant.CLIENT_TYPE) : null;
|
|
|
+ if (StringUtils.isBlank(clientType)) {
|
|
|
+ // 默认使用app
|
|
|
+ clientType = ClientTypeEnum.app.getValue();
|
|
|
}
|
|
|
|
|
|
- // 5. 组装返回结果
|
|
|
+ // 6. 生成token并缓存
|
|
|
+ String token = generateToken(kwsUser.getId(), account, clientType, systemType);
|
|
|
+ log.info("生成token成功,用户ID: {}, token: {}", kwsUser.getId(), token);
|
|
|
+
|
|
|
+ // 7. 缓存用户登录信息
|
|
|
+ saveUserToCache(kwsUser, systemType, clientType, roleId);
|
|
|
+
|
|
|
+ // 8. 缓存企业信息
|
|
|
+ KwsEnterprise kwsEnterprise = null;
|
|
|
+ if ( kwsUser.getEntId() != null) {
|
|
|
+ kwsEnterprise = saveEntToCache(kwsUser.getEntId());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 9. 缓存菜单权限
|
|
|
+ List<KwsMenuResVo> kwsMenuResVos = saveMenusToCache(kwsUser.getId(), systemType, roleId);
|
|
|
+
|
|
|
+ // 10. 组装返回结果
|
|
|
SwitchAccountResVo result = SwitchAccountResVo.builder()
|
|
|
.userId(kwsUser.getId())
|
|
|
.name(kwsUser.getName())
|
|
|
.account(kwsUser.getAccount())
|
|
|
.phone(kwsUser.getPhone())
|
|
|
.photo(kwsUser.getPhoto())
|
|
|
+ .roleId(roleId)
|
|
|
.roleName(roleName)
|
|
|
+ .token(token)
|
|
|
+ .entId(kwsUser.getEntId())
|
|
|
+ .enterpriseName(Optional.ofNullable(kwsEnterprise).map(KwsEnterprise::getFirmName).orElse(""))
|
|
|
+ .menuList(kwsMenuResVos)
|
|
|
.systemType(kwsUser.getSystemType())
|
|
|
.build();
|
|
|
|
|
|
- // 6. 记录切换账号登录日志
|
|
|
+ // 11. 记录切换账号登录日志
|
|
|
if (request != null) {
|
|
|
saveLoginLogs(kwsUser.getId(), request, 2);
|
|
|
}
|
|
|
@@ -1042,6 +1065,20 @@ public class KwsUserService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ @NotNull
|
|
|
+ private KwsUser getKwsUser(String account, Integer systemType) {
|
|
|
+ KwsUser kwsUser = getUserByAccount(account, systemType);
|
|
|
+ if (Objects.isNull(kwsUser)) {
|
|
|
+ throw new SystemException(HttpStatus.QUERY_FAIL_CODE, "账号不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 检查用户状态
|
|
|
+ if (kwsUser.getStatus() != null && kwsUser.getStatus() == Global.YES) {
|
|
|
+ throw new SystemException(HttpStatus.CODE_10301, "账号已被锁定");
|
|
|
+ }
|
|
|
+ return kwsUser;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 退出登录(叉车APP)
|
|
|
*
|
|
|
@@ -1063,19 +1100,28 @@ public class KwsUserService {
|
|
|
}
|
|
|
|
|
|
// 3. 清除用户登录信息
|
|
|
- String fullUserLoginKey = Global.getFullUserLoginKey(kwsUser.getSystemType(), userId);
|
|
|
- RedissonUtils.remove(fullUserLoginKey);
|
|
|
+ for (SystemTypeEnum systemTypeEnum : SystemTypeEnum.values()) {
|
|
|
+ String fullUserLoginKey = Global.getFullUserLoginKey(systemTypeEnum.getCode(), userId);
|
|
|
+ RedissonUtils.remove(fullUserLoginKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ //4.清除企业缓存
|
|
|
+ String key = Global.getFullUserEntKey(kwsUser.getEntId());
|
|
|
+ RedissonUtils.remove(key);
|
|
|
+ //5.清除菜单缓存
|
|
|
+ String menuKey = Global.REDIS_SYS_MENU_PREFIX + userId;
|
|
|
+ RedissonUtils.remove(menuKey);
|
|
|
|
|
|
log.info("退出登录成功,用户ID: {}", userId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成token并缓存
|
|
|
- * @param userId 用户ID
|
|
|
- * @param account 账号
|
|
|
+ *
|
|
|
+ * @param userId 用户ID
|
|
|
+ * @param account 账号
|
|
|
* @param clientType 客户端类型
|
|
|
* @param systemType 系统类型
|
|
|
- * @return token
|
|
|
*/
|
|
|
private String generateToken(Long userId, String account, String clientType, Integer systemType) {
|
|
|
Map<String, Object> info = new HashMap<>(Global.NUMERICAL_SIXTEEN);
|
|
|
@@ -1134,16 +1180,19 @@ public class KwsUserService {
|
|
|
* 缓存企业信息
|
|
|
* @param entId 企业ID
|
|
|
*/
|
|
|
- private void saveEntToCache(Long entId) {
|
|
|
+ private KwsEnterprise saveEntToCache(Long entId) {
|
|
|
try {
|
|
|
- EntCacheResDto enterprise = remoteSystemService.queryEntDetails(entId);
|
|
|
+ //企业信息
|
|
|
+ KwsEnterprise enterprise = kwsEnterpriseDao.selectByKey(entId);
|
|
|
if (enterprise != null) {
|
|
|
String key = Global.getFullUserEntKey(entId);
|
|
|
RedissonUtils.putString(key, JSON.toJSONString(enterprise), Global.APP_TOKEN_EXPIRE);
|
|
|
}
|
|
|
+ return enterprise;
|
|
|
} catch (Exception e) {
|
|
|
log.error("缓存企业信息失败,企业ID: {}", entId, e);
|
|
|
}
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1152,30 +1201,35 @@ public class KwsUserService {
|
|
|
* @param systemType 系统类型
|
|
|
* @param roleId 角色ID
|
|
|
*/
|
|
|
- private void saveMenusToCache(Long userId, Integer systemType, Long roleId) {
|
|
|
+ private List<KwsMenuResVo> saveMenusToCache(Long userId, Integer systemType, Long roleId) {
|
|
|
+ if (Objects.isNull(roleId)){
|
|
|
+ return List.of();
|
|
|
+ }
|
|
|
try {
|
|
|
// 使用本地服务查询菜单
|
|
|
com.sckw.system.model.pojo.FindMenuTreePojo findMenuTreePojo = new com.sckw.system.model.pojo.FindMenuTreePojo();
|
|
|
findMenuTreePojo.setRoleIds(Collections.singletonList(roleId));
|
|
|
|
|
|
- List<com.sckw.system.model.vo.res.KwsMenuResVo> kwsMenuResVos = kwsMenuService.findList(findMenuTreePojo);
|
|
|
+ List<KwsMenuResVo> kwsMenuResVos = kwsMenuService.findList(findMenuTreePojo);
|
|
|
if (CollectionUtils.isEmpty(kwsMenuResVos)) {
|
|
|
RedissonUtils.delete(Global.REDIS_SYS_MENU_PREFIX + systemType + Global.COLON + userId);
|
|
|
log.warn("未查询到角色{}的菜单权限", roleId);
|
|
|
- return;
|
|
|
+ return List.of();
|
|
|
}
|
|
|
|
|
|
List<String> menus = new ArrayList<>();
|
|
|
- for (com.sckw.system.model.vo.res.KwsMenuResVo menuResVo : kwsMenuResVos) {
|
|
|
+ for (KwsMenuResVo menuResVo : kwsMenuResVos) {
|
|
|
String links = menuResVo.getLinks();
|
|
|
if (StringUtils.isNotBlank(links)) {
|
|
|
menus.addAll(Arrays.asList(links.split(",")));
|
|
|
}
|
|
|
}
|
|
|
RedissonUtils.putSet(Global.REDIS_SYS_MENU_PREFIX + userId, menus);
|
|
|
+ return kwsMenuResVos;
|
|
|
} catch (Exception e) {
|
|
|
log.error("缓存菜单权限失败,用户ID: {}, 角色ID: {}", userId, roleId, e);
|
|
|
}
|
|
|
+ return List.of();
|
|
|
}
|
|
|
|
|
|
/**
|