From b9cc7390e8e8ce64c41c26fb369c98ce669d660c Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期三, 07 五月 2025 15:02:30 +0800
Subject: [PATCH] Merge branch '1.2.0'

---
 app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java |  501 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 501 insertions(+), 0 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java b/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java
index e82d14e..075d3db 100644
--- a/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java
+++ b/app/src/main/java/com/hdl/photovoltaic/internet/HttpClient.java
@@ -1,4 +1,505 @@
 package com.hdl.photovoltaic.internet;
 
+import android.net.Uri;
+import android.text.TextUtils;
+
+import androidx.annotation.NonNull;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
+import com.google.gson.reflect.TypeToken;
+import com.hdl.hdlhttp.HxHttp;
+import com.hdl.hdlhttp.HxHttpConfig;
+import com.hdl.hdlhttp.utils.GsonConvert;
+import com.hdl.linkpm.sdk.core.api.HDLCloudUserApi;
+import com.hdl.linkpm.sdk.core.exception.HDLException;
+import com.hdl.linkpm.sdk.core.interceptor.HDLSmartHeader;
+import com.hdl.linkpm.sdk.core.response.HDLResponse;
+import com.hdl.linkpm.sdk.utils.HDLExceptionSubmitUtils;
+import com.hdl.photovoltaic.HDLApp;
+import com.hdl.photovoltaic.R;
+import com.hdl.photovoltaic.bean.HttpResponsePack;
+import com.hdl.photovoltaic.config.AppConfigManage;
+import com.hdl.photovoltaic.config.UserConfigManage;
+import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak;
+import com.hdl.photovoltaic.listener.CloudCallBeak;
+import com.hdl.photovoltaic.other.HdlESLocalJsonLogic;
+import com.hdl.photovoltaic.other.HdlLogLogic;
+import com.hdl.photovoltaic.other.HdlThreadLogic;
+import com.hdl.photovoltaic.utils.LocalManageUtil;
+import com.hdl.photovoltaic.utils.Md5Utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+import io.reactivex.rxjava3.disposables.Disposable;
+import io.reactivex.rxjava3.functions.Consumer;
+import okhttp3.Call;
+import okhttp3.Callback;
+import okhttp3.FormBody;
+import okhttp3.MediaType;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+
 public class HttpClient {
+
+    private static volatile HttpClient sHttpClient;
+
+    private static final boolean isAddToMemory = false;
+
+    public static synchronized HttpClient getInstance() {
+        if (sHttpClient == null) {
+            synchronized (HttpClient.class) {
+                if (sHttpClient == null) {
+                    sHttpClient = new HttpClient();
+                }
+            }
+
+        }
+        return sHttpClient;
+    }
+
+    public static HDLException getException(HDLException e) {
+        try {
+            String str = TextUtils.isEmpty(getErrorSting(e.getCode())) ? e.getMsg() : getErrorSting(e.getCode());
+            return new HDLException(e.getCode(), str, e.getmExtra());
+        } catch (Exception exception) {
+            return e;
+        }
+    }
+
+    private static String getErrorSting(int code) {
+        try {
+            switch (code) {
+                case 1000:
+                    return HDLApp.getInstance().getAppLocaleContext().getString(R.string.no_network_connection);
+                case 1001:
+                    return HDLApp.getInstance().getAppLocaleContext().getString(R.string.parse_error);
+                case 1002:
+                    return HDLApp.getInstance().getAppLocaleContext().getString(R.string.network_error);
+                case 1003:
+                case 1004:
+                    return HDLApp.getInstance().getAppLocaleContext().getString(R.string.ssl_error);
+                case 1005:
+                    return HDLApp.getInstance().getAppLocaleContext().getString(R.string.timeout_error);
+                case 4:
+                    return HDLApp.getInstance().getAppLocaleContext().getString(R.string.signature_error_log_in_again);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return "";
+    }
+
+    /**
+     * 鑾峰彇json璧勬簮鐢ㄧ殑
+     *
+     * @param url      鍦板潃
+     * @param callBack 鍥炶皟
+     */
+    public void requestJsonHttpGet(String url, CloudCallBeak<String> callBack) {
+        if (TextUtils.isEmpty(url)) {
+            if (callBack != null) {
+                callBack.onSuccess("");
+            }
+        }
+
+        HdlThreadLogic.runSubThread(new Runnable() {
+            @Override
+            public void run() {
+                OkHttpClient client = new OkHttpClient();
+                Request request = new Request.Builder()
+                        .url(url)
+                        .addHeader("Accept-Language", UserConfigManage.getInstance().getCurrentAppLanguage())
+                        .build();
+
+                try (Response response = client.newCall(request).execute()) {
+                    if (response.isSuccessful()) {
+                        if (response.body() == null) {
+                            if (callBack != null) {
+                                callBack.onSuccess("");
+                            }
+                        } else {
+                            String str = response.body().string();
+                            if (callBack != null) {
+                                callBack.onSuccess(str);
+                            }
+                        }
+
+                    } else {
+                        if (callBack != null) {
+                            callBack.onSuccess("");
+                        }
+                    }
+                } catch (Exception e) {
+                    if (callBack != null) {
+                        callBack.onFailure(new HDLException(-20002, e.getMessage()));
+                    }
+                }
+            }
+        });
+
+    }
+
+
+    /**
+     * 璇锋眰鏈嶅姟鍣�(get)
+     * 搴曞眰鏂规硶,寮曠敤搴撶殑鏂规硶
+     *
+     * @param requestUrl 璇锋眰缁濆鍦板潃鎺ュ彛
+     * @param callBack   鍥炶皟
+     * @return -
+     */
+    private Disposable requestGet(String requestUrl, CloudCallBeak<String> callBack) {
+//        HdlLogLogic.print("http---鍙戦��---" + requestUrl + "\r\n", isAddToMemory);
+        //寮�鍙戠幆澧冮渶瑕佸姞(鐢ㄦ潵鍖哄垎onpro鍜屽厜浼廰pp浠g爜):HxHttp.builder().headers("x-lbs-version", "hdl-yt")
+        return HxHttp.builder().url(requestUrl).build().get().subscribeWith(new HDLResponse<String>() {
+            @Override
+            public void onResponse(String str) {
+                if (callBack != null) {
+                    callBack.onSuccess(str);
+//                          System.out.println("http---鍥炲---" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString());
+//                    HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + str, isAddToMemory);
+                }
+            }
+
+            @Override
+            public void onFailure(HDLException e) {
+                HDLExceptionSubmitUtils.submit(requestUrl, "", e);
+                if (callBack != null) {
+                    callBack.onFailure(getException(e));
+                    HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory);
+                }
+            }
+        });
+
+    }
+
+    /**
+     * 璇锋眰鏈嶅姟鍣�(post)
+     * 搴曞眰鏂规硶,寮曠敤搴撶殑鏂规硶
+     *
+     * @param api      璇锋眰鎺ュ彛
+     * @param body     璇锋眰鍙傛暟(appKey,timestamp,sign杩欎笁涓弬鏁板唴閮ㄤ細鑷姩娣诲姞))
+     * @param callBack 鍥炶皟
+     * @return -
+     */
+    private Disposable requestPost(String api, String body, CloudCallBeak<String> callBack) {
+        String requestUrl = HDLCloudUserApi.getRequestUrl(api);
+//        HdlLogLogic.print("http---鍙戦��---" + requestUrl + "\r\n" + body, isAddToMemory);
+        //寮�鍙戠幆澧冮渶瑕佸姞(鐢ㄦ潵鍖哄垎onpro鍜屽厜浼廰pp浠g爜):HxHttp.builder().headers("x-lbs-version", "hdl-yt")
+        return HxHttp.builder().url(requestUrl).raw(body).build().post().subscribeWith(new HDLResponse<String>() {
+            @Override
+            public void onResponse(String str) {
+                if (callBack != null) {
+                    callBack.onSuccess(str);
+//                          System.out.println("http---鍥炲---" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString());
+//                    HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + str, isAddToMemory);
+                }
+            }
+
+            @Override
+            public void onFailure(HDLException e) {
+                HDLExceptionSubmitUtils.submit(requestUrl, body, e);
+                if (callBack != null) {
+                    callBack.onFailure(getException(e));
+//                    HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory);
+                }
+            }
+        });
+
+    }
+
+    /**
+     * 璇锋眰鏈嶅姟鍣�
+     * 搴曞眰鏂规硶,寮曠敤搴撶殑鏂规硶
+     *
+     * @param api      璇锋眰鎺ュ彛
+     * @param body     璇锋眰鍙傛暟(appKey,timestamp,sign杩欎笁涓弬鏁板唴閮ㄤ細鑷姩娣诲姞))
+     * @param callBack 鍥炶皟
+     * @return -
+     */
+    private Disposable requestFilePost(String api, File body, CloudCallBeak<String> callBack) {
+
+
+        String requestUrl = HDLCloudUserApi.getRequestUrl(api);
+//        HdlLogLogic.print("http---鍙戦��---" + requestUrl + "\r\n" + body, isAddToMemory);
+        return HxHttp.builder().url(requestUrl).file(body).build().post().subscribeWith(new HDLResponse<String>() {
+            @Override
+            public void onResponse(String str) {
+                if (callBack != null) {
+                    callBack.onSuccess(str);
+//                          System.out.println("http---鍥炲---" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString());
+//                    HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + str, isAddToMemory);
+                }
+            }
+
+            @Override
+            public void onFailure(HDLException e) {
+                HDLExceptionSubmitUtils.submit(requestUrl, body, e);
+                if (callBack != null) {
+                    callBack.onFailure(getException(e));
+//                    HdlLogLogic.print("http---鍥炲---" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", isAddToMemory);
+                }
+            }
+        });
+
+    }
+
+
+    /**
+     * 涓嬭浇鏂囦欢
+     *
+     * @param callBack -
+     */
+    public Disposable downLoadFile(String url, CloudCallBeak<ResponseBody> callBack) {
+        return HxHttp.builder()
+                .url(url)
+                .headers(HDLSmartHeader.IGNORE_SIGN_HEADER, 1)
+                .build()
+                .download()
+                .subscribe(new Consumer<ResponseBody>() {
+                    @Override
+                    public void accept(ResponseBody responseBody) throws Exception {
+                        if (callBack != null) {
+                            callBack.onSuccess(responseBody);
+                        }
+                    }
+                }, new Consumer<Throwable>() {
+                    @Override
+                    public void accept(Throwable throwable) throws Exception {
+                        /**
+                         * 缃戠粶璇锋眰澶辫触
+                         * 鍏蜂綋鍙互鑷繁瀹炵幇
+                         */
+                        HDLExceptionSubmitUtils.submit(url, null, throwable);
+                        if (callBack != null) {
+                            callBack.onFailure(new HDLException(403, throwable.getMessage()));
+                        }
+                    }
+                });
+    }
+
+    /**
+     * 璇锋眰(get)鏈嶅姟鍣�
+     *
+     * @param requestUrl 璇锋眰缁濆鍦板潃鎺ュ彛
+     * @param callBack   鍥炶皟
+     */
+    public Disposable requestHttpGet(String requestUrl, CloudCallBeak<String> callBack) {
+        return requestGet(requestUrl, callBack);
+    }
+
+    /**
+     * 璇锋眰(post)鏈嶅姟鍣�
+     *
+     * @param api      璇锋眰鎺ュ彛
+     * @param body     璇锋眰鍙傛暟(json)
+     * @param callBack 鍥炶皟
+     */
+    public Disposable requestHttp(String api, String body, CloudCallBeak<String> callBack) {
+        return requestPost(api, body, callBack);
+    }
+
+    /**
+     * 涓婁紶鏂囦欢
+     *
+     * @param api      璇锋眰鎺ュ彛
+     * @param body     璇锋眰鍙傛暟(json)
+     * @param callBack 鍥炶皟
+     */
+    public Disposable uploadingFile(String api, File body, CloudCallBeak<String> callBack) {
+        return requestFilePost(api, body, callBack);
+    }
+
+
+    /**
+     * 璇锋眰鏈嶅姟鍣ㄧ殑鏂规硶(宸插純鐢�)
+     *
+     * @param requestUrl     璇锋眰鎺ュ彛
+     * @param json           璇锋眰鏁版嵁
+     * @param isBasicService 鏄惁鏄熀纭�鏈嶅姟鐨勬帴鍙�(鍩虹鏈嶅姟鐨勬帴鍙i渶瑕� appKey,timestamp,sign杩欎笁涓弬鏁�,褰撲负true鏃�,鍐呴儴浼氳嚜鍔ㄦ坊鍔�)
+     * @param isExecute      鏄惁鏄悓姝�(true=鍚屾,false=寮傛)
+     */
+    @Deprecated
+    private void requestHttp(String requestUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) {
+        String fullUrl = AppConfigManage.getUserRegionUrl() + requestUrl;
+        this.requestHttpServer(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak);
+    }
+
+    /**
+     * 璇锋眰鏈嶅姟鍣ㄧ殑鏂规硶(宸插純鐢�)
+     * 鐩墠鍙敤鍦ㄨ幏鍙栬幏鍙栬处鍙峰尯鍩熶俊鎭�
+     *
+     * @param fullUrl        缁濆鍦板潃(鍦板潃+鎺ュ彛)
+     * @param json           璇锋眰鏁版嵁
+     * @param isBasicService 鏄惁鏄熀纭�鏈嶅姟鐨勬帴鍙�(鍩虹鏈嶅姟鐨勬帴鍙i渶瑕� appKey,timestamp,sign杩欎笁涓弬鏁�,褰撲负true鏃�,鍐呴儴浼氳嚜鍔ㄦ坊鍔�)
+     * @param isExecute      鏄惁鏄悓姝�(true=鍚屾,false=寮傛)
+     */
+    @Deprecated
+    private void requestFullHttp(String fullUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) {
+        this.requestHttpServer(fullUrl, json, isBasicService, isExecute, baseSuccessCallBeak);
+    }
+
+
+    /**
+     * 璇锋眰鏈嶅姟鍣ㄧ殑鏂规硶
+     *
+     * @param fullUrl        缁濆璇锋眰鍦板潃
+     * @param json           璇锋眰鏁版嵁
+     * @param isBasicService 鏄惁鏄熀纭�鏈嶅姟鐨勬帴鍙�(鍩虹鏈嶅姟鐨勬帴鍙i渶瑕� appKey,timestamp,sign杩欎笁涓弬鏁�,褰撲负true鏃�,鍐呴儴浼氳嚜鍔ㄦ坊鍔�)
+     * @param isExecute      鏄惁鏄悓姝�(true=鍚屾,false=寮傛)
+     */
+    private void requestHttpServer(String fullUrl, String json, boolean isBasicService, boolean isExecute, BaseSuccessFailureCallBeak baseSuccessCallBeak) {
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                try {
+//                    HdlLogLogic.print("http---鍙戦��---" + fullUrl + "\r\n" + json, isAddToMemory);
+                    OkHttpClient okHttpClient = new OkHttpClient();
+                    RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), getJson(json));
+                    final Request request = new Request.Builder().url(fullUrl)//璇锋眰鐨剈rl
+                            .addHeader("Authorization", UserConfigManage.getInstance().getHeaderPrefix() + UserConfigManage.getInstance().getToken()).post(requestBody).build();
+                    if (isExecute) {
+                        Response response = okHttpClient.newCall(request).execute();//鍚屾
+                        if (response.isSuccessful()) {
+                            String s = Objects.requireNonNull(response.body()).string();
+                            HttpResponsePack httpResponsePack = new Gson().fromJson(s, HttpResponsePack.class);
+//                            HdlLogLogic.print("http---鍥炲---" + response.request().url() + "\r\n" + s, isAddToMemory);
+                            baseSuccessCallBeak.onSuccess(httpResponsePack);
+
+                        } else {
+                            //throw new IOException("Unexpected code " + response);
+                            baseSuccessCallBeak.onFailure(new HDLException(response.code(), response.message()));
+                        }
+                    } else {
+
+                        Call call = okHttpClient.newCall(request);
+                        call.enqueue(new Callback() {//寮傛
+                            @Override
+                            public void onFailure(@NonNull Call call, @NonNull IOException e) {
+//                                HdlLogLogic.print("http---鍥炲---" + "\r\n" + e.getMessage(), isAddToMemory);
+                            }
+
+                            @Override
+                            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
+                                System.out.println("url杩炴帴淇℃伅" + response.code());
+                                if (response.code() == 200) {
+                                    String s = Objects.requireNonNull(response.body()).string();
+                                    HttpResponsePack httpResponsePack = new Gson().fromJson(s, HttpResponsePack.class);
+                                    baseSuccessCallBeak.onSuccess(httpResponsePack);
+//                                    HdlLogLogic.print("http---鍥炲---" + "\r\n" + s, isAddToMemory);
+                                } else {
+                                    baseSuccessCallBeak.onFailure(new HDLException(response.code(), response.message()));
+                                }
+                            }
+                        });
+                    }
+                } catch (Exception e) {
+                    baseSuccessCallBeak.onFailure(new HDLException(-100, e.getMessage()));
+//                    HdlLogLogic.print("http---鍥炲---" + "\r\n" + e.getMessage(), isAddToMemory);
+                }
+
+            }
+        }).start();
+    }
+
+    /**
+     * 璇锋眰鏈嶅姟鍣ㄧ殑鏂规硶
+     *
+     * @param url 缁濆璇锋眰鍦板潃
+     */
+    private void requestHttpServer(String url, CloudCallBeak<String> cloudCallBeak) {
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                OkHttpClient okHttpClient = new OkHttpClient();
+                final Request request = new Request.Builder().url(url).build();
+                try {
+                    Response response = okHttpClient.newCall(request).execute();
+                    String s = Objects.requireNonNull(response.body()).string();
+                    if (cloudCallBeak != null) {
+                        cloudCallBeak.onSuccess(s);
+                    }
+                } catch (Exception e) {
+                    if (cloudCallBeak != null) {
+                        cloudCallBeak.onSuccess(e.getMessage());
+                    }
+
+                }
+            }
+        }).start();
+    }
+
+    /**
+     * 娣诲姞sign瀛楁
+     */
+    private String getJson(String json) {
+        final String timestamp = String.valueOf(System.currentTimeMillis());
+        final String appKey = AppConfigManage.getAppKey();
+        final String appSecret = AppConfigManage.getAppSecret();
+        JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class);
+        if (jsonObject == null) {
+            jsonObject = new JsonObject();
+        }
+        jsonObject.addProperty("appKey", appKey);
+        jsonObject.addProperty("timestamp", timestamp);
+        jsonObject.addProperty("sign", getSign(jsonObject, appSecret));
+
+        return jsonObject.toString();
+    }
+
+    /**
+     * 闇�瑕佹寜瀛楁瘝鎺掑簭
+     *
+     * @param json 鎵�鏈夊瓧娈典娇鐢╱rlParameter鎷兼帴锛岄櫎浜哸ppSecret
+     */
+    private String getSign(JsonObject json, String appSecret) {
+        String builder = jsonToUrlParameter(json) + appSecret;
+        return Md5Utils.encodeMD5(builder);
+    }
+
+
+    private String jsonToUrlParameter(JsonObject object) {
+        final Map<String, String> map = GsonConvert.getGson().fromJson(object, new TypeToken<Map<String, String>>() {
+        }.getType());
+        final Uri.Builder builder = new Uri.Builder();
+        List<String> list = new ArrayList<>(map.keySet());
+        Collections.sort(list);
+        for (String key : list) {
+            //鍒ゆ柇褰撳墠鍊兼槸鍚﹂渶瑕佸弬涓庣鍚嶏紝淇濇寔璺熶簯绔竴鑷�
+            if (IfValueNeedSign(map.get(key))) {
+                builder.appendQueryParameter(key, map.get(key));
+//                HDLSDKLog.e("瑕佺鍚嶏細" + key + " 锛�" + map.get(key));
+            } else {
+//                HDLSDKLog.e("涓嶉渶瑕佺鍚嶏細" + key + " 锛�" + map.get(key));
+            }
+        }
+        return builder.build().getQuery();
+    }
+
+    /**
+     * 鍒ゆ柇褰撳墠鍊兼槸鍚﹂渶瑕佸弬涓庣鍚嶏紝淇濇寔璺熶簯绔竴鑷�
+     * 绌哄瓧绗︿覆涓嶅弬涓�
+     * 鏁扮粍,闆嗗悎,瀵硅薄涓嶅弬涓�
+     *
+     * @param valueStr -
+     * @return -
+     */
+    private boolean IfValueNeedSign(String valueStr) {
+        if (TextUtils.isEmpty(valueStr)) return false;
+        final char[] strChar = valueStr.substring(0, 1).toCharArray();
+        final char firstChar = strChar[0];
+        //System.out.println("getJSONType firstChar = "+firstChar);
+        return firstChar != '{' && firstChar != '[';
+    }
 }

--
Gitblit v1.8.0