admin
2022-04-16 04f09e52ffd4681bdfd85e51acd3da0d1280c3d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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;
    }
 
 
}