From 5e7b0ed4a154ad067cbcf4aa1a1c7cce32f9864c Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期五, 26 四月 2024 18:02:17 +0800 Subject: [PATCH] 唯品会链接解析升级 --- fanli/src/main/java/com/yeshi/fanli/service/manger/ClipboardAnalysisManager.java | 1206 +++++++++++++++++++++++++++++++++++---------------------- 1 files changed, 743 insertions(+), 463 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/service/manger/ClipboardAnalysisManager.java b/fanli/src/main/java/com/yeshi/fanli/service/manger/ClipboardAnalysisManager.java index d3e0ec7..a249d77 100644 --- a/fanli/src/main/java/com/yeshi/fanli/service/manger/ClipboardAnalysisManager.java +++ b/fanli/src/main/java/com/yeshi/fanli/service/manger/ClipboardAnalysisManager.java @@ -1,463 +1,743 @@ -package com.yeshi.fanli.service.manger; - -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import javax.annotation.Resource; - -import org.springframework.stereotype.Component; -import org.yeshi.utils.HttpUtil; -import org.yeshi.utils.NumberUtil; - -import com.yeshi.fanli.dto.douyin.DouYinGoods; -import com.yeshi.fanli.dto.pdd.PDDGoodsDetail; -import com.yeshi.fanli.entity.goods.CommonGoods; -import com.yeshi.fanli.entity.jd.JDGoods; -import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief; -import com.yeshi.fanli.exception.taobao.TaoBaoTokenParseException; -import com.yeshi.fanli.exception.user.TokenRecordException; -import com.yeshi.fanli.service.inter.user.TokenRecordService; -import com.yeshi.fanli.service.manger.goods.jd.JDGoodsLinkParseManager; -import com.yeshi.fanli.util.DouYinUtil; -import com.yeshi.fanli.util.StringUtil; -import com.yeshi.fanli.util.TokenUtil; -import com.yeshi.fanli.util.Utils; -import com.yeshi.fanli.util.VersionUtil; -import com.yeshi.fanli.util.factory.CommonGoodsFactory; -import com.yeshi.fanli.util.jd.JDApiUtil; -import com.yeshi.fanli.util.jd.JDUtil; -import com.yeshi.fanli.util.pinduoduo.PinDuoDuoApiUtil; -import com.yeshi.fanli.util.pinduoduo.PinDuoDuoUtil; -import com.yeshi.fanli.util.taobao.TaoBaoUtil; -import com.yeshi.fanli.util.taobao.TaoKeApiUtil; -import com.yeshi.fanli.vo.msg.TokenVO; -import com.yeshi.fanli.vo.search.GoodsDocParseResultVO; - -import net.sf.json.JSONObject; - -/** - * 绮樿创鏉胯В鏋� - * @author Administrator - * - */ -@Component -public class ClipboardAnalysisManager { - - @Resource - private JDGoodsLinkParseManager jdGoodsLinkParseManager; - - @Resource - private TokenRecordService tokenRecordService; - - /** - * @Title: parseContentType - * @Description: 瑙f瀽鏂囨湰鐨勭被鍨� - * @param text - * @return - * Set<ClipboardContentType> 杩斿洖绫诲瀷 - * @throws - */ - public static Set<ClipboardContentType> parseContentType(String originText) { - Set<ClipboardContentType> typeSet = new HashSet<>(); - StringBuffer textBuffer = new StringBuffer(originText); - String text = textBuffer.toString(); - // 鑷湁鍙d护 - String parseToken = TokenUtil.parseToken(text); - if (!StringUtil.isNullOrEmpty(parseToken)) { - typeSet.add(ClipboardContentType.systemToken); - text = text.replace(parseToken, ""); - } - - // 閾炬帴 - 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); - while (matcher.find()) { - String url = matcher.group(); - if (!StringUtil.isNullOrEmpty(url)) { - typeSet.add(ClipboardContentType.link); - text = text.replace(url, ""); - } - } - - // 娣樺彛浠� - List<String> taoBaoTokenList = TaoBaoUtil.getTokenListFromText(text); - if (taoBaoTokenList != null && taoBaoTokenList.size() > 0) { - typeSet.add(ClipboardContentType.taoBaoToken); - for (String token : taoBaoTokenList) { - text = text.replace(token, ""); - } - } - - // 鏂囨湰 - if (text != null && text.trim().length() > 0) { - typeSet.add(ClipboardContentType.text); - } - - return typeSet; - } - - /** - * - * @Title: parse - * @Description: 瑙f瀽 - * @param text - * @param typeSet - * void 杩斿洖绫诲瀷 - * @throws - */ - public void parse(String platform, String version, String text, Long uid, IClipboardAnalysisResult resultListener) { - if (text == null) - return; - text = text.trim(); - - Set<ClipboardContentType> typeSet = parseContentType(text); - - // 绯荤粺鍙d护 - if (typeSet.contains(ClipboardContentType.systemToken)) { - try { - TokenVO tokenVO = tokenRecordService.discernToken(text, uid, platform, version); - resultListener.onResult(tokenVO); - } catch (TokenRecordException e) { - if (e.getCode() == 1001) { // 闇�瑕佺敤鎴风櫥褰� - resultListener.needLogin(e.getMsg()); - } else { - resultListener.none(); - } - } - return; - } - - // 鍙湁閾炬帴 娣樺疂閾炬帴/浜笢閾炬帴/鎷煎澶氶摼鎺� - if (typeSet.size() == 1) { - if (typeSet.contains(ClipboardContentType.link)) {// 绾摼鎺� - // 瑙f瀽鍟嗗搧 - CommonGoods goods = parseLink(text); - if (goods == null) { - resultListener.none(); - } else { - resultListener.onResult(goods); - } - - } else if (typeSet.contains(ClipboardContentType.taoBaoToken)) {// 绾窐鍙d护 - - try { - CommonGoods commonGoods = parseTaoBaoToken(text); - if (commonGoods != null) - resultListener.onResult(commonGoods); - else - resultListener.onResult(text); - - } catch (TaoBaoTokenParseException e1) { - resultListener.onResult(text); - } - - } else {// 绾枃鏈� - // 鏂囨湰鎺ㄨ崘 - if (text.length() > 256) - resultListener.none(); - else - resultListener.onResult(text); - } - } else if (typeSet.size() == 2) { - List<String> urlList = HttpUtil.getUrlListFromText(text); - List<String> tokenList = TaoBaoUtil.getTokenListFromText(text); - - String link = null; - String token = null; - - if (typeSet.contains(ClipboardContentType.link) && typeSet.contains(ClipboardContentType.taoBaoToken)) {// 閾炬帴+鍙d护 - if (text.indexOf(urlList.get(0)) < text.indexOf(tokenList.get(0)))// 绗竴涓摼鎺ュ湪绗竴涓彛浠ゅ墠闈� - link = urlList.get(0); - else// 绗竴涓彛浠ゅ湪绗竴涓摼鎺ュ墠闈� - token = tokenList.get(0); - - } else if (typeSet.contains(ClipboardContentType.link) && typeSet.contains(ClipboardContentType.text)) {// 閾炬帴+鏂囨湰 - link = urlList.get(0); - - } else {// 鍙d护+鏂囨湰 - token = tokenList.get(0); - } - - if (!StringUtil.isNullOrEmpty(link)) { - CommonGoods goods = parseLink(urlList.get(0)); - if (goods == null || goods.getGoodsId() == null) { - resultListener.onResult(new GoodsDocParseResultVO(text, urlList.get(0))); - } else { - if (VersionUtil.greaterThan_2_0_7(platform, version)) { - resultListener.onResult(new GoodsDocParseResultVO(text, goods)); - } else { - resultListener.onResult(goods); - } - } - } else { - CommonGoods goods = null; - try { - goods = parseTaoBaoToken(token); - } catch (TaoBaoTokenParseException e) { - resultListener.onResult(new GoodsDocParseResultVO(text)); - return; - } - if (goods != null && goods.getGoodsId() != null) { - resultListener.onResult(new GoodsDocParseResultVO(text, goods)); - } else { - resultListener.onResult(new GoodsDocParseResultVO(text)); - } - - } - - } else if (typeSet.size() == 3) {// 鍙d护+閾炬帴+鏂囨湰 - List<String> tokenList = TaoBaoUtil.getTokenListFromText(text); - List<String> urlList = HttpUtil.getUrlListFromText(text); - if (text.contains("銆�") && tokenList != null && tokenList.size() == 1 && urlList != null - && urlList.size() == 1) {// Android娣樺疂閾炬帴澶嶅埗 - CommonGoods goods = null; - try { - goods = parseTaoBaoToken(tokenList.get(0)); - } catch (TaoBaoTokenParseException e1) { - e1.printStackTrace(); - } - if (goods == null) { - if (text.trim().indexOf("銆�") > -1 && text.trim().indexOf("銆�") > 0) { - // 鎴彇鏍囬 - String title = text.trim().substring(text.trim().indexOf("銆�")+1, text.trim().lastIndexOf("銆�")); - goods = new CommonGoods(); - goods.setTitle(title); - goods.setPicture("http://"); - } - } - - if (goods != null) { - resultListener.onResult(goods); - } - - } else {// 鏂囨 - - String link = null; - String token = null; - - if (text.indexOf(urlList.get(0)) < text.indexOf(tokenList.get(0)))// 绗竴涓摼鎺ュ湪绗竴涓彛浠ゅ墠闈� - link = urlList.get(0); - else// 绗竴涓彛浠ゅ湪绗竴涓摼鎺ュ墠闈� - token = tokenList.get(0); - - if (!StringUtil.isNullOrEmpty(link)) { - CommonGoods goods = parseLink(urlList.get(0)); - if (goods == null || goods.getGoodsId() == null) { - resultListener.onResult(new GoodsDocParseResultVO(text, urlList.get(0))); - } else { - resultListener.onResult(new GoodsDocParseResultVO(text, goods)); - } - } else { - CommonGoods goods = null; - try { - goods = parseTaoBaoToken(token); - if (goods != null && goods.getGoodsId() != null) { - resultListener.onResult(new GoodsDocParseResultVO(text, goods)); - } else { - resultListener.onResult(new GoodsDocParseResultVO(text)); - } - } catch (TaoBaoTokenParseException e) { - resultListener.onResult(new GoodsDocParseResultVO(text)); - } - } - } - return; - } - } - - /** - * - * @Title: parseLink - * @Description: 瑙f瀽鍗曢摼鎺� - * @param link - * @return - * CommonGoods 杩斿洖绫诲瀷 - * @throws - */ - public CommonGoods parseLink(String link) { - - TaoBaoGoodsBrief tb = null; - if (link.startsWith("https://a.m.taobao.com/i"))// 娣樺疂璐﹀彿鏈櫥褰曠姸鎬� - { - link = link.substring("https://a.m.taobao.com/i".length(), link.indexOf(".htm")); - if (NumberUtil.isNumeric(link)) { - tb = TaoBaoUtil.isAlimama(link); - } - } else if (link.contains("ju.taobao.com") || link.contains(".juhuasuan.com")) {// 鑱氬垝绠� - int index = link.indexOf("item_id"); - if (index >= 0) { - link = link.substring(index); - int last = link.indexOf("&"); - String id = ""; - if (last > 0) - id = link.substring(link.indexOf("=") + 1, link.indexOf("&")); - else { - id = link.substring(link.indexOf("=" + 1)); - } - tb = TaoBaoUtil.isAlimama(id); - if (tb == null) { - tb = new TaoBaoGoodsBrief(Long.parseLong(id)); - } - } - } else if (link.contains("http://zmnxbc.com")) { // 鎵嬫満绔ぉ鐚獳PP鍒嗕韩 - tb = TaoBaoUtil.parsePhoneShareUrlByTM(link); - } else if (link.contains("h5.m.taobao") || link.contains("detail.m.tmall") || link.contains("item.taobao") - || link.contains("detail.tmall")) { // 鎵嬫満椤甸潰鍜岀數鑴戦〉闈� - Map<String, String> map = Utils.parseURL(link); - String id = ""; - id = map.get("id").replace("}", ""); - tb = TaoBaoUtil.isAlimama(id); - if (tb == null) { - tb = new TaoBaoGoodsBrief(Long.parseLong(id)); - } - } else if (link.contains("v.douyin.com")) { // 鎶栭煶 - tb = analysisDouYin(link); - if (tb != null) { - if (!StringUtil.isNullOrEmpty(tb.getAuctionUrl())) {// 鎶栭煶瑙f瀽鍒版窐瀹濆晢鍝侀摼鎺� - return parseLink(tb.getAuctionUrl()); - } - } - } else { - tb = TaoBaoUtil.parsePhoneShareUrlByTB(link); - } - - // 娌¤В鏋愬埌娣樺疂鐩稿叧鍟嗗搧 - if (tb == null) { - String goodsId = TaoBaoUtil.getGoodsIdByPhoneShareUrl(link); - if (!StringUtil.isNullOrEmpty(goodsId)) { - tb = TaoBaoUtil.isAlimama(goodsId); - if (tb == null) { - tb = new TaoBaoGoodsBrief(Long.parseLong(goodsId)); - } - } - } - - CommonGoods commonGoods = null; - - // 娣樺疂鍟嗗搧瑙f瀽缁撴潫 - if (tb != null) { - if (StringUtil.isNullOrEmpty(tb.getTitle())) { - tb = TaoBaoUtil.getTaoBaoGoodsBriefNotInPub(tb.getId()); - if (tb != null) { - commonGoods = new CommonGoods(); - commonGoods.setPicture(tb.getPictUrl()); - commonGoods.setTitle(tb.getTitle()); - } - } else { - commonGoods = CommonGoodsFactory.create(tb); - } - } else { - // 瑙f瀽鍏朵粬鍟嗗搧 - String jdId = JDUtil.getJDGoodsId(link); - - // 寰俊閾炬帴 - if (StringUtil.isNullOrEmpty(jdId)) { - jdId = JDUtil.getJDGoodsIdByWeiXin(link); - } - - // 棰嗗埜鐭繛鎺� - if (StringUtil.isNullOrEmpty(jdId) && link.contains("u.jd.com")) { - jdId = jdGoodsLinkParseManager.parseGoodsIdByJDShortUrl(link); - } - - if (!StringUtil.isNullOrEmpty(jdId)) { - JDGoods goods = JDApiUtil.getGoodsDetail(Long.parseLong(jdId)); - if (goods != null) { - // 楂樼骇鎺ュ彛 -- 淇℃伅鏇村畬鏁� - JDGoods jdGoods = JDApiUtil.queryGoodsDetail(Long.parseLong(jdId)); - if (jdGoods != null) { - commonGoods = CommonGoodsFactory.create(jdGoods); - } else { - commonGoods = CommonGoodsFactory.create(goods); - } - } else { - goods = JDUtil.getSimpleGoodsInfo(jdId); - if (goods != null) { - commonGoods = new CommonGoods(); - commonGoods.setTitle(goods.getSkuName()); - commonGoods.setPicture(goods.getPicUrl()); - } - } - } else { - String pddId = PinDuoDuoUtil.getPDDGoodsId(link); - if (!StringUtil.isNullOrEmpty(pddId)) { - PDDGoodsDetail goods = PinDuoDuoApiUtil.getGoodsDetail(Long.parseLong(pddId)); - if (goods != null) { - commonGoods = CommonGoodsFactory.create(goods); - } else { - goods = PinDuoDuoUtil.getPDDGoodsInfo(pddId); - if (goods != null) { - commonGoods = new CommonGoods(); - commonGoods.setTitle(goods.getGoodsName()); - commonGoods.setPicture(goods.getGoodsThumbnailUrl()); - } - } - } - } - } - return commonGoods; - } - - private CommonGoods parseTaoBaoToken(String token) throws TaoBaoTokenParseException { - - Long auctionId = TaoKeApiUtil.tokenConvertAuctionId(token); - if (auctionId != null) {// - TaoBaoGoodsBrief goods = TaoBaoUtil.isAlimama(auctionId + ""); - if (goods == null) { - goods = TaoBaoUtil.getTaoBaoGoodsBriefNotInPub(auctionId); - } - if (goods != null) { - CommonGoods commonGoods = CommonGoodsFactory.create(goods); - return commonGoods; - } else - return null; - } else { - return null; - } - } - - /** - * - * @Title: analysisDouYin - * @Description: 鎶栭煶瑙f瀽 - * @param link - * @return - * CommonGoods 杩斿洖绫诲瀷 - * @throws - */ - private TaoBaoGoodsBrief analysisDouYin(String link) { - String location = HttpUtil.getLocation(link); - if (!StringUtil.isNullOrEmpty(location)) { - if (location.contains("taobao.com")) { // 娣樺疂鍟嗗搧 - String realUrl = TaoBaoUtil.getRealUrl(location); - if (!StringUtil.isNullOrEmpty(realUrl)) { - TaoBaoGoodsBrief goods = new TaoBaoGoodsBrief(); - goods.setAuctionUrl(realUrl); - return goods; - } - } else if (location.contains("haohuo.jinritemai.com")) { // 鎶栭煶鍟嗗搧 - DouYinGoods goods = DouYinUtil.getGoodsInfo(location); - JSONObject goodsJSON = new JSONObject(); - goodsJSON.put("title", goods.getName()); - goodsJSON.put("pictUrl", goods.getImg()); - - TaoBaoGoodsBrief commonGoods = new TaoBaoGoodsBrief(); - commonGoods.setPictUrl(goods.getImg()); - commonGoods.setTitle(goods.getName()); - return commonGoods; - } - } - return null; - } -} - -/** - * 绮樿创鏉胯В鏋愮粨鏋� - * @author Administrator - * - */ +package com.yeshi.fanli.service.manger; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.annotation.Resource; + +import com.yeshi.fanli.entity.taobao.SearchFilter; +import com.yeshi.fanli.entity.taobao.TaoBaoSearchResult; +import com.yeshi.fanli.util.*; +import com.yeshi.fanli.util.goods.douyin.CSJCPSApiUtil; +import com.yeshi.fanli.util.goods.douyin.DYUtil; +import com.yeshi.fanli.util.goods.douyin.vo.DYGoodsDetail; +import com.yeshi.fanli.util.taobao.TaoKeApiUtil; +import com.yeshi.goods.facade.utils.taobao.DaTaoKeApiUtil; +import org.jsoup.Jsoup; +import org.springframework.stereotype.Component; +import org.yeshi.utils.HttpUtil; +import org.yeshi.utils.NumberUtil; + +import com.yeshi.fanli.dto.douyin.DouYinGoods; +import com.yeshi.fanli.dto.pdd.PDDGoodsDetail; +import com.yeshi.fanli.dto.suning.SuningGoodsInfo; +import com.yeshi.fanli.dto.vip.goods.VIPGoodsInfo; +import com.yeshi.fanli.entity.goods.CommonGoods; +import com.yeshi.fanli.entity.jd.JDGoods; +import com.yeshi.goods.facade.entity.taobao.TaoBaoGoodsBrief; +import com.yeshi.fanli.exception.taobao.TaoBaoTokenParseException; +import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException; +import com.yeshi.fanli.exception.user.TokenRecordException; +import com.yeshi.fanli.service.inter.user.TokenRecordService; +import com.yeshi.fanli.service.manger.goods.jd.JDGoodsLinkParseManager; +import com.yeshi.fanli.service.manger.goods.tb.TaoBaoTokenAuctionIdMapManager; +import com.yeshi.fanli.util.factory.CommonGoodsFactory; +import com.yeshi.fanli.util.jd.JDApiUtil; +import com.yeshi.fanli.util.jd.JDUtil; +import com.yeshi.fanli.util.pinduoduo.PinDuoDuoApiUtil; +import com.yeshi.fanli.util.pinduoduo.PinDuoDuoUtil; +import com.yeshi.fanli.util.suning.SuningApiUtil; +import com.yeshi.fanli.util.suning.SuningUtil; +import com.yeshi.fanli.util.taobao.TaoBaoUtil; +import com.yeshi.fanli.util.vipshop.VipShopApiUtil; +import com.yeshi.fanli.util.vipshop.VipShopUtil; +import com.yeshi.fanli.vo.msg.TokenVO; +import com.yeshi.fanli.vo.search.GoodsDocParseResultVO; + +import net.sf.json.JSONObject; + +/** + * 绮樿创鏉胯В鏋� + * + * @author Administrator + */ +@Component +public class ClipboardAnalysisManager { + + @Resource + private JDGoodsLinkParseManager jdGoodsLinkParseManager; + + @Resource + private TokenRecordService tokenRecordService; + + @Resource + private TaoBaoTokenAuctionIdMapManager taoBaoTokenAuctionIdMapManager; + + @Resource + private RedisManager redisManager; + + /** + * @Title: parseContentType @Description: 瑙f瀽鏂囨湰鐨勭被鍨� @param text @return Set + * <ClipboardContentType> 杩斿洖绫诲瀷 @throws + */ + public static Set<ClipboardContentType> parseContentType(String originText, String platform, String version) { + Set<ClipboardContentType> typeSet = new HashSet<>(); + StringBuffer textBuffer = new StringBuffer(originText); + String text = textBuffer.toString(); + // 鑷湁鍙d护 + String parseToken = TokenUtil.parseToken(text); + if (!StringUtil.isNullOrEmpty(parseToken)) { + typeSet.add(ClipboardContentType.systemToken); + text = text.replace(parseToken, ""); + } + + // 閾炬帴 + 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); + while (matcher.find()) { + String url = matcher.group(); + if (!StringUtil.isNullOrEmpty(url)) { + typeSet.add(ClipboardContentType.link); + text = text.replace(url, ""); + } + } + + // 娣樺彛浠� + List<String> taoBaoTokenList = null; + if (VersionUtil.greaterThan_2_0_7(platform, version)) + taoBaoTokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(text); + else + taoBaoTokenList = TaoBaoUtil.getTokenListFromText(text); + if (taoBaoTokenList != null && taoBaoTokenList.size() > 0) { + typeSet.add(ClipboardContentType.taoBaoToken); + for (String token : taoBaoTokenList) { + text = text.replace(token, ""); + } + } + // 鏂囨湰 + if (text != null && text.trim().length() > 0) { + typeSet.add(ClipboardContentType.text); + } + + + return typeSet; + } + + + /** + * 鐗逛环瑙f瀽绮樿创鏉� + * 鏃犲钩鍙帮紝鏍规嵁鏍煎紡鍒ゆ柇骞冲彴 + * + * @param text + */ + public Integer tejiaParseSourceType(String text) { + if (text == null) + return null; + text = text.trim(); + //鍒ゆ柇鏄惁鏈夐摼鎺� + List<String> linkList = HttpUtil.getUrlListFromText(text); + if (linkList != null && linkList.size() > 0) { + String link = linkList.get(0); + if (TaoBaoUtil.isTaoBaoLink(link)) { + return Constant.SOURCE_TYPE_TAOBAO; + } else if (JDUtil.isJDLink(link)) { + return Constant.SOURCE_TYPE_JD; + } else if (PinDuoDuoUtil.isPDDLink(link)) { + return Constant.SOURCE_TYPE_PDD; + } else if (VipShopUtil.isVIPShopLink(link)) { + return Constant.SOURCE_TYPE_VIP; + } else if (SuningUtil.isSuningLink(link)) { + return Constant.SOURCE_TYPE_SUNING; + } + //閾炬帴闈炵數鍟嗗钩鍙伴摼鎺� + return null; + } + + //鍒ゆ柇鏄惁鐤戜技鍙d护 + if (TaoBaoUtil.isHaveNewVersionTaoBaoToken(text)) { + return Constant.SOURCE_TYPE_TAOBAO; + } + return null; + } + + /** + * @Title: parse @Description: 瑙f瀽 @param text @param typeSet void + * 杩斿洖绫诲瀷 @throws + */ + public void parse(String platform, String version, String text, Long uid, IClipboardAnalysisResult resultListener) { + if (text == null) + return; + text = text.trim(); + + Set<ClipboardContentType> typeSet = parseContentType(text, platform, version); + //鏂扮増鍙d护 + String goodsId = null; + if (text.contains("https://m.tb.cn/")) { + String tempGoodsId = DaTaoKeApiUtil.parseGoodsIdFromToken(text); + if (tempGoodsId != null) { + goodsId = tempGoodsId; + typeSet.add(ClipboardContentType.taoBaoToken); + } else { + try { + TaoKeApiUtil.TokenConvertResult result = TaoKeApiUtil.tokenConvert(text); + if (result != null) { + goodsId = result.getNum_iid(); + typeSet.add(ClipboardContentType.taoBaoToken); + } + } catch (TaoBaoTokenParseException e) { + e.printStackTrace(); + } + } + } + + // 绯荤粺鍙d护 + if (typeSet.contains(ClipboardContentType.systemToken)) { + try { + TokenVO tokenVO = tokenRecordService.discernToken(text, uid, platform, version); + resultListener.onResult(tokenVO); + } catch (TokenRecordException e) { + if (e.getCode() == 1001) { // 闇�瑕佺敤鎴风櫥褰� + resultListener.needLogin(e.getMsg()); + } else { + resultListener.none(); + } + } + return; + } + //浼樺厛瑙f瀽娣樺彛浠� + if (goodsId != null) { + TaoBaoGoodsBrief taobaoGoods =null; + try { + taobaoGoods = TaoKeApiUtil.getSimpleGoodsInfoForOwnBuy(goodsId); + } catch (TaobaoGoodsDownException e) { + e.printStackTrace(); + } + if (taobaoGoods==null) { + int startIndex = text.indexOf("銆�"); + int endIndex = text.indexOf("銆�"); + if (startIndex > 0 && endIndex > 0) { + resultListener.onResult(text.substring(startIndex+1,endIndex)); + return; + } + } else { + CommonGoods commonGoods = CommonGoodsFactory.create(taobaoGoods); + if (commonGoods != null) + resultListener.onResult(commonGoods); + else + resultListener.onResult(text); + } + return; + } + + + // 鍙湁閾炬帴 娣樺疂閾炬帴/浜笢閾炬帴/鎷煎澶氶摼鎺� + if (typeSet.size() == 1) { + if (typeSet.contains(ClipboardContentType.link)) {// 绾摼鎺� + // 瑙f瀽鍟嗗搧 + CommonGoods goods = parseLink(text); + if (goods == null) { + // 鍒ゆ柇閾炬帴涔嬩腑鏄惁鍖呭惈鍙d护 + try { + String newText = URLDecoder.decode(text, "UTF-8"); + List<String> tokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(newText); + if (tokenList != null && tokenList.size() > 0) { + try { + goods = parseTaoBaoToken(tokenList.get(0)); + } catch (TaoBaoTokenParseException e) { + } + if (goods != null && goods.getGoodsId() != null) { + resultListener.onResult(goods); + return; + } + } + + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + //TODO 瑙f瀽閾炬帴涓殑title +// Jsoup.connect(text).get(); + + + resultListener.none(); + } else { + resultListener.onResult(goods); + } + + } else if (typeSet.contains(ClipboardContentType.taoBaoToken)) {// 绾窐鍙d护 + + try { + CommonGoods commonGoods = parseTaoBaoToken(text); + if (commonGoods != null) + resultListener.onResult(commonGoods); + else + resultListener.onResult(text); + + } catch (TaoBaoTokenParseException e1) { + resultListener.onResult(text); + } + + } else {// 绾枃鏈� + // 鏂囨湰鎺ㄨ崘 + if (text.length() > 256) + resultListener.none(); + else + resultListener.onResult(text); + } + } else if (typeSet.size() == 2) { + List<String> urlList = HttpUtil.getUrlListFromText(text); + List<String> tokenList = null; + if (VersionUtil.greaterThan_2_0_7(platform, version)) + tokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(text); + else + tokenList = TaoBaoUtil.getTokenListFromText(text); + + String link = null; + String token = null; + + if (typeSet.contains(ClipboardContentType.link) && typeSet.contains(ClipboardContentType.taoBaoToken)) {// 閾炬帴+鍙d护 + if (text.indexOf(urlList.get(0)) < text.indexOf(tokenList.get(0)))// 绗竴涓摼鎺ュ湪绗竴涓彛浠ゅ墠闈� + link = urlList.get(0); + else// 绗竴涓彛浠ゅ湪绗竴涓摼鎺ュ墠闈� + token = tokenList.get(0); + } else if (typeSet.contains(ClipboardContentType.link) && typeSet.contains(ClipboardContentType.text)) {// 閾炬帴+鏂囨湰 + link = urlList.get(0); + + } else {// 鍙d护+鏂囨湰 + token = tokenList.get(0); + } + + if (!StringUtil.isNullOrEmpty(link)) { + CommonGoods goods = parseLink(urlList.get(0)); + if (goods == null || goods.getGoodsId() == null) { + if (tokenList.size() == 0 && urlList.size() == 2 + && urlList.get(0).indexOf("://uland.taobao.com") > -1 + && urlList.get(1).indexOf("?id=") > -1) {// 鏈�2涓摼鎺ョ殑鏂囨,绗竴涓槸搴楅摵鍒搁摼鎺ワ紝鍏朵腑浜屼釜鏄窐瀹濆晢鍝佽鎯� + CommonGoods goods1 = parseLink(urlList.get(1)); + if (goods1 != null && goods1.getGoodsType() == Constant.SOURCE_TYPE_TAOBAO) { + resultListener.onResult(goods1); + } else { + resultListener.onResult(new GoodsDocParseResultVO(text, urlList.get(0))); + } + + } else if (urlList.size() > 1) + resultListener.onResult(new GoodsDocParseResultVO(text, urlList.get(0))); + else {// 鍗曢摼鎺� + // 鍒ゆ柇鏄惁鍖呭惈鍙浆閾剧殑閾炬帴 + if (!isContainsLinkConvert(link)) + resultListener.none(); + else + resultListener.onResult(new GoodsDocParseResultVO(text, link)); + } + } else { + if (VersionUtil.greaterThan_2_0_7(platform, version)) { + if (urlList.size() == 1)// 鍗曢摼鎺�+鏂囨湰 + resultListener.onResult(goods); + else + resultListener.onResult(new GoodsDocParseResultVO(text, goods)); + } else { + resultListener.onResult(goods); + } + } + } else { + CommonGoods goods = null; + try { + goods = parseTaoBaoToken(token); + } catch (TaoBaoTokenParseException e) { + resultListener.onResult(new GoodsDocParseResultVO(text)); + return; + } + if (goods != null && goods.getGoodsId() != null) { + if (tokenList.size() == 1) {// 鍗曚釜鍙d护 + resultListener.onResult(goods); + } else + resultListener.onResult(new GoodsDocParseResultVO(text, goods)); + } else { + resultListener.onResult(new GoodsDocParseResultVO(text)); + } + } + + } else if (typeSet.size() == 3) {// 鍙d护+閾炬帴+鏂囨湰 + // 娣樺彛浠� + List<String> tokenList = null; + if (VersionUtil.greaterThan_2_0_7(platform, version)) + tokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(text); + else + tokenList = TaoBaoUtil.getTokenListFromText(text); + + List<String> urlList = HttpUtil.getUrlListFromText(text); + if (text.contains("銆�") && tokenList != null && tokenList.size() == 1 && urlList != null + && urlList.size() == 1) {// Android娣樺疂閾炬帴澶嶅埗 + CommonGoods goods = null; + try { + goods = parseTaoBaoToken(tokenList.get(0)); + } catch (TaoBaoTokenParseException e1) { + e1.printStackTrace(); + } + if (goods == null) { + if (text.trim().indexOf("銆�") > -1 && text.trim().indexOf("銆�") > 0) { + // 鎴彇鏍囬 + String title = text.trim().substring(text.trim().indexOf("銆�") + 1, + text.trim().lastIndexOf("銆�")); + goods = new CommonGoods(); + goods.setTitle(title); + goods.setPicture("http://"); + } + } + + if (goods != null) { + resultListener.onResult(goods); + } + + } else {// 鏂囨 + + String token = null; + token = tokenList.get(0); + + if (!StringUtil.isNullOrEmpty(token)) { + CommonGoods goods = null; + try { + goods = parseTaoBaoToken(token); + if (goods != null && goods.getGoodsId() != null) { + // 濡�:http://rd.wechat.com/qrcode/confirm?block_type=101&content=%E3%80%90%E5%8E%9F%E4%BB%B7%E3%80%9120%E5%85%83%0A%E3%80%90%E5%88%B8%E5%90%8E%E4%BB%B7%E3%80%9120%E5%85%83%0A-----------------%0A%E5%BE%A9%E8%A3%BD%E8%BF%99%E6%9D%A1%EF%BC%8C(psNX1UPRJNe)%EF%BC%8C%E8%BF%9B%E5%85%A5%E3%80%90Tao%E5%AE%9D%E3%80%91%E5%8D%B3%E5%8F%AF%E6%8A%A2%E8%B4%AD&lang=zh_CN&scene=37 + if (text.contains("//rd.wechat.com/qrcode/")) {// 寰俊澶嶅埗杩囨潵鐨� + resultListener.onResult(goods); + } else + resultListener.onResult(new GoodsDocParseResultVO(text, goods)); + } else { + resultListener.onResult(new GoodsDocParseResultVO(text)); + } + } catch (TaoBaoTokenParseException e) { + resultListener.onResult(new GoodsDocParseResultVO(text)); + } + } else { + CommonGoods goods = parseLink(urlList.get(0)); + if (goods == null || goods.getGoodsId() == null) { + resultListener.onResult(new GoodsDocParseResultVO(text, urlList.get(0))); + } else { + resultListener.onResult(new GoodsDocParseResultVO(text, goods)); + } + } + } + return; + } + } + + /** + * @Title: parseLink @Description: 瑙f瀽鍗曢摼鎺� @param link @return CommonGoods + * 杩斿洖绫诲瀷 @throws + */ + public CommonGoods parseLink(String link) { + + SimpleCommonGoods simpleGoods = parseGoodsIdFromLink(link); + if (simpleGoods == null) { + return null; + } + + CommonGoods commonGoods = null; + if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_TAOBAO) { + TaoBaoGoodsBrief goods = null; + try { + goods = redisManager.getTaoBaoGoodsBrief(simpleGoods.getGoodsId()); + } catch (NumberFormatException e) { + e.printStackTrace(); + } catch (TaobaoGoodsDownException e) { + e.printStackTrace(); + } + + if (goods != null) { + commonGoods = CommonGoodsFactory.create(goods); + } else { + SearchFilter sf=new SearchFilter(); + sf.setKey("https://item.taobao.com/item.htm?id="+simpleGoods.getGoodsId()); + TaoBaoSearchResult searchResult = TaoKeApiUtil.searchWuLiaoNew(sf,null,null); + if(searchResult!=null&&searchResult.getTaoBaoGoodsBriefs()!=null&&searchResult.getTaoBaoGoodsBriefs().size()>0){ + TaoBaoGoodsBrief tempGoods = searchResult.getTaoBaoGoodsBriefs().get(0); + commonGoods = new CommonGoods(); + commonGoods.setTitle(tempGoods.getTitle()); + commonGoods.setPicture(tempGoods.getPictUrl()); + }else { + goods = TaoBaoUtil.getTmallGoodsInfo(simpleGoods.getGoodsId()); + if (goods != null) { + commonGoods = new CommonGoods(); + commonGoods.setTitle(goods.getTitle()); + commonGoods.setPicture(goods.getPictUrl()); + } else { + // TODO 鏍规嵁娣樺疂鍟嗗搧ID瑙f瀽鏍囬涓庡浘鐗� + } + } + } + + } else if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_JD) { + // 浼樺厛閲囩敤楂樼骇鎺ュ彛璇锋眰鏁版嵁 + JDGoods jdGoods = null; + try { + // 闃叉楂樼骇鎺ュ彛琚敹鍥� + jdGoods = JDApiUtil.queryGoodsDetail(Long.parseLong(simpleGoods.getGoodsId())); + } catch (Exception e) { + } + if (jdGoods == null) { + jdGoods = JDApiUtil.getGoodsDetail(Long.parseLong(simpleGoods.getGoodsId())); + } + if (jdGoods != null) { + commonGoods = CommonGoodsFactory.create(jdGoods); + } else {// 鍟嗗搧骞舵湭鍙備笌鎺ㄥ箍 + jdGoods = JDUtil.getSimpleGoodsInfo(simpleGoods.getGoodsId()); + if (jdGoods != null) { + commonGoods = new CommonGoods(); + commonGoods.setTitle(jdGoods.getSkuName()); + commonGoods.setPicture(jdGoods.getPicUrl()); + } + } + } else if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_PDD) { + PDDGoodsDetail goods = PinDuoDuoApiUtil.getGoodsDetail(Long.parseLong(simpleGoods.getGoodsId())); + if (goods != null) { + commonGoods = CommonGoodsFactory.create(goods); + } else { + goods = PinDuoDuoUtil.getPDDGoodsInfo(simpleGoods.getGoodsId()); + if (goods != null) { + commonGoods = new CommonGoods(); + commonGoods.setTitle(goods.getGoodsName()); + commonGoods.setPicture(goods.getGoodsThumbnailUrl()); + } + } + } else if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_VIP) { + VIPGoodsInfo goods = VipShopApiUtil.getGoodsDetail(simpleGoods.getGoodsId()); + if (goods != null) { + commonGoods = CommonGoodsFactory.create(goods); + } + } else if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_SUNING) { + SuningGoodsInfo goods = SuningApiUtil.getGoodsDetail(simpleGoods.getGoodsId().split("-")[1], + simpleGoods.getGoodsId().split("-")[0]); + if (goods != null) { + commonGoods = CommonGoodsFactory.create(goods); + } + } else if (simpleGoods.getGoodsType() == Constant.SOURCE_TYPE_DY) { + DYGoodsDetail goods = CSJCPSApiUtil.goodsDetail(Long.parseLong(simpleGoods.getGoodsId())); + if (goods != null) { + commonGoods = CommonGoodsFactory.create(goods); + } + } + + return commonGoods; + } + + /** + * 浠庨摼鎺ヤ腑瑙f瀽鍟嗗搧ID + * + * @return + */ + public SimpleCommonGoods parseGoodsIdFromLink(String link) { + + String jdId = parseJDSkuIdFromLink(link); + + if (!StringUtil.isNullOrEmpty(jdId)) { + return new SimpleCommonGoods(jdId, Constant.SOURCE_TYPE_JD); + } + + // 瑙f瀽鎷煎澶� + String pddId = PinDuoDuoUtil.getPDDGoodsId(link); + if (!StringUtil.isNullOrEmpty(pddId)) { + return new SimpleCommonGoods(pddId, Constant.SOURCE_TYPE_PDD); + } + + // 瑙f瀽鑻忓畞 + String suningId = SuningUtil.parseGoodsIdByUrl(link); + if (!StringUtil.isNullOrEmpty(suningId)) { + return new SimpleCommonGoods(suningId, Constant.SOURCE_TYPE_SUNING); + } + + // 瑙f瀽鍞搧浼� + String vipId = VipShopUtil.parseGoodsIdByUrl(link); + if (!StringUtil.isNullOrEmpty(vipId)) { + return new SimpleCommonGoods(vipId, Constant.SOURCE_TYPE_VIP); + } + String dyProductId = DYUtil.parseProdectIdFromLink(link); + if (!StringUtil.isNullOrEmpty(dyProductId)) { + return new SimpleCommonGoods(dyProductId, Constant.SOURCE_TYPE_DY); + } + + // 瑙f瀽娣樺疂 + String auctionId = parseTBAuctionIdFromLink(link); + if (!StringUtil.isNullOrEmpty(auctionId)) { + return new SimpleCommonGoods(auctionId, Constant.SOURCE_TYPE_TAOBAO); + } + + return null; + } + + /** + * 浠庨摼鎺ヤ腑瑙f瀽浜笢鍟嗗搧鐨凷KUID + * + * @param link + * @return + */ + public String parseJDSkuIdFromLink(String link) { + // 瑙f瀽鍏朵粬鍟嗗搧 + String jdId = JDUtil.getJDGoodsId(link); + + // 寰俊閾炬帴 + if (StringUtil.isNullOrEmpty(jdId)) { + jdId = JDUtil.getJDGoodsIdByWeiXin(link); + } + + // 棰嗗埜鐭繛鎺� + if (StringUtil.isNullOrEmpty(jdId) && (link.contains("u.jd.com")||link.contains("//3.cn/"))) { + jdId = jdGoodsLinkParseManager.parseGoodsIdByJDShortUrl(link); + } + + return jdId; + } + + + public String parseTBAuctionIdFromLink(String link) { + TaoBaoGoodsBrief tb = null; + String auctionId = null; + if (link.startsWith("https://a.m.taobao.com/i"))// 娣樺疂璐﹀彿鏈櫥褰曠姸鎬� + { + link = link.substring("https://a.m.taobao.com/i".length(), link.indexOf(".htm")); + if (NumberUtil.isNumeric(link)) { + auctionId = link + ""; + } + } else if (link.contains("ju.taobao.com") || link.contains(".juhuasuan.com")) {// 鑱氬垝绠� + int index = link.indexOf("item_id"); + if (index >= 0) { + link = link.substring(index); + int last = link.indexOf("&"); + String id = ""; + if (last > 0) + id = link.substring(link.indexOf("=") + 1, link.indexOf("&")); + else { + id = link.substring(link.indexOf("=" + 1)); + } + auctionId = id; + } + } else if (link.contains("http://zmnxbc.com")) { // 鎵嬫満绔ぉ鐚獳PP鍒嗕韩 + tb = TaoBaoUtil.parsePhoneShareUrlByTM(link); + if (tb != null) + auctionId = tb.getAuctionId() + ""; + } else if (link.contains("h5.m.taobao") || link.contains("detail.m.tmall") || link.contains("item.taobao") + || link.contains("detail.tmall")) { // 鎵嬫満椤甸潰鍜岀數鑴戦〉闈� + Map<String, String> map = Utils.parseURL(link); + String id = ""; + id = map.get("id").replace("}", ""); + auctionId = id; + } else if (link.contains("v.douyin.com")) { // 鎶栭煶 + tb = analysisDouYin(link); + if (tb != null) { + if (!StringUtil.isNullOrEmpty(tb.getAuctionUrl())) {// 鎶栭煶瑙f瀽鍒版窐瀹濆晢鍝侀摼鎺� + CommonGoods goods = parseLink(tb.getAuctionUrl()); + if (goods != null) { + auctionId = goods.getGoodsId() + ""; + } + } + } + } else { + tb = TaoBaoUtil.parsePhoneShareUrlByTB(link); + if (tb != null) + auctionId = tb.getAuctionId() + ""; + } + + return auctionId; + + } + + private CommonGoods parseTaoBaoToken(String token) throws TaoBaoTokenParseException { + String auctionId = taoBaoTokenAuctionIdMapManager.getAuctionIdByToken(token); + if (auctionId != null) {// + return getTaoBaoGoods(auctionId); + } else { + return null; + } + } + + private CommonGoods getTaoBaoGoods(String auctionId) { + TaoBaoGoodsBrief goods = TaoBaoUtil.isAlimama(auctionId + ""); + if (goods == null) { + goods = TaoBaoUtil.getTaoBaoGoodsBriefNotInPub(auctionId); + } + if (goods != null) { + CommonGoods commonGoods = CommonGoodsFactory.create(goods); + return commonGoods; + } else + return null; + } + + /** + * @Title: analysisDouYin @Description: 鎶栭煶瑙f瀽 @param link @return CommonGoods + * 杩斿洖绫诲瀷 @throws + */ + private TaoBaoGoodsBrief analysisDouYin(String link) { + String location = HttpUtil.getLocation(link); + if (!StringUtil.isNullOrEmpty(location)) { + if (location.contains("taobao.com")) { // 娣樺疂鍟嗗搧 + String realUrl = TaoBaoUtil.getRealUrl(location); + if (!StringUtil.isNullOrEmpty(realUrl)) { + TaoBaoGoodsBrief goods = new TaoBaoGoodsBrief(); + goods.setAuctionUrl(realUrl); + return goods; + } + } else if (location.contains("haohuo.jinritemai.com")) { // 鎶栭煶鍟嗗搧 + DouYinGoods goods = DouYinUtil.getGoodsInfo(location); + JSONObject goodsJSON = new JSONObject(); + goodsJSON.put("title", goods.getName()); + goodsJSON.put("pictUrl", goods.getImg()); + + TaoBaoGoodsBrief commonGoods = new TaoBaoGoodsBrief(); + commonGoods.setPictUrl(goods.getImg()); + commonGoods.setTitle(goods.getName()); + return commonGoods; + } + } + return null; + } + + private boolean isContainsLinkConvert(String link) { + if (link.contains(".jd.com")) + return true; + else if (link.contains(".taobao.com") || link.contains(".tmall")) + return true; + else if (link.contains(".pinduoduo.com") || link.contains(".yangkeduo.com")) + return true; + return false; + } + + class SimpleCommonGoods { + private String goodsId; + private int goodsType; + + public SimpleCommonGoods(String goodsId, int goodsType) { + this.goodsId = goodsId; + this.goodsType = goodsType; + } + + public String getGoodsId() { + return goodsId; + } + + public void setGoodsId(String goodsId) { + this.goodsId = goodsId; + } + + public int getGoodsType() { + return goodsType; + } + + public void setGoodsType(int goodsType) { + this.goodsType = goodsType; + } + } + + + public static void main(String[] args) { +//String originText, String platform, String version + + new ClipboardAnalysisManager(). parseJDSkuIdFromLink("https://3.cn/1-U6tjhC"); + + } +} + +/** + * 绮樿创鏉胯В鏋愮粨鏋� + * + * @author Administrator + */ + + -- Gitblit v1.8.0