package com.ks.lijin.utils.taobao;
|
|
|
import com.ks.lijin.exception.LiJinException;
|
import com.ks.lijin.exception.TaoKeApiException;
|
import com.ks.lijin.pojo.DTO.taobao.TaoKeAppInfo;
|
import com.ks.lijin.pojo.DTO.taobao.TaoLiJinDTO;
|
import com.taobao.api.internal.util.StringUtils;
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
import org.apache.commons.httpclient.HttpClient;
|
import org.apache.commons.httpclient.methods.GetMethod;
|
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
|
import org.yeshi.utils.*;
|
import org.yeshi.utils.taobao.TbImgUtil;
|
|
import java.math.BigDecimal;
|
import java.net.URLEncoder;
|
import java.util.*;
|
|
//淘宝客API接口
|
public class TaoKeApiUtil {
|
|
private static long lastTime = 0;
|
private static Map<String, Integer> invalidMap = new HashMap<>();
|
|
// 淘礼金创建
|
public static TaoLiJinDTO createTaoLiJin(Long auctionId, String name, BigDecimal perface, int totalNum,
|
Date sendStartTime, Date sendEndTime, Date useStartTime, Date useEndTime, TaoKeAppInfo app)
|
throws LiJinException, TaoKeApiException {
|
|
Map<String, String> map = new HashMap<>();
|
map.put("method", "taobao.tbk.dg.vegas.tlj.create");
|
map.put("adzone_id", app.getPid().split("_")[3]);
|
map.put("item_id", auctionId + "");
|
map.put("total_num", totalNum + "");
|
map.put("name", name);
|
map.put("user_total_win_num_limit", "1");
|
map.put("security_switch", "false");
|
map.put("per_face", perface.toString());
|
map.put("send_start_time", TimeUtil.getGernalTime(sendStartTime.getTime(), "yyyy-MM-dd HH:mm:ss"));
|
|
if (sendEndTime != null)
|
map.put("send_end_time", TimeUtil.getGernalTime(sendEndTime.getTime(), "yyyy-MM-dd HH:mm:ss"));
|
|
if (useEndTime != null) {
|
map.put("use_end_time", TimeUtil.getGernalTime(useEndTime.getTime(), "yyyy-MM-dd"));
|
map.put("use_end_time_mode", "2");
|
}
|
|
if (useStartTime != null)
|
map.put("use_start_time", TimeUtil.getGernalTime(useStartTime.getTime(), "yyyy-MM-dd"));
|
try {
|
String result = baseRequestForThreeTimes(map, app);
|
JSONObject json = JSONObject.fromObject(result);
|
System.out.println(json);
|
JSONObject root = json.optJSONObject("tbk_dg_vegas_tlj_create_response");
|
if (root != null && root.optJSONObject("result") != null) {
|
|
if (root.optJSONObject("result").optBoolean("success")) {
|
JSONObject modelJson = root.optJSONObject("result").optJSONObject("model");
|
TaoLiJinDTO dto = new TaoLiJinDTO();
|
dto.setRightsId(modelJson.optString("rights_id"));
|
dto.setSendUrl(modelJson.optString("send_url"));
|
return dto;
|
} else {
|
// 日志记录 TODO
|
}
|
|
// 接口返回异常
|
String msgCode = root.optJSONObject("result").optString("msg_code");
|
if (!StringUtil.isNullOrEmpty(msgCode)) {
|
// TLJLogHelper.info(auctionId, root.toString());// 淘礼金异常信息记录 TODO
|
switch (msgCode) {
|
case "FAIL_BIZ_ITEM_FORBIDDEN":
|
throw new LiJinException(LiJinException.CODE_TLJ_FORBIDDEN, "该商品不支持创建淘礼金红包");
|
case "FAIL_BIZ_ACCOUNT_UN_PAID":
|
throw new LiJinException(LiJinException.CODE_TLJ_UN_PAID, "您的淘礼金账户暂未创建,请前往资金管理页面充值");
|
case "PRE_FREEZE_ASSET_ACCOUNT_ERROR":
|
throw new LiJinException(LiJinException.CODE_TLJ_NO_MONEY, "官方玩法钱包余额不足");
|
default:
|
throw new TaoKeApiException(Integer.parseInt(msgCode), root.toString());
|
}
|
}
|
}
|
} catch (LiJinException e) {
|
throw e;
|
}
|
|
return null;
|
}
|
|
|
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();
|
}
|
|
|
public static JSONObject baseRequest(Map<String, String> param, TaoKeAppInfo app) throws TaoKeApiException {
|
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));
|
}
|
}
|
|
// 获取有效的APPKey
|
if (app == null) {
|
throw new TaoKeApiException(TaoKeApiException.CODE_NO_USE, "无appkey可用");
|
}
|
|
// 签名
|
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());
|
String result = 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;
|
}
|
|
/**
|
* 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);
|
}
|
|
/**
|
* 报告该APPKey不能用
|
*
|
* @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;
|
}
|
|
|
/**
|
* 获取签名参数
|
*
|
* @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());
|
}
|
|
|
public static String taoKeGet(Map<String, String> params) {
|
String result = get("https://eco.taobao.com/router/rest", params, false);
|
return result;
|
}
|
|
|
public static String get(String url, Map<String, String> params, boolean proxy) {
|
HttpClient client = new HttpClient();
|
try {
|
Iterator<String> keys = params.keySet().iterator();
|
url += "?";
|
while (keys.hasNext()) {
|
String key = keys.next();
|
url += String.format("%s=%s&", key, URLEncoder.encode(params.get(key), "UTF-8"));
|
}
|
GetMethod method = new GetMethod(url);
|
// 3S的响应超时
|
HttpConnectionManagerParams hparams = new HttpConnectionManagerParams();
|
hparams.setConnectionTimeout(3000);
|
client.getHttpConnectionManager().setParams(hparams);
|
return method.getResponseBodyAsString();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return "";
|
}
|
}
|