diff --git a/src/main/java/com/chint/infrastructure/constant/Constant.java b/src/main/java/com/chint/infrastructure/constant/Constant.java index 841191e3..6b84dcd3 100644 --- a/src/main/java/com/chint/infrastructure/constant/Constant.java +++ b/src/main/java/com/chint/infrastructure/constant/Constant.java @@ -149,6 +149,10 @@ public class Constant { public static final String C_TRIP_TOKEN_PATH = "/dataservice/token/getAccessToken"; public static final String C_TRIP_ORDER_SEARCH_PATH = "/switchapi/Order/SearchOrder"; + public static final String C_TRIP_AUTH_LOGIN = "/corpservice/authorize/login"; + + public static final String C_TRIP_SINGLE_LOGIN = "/m/SingleSignOn/H5SignInfo"; + //同程 public static final String L_Y_BASE_URL = "https://api.qa.dttrip.cn"; diff --git a/src/main/java/com/chint/interfaces/rest/ctrip/CTripLoginRequest.java b/src/main/java/com/chint/interfaces/rest/ctrip/CTripLoginRequest.java index 7ee046cf..1ed86bf9 100644 --- a/src/main/java/com/chint/interfaces/rest/ctrip/CTripLoginRequest.java +++ b/src/main/java/com/chint/interfaces/rest/ctrip/CTripLoginRequest.java @@ -1,10 +1,34 @@ package com.chint.interfaces.rest.ctrip; +import com.chint.infrastructure.util.Digest; +import com.chint.infrastructure.util.Result; import com.chint.interfaces.rest.base.PostRequest; +import com.chint.interfaces.rest.ctrip.dto.AuthenticationResponseList; +import com.chint.interfaces.rest.ctrip.dto.login.CTripAuthLoginParam; +import com.chint.interfaces.rest.ctrip.dto.login.CTripHSingleLoginParam; import com.chint.interfaces.rest.ctrip.dto.login.CTripLoginParam; +import com.chint.interfaces.rest.ly.dto.LYBaseRequest; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.HttpClient; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + import static com.chint.infrastructure.constant.Constant.*; @Component @@ -12,9 +36,20 @@ public class CTripLoginRequest { @Autowired private PostRequest postRequest; - + @Autowired + private CTripTicketRequest ticketRequest; + @Autowired + private CTripTokenRequest tokenRequest; private String loginUrl = C_TRIP_BASE_URL + C_TRIP_LOGIN_PATH; + //PC单点登录 + private String authLoginUrl = C_TRIP_BASE_URL + C_TRIP_AUTH_LOGIN; + + private String hSinngleLoginUrl = C_TRIP_BASE_URL + C_TRIP_SINGLE_LOGIN; + + //H5单点登录 + private String singleLoginUrl = C_TRIP_BASE_URL + C_TRIP_SINGLE_LOGIN; + private String IDPEntityID = C_TRIP_ENTITY_ID; private String corpId = C_TRIP_CORP_ID; @@ -22,4 +57,133 @@ public class CTripLoginRequest { loginParam.setCorpID(corpId); return postRequest.post(loginUrl + IDPEntityID, loginParam, String.class); } + + + public HttpResponse authLogin(CTripAuthLoginParam authLoginParam) { + + String ticket = ticketRequest.loadTicket(); + String token = tokenRequest.getToken(); + authLoginParam.setTicket(ticket); + + String appKey = C_TRIP_APP_KEY; + String uid = StringUtils.isNotBlank(authLoginParam.getUID())?authLoginParam.getUID():""; + + String employeeId = StringUtils.isNotBlank(authLoginParam.getEmployeeID())?authLoginParam.getEmployeeID():""; + String email = StringUtils.isNotBlank(authLoginParam.getEmail())?authLoginParam.getEmail():""; + String ta = StringUtils.isNotBlank(authLoginParam.getTA())?authLoginParam.getTA():""; + Integer forCorp = authLoginParam.getForCorp() == null? authLoginParam.getForCorp():0; + String forCopStr = forCorp.toString(); + + String cost1 = StringUtils.isNotBlank(authLoginParam.getCost1())?authLoginParam.getCost1():""; + if (isContainsChinese(cost1)){ + ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(cost1); + cost1 = StandardCharsets.UTF_8.decode(byteBuffer).toString(); + } + String cost2 = StringUtils.isNotBlank(authLoginParam.getCost2())?authLoginParam.getCost2():""; + if (isContainsChinese(cost2)){ + ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(cost2); + cost2 = StandardCharsets.UTF_8.decode(byteBuffer).toString(); + } + String cost3 = StringUtils.isNotBlank(authLoginParam.getCost3())?authLoginParam.getCost3():""; + if (isContainsChinese(cost3)){ + ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(cost3); + cost3 = StandardCharsets.UTF_8.decode(byteBuffer).toString(); + } + String mdAppSecurity = Digest.md5(C_TRIP_APP_SECURITY); + + + String finallySign = + Digest.md5(appKey+uid+employeeId+email+ta+forCopStr+cost1+cost2+cost3+mdAppSecurity); + + authLoginParam.setSignature(finallySign); +// Result post = postRequest.post(authLoginUrl, authLoginParam, Result.class); + +// HttpClient client = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(authLoginUrl); + List entitys = new ArrayList<>(); + entitys.add(new BasicNameValuePair("AppKey",appKey)); + entitys.add(new BasicNameValuePair("Token",token)); + + entitys.add(new BasicNameValuePair("Ticket",ticket)); + entitys.add(new BasicNameValuePair("EmployeeID",authLoginParam.getEmployeeID())); + entitys.add(new BasicNameValuePair("Signature",finallySign)); + entitys.add(new BasicNameValuePair("ForCorp","0")); + + try { + httpPost.setEntity(new UrlEncodedFormEntity(entitys)); + HttpClient client = new DefaultHttpClient(); + try { + return client.execute(httpPost); + } catch (IOException e) { + throw new RuntimeException(e); + } + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + + + public HttpResponse hSingleLogin(CTripHSingleLoginParam hSingleLoginParam) { + String ticket = ticketRequest.loadTicket(); + String token = tokenRequest.getToken(); + String accessUserId = C_TRIP_APP_KEY; + String employeeId = StringUtils.isNotBlank(hSingleLoginParam.getEmployeeId())?hSingleLoginParam.getEmployeeId():""; + String corpPayType = StringUtils.isNotBlank(hSingleLoginParam.getCorpPayType())? + hSingleLoginParam.getCorpPayType():"public"; + + String costCenter1 = StringUtils.isNotBlank(hSingleLoginParam.getCostCenter1())? + hSingleLoginParam.getCostCenter1():""; + if (isContainsChinese(costCenter1)){ + ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(costCenter1); + costCenter1 = StandardCharsets.UTF_8.decode(byteBuffer).toString(); + } + + String costCenter2 = StringUtils.isNotBlank(hSingleLoginParam.getCostCenter2())? + hSingleLoginParam.getCostCenter2():""; + if (isContainsChinese(costCenter2)){ + ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(costCenter2); + costCenter2 = StandardCharsets.UTF_8.decode(byteBuffer).toString(); + } + + String costCenter3 = StringUtils.isNotBlank(hSingleLoginParam.getCostCenter3())? + hSingleLoginParam.getCostCenter3():""; + if (isContainsChinese(costCenter3)){ + ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(costCenter3); + costCenter3 = StandardCharsets.UTF_8.decode(byteBuffer).toString(); + } + String mdAppSecurity = Digest.md5(C_TRIP_APP_SECURITY); + String finallySign = + Digest.md5(accessUserId+employeeId+corpPayType+costCenter1+costCenter2+costCenter3+mdAppSecurity); + + hSingleLoginParam.setSignature(finallySign); + + HttpPost httpPost = new HttpPost(hSinngleLoginUrl); + List entitys = new ArrayList<>(); + entitys.add(new BasicNameValuePair("accessUserId",accessUserId)); + entitys.add(new BasicNameValuePair("employeeId",employeeId)); + entitys.add(new BasicNameValuePair("token",token)); + entitys.add(new BasicNameValuePair("appId","zhengtai")); + entitys.add(new BasicNameValuePair("signature",finallySign)); + entitys.add(new BasicNameValuePair("corpPayType","public")); + try { + UrlEncodedFormEntity entity = new UrlEncodedFormEntity(entitys,"UTF-8"); + + httpPost.setEntity(entity); + HttpClient client = new DefaultHttpClient(); + try { + return client.execute(httpPost); + } catch (IOException e) { + throw new RuntimeException(e); + } + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + + private static boolean isContainsChinese(String input) { + Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]"); + Matcher matcher = pattern.matcher(input); + return matcher.find(); + } + } diff --git a/src/main/java/com/chint/interfaces/rest/ctrip/dto/login/CTripAuthLoginParam.java b/src/main/java/com/chint/interfaces/rest/ctrip/dto/login/CTripAuthLoginParam.java new file mode 100644 index 00000000..a8727baa --- /dev/null +++ b/src/main/java/com/chint/interfaces/rest/ctrip/dto/login/CTripAuthLoginParam.java @@ -0,0 +1,36 @@ +package com.chint.interfaces.rest.ctrip.dto.login; + +import lombok.Data; + +@Data +public class CTripAuthLoginParam { + private String AppKey; + private String Ticket; + private String UID; + private String EmployeeID; + private String Email; + private String Signature; + private String Token; + + private String TA; + private Integer ForCorp; + private String Cost1; + private String Cost2; + private String Cost3; + private String Cost4; + private String Cost5; + private String Cost6; + private String JourneyReason; + private String Project; + private String DefineFlag; + private String DefineFlag2; + private String SearchType; + private String AuthorizerEmployeeID; + private String Authorizer2EmployeeID; + private String InitPage; + private String CurrentLang; + private String OrderId; + private Boolean OnlyInitPage; + + +} diff --git a/src/main/java/com/chint/interfaces/rest/ctrip/dto/login/CTripHSingleLoginParam.java b/src/main/java/com/chint/interfaces/rest/ctrip/dto/login/CTripHSingleLoginParam.java new file mode 100644 index 00000000..5c6c4094 --- /dev/null +++ b/src/main/java/com/chint/interfaces/rest/ctrip/dto/login/CTripHSingleLoginParam.java @@ -0,0 +1,38 @@ +package com.chint.interfaces.rest.ctrip.dto.login; + +import lombok.Data; + +@Data +public class CTripHSingleLoginParam { + private String AccessUserId; + private String EmployeeId; + private String Token; + private String AppId; + private String Signature; + private String EndorsementID; + private String SearchType; + private String WebViewEnv; + private String CorpPayType; + private String InitPage; + private String site; + + private String orderNumber; + private Integer Callback; + private String browserBack; + private String CostCenter1; + private String CostCenter2; + private String CostCenter3; + private String CostCenter4; + private String CostCenter5; + private String CostCenter6; + private String Project; + private String JourneyReason; + private String CostCenterCustom1; + private String CostCenterCustom2; + private String Language; + private String NeedConfirmPerson1; + private String NeedConfirmPerson2; + private String OnError; + + +} diff --git a/src/test/java/com/chint/CTripTest.java b/src/test/java/com/chint/CTripTest.java index af2483a4..3da32341 100644 --- a/src/test/java/com/chint/CTripTest.java +++ b/src/test/java/com/chint/CTripTest.java @@ -3,6 +3,7 @@ package com.chint; import com.chint.domain.aggregates.user.User; import com.chint.domain.repository.CityRepository; import com.chint.infrastructure.util.BaseContext; +import com.chint.infrastructure.util.Result; import com.chint.interfaces.rest.ctrip.CTripEstimateRequest; import com.chint.interfaces.rest.ctrip.CTripLocationHttpRequest; import com.chint.interfaces.rest.ctrip.CTripLoginRequest; @@ -13,11 +14,17 @@ import com.chint.interfaces.rest.ctrip.dto.estimate.request.RouteInfo; import com.chint.interfaces.rest.ctrip.dto.estimate.response.BookingRelatedApiResponse; import com.chint.interfaces.rest.ctrip.dto.location.CTripCity; import com.chint.interfaces.rest.ctrip.dto.location.CTripCountry; +import com.chint.interfaces.rest.ctrip.dto.login.CTripAuthLoginParam; +import com.chint.interfaces.rest.ctrip.dto.login.CTripHSingleLoginParam; import com.chint.interfaces.rest.ctrip.dto.login.CTripLoginParam; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.util.EntityUtils; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.io.IOException; import java.util.List; import java.util.stream.Collectors; @@ -79,6 +86,50 @@ public class CTripTest { loginRequest.login(cTripLoginParam); } + @Test + void authLogin() { +// BaseContext.setCurrentUser(user); +// CTripLoginParam cTripLoginParam = new CTripLoginParam(); +// cTripLoginParam.setEmployeeID(String.valueOf(user.getEmployeeNo())); + + CTripAuthLoginParam param = new CTripAuthLoginParam(); + param.setEmployeeID("230615020"); + param.setForCorp(0); + HttpResponse response = loginRequest.authLogin(param); + + HttpEntity entity = response.getEntity(); + String result = null; + try { + result = EntityUtils.toString(entity,"UTF-8"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + System.out.println(result); + + } + + + @Test + void hSingleLogin() { +// BaseContext.setCurrentUser(user); +// CTripLoginParam cTripLoginParam = new CTripLoginParam(); +// cTripLoginParam.setEmployeeID(String.valueOf(user.getEmployeeNo())); + + CTripHSingleLoginParam param = new CTripHSingleLoginParam(); + param.setEmployeeId("230615020"); + param.setCorpPayType("public"); + HttpResponse response = loginRequest.hSingleLogin(param); + HttpEntity entity = response.getEntity(); + String result = null; + try { + result = EntityUtils.toString(entity,"UTF-8"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + System.out.println(result); + } @Test void estimate() { BaseContext.setCurrentUser(user);