admin
2024-01-23 81da61b828e29b7745e1382dfbbaeb685dc083ef
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.yeshi.fanli.util.goods.douyin;
 
import com.yeshi.fanli.dto.suning.SuningGoodsInfo;
import com.yeshi.fanli.util.goods.douyin.vo.DYGoods;
import com.yeshi.fanli.util.goods.douyin.vo.DYGoodsDetail;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.yeshi.utils.BigDecimalUtil;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.MoneyBigDecimalUtil;
 
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLDecoder;
 
/**
 * @author hxh
 * @title: DYUtil
 * @description: 抖音帮助类
 * @date 2022/10/8 14:39
 */
public class DYUtil {
 
    public static String createFanLiExtraInfo(Long uid) {
        return "b_" + uid;
    }
 
    public static String createShareExtraInfo(Long uid) {
        return "s_" + uid;
    }
 
    public static String getTypeFromExtraInfo(String extra_info) {
        if (extra_info != null && extra_info.startsWith("s_")) {
            return "share";
        } else {
            return "buy";
        }
    }
 
    public static Long getUid(String extra_info) {
        if (extra_info == null || extra_info.indexOf("_") < 0) {
            return null;
        }
        return Long.parseLong(extra_info.split("_")[1].trim());
    }
 
 
    public static BigDecimal getCosRatio(DYGoods goods) {
        return new BigDecimal(goods.getCos_ratio()).divide(new BigDecimal(10000), 4, RoundingMode.FLOOR);
    }
 
    public static BigDecimal getCosRatio(DYGoodsDetail goods) {
        return new BigDecimal(goods.getCos_ratio()).divide(new BigDecimal(10000), 4, RoundingMode.FLOOR);
    }
 
 
    public static BigDecimal getGoodsFanLiMoney(DYGoodsDetail goods, BigDecimal rate) {
 
        BigDecimal hundred = new BigDecimal(100);
        rate = MoneyBigDecimalUtil.div(rate, hundred);
 
        BigDecimal afterUseCouponPrice = MoneyBigDecimalUtil.div(new BigDecimal(goods.getPrice()), hundred);
        if (goods.getCoupon_price() > 0) {
            afterUseCouponPrice = MoneyBigDecimalUtil.div(new BigDecimal(goods.getCoupon_price()), hundred);
        }
 
        BigDecimal commission = MoneyBigDecimalUtil.mul(afterUseCouponPrice, getCosRatio(goods));
 
        BigDecimal money = MoneyBigDecimalUtil.mul(commission,
                rate);
        return BigDecimalUtil.getWithNoZera(money).setScale(2);
    }
 
    public static BigDecimal getGoodsFanLiMoney(DYGoods goods, BigDecimal rate) {
 
        BigDecimal hundred = new BigDecimal(100);
        rate = MoneyBigDecimalUtil.div(rate, hundred);
 
        BigDecimal afterUseCouponPrice = getCouponPrice(goods);
 
        BigDecimal commission = MoneyBigDecimalUtil.mul(afterUseCouponPrice, getCosRatio(goods));
 
        BigDecimal money = MoneyBigDecimalUtil.mul(commission,
                rate);
        return BigDecimalUtil.getWithNoZera(money).setScale(2);
    }
 
 
    /**
     * @return java.math.BigDecimal
     * @author hxh
     * @description 获取券后价
     * @date 18:09 2022/10/8
     * @param: goods
     **/
    public static BigDecimal getCouponPrice(DYGoods goods) {
        BigDecimal price = new BigDecimal(goods.getPrice()).divide(new BigDecimal(100), 2, RoundingMode.FLOOR);
        if (goods.getCoupon_price() > 0) {
            price = new BigDecimal(goods.getCoupon_price()).divide(new BigDecimal(100), 2, RoundingMode.FLOOR);
        }
        return price;
    }
 
 
    /**
     * @return java.math.BigDecimal
     * @author hxh
     * @description 获取券后价
     * @date 18:09 2022/10/8
     * @param: goods
     **/
    public static BigDecimal getCouponPrice(DYGoodsDetail goods) {
        BigDecimal price = new BigDecimal(goods.getPrice()).divide(new BigDecimal(100), 2, RoundingMode.FLOOR);
        if (goods.getCoupon_price() > 0) {
            price = new BigDecimal(goods.getCoupon_price()).divide(new BigDecimal(100), 2, RoundingMode.FLOOR);
        }
        return price;
    }
 
    /**
     * @return java.lang.String
     * @author hxh
     * @description 从抖音短链中解析商品ID
     * @date 18:29 2022/10/8
     * @param: url
     **/
    public static String parseProdectIdFromLink(String url) {
        if (url == null || !url.startsWith("https://v.douyin.com/")) {
            return null;
        }
 
        String id = CSJCPSApiUtil.parseProductId(url);
        return id;
//
//        HttpClient client = new HttpClient();
//        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
//        PostMethod pm = new PostMethod(url);
//        try {
//            client.executeMethod(pm);
//            Header header = pm.getResponseHeader("location");
//            if (header == null) {
//                return null;
//            }
//            if (header.getValue() == null) {
//                return null;
//            }
//            if (!header.getValue().contains("detail/index.html")) {
//                return null;
//            }
//            String value = URLDecoder.decode(header.getValue(), "UTF-8");
//            value = value.split("\\?")[1];
//            String[] results = value.split("&");
//            for (String r : results) {
//                String key = r.split("=")[0].trim();
//                String val = r.split("=")[1];
//                if (!key.equalsIgnoreCase("product_id") && !key.equalsIgnoreCase("id")) {
//                    continue;
//                }
//                return val.trim();
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        return null;
    }
 
    public static BigDecimal getPrice(int money) {
        return new BigDecimal(money).divide(new BigDecimal(100), 2, RoundingMode.FLOOR);
    }
 
 
    public static void main(String[] args) {
        parseProdectIdFromLink("https://v.douyin.com/M8dkcv4");
 
    }
 
}