From 30d8e227e8d823b6c38c3b9c90ac2df03b63befe Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 25 二月 2025 16:41:22 +0800 Subject: [PATCH] 淘宝转链接口更新 --- fanli/src/main/java/com/yeshi/fanli/util/taobao/TaoKeBaseUtil.java | 637 ++++++++++++++++++++++++++++++++------------------------- 1 files changed, 362 insertions(+), 275 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/util/taobao/TaoKeBaseUtil.java b/fanli/src/main/java/com/yeshi/fanli/util/taobao/TaoKeBaseUtil.java index eb562d1..cae9644 100644 --- a/fanli/src/main/java/com/yeshi/fanli/util/taobao/TaoKeBaseUtil.java +++ b/fanli/src/main/java/com/yeshi/fanli/util/taobao/TaoKeBaseUtil.java @@ -1,275 +1,362 @@ -package com.yeshi.fanli.util.taobao; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -import com.taobao.api.internal.util.StringUtils; -import com.yeshi.fanli.entity.taobao.TaoKeAppInfo; -import com.yeshi.fanli.exception.taobao.TaoKeApiException; -import com.yeshi.fanli.log.LogHelper; -import com.yeshi.fanli.log.TaoKeLogHelper; -import com.yeshi.fanli.util.StringUtil; -import com.yeshi.fanli.util.TaoBaoHttpUtil; -import com.yeshi.fanli.util.TimeUtil; -import org.yeshi.utils.HttpUtil; - -import net.sf.json.JSONObject; - -public class TaoKeBaseUtil { - - private static TaoKeAppInfo taoKeAppInfo = null; - private static long lastTime = 0; - - public static JSONObject baseRequest(Map<String, String> param, boolean needAdzoneId) throws TaoKeApiException { - // 澶嶅埗params - Map<String, String> params = new HashMap<>(); - if (param != null) { - Iterator<String> its = param.keySet().iterator(); - while (its.hasNext()) { - String key = its.next(); - params.put(key, param.get(key)); - } - } - - // 鑾峰彇鏈夋晥鐨凙PPKey - TaoKeAppInfo app = getAvailableTaoKeAppInfo(); - if (app == null) - throw new TaoKeApiException(TaoKeApiException.CODE_NO_USE, "鏃燼ppkey鍙敤"); - // 绛惧悕 - params.put("app_key", app.getAppKey()); - params.put("sign_method", "md5"); - params.put("v", "2.0"); - params.put("timestamp", TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); - params.put("format", "json"); - if (needAdzoneId) - params.put("adzone_id", app.getAdzoneId()); - params.put("sign", getSign(params, "md5", app).toUpperCase()); - String result = TaoBaoHttpUtil.taoKeGet(params); - JSONObject data = JSONObject.fromObject(result); - if (data != null) { - if (data.optJSONObject("error_response") != null - && data.optJSONObject("error_response").optInt("code") == 7) { - reportAppInvalid(app.getAppKey()); - TaoKeLogHelper.error(params, result); - throw new TaoKeApiException(TaoKeApiException.CODE_APPKEY_LIMIT, "娣樺疂璇锋眰闄愬埗:" + result, params); - } else if (data.optJSONObject("error_response") != null) { - throw new TaoKeApiException(TaoKeApiException.CODE_API_ERROR, result, params); - } - } else - throw new TaoKeApiException(TaoKeApiException.CODE_OTHER, ":" + result, params); - return data; - } - - public static JSONObject baseRequest(Map<String, String> param, TaoKeAppInfo app) throws TaoKeApiException { - // 澶嶅埗params - Map<String, String> params = new HashMap<>(); - if (param != null) { - Iterator<String> its = param.keySet().iterator(); - while (its.hasNext()) { - String key = its.next(); - params.put(key, param.get(key)); - } - } - - // 鑾峰彇鏈夋晥鐨凙PPKey - if (app == null) - throw new TaoKeApiException(TaoKeApiException.CODE_NO_USE, "鏃燼ppkey鍙敤"); - // 绛惧悕 - params.put("app_key", app.getAppKey()); - params.put("sign_method", "md5"); - params.put("v", "2.0"); - params.put("timestamp", TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); - params.put("format", "json"); - params.put("adzone_id", app.getAdzoneId()); - params.put("sign", getSign(params, "md5", app).toUpperCase()); - String result = TaoBaoHttpUtil.taoKeGet(params); - JSONObject data = JSONObject.fromObject(result); - if (data != null) { - if (data.optJSONObject("error_response") != null - && data.optJSONObject("error_response").optInt("code") == 7) { - reportAppInvalid(app.getAppKey()); - throw new TaoKeApiException(TaoKeApiException.CODE_APPKEY_LIMIT, "娣樺疂璇锋眰闄愬埗:" + result, params); - } else if (data.optJSONObject("error_response") != null) { - throw new TaoKeApiException(TaoKeApiException.CODE_API_ERROR, result, params); - } - } else - throw new TaoKeApiException(TaoKeApiException.CODE_OTHER, ":" + result, params); - return data; - } - - /** - * 閲嶅3娆¤姹傦紝闄嶄綆鍑洪敊姒傜巼 - * - * @param params - * @return - */ - public static String baseRequestForThreeTimes(Map<String, String> params, boolean needAdzoneId) { - JSONObject data = null; - int count = 0; - String result = null; - while (data == null && count < 2) { - count++; - try { - data = baseRequest(params, needAdzoneId); - } catch (TaoKeApiException e) { - // 璁板綍鐜板満 - TaoKeLogHelper.error(e.getParams(), e.getMsg()); - if (e.getCode() == TaoKeApiException.CODE_API_ERROR) { - result = e.getMsg(); - } - } - } - if (!StringUtil.isNullOrEmpty(result)) { - try { - data = JSONObject.fromObject(result); - } catch (Exception e) { - } - - } - - if (data != null) - return data.toString(); - else - return new JSONObject().toString(); - } - - public static String baseRequestForThreeTimes(Map<String, String> params, TaoKeAppInfo app) { - JSONObject data = null; - int count = 0; - while (data == null && count < 3) { - count++; - try { - data = baseRequest(params, app); - } catch (TaoKeApiException e) { - // 璁板綍鐜板満 - TaoKeLogHelper.error(e.getParams(), e.getMsg()); - } - } - if (data != null) - return data.toString(); - else - return new JSONObject().toString(); - } - - /** - * 鑾峰彇绛惧悕鍙傛暟 - * - * @param params - * @param signMethod - * @param app - * @return - */ - public static String getSign(Map<String, String> params, String signMethod, TaoKeAppInfo app) { - // 绗竴姝ワ細妫�鏌ュ弬鏁版槸鍚﹀凡缁忔帓搴� - String[] keys = params.keySet().toArray(new String[0]); - Arrays.sort(keys); - - // 绗簩姝ワ細鎶婃墍鏈夊弬鏁板悕鍜屽弬鏁板�间覆鍦ㄤ竴璧� - StringBuilder query = new StringBuilder(); - if ("md5".equals(signMethod)) { - query.append(app.getAppSecret()); - } - for (String key : keys) { - String value = params.get(key); - if (StringUtils.areNotEmpty(key, value)) { - query.append(key).append(value); - } - } - - query.append(app.getAppSecret()); - return StringUtil.Md5(query.toString()); - } - - static TaoKeAppInfo getAvailableTaoKeAppInfo() { - if (System.currentTimeMillis() - lastTime > 1000 * 20L) - taoKeAppInfo = null; - if (taoKeAppInfo == null) { - System.out.println("璇锋眰銆傘�傘�傘�傘��"); - String result = null; - try { - result = HttpUtil.get("http://193.112.35.168:8091/tb/taoke/getcanuseapp"); - } catch (Exception e) { - } - - // 鎺ュ彛璇锋眰澶辫触,榛樿璁剧疆鎴愬奖瑙嗗ぇ鍏↖OS鐨勫獟浣撲俊鎭� - if (StringUtil.isNullOrEmpty(result)) { - taoKeAppInfo = new TaoKeAppInfo(); - taoKeAppInfo.setAdzoneId("381938426"); - taoKeAppInfo.setAppKey("24838852"); - taoKeAppInfo.setAppSecret("bc8265e2bf8d8115329d652f9d3d4cd8"); - taoKeAppInfo.setPid("mm_124933865_43788020_381938426"); - lastTime = System.currentTimeMillis(); - return taoKeAppInfo; - } - - JSONObject data = JSONObject.fromObject(result); - if (data.optInt("code") == 0) { - TaoKeAppInfo info = new TaoKeAppInfo(); - info.setAppKey(data.optJSONObject("data").optString("appkey")); - info.setAppSecret(data.optJSONObject("data").optString("appsecret")); - info.setPid(data.optJSONObject("data").optString("pid")); - String[] sts = info.getPid().split("_"); - info.setAdzoneId(sts[sts.length - 1]); - taoKeAppInfo = info; - lastTime = System.currentTimeMillis(); - } else {// 闃叉鎵�鏈夌殑澶辨晥 - taoKeAppInfo = new TaoKeAppInfo(); - taoKeAppInfo.setAdzoneId("381938426"); - taoKeAppInfo.setAppKey("24838852"); - taoKeAppInfo.setAppSecret("bc8265e2bf8d8115329d652f9d3d4cd8"); - taoKeAppInfo.setPid("mm_124933865_43788020_381938426"); - return taoKeAppInfo; - } - } - return taoKeAppInfo; - } - - static void filterTaoKeResponse(String result, TaoKeAppInfo app) { - if (!StringUtil.isNullOrEmpty(result)) { - try { - JSONObject data = JSONObject.fromObject(result); - if (data != null) { - if (data.optJSONObject("error_response") != null - && data.optJSONObject("error_response").optInt("code") == 7) { - reportAppInvalid(app.getAppKey()); - } - } else - LogHelper.test(result); - } catch (Exception e) { - try { - LogHelper.errorDetailInfo(e); - } catch (Exception e1) { - e1.printStackTrace(); - } - } - } - } - - static Map<String, Integer> invalidMap = new HashMap<>(); - - /** - * 鎶ュ憡璇PPKey涓嶈兘鐢� - * - * @param appkey - */ - static void reportAppInvalid(String appkey) { - // 閿欒涓夋鍚庡啀鐪熸涓婃姤 - if (invalidMap.get(appkey) == null) - invalidMap.put(appkey, 1); - else - invalidMap.put(appkey, invalidMap.get(appkey) + 1); - - if (invalidMap.get(appkey) < 4) - return; - invalidMap.put(appkey, 0); - HttpUtil.get("http://193.112.35.168:8091/tb/taoke/reportappcannotuse?appkey=" + appkey); - lastTime = 0; - } - - public static void setAppValid() { - HttpUtil.get("http://193.112.35.168:8091/tb/taoke/setappcanuse"); - } - -} +package com.yeshi.fanli.util.taobao; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import com.google.gson.Gson; +import com.taobao.api.internal.util.StringUtils; +import com.yeshi.common.entity.taobao.TaoKeAppInfo; +import com.yeshi.fanli.exception.taobao.TaoKeApiException; +import com.yeshi.fanli.log.LogHelper; +import com.yeshi.fanli.log.TaoKeLogHelper; +import com.yeshi.fanli.util.EmergencyUtil; +import com.yeshi.fanli.util.StringUtil; +import com.yeshi.fanli.util.TaoBaoHttpUtil; +import org.yeshi.utils.TimeUtil; +import org.yeshi.utils.HttpUtil; + +import net.sf.json.JSONObject; + +public class TaoKeBaseUtil { + + private static TaoKeAppInfo taoKeAppInfo = null; + private static long lastTime = 0; + + public static JSONObject baseRequest(Map<String, String> param, boolean needAdzoneId) throws TaoKeApiException { + // 澶嶅埗params + Map<String, String> params = new HashMap<>(); + if (param != null) { + Iterator<String> its = param.keySet().iterator(); + while (its.hasNext()) { + String key = its.next(); + params.put(key, param.get(key)); + } + } + + // 鑾峰彇鏈夋晥鐨凙PPKey + TaoKeAppInfo app = getAvailableTaoKeAppInfo(); + if (app == null) + throw new TaoKeApiException(TaoKeApiException.CODE_NO_USE, "鏃燼ppkey鍙敤"); + // 绛惧悕 + params.put("app_key", app.getAppKey()); + params.put("sign_method", "md5"); + params.put("v", "2.0"); + params.put("timestamp", TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); + params.put("format", "json"); + if (needAdzoneId) + params.put("adzone_id", app.getAdzoneId()); + params.put("sign", getSign(params, "md5", app).toUpperCase()); + String result = TaoBaoHttpUtil.taoKeGet(params); + JSONObject data = JSONObject.fromObject(result); + if (data != null) { + if (data.optJSONObject("error_response") != null && data.optJSONObject("error_response").optInt("code") == 7 + && "accesscontrol.limited-by-app-access-count" + .equalsIgnoreCase(data.optJSONObject("error_response").optString("sub_code"))) { + reportAppInvalid(app.getAppKey()); + TaoKeLogHelper.error(params, result); + try { + EmergencyUtil.baoJin("monitor-error-tb-app-limit-" + params.get("app_key"), + "娣樺疂APPKey璇锋眰闄愬埗銆�" + params.get("app_key") + "銆�", new String[] { "18581318252" }); + } catch (Exception e1) { + } + throw new TaoKeApiException(TaoKeApiException.CODE_APPKEY_LIMIT, "娣樺疂璇锋眰闄愬埗:" + result, params); + } else if (data.optJSONObject("error_response") != null) { + throw new TaoKeApiException(TaoKeApiException.CODE_API_ERROR, result, params); + } + } else + throw new TaoKeApiException(TaoKeApiException.CODE_OTHER, ":" + result, params); + reValid(app.getAppKey()); + return data; + } + + + /** + * 鏈嶅姟鍟嗚姹� + * @Title: baseScRequest + * @Description: + * @param param + * @param session + * @return + * @throws TaoKeApiException + * JSONObject 杩斿洖绫诲瀷 + * @throws + */ + public static JSONObject baseScRequest(Map<String, String> param,TaoKeAppInfo app, String session,String targetAppKey) throws TaoKeApiException { + // 澶嶅埗params + Map<String, String> params = new HashMap<>(); + if (param != null) { + Iterator<String> its = param.keySet().iterator(); + while (its.hasNext()) { + String key = its.next(); + params.put(key, param.get(key)); + } + } + + if (app == null) + throw new TaoKeApiException(TaoKeApiException.CODE_NO_USE, "鏃燼ppkey鍙敤"); + // 绛惧悕 + params.put("app_key", app.getAppKey()); + params.put("sign_method", "md5"); + params.put("v", "2.0"); + params.put("timestamp", TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); + params.put("format", "json"); + params.put("session", session); +// params.put("target_app_key", targetAppKey); + params.put("sign", getSign(params, "md5", app).toUpperCase()); + String result = TaoBaoHttpUtil.taoKeGet(params); + JSONObject data = JSONObject.fromObject(result); + if (data != null) { + if (data.optJSONObject("error_response") != null && data.optJSONObject("error_response").optInt("code") == 7 + && "accesscontrol.limited-by-app-access-count" + .equalsIgnoreCase(data.optJSONObject("error_response").optString("sub_code"))) { + reportAppInvalid(app.getAppKey()); + TaoKeLogHelper.error(params, result); + try { + EmergencyUtil.baoJin("monitor-error-tb-app-limit-" + params.get("app_key"), + "娣樺疂APPKey璇锋眰闄愬埗銆�" + params.get("app_key") + "銆�", new String[] { "18581318252" }); + } catch (Exception e1) { + } + throw new TaoKeApiException(TaoKeApiException.CODE_APPKEY_LIMIT, "娣樺疂璇锋眰闄愬埗:" + result, params); + } else if (data.optJSONObject("error_response") != null) { + throw new TaoKeApiException(TaoKeApiException.CODE_API_ERROR, result, params); + } + } else + throw new TaoKeApiException(TaoKeApiException.CODE_OTHER, ":" + result, params); +// reValid(app.getAppKey()); + return data; + } + + public static JSONObject baseRequest(Map<String, String> param, TaoKeAppInfo app) throws TaoKeApiException { + // 澶嶅埗params + Map<String, String> params = new HashMap<>(); + if (param != null) { + Iterator<String> its = param.keySet().iterator(); + while (its.hasNext()) { + String key = its.next(); + params.put(key, param.get(key)); + } + } + + // 鑾峰彇鏈夋晥鐨凙PPKey + if (app == null) + throw new TaoKeApiException(TaoKeApiException.CODE_NO_USE, "鏃燼ppkey鍙敤"); + // 绛惧悕 + params.put("app_key", app.getAppKey()); + params.put("sign_method", "md5"); + params.put("v", "2.0"); + params.put("timestamp", TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")); + params.put("format", "json"); + if (!StringUtil.isNullOrEmpty(app.getAdzoneId())) + params.put("adzone_id", app.getAdzoneId()); + params.put("sign", getSign(params, "md5", app).toUpperCase()); + + System.out.println(new Gson().toJson(params)); + + String result = TaoBaoHttpUtil.taoKeGet(params); + JSONObject data = JSONObject.fromObject(result); + if (data != null) { + if (data.optJSONObject("error_response") != null && data.optJSONObject("error_response").optInt("code") == 7 + && "accesscontrol.limited-by-app-access-count" + .equalsIgnoreCase(data.optJSONObject("error_response").optString("sub_code"))) { + reportAppInvalid(app.getAppKey()); + throw new TaoKeApiException(TaoKeApiException.CODE_APPKEY_LIMIT, "娣樺疂璇锋眰闄愬埗:" + result, params); + } else if (data.optJSONObject("error_response") != null) { + throw new TaoKeApiException(TaoKeApiException.CODE_API_ERROR, result, params); + } + } else + throw new TaoKeApiException(TaoKeApiException.CODE_OTHER, ":" + result, params); + reValid(app.getAppKey()); + return data; + } + + /** + * 閲嶅3娆¤姹傦紝闄嶄綆鍑洪敊姒傜巼 + * + * @param params + * @return + */ + public static String baseRequestForThreeTimes(Map<String, String> params, boolean needAdzoneId) { + JSONObject data = null; + int count = 0; + String result = null; + while (data == null && count < 2) { + count++; + try { + data = baseRequest(params, needAdzoneId); + } catch (TaoKeApiException e) { + // 璁板綍鐜板満 + TaoKeLogHelper.error(e.getParams(), e.getMsg()); + if (e.getCode() == TaoKeApiException.CODE_API_ERROR) { + result = e.getMsg(); + } + } + } + if (!StringUtil.isNullOrEmpty(result)) { + try { + data = JSONObject.fromObject(result); + } catch (Exception e) { + } + + } + + if (data != null) + return data.toString(); + else + return new JSONObject().toString(); + } + + public static String baseRequestForThreeTimes(Map<String, String> params, TaoKeAppInfo app) { + JSONObject data = null; + int count = 0; + while (data == null && count < 3) { + count++; + try { + data = baseRequest(params, app); + } catch (TaoKeApiException e) { + // 璁板綍鐜板満 + TaoKeLogHelper.error(e.getParams(), e.getMsg()); + } + } + if (data != null) + return data.toString(); + else + return new JSONObject().toString(); + } + + /** + * 鑾峰彇绛惧悕鍙傛暟 + * + * @param params + * @param signMethod + * @param app + * @return + */ + public static String getSign(Map<String, String> params, String signMethod, TaoKeAppInfo app) { + // 绗竴姝ワ細妫�鏌ュ弬鏁版槸鍚﹀凡缁忔帓搴� + String[] keys = params.keySet().toArray(new String[0]); + Arrays.sort(keys); + + // 绗簩姝ワ細鎶婃墍鏈夊弬鏁板悕鍜屽弬鏁板�间覆鍦ㄤ竴璧� + StringBuilder query = new StringBuilder(); + if ("md5".equals(signMethod)) { + query.append(app.getAppSecret()); + } + for (String key : keys) { + String value = params.get(key); + if (StringUtils.areNotEmpty(key, value)) { + query.append(key).append(value); + } + } + + query.append(app.getAppSecret()); + return StringUtil.Md5(query.toString()); + } + + static TaoKeAppInfo getAvailableTaoKeAppInfo() { + if (System.currentTimeMillis() - lastTime > 1000 * 20L) + taoKeAppInfo = null; + if (taoKeAppInfo == null) { + System.out.println("璇锋眰銆傘�傘�傘�傘��"); + String result = null; +// try { +// result = HttpUtil.get("http://193.112.35.168:8091/tb/taoke/getcanuseapp"); +// } catch (Exception e) { +// } + + // 鎺ュ彛璇锋眰澶辫触,榛樿璁剧疆鎴愬奖瑙嗗ぇ鍏↖OS鐨勫獟浣撲俊鎭� + if (StringUtil.isNullOrEmpty(result)) { + taoKeAppInfo = new TaoKeAppInfo(); + taoKeAppInfo.setAdzoneId("381938426"); + taoKeAppInfo.setAppKey("24838852"); + taoKeAppInfo.setAppSecret("bc8265e2bf8d8115329d652f9d3d4cd8"); + taoKeAppInfo.setPid("mm_124933865_43788020_381938426"); + lastTime = System.currentTimeMillis(); + return taoKeAppInfo; + } + + JSONObject data = JSONObject.fromObject(result); + if (data.optInt("code") == 0) { + TaoKeAppInfo info = new TaoKeAppInfo(); + info.setAppKey(data.optJSONObject("data").optString("appkey")); + info.setAppSecret(data.optJSONObject("data").optString("appsecret")); + info.setPid(data.optJSONObject("data").optString("pid")); + String[] sts = info.getPid().split("_"); + info.setAdzoneId(sts[sts.length - 1]); + taoKeAppInfo = info; + lastTime = System.currentTimeMillis(); + } else {// 闃叉鎵�鏈夌殑澶辨晥 + taoKeAppInfo = new TaoKeAppInfo(); + taoKeAppInfo.setAdzoneId("381938426"); + taoKeAppInfo.setAppKey("24838852"); + taoKeAppInfo.setAppSecret("bc8265e2bf8d8115329d652f9d3d4cd8"); + taoKeAppInfo.setPid("mm_124933865_43788020_381938426"); + return taoKeAppInfo; + } + } + return taoKeAppInfo; + } + + static void filterTaoKeResponse(String result, TaoKeAppInfo app) { + if (!StringUtil.isNullOrEmpty(result)) { + try { + JSONObject data = JSONObject.fromObject(result); + if (data != null) { + if (data.optJSONObject("error_response") != null + && data.optJSONObject("error_response").optInt("code") == 7) { + reportAppInvalid(app.getAppKey()); + } + } else + LogHelper.test(result); + } catch (Exception e) { + try { + LogHelper.errorDetailInfo(e); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + } + } + + static Map<String, Integer> invalidMap = new HashMap<>(); + + /** + * 鎶ュ憡璇PPKey涓嶈兘鐢� + * + * @param appkey + */ + static void reportAppInvalid(String appkey) { + if (invalidMap == null) + return; + // 閿欒涓夋鍚庡啀鐪熸涓婃姤 + if (invalidMap.get(appkey) == null) + invalidMap.put(appkey, 1); + else + invalidMap.put(appkey, invalidMap.get(appkey) + 1); + + if (invalidMap.get(appkey) < 4) + return; + invalidMap.put(appkey, 0); + HttpUtil.get("http://193.112.35.168:8091/tb/taoke/reportappcannotuse?appkey=" + appkey); + lastTime = 0; + } + + /** + * APPKey鎭㈠鍙敤 + * + * @param appKey + */ + static void reValid(String appKey) { + if (invalidMap == null) + return; + Integer count = invalidMap.get(appKey); + if (count != null && count > 0) + invalidMap.put(appKey, count - 1); + } + + public static void setAppValid() { + HttpUtil.get("http://193.112.35.168:8091/tb/taoke/setappcanuse"); + } + +} -- Gitblit v1.8.0