|
|
@@ -3,7 +3,9 @@ package com.izouma.yags.camp.api;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.izouma.yags.domain.CampToken;
|
|
|
+import com.izouma.yags.dto.JsonBody;
|
|
|
import com.izouma.yags.exception.BusinessException;
|
|
|
+import com.izouma.yags.repo.CampTokenRepo;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.*;
|
|
|
import okhttp3.logging.HttpLoggingInterceptor;
|
|
|
@@ -16,7 +18,12 @@ import java.util.List;
|
|
|
@Slf4j
|
|
|
public class CampApiService {
|
|
|
|
|
|
- private volatile OkHttpClient client;
|
|
|
+ private volatile OkHttpClient client;
|
|
|
+ private final CampTokenRepo campTokenRepo;
|
|
|
+
|
|
|
+ public CampApiService(CampTokenRepo campTokenRepo) {
|
|
|
+ this.campTokenRepo = campTokenRepo;
|
|
|
+ }
|
|
|
|
|
|
private OkHttpClient getClient() {
|
|
|
if (client == null) {
|
|
|
@@ -31,104 +38,117 @@ public class CampApiService {
|
|
|
return client;
|
|
|
}
|
|
|
|
|
|
- private CampToken getCampToken() {
|
|
|
- CampToken campToken = new CampToken();
|
|
|
- campToken.setUserId("436174868");
|
|
|
- campToken.setToken("YWUU7xKq");
|
|
|
- return campToken;
|
|
|
+ private ApiResponse request(String url, RequestBody requestBody) {
|
|
|
+ ApiResponse apiResponse = null;
|
|
|
+ for (CampToken token : campTokenRepo.findAll()) {
|
|
|
+ if (requestBody instanceof FormBody) {
|
|
|
+ FormBody formBody = (FormBody) requestBody;
|
|
|
+ FormBody.Builder builder = new FormBody.Builder();
|
|
|
+ for (int i = 0; i < formBody.size(); i++) {
|
|
|
+ builder.add(formBody.name(i), formBody.value(i));
|
|
|
+ }
|
|
|
+ builder.add("token", token.getToken());
|
|
|
+ builder.add("userId", token.getUserId());
|
|
|
+ requestBody = builder.build();
|
|
|
+ } else if (requestBody instanceof MultipartBody) {
|
|
|
+ MultipartBody multipartBody = (MultipartBody) requestBody;
|
|
|
+ MultipartBody.Builder builder = new MultipartBody.Builder();
|
|
|
+ for (int i = 0; i < multipartBody.size(); i++) {
|
|
|
+ builder.addPart(multipartBody.part(i));
|
|
|
+ }
|
|
|
+ builder.addFormDataPart("token", token.getToken());
|
|
|
+ builder.addFormDataPart("userId", token.getUserId());
|
|
|
+ requestBody = builder.build();
|
|
|
+ } else if (requestBody instanceof JsonBody) {
|
|
|
+ JsonBody jsonBody = (JsonBody) requestBody;
|
|
|
+ JSONObject jsonObject = jsonBody.getJson();
|
|
|
+ jsonObject.put("token", token.getToken());
|
|
|
+ jsonObject.put("userId", token.getUserId());
|
|
|
+ requestBody = new JsonBody(jsonObject);
|
|
|
+ }
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .addHeader("userId", token.getUserId())
|
|
|
+ .addHeader("token", token.getToken())
|
|
|
+ .url(url)
|
|
|
+ .post(requestBody)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ try {
|
|
|
+ Response response = getClient().newCall(request).execute();
|
|
|
+ if (response.isSuccessful() && response.body() != null) {
|
|
|
+ ApiResponse res = JSON.parseObject(response.body().string(), ApiResponse.class);
|
|
|
+ if (res.getReturnCode() == 0) {
|
|
|
+ apiResponse = res;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ response.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("请求失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return apiResponse;
|
|
|
}
|
|
|
|
|
|
- public List<RongYaoRole> getRoleItems(String userId) throws IOException {
|
|
|
- OkHttpClient client = new OkHttpClient();
|
|
|
- CampToken token = getCampToken();
|
|
|
+ public List<RongYaoRole> getRoleItems(String userId) {
|
|
|
RequestBody formBody = new FormBody.Builder()
|
|
|
.add("friendUserId", userId)
|
|
|
.add("gameId", "20001")
|
|
|
- .add("token", token.getToken())
|
|
|
- .add("userId", token.getUserId())
|
|
|
.build();
|
|
|
- Request request = new Request.Builder()
|
|
|
- .addHeader("userId", token.getUserId())
|
|
|
- .addHeader("token", token.getToken())
|
|
|
- .url("https://ssl.kohsocialapp.qq.com:10001/game/battleprofile")
|
|
|
- .post(formBody)
|
|
|
- .build();
|
|
|
- try (Response response = getClient().newCall(request).execute()) {
|
|
|
- ResponseBody responseBody = response.body();
|
|
|
- if (responseBody != null) {
|
|
|
- String body = responseBody.string();
|
|
|
- ApiResponse res = JSON.parseObject(body, ApiResponse.class);
|
|
|
- if (res.getReturnCode() == 0) {
|
|
|
- QueryRole data = ((JSONObject) res.getData()).toJavaObject(QueryRole.class);
|
|
|
- return data.getRolelist();
|
|
|
- } else {
|
|
|
- log.error("获取角色列表失败, returnCode={}, msg={}", res.getReturnCode(), res.getReturnMsg());
|
|
|
- throw new BusinessException("查询游戏角色失败");
|
|
|
- }
|
|
|
+ ApiResponse res = request("https://ssl.kohsocialapp.qq.com:10001/game/battleprofile", formBody);
|
|
|
+ try {
|
|
|
+ if (res.getReturnCode() == 0) {
|
|
|
+ QueryRole data = ((JSONObject) res.getData()).toJavaObject(QueryRole.class);
|
|
|
+ return data.getRolelist();
|
|
|
+ } else {
|
|
|
+ log.error("获取角色列表失败, returnCode={}, msg={}", res.getReturnCode(), res.getReturnMsg());
|
|
|
+ throw new BusinessException("查询游戏角色失败");
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取角色列表失败", e);
|
|
|
+ throw new BusinessException("查询游戏角色失败");
|
|
|
}
|
|
|
- throw new BusinessException("查询游戏角色失败");
|
|
|
}
|
|
|
|
|
|
- public QueryBattle queryBattle(String roleId) throws IOException {
|
|
|
- CampToken token = getCampToken();
|
|
|
- JSONObject content = new JSONObject();
|
|
|
- content.put("friendRoleId", roleId);
|
|
|
- content.put("userId", token.getUserId());
|
|
|
- Request request = new Request.Builder()
|
|
|
- .addHeader("userId", token.getUserId())
|
|
|
- .addHeader("token", token.getToken())
|
|
|
- .url("https://kohcamp.qq.com/game/morebattlelist")
|
|
|
- .post(RequestBody.create(MediaType.parse("application/json"), content.toJSONString()))
|
|
|
+ public QueryBattle queryBattle(String roleId) {
|
|
|
+ JsonBody jsonBody = new JsonBody.Builder()
|
|
|
+ .add("friendRoleId", roleId)
|
|
|
.build();
|
|
|
- try (Response response = getClient().newCall(request).execute()) {
|
|
|
- ResponseBody responseBody = response.body();
|
|
|
- if (responseBody != null) {
|
|
|
- String body = responseBody.string();
|
|
|
- ApiResponse res = JSON.parseObject(body, ApiResponse.class);
|
|
|
- if (res.getReturnCode() == 0) {
|
|
|
- QueryBattle data = ((JSONObject) res.getData()).toJavaObject(QueryBattle.class);
|
|
|
- return data;
|
|
|
- } else {
|
|
|
- log.error("查询对战信息失败, returnCode={}, msg={}", res.getReturnCode(), res.getReturnMsg());
|
|
|
- throw new BusinessException("查询对战信息失败");
|
|
|
- }
|
|
|
+ try {
|
|
|
+ ApiResponse res = request("https://kohcamp.qq.com/game/morebattlelist", jsonBody);
|
|
|
+ if (res.getReturnCode() == 0) {
|
|
|
+ QueryBattle data = ((JSONObject) res.getData()).toJavaObject(QueryBattle.class);
|
|
|
+ return data;
|
|
|
+ } else {
|
|
|
+ log.error("查询对战信息失败, returnCode={}, msg={}", res.getReturnCode(), res.getReturnMsg());
|
|
|
+ throw new BusinessException("查询对战信息失败");
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("查询对战信息失败", e);
|
|
|
+ throw new BusinessException("查询对战信息失败");
|
|
|
}
|
|
|
- throw new BusinessException("查询对战信息失败");
|
|
|
}
|
|
|
|
|
|
public QueryBattleDetail queryBattleDetail(String gameSeq, String gameSvrId, Integer pvpType, String toAppRoleId, String relaySvrId) throws IOException {
|
|
|
- CampToken token = getCampToken();
|
|
|
RequestBody content = new FormBody.Builder()
|
|
|
.add("gameSeq", gameSeq)
|
|
|
.add("gameSvrId", gameSvrId)
|
|
|
.add("pvpType", pvpType.toString())
|
|
|
.add("toAppRoleId", toAppRoleId)
|
|
|
.add("relaySvrId", relaySvrId)
|
|
|
- .add("userId", token.getUserId())
|
|
|
- .add("token", token.getToken())
|
|
|
- .build();
|
|
|
- Request request = new Request.Builder()
|
|
|
- .addHeader("userId", token.getUserId())
|
|
|
- .addHeader("token", token.getToken())
|
|
|
- .url("https://ssl.kohsocialapp.qq.com:10001/role/h5getplaydetail")
|
|
|
- .post(content)
|
|
|
.build();
|
|
|
- try (Response response = getClient().newCall(request).execute()) {
|
|
|
- ResponseBody responseBody = response.body();
|
|
|
- if (responseBody != null) {
|
|
|
- String body = responseBody.string();
|
|
|
- ApiResponse res = JSON.parseObject(body, ApiResponse.class);
|
|
|
- if (res.getReturnCode() == 0) {
|
|
|
- QueryBattleDetail data = ((JSONObject) res.getData()).toJavaObject(QueryBattleDetail.class);
|
|
|
- return data;
|
|
|
- } else {
|
|
|
- log.error("查询对战信息失败, returnCode={}, msg={}", res.getReturnCode(), res.getReturnMsg());
|
|
|
- throw new BusinessException("查询对战信息失败");
|
|
|
- }
|
|
|
+ try {
|
|
|
+ ApiResponse res = request("https://ssl.kohsocialapp.qq.com:10001/role/h5getplaydetail", content);
|
|
|
+ if (res.getReturnCode() == 0) {
|
|
|
+ QueryBattleDetail data = ((JSONObject) res.getData()).toJavaObject(QueryBattleDetail.class);
|
|
|
+ return data;
|
|
|
+ } else {
|
|
|
+ log.error("查询对战信息失败, returnCode={}, msg={}", res.getReturnCode(), res.getReturnMsg());
|
|
|
+ throw new BusinessException("查询对战信息失败");
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("查询对战信息失败", e);
|
|
|
+ throw new BusinessException("查询对战信息失败");
|
|
|
}
|
|
|
- throw new BusinessException("查询对战信息失败");
|
|
|
}
|
|
|
}
|