同步代码

This commit is contained in:
lulz1 2024-02-28 09:42:59 +08:00
parent 0654ee094a
commit d1acab14e5
6 changed files with 20 additions and 22 deletions

View File

@ -2,6 +2,7 @@ package com.chint.application.out;
import com.chint.application.commands.OrderCreateCommand; import com.chint.application.commands.OrderCreateCommand;
import com.chint.application.services.login.strategy.PailaLoginStrategy;
import com.chint.domain.aggregates.user.User; import com.chint.domain.aggregates.user.User;
import com.chint.domain.exceptions.OrderException; import com.chint.domain.exceptions.OrderException;
import com.chint.domain.service.SystemDomainService; import com.chint.domain.service.SystemDomainService;
@ -16,6 +17,7 @@ import com.chint.infrastructure.util.Digest;
import com.chint.infrastructure.util.Result; import com.chint.infrastructure.util.Result;
import com.chint.interfaces.rest.ctrip.CTripUserSaveRequest; import com.chint.interfaces.rest.ctrip.CTripUserSaveRequest;
import com.chint.interfaces.rest.ly.LYUserRequest; import com.chint.interfaces.rest.ly.LYUserRequest;
import com.chint.interfaces.rest.ly.dto.login.LoginParam;
import com.chint.interfaces.rest.user.UserHttpRequest; import com.chint.interfaces.rest.user.UserHttpRequest;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -43,6 +45,9 @@ public class LoginController {
@Autowired @Autowired
private SystemDomainService systemDomainService; private SystemDomainService systemDomainService;
@Autowired
private PailaLoginStrategy pailaLoginStrategy;
@ApiOperation("财务共享登录") @ApiOperation("财务共享登录")
@Transactional @Transactional
@GetMapping("/login") @GetMapping("/login")
@ -98,9 +103,11 @@ public class LoginController {
@ApiOperation("商旅平台单点") @ApiOperation("商旅平台单点")
@Transactional @Transactional
@PostMapping("/login/sso") @PostMapping("/login/sso")
public Result<UserLoginResult> loginSSO(){ public Result<UserLoginResult> loginSSO(@RequestBody UserLoginParam userLoginParam){
UserLoginParam userLoginParam = new UserLoginParam();
userLoginParam.setSfno("170717012"); String employeeNo = pailaLoginStrategy.login(userLoginParam.getCode());
userLoginParam.setSfno(employeeNo);
UserLoginResult userLoginResult = authenticateService UserLoginResult userLoginResult = authenticateService
.authenticateEmployeeNo(userLoginParam); .authenticateEmployeeNo(userLoginParam);

View File

@ -15,7 +15,7 @@ public class LocalLoginStrategy implements LoginStrategy {
} }
@Override @Override
public User getUserInfo(String accessToken) { public String getUserInfo(String accessToken) {
return null; return null;
} }
} }

View File

@ -20,14 +20,14 @@ import java.util.regex.Pattern;
public interface LoginStrategy { public interface LoginStrategy {
Logger log = LoggerFactory.getLogger("UserService"); Logger log = LoggerFactory.getLogger("UserService");
default User login(String code) { default String login(String code) {
Optional<String> accessToken = getAccessToken(code); Optional<String> accessToken = getAccessToken(code);
return getUserInfo(accessToken.orElseThrow(()-> new AuthException("Failed to obtain access token"))); return getUserInfo(accessToken.orElseThrow(()-> new AuthException("Failed to obtain access token")));
} }
Optional<String> getAccessToken(String code); Optional<String> getAccessToken(String code);
User getUserInfo(String accessToken); String getUserInfo(String accessToken);
static Optional<String> getAccessTokenMethod(HttpRequestBase request, String tokenName) { static Optional<String> getAccessTokenMethod(HttpRequestBase request, String tokenName) {
String responseBody = null; String responseBody = null;

View File

@ -3,8 +3,7 @@ package com.chint.application.services.login.strategy;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.chint.application.dtos.UserDTO; import com.chint.application.dtos.UserDTO;
import com.chint.application.services.login.LoginStrategy; import com.chint.application.services.login.LoginStrategy;
import com.chint.domain.aggregates.user.User; import com.chint.domain.exceptions.NotFoundException;
import com.chint.domain.exceptions.AuthException;
import com.chint.domain.factoriy.user.UserFactory; import com.chint.domain.factoriy.user.UserFactory;
import com.chint.domain.repository.UserRepository; import com.chint.domain.repository.UserRepository;
import com.chint.interfaces.rest.user.UserHttpRequest; import com.chint.interfaces.rest.user.UserHttpRequest;
@ -71,7 +70,7 @@ public class PailaLoginStrategy implements LoginStrategy {
} }
@Override @Override
public User getUserInfo(String accessToken) { public String getUserInfo(String accessToken) {
List<NameValuePair> userInfoParams = Collections.singletonList( List<NameValuePair> userInfoParams = Collections.singletonList(
@ -89,18 +88,10 @@ public class PailaLoginStrategy implements LoginStrategy {
log.error(e.getMessage()); log.error(e.getMessage());
} }
UserDTO userDTO = JSON.parseObject(userInfoResBody, UserDTO.class); UserDTO userDTO = JSON.parseObject(userInfoResBody, UserDTO.class);
User user; if (userDTO == null) {
if (userDTO != null) { throw new NotFoundException(NOT_FOUND);
user = userRepository.findByUserEmployeeNo(userDTO.getUid());
if (user == null) {
user = userFactory.create(userDTO.getUid());
userHttpRequest.loadUserInfo(user);
userRepository.save(user);
}
} else {
throw new AuthException(NOT_FOUND);
} }
return user; return userDTO.getUid();
} }
private HttpGet getRequest(String path, List<NameValuePair> parameters) { private HttpGet getRequest(String path, List<NameValuePair> parameters) {

View File

@ -41,7 +41,7 @@ public class AuthenticateServiceImpl implements AuthenticateService {
@Override @Override
public UserLoginResult authenticateEmployeeNo(UserLoginParam userLoginParam) { public UserLoginResult authenticateEmployeeNo(UserLoginParam userLoginParam) {
User user = userRepository.findByUserEmployeeNo(userLoginParam.getSfno()); User user = userRepository.findByUserEmployeeNo(userLoginParam.getSfno());
if (user != null) { if (user != null) {
// 部分数据需要通过查询外部的http来获取 // 部分数据需要通过查询外部的http来获取
BaseContext.setCurrentUser(user); BaseContext.setCurrentUser(user);
user.setCompanyCode(userLoginParam.getCompanyCode()); user.setCompanyCode(userLoginParam.getCompanyCode());

View File

@ -121,7 +121,7 @@ class RouteApplicationTests {
// @Test // @Test
void ssoLogin(){ void ssoLogin(){
User login = pailaLoginStrategy.login("OC-5909-zRqrWjZGNThNXJiAV1kA7dPXTojGzVxK3nE"); String login = pailaLoginStrategy.login("OC-5909-zRqrWjZGNThNXJiAV1kA7dPXTojGzVxK3nE");
System.out.println(login); System.out.println(login);
} }
} }