This commit is contained in:
zhangchenbiao
2023-05-23 16:37:25 +08:00
parent 63505941d0
commit 799b6b8cf5
59 changed files with 1994 additions and 76 deletions

View File

@@ -1,4 +1,4 @@
package com.cool.store.service.config.redis;
package com.cool.store.config.redis;
import com.cool.store.utils.RedisUtilPool;
import org.springframework.beans.factory.annotation.Value;

View File

@@ -0,0 +1,17 @@
package com.cool.store.config.rest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author dong_gui on 2020/5/20.
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

View File

@@ -1,5 +1,6 @@
package com.cool.store.service.context;
package com.cool.store.context;
import com.cool.store.entity.SysRoleDO;
import lombok.Data;
@@ -36,4 +37,6 @@ public class CurrentUser {
private String roleAuth;
private String mainCorpId;
private SysRoleDO sysRoleDO;
}

View File

@@ -1,4 +1,4 @@
package com.cool.store.service.context;
package com.cool.store.context;
import java.util.ArrayList;
import java.util.List;

View File

@@ -1,4 +1,4 @@
package com.cool.store.service.context;
package com.cool.store.context;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;

View File

@@ -0,0 +1,43 @@
package com.cool.store.http;
import com.alibaba.fastjson.JSONObject;
import com.cool.store.utils.RestTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.util.Objects;
/**
* @author zhangchenbiao
* @FileName: ISVHttpRequest
* @Description:
* @date 2023-05-23 16:03
*/
@Service
@Slf4j
public class ISVHttpRequest {
@Value("${isv.domain:null}")
private String isvDomain;
@Value("${get.user.id.by.code:null}")
private String getUserIdByCode;
public JSONObject getUserIdByCode(String paramStr){
String url = isvDomain + getUserIdByCode + paramStr;
ResponseEntity<JSONObject> responseEntity = null;
try {
responseEntity = RestTemplateUtil.loadGet(url, JSONObject.class);
log.info("url:{}, response:{}", url, JSONObject.toJSONString(responseEntity));
} catch (Exception e) {
log.info("调用isv出错{}", e);
}
if(Objects.isNull(responseEntity)){
return null;
}
return responseEntity.getBody();
}
}

View File

@@ -0,0 +1,9 @@
package com.cool.store.service;
public interface LoginService {
Object feiShuLogin(String userId, String corpId, Boolean needRefreshToken , String appType, String avatar);
}

View File

@@ -1,8 +1,8 @@
package com.cool.store.service.impl;
import com.cool.store.service.EnterpriseConfigService;
import com.cool.store.dao.EnterpriseConfigDAO;
import com.cool.store.entity.EnterpriseConfigDO;
import com.cool.store.service.EnterpriseConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

View File

@@ -0,0 +1,132 @@
package com.cool.store.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cool.store.constants.CommonConstants;
import com.cool.store.context.CurrentUser;
import com.cool.store.context.DataSourceContext;
import com.cool.store.dao.*;
import com.cool.store.dto.login.RefreshUser;
import com.cool.store.entity.EnterpriseConfigDO;
import com.cool.store.entity.EnterpriseUserDO;
import com.cool.store.entity.SysRoleDO;
import com.cool.store.enums.AppTypeEnum;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.UserStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.model.entity.EnterpriseDO;
import com.cool.store.service.LoginService;
import com.cool.store.utils.DataSourceHelper;
import com.cool.store.utils.RedisUtilPool;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;
/**
* @author zhangchenbiao
* @FileName: LoginServiceImpl
* @Description:
* @date 2023-05-23 9:57
*/
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
@Resource
private EnterpriseConfigDAO enterpriseConfigDAO;
@Resource
private EnterpriseDAO enterpriseDAO;
@Resource
private EnterpriseUserDAO enterpriseUserDAO;
@Resource
private SysRoleDAO sysRoleDAO;
@Resource
private LoginRecordDAO loginRecordDAO;
@Resource
private RedisUtilPool redisUtilPool;
@Override
public Object feiShuLogin(String userId, String corpId, Boolean needRefreshToken, String appType, String avatar) {
log.info("isvLogin, corpId={}, userId={}, appType={}", corpId, userId, appType);
DataSourceContext.clearDataSourceType();
if (StringUtils.isEmpty(userId)) {
throw new ServiceException(ErrorCodeEnum.USER_NOT_EXIST);
}
if (StringUtils.isEmpty(corpId)) {
throw new ServiceException(ErrorCodeEnum.ENTERPRISE_NOT_EXIST);
}
EnterpriseConfigDO enterpriseConfig = enterpriseConfigDAO.getConfigByCorpIdAndAppType(corpId, appType);
if (enterpriseConfig == null) {
throw new ServiceException(ErrorCodeEnum.ENTERPRISE_INIT);
}
EnterpriseDO enterprise = enterpriseDAO.getEnterpriseById(enterpriseConfig.getEnterpriseId());
if (enterprise == null || enterprise.getStatus() == CommonConstants.ZERO) {
throw new ServiceException(ErrorCodeEnum.ENTERPRISE_INIT);
}
CurrentUser currentUser = new CurrentUser();
RefreshUser refreshUser = new RefreshUser();
String dbName = enterpriseConfig.getDbName();
// 切到企业库
DataSourceContext.setDataSourceType(dbName);
// 查企业用户
EnterpriseUserDO enterpriseUser = enterpriseUserDAO.getUserInfoById(enterprise.getId(), userId);
if(enterpriseUser == null){
throw new ServiceException(ErrorCodeEnum.NOT_AUTH);
}
//冻结登录 超登用户过滤
if (enterprise.getStatus() == CommonConstants.HUNDRED && !StringUtils.equals(enterpriseUser.getUserId(), "a100000001")) {
throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR.getCode(), "企业被冻结");
}
SysRoleDO sysRole = sysRoleDAO.getHighestPrioritySysRoleDoByUserId(enterprise.getId(), userId);
if(Objects.isNull(sysRole)){
log.info("当前用户没角色:{}", userId);
throw new ServiceException(ErrorCodeEnum.NOT_AUTH);
}
if(UserStatusEnum.FREEZE.getCode().equals(enterpriseUser.getUserStatus())){
throw new ServiceException(ErrorCodeEnum.USER_FREEZE);
}
if(UserStatusEnum.WAIT_AUDIT.getCode().equals(enterpriseUser.getUserStatus())){
throw new ServiceException(ErrorCodeEnum.USER_WAIT_AUDIT);
}
//用户是否已有头像
Boolean hasAvatar = StringUtils.isNotEmpty(enterpriseUser.getAvatar()) && !enterpriseUser.getAvatar().contains(CommonConstants.DEFAULT_AVATAR);
String finalAvatar = hasAvatar ? enterpriseUser.getAvatar() : avatar;
enterpriseUser.setAvatar(finalAvatar);
enterpriseUser.setFaceUrl(finalAvatar);
currentUser.setUserId(enterpriseUser.getUserId());
currentUser.setDbName(enterpriseConfig.getDbName());
currentUser.setIsAdmin(enterpriseUser.getIsAdmin());
currentUser.setMainCorpId(enterpriseConfig.getMainCorpId());
//设置当前登录人使用的企业相关信息
currentUser.setRoleAuth(sysRole.getRoleAuth());
currentUser.setSysRoleDO(sysRole);
currentUser.setEnterpriseId(enterprise.getId());
//生成令牌
RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
String token = randomNumberGenerator.nextBytes().toHex();
String refreshToken = randomNumberGenerator.nextBytes().toHex();
currentUser.setName(enterpriseUser.getName());
currentUser.setDingCorpId(enterpriseConfig.getDingCorpId());
currentUser.setAccessToken(token);
currentUser.setAppType(appType);
JSONObject jsonObject = new JSONObject();
jsonObject.put("action_token", token);
jsonObject.put("user", currentUser);
redisUtilPool.setString(CommonConstants.ACCESS_TOKEN_KEY + ":" + token, JSON.toJSONString(currentUser), CommonConstants.ACTION_TOKEN_EXPIRE);
jsonObject.put("expire", CommonConstants.ACTION_TOKEN_EXPIRE);
redisUtilPool.setString(currentUser.getUserId(), token);
redisUtilPool.setString(CommonConstants.REFRESH_TOKEN_KEY+":"+refreshToken,JSON.toJSONString(refreshUser), CommonConstants.REFRESH_TOKEN_EXPIRE);
jsonObject.put("refresh_token",refreshToken);
loginRecordDAO.addLoginRecord(currentUser.getEnterpriseId(),currentUser.getUserId());
log.info("[" + enterpriseUser.getName() + "; action_token"+ token + "; userId" + currentUser.getUserId() +"]登入系统成功");
return jsonObject;
}
}

View File

@@ -1,7 +1,7 @@
package com.cool.store.service.utils;
package com.cool.store.utils;
import com.cool.store.service.context.DataSourceContext;
import com.cool.store.service.context.UserContext;
import com.cool.store.context.DataSourceContext;
import com.cool.store.context.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;