admin
2020-07-14 eec7e789a87863c25d92c10ad5dfc22ad80c448d
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package com.yeshi.fanli.controller.client.v1;
 
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.taobao.TbImgUtil;
 
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.taobao.SearchFilter;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult;
import com.yeshi.fanli.entity.taobao.dataoke.DaTaoKeDetailV2;
import com.yeshi.fanli.exception.taobao.TaoBaoTokenParseException;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionConfigService;
import com.yeshi.fanli.service.inter.taobao.dataoke.DaTaoKeGoodsDetailV2Service;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.manger.goods.tb.TaoBaoTokenAuctionIdMapManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.Utils;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping("api/v1/dynamic")
public class DynamicRecommendController {
 
    @Resource
    private HongBaoManageService hongBaoManageService;
 
    @Resource
    private UserInfoService userInfoService;
 
    @Resource
    private ConfigService configService;
 
    @Resource
    private TaoBaoUnionConfigService taoBaoUnionConfigService;
 
    @Resource
    private DaTaoKeGoodsDetailV2Service daTaoKeGoodsDetailV2Service;
 
    @Resource
    private TaoBaoTokenAuctionIdMapManager taoBaoTokenAuctionIdMapManager;
 
    /**
     * 获取商品信息
     * 
     * @param acceptData
     * @param url
     *            商品链接
     * @param out
     */
    @RequestMapping(value = "getGoodsInfo", method = RequestMethod.POST)
    public void getGoodsInfo(AcceptData acceptData, String url, PrintWriter out) {
 
        TaoBaoGoodsBrief tb = null;
        String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
 
        Pattern p = Pattern.compile(URL_REGEX);
        Matcher matcher = p.matcher(url);
        if (!matcher.find()) {// 不包含链接
            out.print(JsonUtil.loadFalseResult("暂不支持该类型!"));
            return;
        }
        url = matcher.group();
        if (url.contains("ju.taobao.com") || url.contains(".juhuasuan.com")) {// 聚划算
            int index = url.indexOf("item_id");
            if (index < 0) {
                out.println(JsonUtil.loadFalseResult("暂未找到该商品,请稍后再试!"));
                return;
            }
            url = url.substring(index);
            int last = url.indexOf("&");
            String id = "";
            if (last > 0)
                id = url.substring(url.indexOf("=") + 1, url.indexOf("&"));
            else {
                id = url.substring(url.indexOf("=" + 1));
            }
            tb = TaoBaoUtil.isAlimama(id);
            if (tb == null) {
                tb = TaoBaoUtil.parsePhoneTmAndTb(id);
            }
        } else if (url.contains("http://zmnxbc.com")) { // 手机端天猫APP分享
            tb = TaoBaoUtil.parsePhoneShareUrlByTM(url);
        } /*
             * else if(url.contains("c.b1yt.com") || url.contains("m.qtilg.com")
             * || url.contains("z.icyad.com")){ //手机端淘宝和一淘APP分享
             * tb=TaoBaoUtil.parsePhoneShareUrlByTB(url); }
             */else if (url.contains("h5.m.taobao") || url.contains("detail.m.tmall") || url.contains("item.taobao")
                || url.contains("detail.tmall")) { // 手机页面和电脑页面
            Map<String, String> map = Utils.parseURL(url);
            String id = "";
            id = map.get("id").replace("}", "");
            tb = TaoBaoUtil.isAlimama(id);
            if (tb == null) {
                tb = TaoBaoUtil.parsePhoneTmAndTb(id);
            }
        } else {
            tb = TaoBaoUtil.parsePhoneShareUrlByTB(url);
        }
 
        if (tb == null) {
            out.println(JsonUtil.loadFalseResult("暂未找到该商品,请稍后再试!"));
            return;
        }
        JSONObject data = new JSONObject();
        JSONObject taoBaoGoodsJSON = new JSONObject();
        taoBaoGoodsJSON.put("title", tb.getTitle());
        taoBaoGoodsJSON.put("zkPrice", tb.getZkPrice());
        taoBaoGoodsJSON.put("auctionId", tb.getAuctionId());
        taoBaoGoodsJSON.put("url", "http://item.taobao.com/item.htm?id=" + tb.getAuctionId());
        data.put("taoBaoGoodsBrief", taoBaoGoodsJSON);
        data.put("tbImgs", tb.getImgList());
        out.print(JsonUtil.loadTrueResult(data));
    }
 
    /**
     * 粘贴板信息推荐
     * 
     * @param acceptData
     * @param url
     *            商品链接
     * @param out
     */
    @RequestMapping(value = "getNewGoodsInfo", method = RequestMethod.POST)
    public void getNewGoodsInfo(AcceptData acceptData, String text, PrintWriter out) {
        if (StringUtil.isNullOrEmpty(text)) {
            out.print(JsonUtil.loadFalseResult("值为空"));
            return;
        }
 
        if (text.length() > 256) {
            out.print(JsonUtil.loadFalseResult("值过长"));
            return;
        }
 
        TaoBaoGoodsBrief tb = null;
        String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
 
        Pattern p = Pattern.compile(URL_REGEX);
        Matcher matcher = p.matcher(text);
        if (!matcher.find()) {// 不包含链接
            // 解析淘口令
            if (analysisTaoToken(text, out))
                return;
            // 商品详情
            // 发现
            // 没有链接,标题过长也不处理
            if (text.startsWith("【") && text.contains("】")) {
                int end = text.indexOf("】");
                if (end > 2)
                    text = text.substring(1, end);
            }
 
            LogHelper.test("根据粘贴板推荐:" + text);
 
            if (text.length() > 80) {
                out.print(JsonUtil.loadFalseResult("值过长"));
                return;
            }
 
            SearchFilter sf = new SearchFilter();
            sf.setKey(text);
            JSONObject root = new JSONObject();
 
            TaoBaoSearchResult result = TaoBaoUtil.search(sf);
            if (result != null && result.getTaoBaoGoodsBriefs() != null && result.getTaoBaoGoodsBriefs().size() > 0)
                for (TaoBaoGoodsBrief goods : result.getTaoBaoGoodsBriefs()) {
                    // 是属于淘宝联盟商品
                    if (goods.getTitle().equalsIgnoreCase(text)) {
                        root.put("type", 2);
                        JSONObject data = new JSONObject();
                        data.put("title", goods.getTitle());
                        root.put("data", data);
                        out.print(JsonUtil.loadTrueResult(root));
                        return;
                    }
                }
 
            // 查询大淘客标题
            List<DaTaoKeDetailV2> list = daTaoKeGoodsDetailV2Service.listByDtitle(text);
            if (list != null && list.size() > 0) {
                root.put("type", 2);
                JSONObject data = new JSONObject();
                data.put("title", list.get(0).getDtitle());
                root.put("data", data);
                out.print(JsonUtil.loadTrueResult(root));
                return;
            }
 
            out.print(JsonUtil.loadFalseResult("暂不支持该类型!"));
            return;
        }
 
        text = matcher.group();
        if (text.contains("ju.taobao.com") || text.contains(".juhuasuan.com")) {// 聚划算
            int index = text.indexOf("item_id");
            if (index < 0) {
                out.println(JsonUtil.loadFalseResult("暂未找到该商品,请稍后再试!"));
                return;
            }
            text = text.substring(index);
            int last = text.indexOf("&");
            String id = "";
            if (last > 0)
                id = text.substring(text.indexOf("=") + 1, text.indexOf("&"));
            else {
                id = text.substring(text.indexOf("=" + 1));
            }
            tb = TaoBaoUtil.isAlimama(id);
            if (tb == null) {
                tb = TaoBaoUtil.parsePhoneTmAndTb(id);
            }
        } else if (text.contains("http://zmnxbc.com")) { // 手机端天猫APP分享
            tb = TaoBaoUtil.parsePhoneShareUrlByTM(text);
        } else if (text.contains("h5.m.taobao") || text.contains("detail.m.tmall") || text.contains("item.taobao")
                || text.contains("detail.tmall")) { // 手机页面和电脑页面
            Map<String, String> map = Utils.parseURL(text);
            String id = "";
            id = map.get("id").replace("}", "");
            tb = TaoBaoUtil.isAlimama(id);
            if (tb == null) {
                tb = TaoBaoUtil.parsePhoneTmAndTb(id);
            }
        } else {
            tb = TaoBaoUtil.parsePhoneShareUrlByTB(text);
        }
 
        if (tb == null) {
            out.println(JsonUtil.loadFalseResult("暂未找到该商品,请稍后再试!"));
            return;
        }
        JSONObject data = new JSONObject();
        JSONObject taoBaoGoodsJSON = new JSONObject();
        taoBaoGoodsJSON.put("title", tb.getTitle());
        taoBaoGoodsJSON.put("zkPrice", tb.getZkPrice());
        taoBaoGoodsJSON.put("auctionId", tb.getAuctionId());
        taoBaoGoodsJSON.put("url", "http://item.taobao.com/item.htm?id=" + tb.getAuctionId());
        data.put("taoBaoGoodsBrief", taoBaoGoodsJSON);
        if (!StringUtil.isNullOrEmpty(tb.getPictUrl())) {
            if (tb.getImgList() == null)
                tb.setImgList(new ArrayList<>());
            tb.getImgList().add(0, TbImgUtil.getTBSize220Img(tb.getPictUrl()));
        }
        data.put("tbImgs", tb.getImgList());
        JSONObject root = new JSONObject();
        root.put("type", 1);
        root.put("data", data);
 
        out.print(JsonUtil.loadTrueResult(root));
    }
 
    /**
     * 解析淘口令
     * 
     * @param text
     * @param out
     * @return
     */
    private boolean analysisTaoToken(String text, PrintWriter out) {
        List<String> tokenList = TaoBaoUtil.getTokenListFromText(text);
        if (tokenList != null && tokenList.size() > 0) {
            String token = tokenList.get(0);
            Long auctionId = taoBaoTokenAuctionIdMapManager.getAuctionIdByToken(token);
            if (auctionId != null) {
                TaoBaoGoodsBrief tb = null;
                try {
                    tb = TaoKeApiUtil.getSimpleGoodsInfo(auctionId);
                } catch (TaobaoGoodsDownException e) {
                    e.printStackTrace();
                }
 
                if (tb != null) {
                    JSONObject data = new JSONObject();
                    JSONObject taoBaoGoodsJSON = new JSONObject();
                    taoBaoGoodsJSON.put("title", tb.getTitle());
                    taoBaoGoodsJSON.put("zkPrice", tb.getZkPrice());
                    taoBaoGoodsJSON.put("auctionId", tb.getAuctionId());
                    taoBaoGoodsJSON.put("url", "http://item.taobao.com/item.htm?id=" + tb.getAuctionId());
                    data.put("taoBaoGoodsBrief", taoBaoGoodsJSON);
                    if (!StringUtil.isNullOrEmpty(tb.getPictUrl())) {
                        if (tb.getImgList() == null)
                            tb.setImgList(new ArrayList<>());
                        tb.getImgList().add(0, TbImgUtil.getTBSize220Img(tb.getPictUrl()));
                    }
                    data.put("tbImgs", tb.getImgList());
                    JSONObject root = new JSONObject();
                    root.put("type", 1);
                    root.put("data", data);
                    out.print(JsonUtil.loadTrueResult(root));
                    return true;
                }
            }
        }
        return false;
    }
 
}