package com.yeshi.buwan.util.tb;
|
|
import com.yeshi.buwan.dto.tb.TaoKeAppInfo;
|
import com.yeshi.buwan.dto.tb.TaoLiJinDTO;
|
import com.yeshi.buwan.exception.taobao.TaoKeApiException;
|
import com.yeshi.buwan.exception.taobao.TaoLiJinCreateException;
|
import net.sf.json.JSONObject;
|
import org.yeshi.utils.StringUtil;
|
import org.yeshi.utils.TimeUtil;
|
|
import java.math.BigDecimal;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
//淘宝客API接口
|
public class TaoKeApiUtil {
|
|
public static String getTKToken(String logo, String text, String url) {
|
if (text == null)
|
return null;
|
if (text.length() < 5)
|
text = "好货:" + text;
|
|
Map<String, String> map = new HashMap<>();
|
map.put("method", "taobao.tbk.tpwd.create");
|
map.put("url", url);
|
map.put("text", text);
|
map.put("logo", logo);
|
String resultStr = TaoKeBaseUtil.baseRequestForThreeTimes(map, true);
|
|
JSONObject data = JSONObject.fromObject(resultStr);
|
if (data.optJSONObject("tbk_tpwd_create_response").optJSONObject("data") != null)
|
return data.optJSONObject("tbk_tpwd_create_response").optJSONObject("data").optString("model");
|
return null;
|
}
|
|
|
// 淘礼金创建
|
//{"result":{"msg_code":"FAIL_CHECK_ITEM_DAILY_SEND_NUM_CHECK_ERROR","msg_info":"今日该商品淘礼金创建数已超上限,请您明日再试","success":false},"request_id":"10p30v02qadkq"}
|
//{"result":{"msg_code":"ASSET_ACCOUNT_BALANCE_NOT_ENOUGH","msg_info":"账户预算不足","success":false},"request_id":"5caz9izr7jqp"}
|
public static TaoLiJinDTO createTaoLiJin(Long auctionId, String name, BigDecimal perface, int totalNum,
|
Date sendStartTime, Date sendEndTime, Date useStartTime, Date useEndTime, TaoKeAppInfo app)
|
throws TaoLiJinCreateException, 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 = TaoKeBaseUtil.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 {
|
}
|
|
// 接口返回异常
|
String msgCode = root.optJSONObject("result").optString("msg_code");
|
if (!StringUtil.isNullOrEmpty(msgCode)) {
|
switch (msgCode) {
|
case "FAIL_BIZ_ITEM_FORBIDDEN":
|
throw new TaoLiJinCreateException(TaoLiJinCreateException.CODE_TLJ_FORBIDDEN, "该商品不支持创建淘礼金红包");
|
case "FAIL_BIZ_ACCOUNT_UN_PAID":
|
case "PRE_FREEZE_ASSET_ACCOUNT_ERROR":
|
throw new TaoLiJinCreateException(TaoLiJinCreateException.CODE_TLJ_NO_MONEY, "官方玩法钱包余额不足");
|
default:
|
throw new TaoKeApiException(Integer.parseInt(msgCode), root.toString());
|
}
|
}
|
}
|
} catch (TaoKeApiException e) {
|
throw e;
|
}
|
|
return null;
|
}
|
|
/**
|
* 获取淘宝系统时间
|
*
|
* @return
|
*/
|
public static Date getTaoBaoSystemTime() {
|
Map<String, String> map = new HashMap<>();
|
map.put("method", "taobao.time.get");
|
try {
|
JSONObject json = TaoKeBaseUtil.baseRequest(map, false);
|
String time = json.optJSONObject("time_get_response").optString("time");
|
return new Date(TimeUtil.convertToTimeTemp(time, "yyyy-MM-dd HH:mm:ss"));
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
}
|