diff --git a/src/main/java/com/chint/application/out/LoginController.java b/src/main/java/com/chint/application/out/LoginController.java index f2adef3e..8be57fe9 100644 --- a/src/main/java/com/chint/application/out/LoginController.java +++ b/src/main/java/com/chint/application/out/LoginController.java @@ -130,4 +130,35 @@ public class LoginController { userLoginResult.getUser().setManaLevel(null); return Result.Success(CommonMessageConstant.SUCCESS, userLoginResult); } + + @ApiOperation("商旅平台单点") + @Transactional + @PostMapping("/login/sso/mobile") + public Result loginSSOMobile(@RequestBody UserLoginParam userLoginParam){ + + String employeeNo = pailaLoginStrategy.login(userLoginParam.getCode()); + + userLoginParam.setSfno(employeeNo); + UserLoginResult userLoginResult = authenticateService + .authenticateEmployeeNo(userLoginParam); + + //异步执行更新用户信息到同程 + User currentUser = userLoginResult.getUser(); + CompletableFuture.runAsync(() -> { + BaseContext.setCurrentUser(currentUser); + lyUserRequest.saveCurrentUser(); + BaseContext.removeCurrentUser(); + }); + //异步执行更新用户信息到携程 + CompletableFuture.runAsync(() -> { + BaseContext.setCurrentUser(currentUser); + cTripUserSaveRequest.saveUserToCTrip(); + BaseContext.removeCurrentUser(); + }); + + //清除职级信息 + userLoginResult.getUser().setProfLevel(null); + userLoginResult.getUser().setManaLevel(null); + return Result.Success(CommonMessageConstant.SUCCESS, userLoginResult); + } } diff --git a/src/main/java/com/chint/application/services/login/strategy/PailaLoginStrategy.java b/src/main/java/com/chint/application/services/login/strategy/PailaLoginStrategy.java index 658c7355..2b228be9 100644 --- a/src/main/java/com/chint/application/services/login/strategy/PailaLoginStrategy.java +++ b/src/main/java/com/chint/application/services/login/strategy/PailaLoginStrategy.java @@ -66,7 +66,6 @@ public class PailaLoginStrategy implements LoginStrategy { new BasicNameValuePair("client_secret", clientSecret), new BasicNameValuePair("code", code), new BasicNameValuePair("redirect_uri", redirectUri) - ); HttpPost getMethod = postRequest("/esc-sso/oauth2.0/accessToken", parameters); diff --git a/src/main/java/com/chint/application/services/login/strategy/PailaMobileLoginStrategy.java b/src/main/java/com/chint/application/services/login/strategy/PailaMobileLoginStrategy.java new file mode 100644 index 00000000..c375938c --- /dev/null +++ b/src/main/java/com/chint/application/services/login/strategy/PailaMobileLoginStrategy.java @@ -0,0 +1,125 @@ +package com.chint.application.services.login.strategy; + +import com.alibaba.fastjson.JSON; +import com.chint.application.dtos.UserDTO; +import com.chint.application.services.login.LoginStrategy; +import com.chint.domain.exceptions.SSOLoginException; +import com.chint.domain.factoriy.user.UserFactory; +import com.chint.domain.repository.UserRepository; +import com.chint.interfaces.rest.user.UserHttpRequest; +import lombok.extern.slf4j.Slf4j; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +import static com.chint.infrastructure.constant.CommonMessageConstant.NOT_FOUND; + + +@Slf4j +@Component +public class PailaMobileLoginStrategy implements LoginStrategy { + + @Value("${paila.base-url}") + private String baseUrl; + + @Value("${paila.client-mobile-id}") + private String clientId; + + @Value("${paila.client-mobile-secret}") + private String clientSecret; + + @Value("${paila.redirect-mobile-url}") + private String redirectUri; + + @Autowired + private UserRepository userRepository; + + @Autowired + private UserHttpRequest userHttpRequest; + + @Autowired + private UserFactory userFactory; + + @Override + public Optional getAccessToken(String code) { + + log.info("开始执行登录"); + List parameters = Arrays.asList( + new BasicNameValuePair("grant_type", "authorization_code"), + new BasicNameValuePair("client_id", clientId), + new BasicNameValuePair("client_secret", clientSecret), + new BasicNameValuePair("code", code), + new BasicNameValuePair("redirect_uri", redirectUri) + ); + + HttpPost getMethod = postRequest("/esc-sso/oauth2.0/accessToken", parameters); + return LoginStrategy.getAccessTokenMethod(getMethod, "access_token"); + } + + @Override + public String getUserInfo(String accessToken) { + + + List userInfoParams = Collections.singletonList( + new BasicNameValuePair("access_token", accessToken) + ); + + HttpGet getMethodUserInfo = getRequest("/esc-sso/oauth2.0/profile", userInfoParams); + + String userInfoResBody = null; + HttpClient client = HttpClients.createDefault(); + try { + HttpResponse userInfoRes = client.execute(getMethodUserInfo); + userInfoResBody = EntityUtils.toString(userInfoRes.getEntity(), "UTF-8"); + } catch (IOException e) { + log.error(e.getMessage()); + } + UserDTO userDTO = JSON.parseObject(userInfoResBody, UserDTO.class); + if (userDTO == null) { + throw new SSOLoginException(NOT_FOUND); + } + return userDTO.getAttributes().getAccount_no(); + } + + private HttpGet getRequest(String path, List parameters) { + String userInfoUrl = null; + try { + userInfoUrl = new URIBuilder(baseUrl).setPath(path) + .setParameters(parameters).build().toString(); + } catch (URISyntaxException e) { + log.error(e.getMessage()); + } + HttpGet request = new HttpGet(userInfoUrl); + request.setHeader("Content-Type", "application/json"); + return request; + } + + private HttpPost postRequest(String path, List parameters) { + String userInfoUrl = null; + try { + userInfoUrl = new URIBuilder(baseUrl).setPath(path) + .setParameters(parameters).build().toString(); + } catch (URISyntaxException e) { + log.error(e.getMessage()); + } + HttpPost request = new HttpPost(userInfoUrl); + request.setHeader("Content-Type", "application/json"); + return request; + } +} diff --git a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcLocationRepository.java b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcLocationRepository.java index c5d16d68..7a1a4841 100644 --- a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcLocationRepository.java +++ b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcLocationRepository.java @@ -25,10 +25,7 @@ public interface JdbcLocationRepository extends CrudRepository { Location findByLocationName(String locationName); + List findByLocationNameIn(Collection locationName); + List findByLocationIdIn(Collection locationId); - - List findByLocationNameInAndLocationPathContainingAndLevel(Collection locationName, - String locationPath, - Integer level); - } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 58d1ae82..0e182bf0 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -57,6 +57,9 @@ paila: client-id: 0053df85723db94491e8 client-secret: 7368bcec4c0f004c40585f6ed1087d887897 redirect-url: https://gxdev03.chint.com/businesstravelhome/#/ + client-mobile-id: 9b24c91ead42ee4b6918 + client-mobile-secret: e54494f1272ffd41b818980f1f8524fd68ef + redirect-mobile-url: https://gxdev03.chint.com/businesstravelmhome/#/ base-url: https://signin-test.chint.com token-name: token diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index acbeb3a9..b44eb244 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -27,5 +27,8 @@ paila: client-id: 0053df85723db94491e8 client-secret: 7368bcec4c0f004c40585f6ed1087d887897 redirect-url: https://gxdev03.chint.com/businesstravelhome/#/ + client-mobile-id: 9b24c91ead42ee4b6918 + client-mobile-secret: e54494f1272ffd41b818980f1f8524fd68ef + redirect-mobile-url: https://gxdev03.chint.com/businesstravelmhome/#/ base-url: https://signin-test.chint.com token-name: token diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml index bdad94d4..97082d48 100644 --- a/src/main/resources/application-test.yml +++ b/src/main/resources/application-test.yml @@ -46,6 +46,9 @@ paila: client-id: 0053df85723db94491e8 client-secret: 7368bcec4c0f004c40585f6ed1087d887897 redirect-url: https://gxdev03.chint.com/businesstravelhome/#/ + client-mobile-id: 9b24c91ead42ee4b6918 + client-mobile-secret: e54494f1272ffd41b818980f1f8524fd68ef + redirect-mobile-url: https://gxdev03.chint.com/businesstravelmhome/#/ base-url: https://signin-test.chint.com token-name: token diff --git a/src/test/java/com/chint/RouteApplicationTests.java b/src/test/java/com/chint/RouteApplicationTests.java index 4a5bec2e..433838ca 100644 --- a/src/test/java/com/chint/RouteApplicationTests.java +++ b/src/test/java/com/chint/RouteApplicationTests.java @@ -126,7 +126,7 @@ class RouteApplicationTests { System.out.println(login); } - @Test +// @Test void arrayToStr(){ List ids = List.of(1L,2L,3L,4L); System.out.println(Arrays.toString(ids.toArray()));