From 6628f66b3f7d487591a3f819ac361b7514e1372c Mon Sep 17 00:00:00 2001 From: ningwenqiang Date: Fri, 25 Oct 2024 15:20:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E7=9A=84ui=E5=8F=8A=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=A7=A3=E5=AF=86=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/yunbao/common/http/API.java | 2 +- .../com/yunbao/common/http/HttpClient.java | 2 +- .../common/http/JavaConverterFactory.java | 97 +++++++++++++++++++ .../java/com/yunbao/common/http/JsonBean.java | 56 ++++++++++- .../com/yunbao/common/http/PDLiveApi.java | 13 ++- .../common/http/live/LiveNetManager.java | 1 + .../common/http/main/MainNetManager.java | 3 +- .../com/yunbao/common/utils/AesUtils.java | 74 ++++++++++++++ config.gradle | 2 +- 9 files changed, 242 insertions(+), 8 deletions(-) create mode 100644 common/src/main/java/com/yunbao/common/http/JavaConverterFactory.java create mode 100644 common/src/main/java/com/yunbao/common/utils/AesUtils.java diff --git a/common/src/main/java/com/yunbao/common/http/API.java b/common/src/main/java/com/yunbao/common/http/API.java index 89c03afcb..48b662f75 100644 --- a/common/src/main/java/com/yunbao/common/http/API.java +++ b/common/src/main/java/com/yunbao/common/http/API.java @@ -80,7 +80,7 @@ public class API extends BaseApi { .addInterceptor(initQuery(isNeedUid, isNeedToken, CommonAppContext.sInstance.getApplicationContext())) .addInterceptor(loggingInterceptor); return create(builder.build(), - GsonConverterFactory.create(gson), RxJava2CallAdapterFactory.create(), CommonAppConfig.HOST, PDLiveApi.class); + JavaConverterFactory.create(gson), RxJava2CallAdapterFactory.create(), CommonAppConfig.HOST, PDLiveApi.class); } //公共参数 diff --git a/common/src/main/java/com/yunbao/common/http/HttpClient.java b/common/src/main/java/com/yunbao/common/http/HttpClient.java index 0378dc175..96aea4fb0 100644 --- a/common/src/main/java/com/yunbao/common/http/HttpClient.java +++ b/common/src/main/java/com/yunbao/common/http/HttpClient.java @@ -34,7 +34,7 @@ public class HttpClient { private String mUrl; private HttpClient() { - mUrl = CommonAppConfig.HOST + "/api/public/?service="; + mUrl = CommonAppConfig.HOST + "/";//去掉 api/public/?service=" } public static HttpClient getInstance() { diff --git a/common/src/main/java/com/yunbao/common/http/JavaConverterFactory.java b/common/src/main/java/com/yunbao/common/http/JavaConverterFactory.java new file mode 100644 index 000000000..898f5eb9b --- /dev/null +++ b/common/src/main/java/com/yunbao/common/http/JavaConverterFactory.java @@ -0,0 +1,97 @@ +package com.yunbao.common.http; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.google.gson.Gson; +import com.google.gson.TypeAdapter; +import com.google.gson.reflect.TypeToken; +import com.yunbao.common.utils.AesUtils; +import com.yunbao.common.utils.L; + +import java.io.IOException; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.util.Base64; + +import okhttp3.RequestBody; +import okhttp3.ResponseBody; +import retrofit2.Converter; +import retrofit2.Retrofit; + +public final class JavaConverterFactory extends Converter.Factory { + /** + * Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and + * decoding from JSON (when no charset is specified by a header) will use UTF-8. + */ + public static JavaConverterFactory create() { + return create(new Gson()); + } + + /** + * Create an instance using {@code gson} for conversion. Encoding to JSON and + * decoding from JSON (when no charset is specified by a header) will use UTF-8. + */ + public static JavaConverterFactory create(Gson gson) { + return new JavaConverterFactory(gson); + } + + private final Gson gson; + + private JavaConverterFactory(Gson gson) { + if (gson == null) throw new NullPointerException("gson == null"); + this.gson = gson; + } + + @Override + public Converter responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { + TypeAdapter adapter = gson.getAdapter(TypeToken.get(type)); + return new JsonResponseBodyConverter<>(gson, adapter); + } + + + @Override + public Converter requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { + return super.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit); + } + + final class JsonResponseBodyConverter implements Converter { + private final Gson gson; + private final TypeAdapter adapter; + + JsonResponseBodyConverter(Gson gson, TypeAdapter adapter) { + this.gson = gson; + this.adapter = adapter; + } + + @Override + public T convert(ResponseBody value) throws IOException { + String response = value.string(); + String allResponseData; + L.e("解密前::" + response); + JSONObject jsonObject = JSONObject.parseObject(response); + allResponseData = jsonObject.getString("data"); + if (allResponseData.indexOf(":") <= 0) { + byte[] decodedData = null; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + decodedData = Base64.getDecoder().decode(allResponseData); + } + response = AesUtils.decryptToString(decodedData); + jsonObject.put("data", JSONObject.parseObject(response)); + + response = JSON.toJSONString(jsonObject); + L.e("解密后::" + response); + //获取加密数据,解密,之后再让adapter去处理json串,解析具体的数据就可以了 + try { + return adapter.fromJson(response); + } catch (Exception e) { + e.printStackTrace(); + } finally { + value.close(); + } + } else { + return adapter.fromJson(response); + } + return null; + } + } +} \ No newline at end of file diff --git a/common/src/main/java/com/yunbao/common/http/JsonBean.java b/common/src/main/java/com/yunbao/common/http/JsonBean.java index 392fe4e60..d9f1e51c5 100644 --- a/common/src/main/java/com/yunbao/common/http/JsonBean.java +++ b/common/src/main/java/com/yunbao/common/http/JsonBean.java @@ -1,5 +1,18 @@ package com.yunbao.common.http; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONException; +import com.alibaba.fastjson.JSONObject; +import com.blankj.utilcode.util.GsonUtils; +import com.google.gson.Gson; +import com.yunbao.common.bean.AdBean; +import com.yunbao.common.utils.AesUtils; +import com.yunbao.common.utils.L; + +import java.util.Base64; +import java.util.List; + /** * Created by cxf on 2017/8/5. */ @@ -7,7 +20,7 @@ package com.yunbao.common.http; public class JsonBean { private int ret; private String msg; - private Data data; + private String data; public int getRet() { return ret; @@ -26,10 +39,47 @@ public class JsonBean { } public Data getData() { - return data; + L.e("data:" + data); + if (data.indexOf(":") > 0) { + L.e("未加密::" + data); + return JSON.parseObject(data, Data.class); + //return GsonUtils.fromJson(data, Data.class); + } else { + Data resultData = new Data(); + byte[] decodedData = null; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + decodedData = Base64.getDecoder().decode(data); + } + String decryptedText = AesUtils.decryptToString(decodedData); + L.e("解码前:" + data); + L.e("解码前-转成base64:" + decodedData); + L.e("解码前-解密後:" + decryptedText); + JSONObject object = JSON.parseObject(decryptedText); + try { + JSONArray jsonArray = object.getJSONArray("info"); + String[] array = new String[jsonArray.size()]; + for (int i = 0; i < array.length; i++) { + array[i] = jsonArray.getString(i); + } + resultData.setInfo(array); + resultData.setCode(object.getInteger("code")); + resultData.setMsg(object.getString("msg")); + } catch (Exception e) { + try { + String[] array = new String[1]; + array[0] = object.getString("info"); + resultData.setInfo(array); + resultData.setCode(object.getInteger("code")); + resultData.setMsg(object.getString("msg")); + } catch (JSONException ex) { + L.e("字符串格式错误"); + } + } + return resultData; + } } - public void setData(Data data) { + public void setData(String data) { this.data = data; } } diff --git a/common/src/main/java/com/yunbao/common/http/PDLiveApi.java b/common/src/main/java/com/yunbao/common/http/PDLiveApi.java index eec72fa03..501fd1f31 100644 --- a/common/src/main/java/com/yunbao/common/http/PDLiveApi.java +++ b/common/src/main/java/com/yunbao/common/http/PDLiveApi.java @@ -55,6 +55,7 @@ import com.yunbao.common.bean.LiveRoomVoteModel; import com.yunbao.common.bean.LiveStetUpStatusModel; import com.yunbao.common.bean.LiveTaskModel; import com.yunbao.common.bean.LiveUserMailBoxModel; +import com.yunbao.common.bean.MainMessageChatListTimer; import com.yunbao.common.bean.MedalAchievementModel; import com.yunbao.common.bean.MessageChatIsAnchor; import com.yunbao.common.bean.MessageChatUserBean; @@ -1068,7 +1069,6 @@ public interface PDLiveApi { @Query("room_name") String roomName, @Query("golden_bean_number") String goldenBeanNumber, @Query("currency_type") String currencyType, - @Query("activity_game") String isActivity, @Query("game_id") String gameId); /** @@ -1488,6 +1488,17 @@ public interface PDLiveApi { Observable> ballClearanceTableStatus( @Query("room_id")String roomId ); + + + @GET("/ylahuoququanbuxinxi") + Observable>> getOtherList(@Query("ids") String uids); + + @GET("/ylapdlzhuboyonghuguanxi") + Observable>> getRelation(@Query("type") String type); + @GET("/ylahuoquzhidingyonghzhiboxinxi") + Observable>> getIsLook(@Query("ids") String uids); + + // @GET("/ylahuoquyouxijiesuanjifen") // Observable>> ballClearanceTableGetResultScore( // @Query("room_id")String roomId diff --git a/common/src/main/java/com/yunbao/common/http/live/LiveNetManager.java b/common/src/main/java/com/yunbao/common/http/live/LiveNetManager.java index bbf5d0bde..8922e87a0 100644 --- a/common/src/main/java/com/yunbao/common/http/live/LiveNetManager.java +++ b/common/src/main/java/com/yunbao/common/http/live/LiveNetManager.java @@ -2358,6 +2358,7 @@ public class LiveNetManager { }).isDisposed(); } + //TODO 合并后有POPO了一个参数 public void createSudRoom(String roomName, String goldenBeanNumber, String currencyType, String gameId, HttpCallback callback) { API.get().pdLiveApi(mContext) .createSudRoom(roomName, goldenBeanNumber, currencyType, gameId) diff --git a/common/src/main/java/com/yunbao/common/http/main/MainNetManager.java b/common/src/main/java/com/yunbao/common/http/main/MainNetManager.java index 00dd5a6bc..1abcea8c0 100644 --- a/common/src/main/java/com/yunbao/common/http/main/MainNetManager.java +++ b/common/src/main/java/com/yunbao/common/http/main/MainNetManager.java @@ -13,6 +13,7 @@ import com.yunbao.common.http.API; import com.yunbao.common.http.ResponseData; import com.yunbao.common.http.ResponseModel; import com.yunbao.common.http.base.HttpCallback; +import com.yunbao.common.manager.imrongcloud.RongcloudIMManager; import com.yunbao.common.utils.WordUtil; import java.util.HashMap; @@ -55,7 +56,7 @@ public class MainNetManager { * @param callback 网络请求回调 */ public void login(String phoneNum, String pwd, String uuid, HttpCallback callback) { - API.get().pdLiveApi(mContext).loginByManager(phoneNum, pwd, uuid, "", "Android", WordUtil.isNewZh()?"chinese":"english") + API.get().pdLiveApi(mContext).loginByManager(phoneNum, pwd, uuid, "", "Android", WordUtil.isNewZh()?"chinese":"english",RongcloudIMManager.RONG_IM_KEY) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer>() { diff --git a/common/src/main/java/com/yunbao/common/utils/AesUtils.java b/common/src/main/java/com/yunbao/common/utils/AesUtils.java new file mode 100644 index 000000000..f550c4bda --- /dev/null +++ b/common/src/main/java/com/yunbao/common/utils/AesUtils.java @@ -0,0 +1,74 @@ +package com.yunbao.common.utils; + +import com.alibaba.fastjson.JSON; + +import java.io.UnsupportedEncodingException; +import javax.crypto.Cipher; +import javax.crypto.spec.SecretKeySpec; + +public class AesUtils { + /** + * 使用AES/ECB/PKCS5Padding模式解密数据 + * + * @param encryptedData 加密后的字节数组(Base64解码后的结果,如果原始数据是Base64编码的话) + * @param keyBytes AES密钥(16/24/32字节长,对应AES-128/192/256) + * @return 解密后的明文字节数组,如果解密失败则返回null + */ + public static byte[] decrypt(byte[] encryptedData, byte[] keyBytes) { + try { + // 创建密钥规格 + SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES-128-ECB"); + + // 获取Cipher实例并初始化为解密模式 + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + cipher.init(Cipher.DECRYPT_MODE, keySpec); + // 执行解密 + byte[] decryptedBytes = cipher.doFinal(encryptedData); + // 返回解密后的字节数组 + return decryptedBytes; + } catch (Exception e) { + e.printStackTrace(); + return null; // 解密失败时返回null + } + } + + /** + * 如果需要将解密后的字节数组转换为字符串(注意:这可能会导致数据丢失或乱码) + * + * @param encryptedData 加密后的字节数组(或Base64解码后的结果) + * @return 解密后的字符串,如果解密失败则返回null + */ + public static String decryptToString(byte[] encryptedData) { + byte[] decryptedBytes = decrypt(encryptedData,"LhHBfcsN2VmBpHCn".getBytes()); + if (decryptedBytes != null) { + // 尝试将字节数组转换为字符串(使用指定的字符集) + try { + return decodeUnicode(new String(decryptedBytes, "UTF-8")); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + return null; + } + + public static String decodeUnicode(String unicode) { +/* StringBuilder sb = new StringBuilder(); + for (int i = 0; i < unicode.length();) { + if (unicode.charAt(i) == '\\') { + if (i + 5 < unicode.length()) { + String codePointStr = unicode.substring(i + 2, i + 6); + try { + int codePoint = Integer.parseInt(codePointStr, 16); + sb.append((char) codePoint); + i += 6; + continue; + } catch (NumberFormatException e) { + // Handle format error + } + } + } + sb.append(unicode.charAt(i++)); + }*/ + return JSON.parseObject(unicode).toString(); + } +} diff --git a/config.gradle b/config.gradle index 19221269c..b97f07110 100644 --- a/config.gradle +++ b/config.gradle @@ -10,7 +10,7 @@ ext { ] manifestPlaceholders = [ //正式、 - serverHost : "https://napi.yaoulive.com", + serverHost : "https://api.yolaashow.com",//新https://api.yolaashow.com 老https://napi.yaoulive.com // 测试 testServerHost : "https://ceshi.yaoulive.com", buildTime : new Date().format("MM-dd HH:mm", TimeZone.getTimeZone("GMT+8")),