From ca1ad791b7b7b04e57db6bd4fb93f0c1066a9f99 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期四, 24 九月 2020 18:31:23 +0800 Subject: [PATCH] 淘口令格式兼容 --- fanli/src/main/java/com/yeshi/fanli/service/manger/goods/ConvertLinkManager.java | 453 +++++++++++++++++++++++++++++--------------------------- 1 files changed, 235 insertions(+), 218 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/service/manger/goods/ConvertLinkManager.java b/fanli/src/main/java/com/yeshi/fanli/service/manger/goods/ConvertLinkManager.java index a1ed9bf..024c579 100644 --- a/fanli/src/main/java/com/yeshi/fanli/service/manger/goods/ConvertLinkManager.java +++ b/fanli/src/main/java/com/yeshi/fanli/service/manger/goods/ConvertLinkManager.java @@ -32,152 +32,159 @@ import com.yeshi.goods.facade.utils.taobao.HaoDanKuApiUtil; import com.yeshi.fanli.util.taobao.TaoBaoUtil; import com.yeshi.fanli.util.taobao.TaoKeApiUtil; +import org.yeshi.utils.UrlUtils; /** * 杞摼缁勪欢 - * - * @author Administrator * + * @author Administrator */ @Component public class ConvertLinkManager { - @Resource - private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService; + @Resource + private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService; - @Resource - private TaoBaoBuyRelationMapService taoBaoBuyRelationMapService; + @Resource + private TaoBaoBuyRelationMapService taoBaoBuyRelationMapService; - @Resource - private RedisManager redisManager; + @Resource + private RedisManager redisManager; - @Resource - private TaoBaoTokenAuctionIdMapManager taoBaoTokenAuctionIdMapManager; + @Resource + private TaoBaoTokenAuctionIdMapManager taoBaoTokenAuctionIdMapManager; - /** - * - * @Title: convertJDLinkFromText @Description:杞摼鏂囨湰涓殑浜笢鐭摼 @param text @param - * uid @param share @return @throws Exception String 杩斿洖绫诲瀷 @throws - */ + /** + * @Title: convertJDLinkFromText @Description:杞摼鏂囨湰涓殑浜笢鐭摼 @param text @param + * uid @param share @return @throws Exception String 杩斿洖绫诲瀷 @throws + */ - public String convertJDLinkFromText(String text, Long uid, boolean share) throws Exception { + public String convertJDLinkFromText(String text, Long uid, boolean share, boolean simpleTKToken) throws Exception { - StringBuilder builder = new StringBuilder(text); - String newText = builder.toString(); - // 浜笢杞摼 - // 鎻愬彇浜笢鎵�鏈夌殑閾炬帴 - List<String> jdShortLink = JDUtil.getJDShortLinksFromText(text); - Set<String> linkSet = new HashSet<>(); - Map<String, String> linkMap = new HashMap<>(); - if (jdShortLink != null && jdShortLink.size() > 0) { - linkSet.addAll(jdShortLink); - for (String shortLink : linkSet) { - String link = null; - if (share) - link = JDApiUtil.convertLinkWithSubUnionId(shortLink, null, JDApiUtil.POSITION_SHARE + "", - uid + ""); - else - link = JDApiUtil.convertLinkWithSubUnionId(shortLink, null, JDApiUtil.POSITION_FANLI + "", - uid + ""); - if (!StringUtil.isNullOrEmpty(link)) { - linkMap.put(shortLink, link); - } - } - } + StringBuilder builder = new StringBuilder(text); + String newText = builder.toString(); + // 浜笢杞摼 + // 鎻愬彇浜笢鎵�鏈夌殑閾炬帴 + List<String> jdShortLink = JDUtil.getJDShortLinksFromText(text); + Set<String> linkSet = new HashSet<>(); + Map<String, String> linkMap = new HashMap<>(); + if (jdShortLink != null && jdShortLink.size() > 0) { + linkSet.addAll(jdShortLink); + for (String shortLink : linkSet) { + String link = null; + if (share) + link = JDApiUtil.convertLinkWithSubUnionId(shortLink, null, JDApiUtil.POSITION_SHARE + "", + uid + ""); + else + link = JDApiUtil.convertLinkWithSubUnionId(shortLink, null, JDApiUtil.POSITION_FANLI + "", + uid + ""); + if (!StringUtil.isNullOrEmpty(link)) { + linkMap.put(shortLink, link); + } + } + } - for (String link : linkSet) { - if (linkMap.get(link) == null) - throw new Exception("浜笢杞摼澶辫触"); - newText = newText.replace(link, linkMap.get(link)); - } + for (String link : linkSet) { + if (linkMap.get(link) == null) + throw new Exception("浜笢杞摼澶辫触"); + newText = newText.replace(link, linkMap.get(link)); + } - return newText; - } + return newText; + } - public String convertPDDLinkFromText(String text, Long uid, boolean share) throws Exception { + public String convertPDDLinkFromText(String text, Long uid, boolean share) throws Exception { - StringBuilder builder = new StringBuilder(text); - String newText = builder.toString(); - // 浜笢杞摼 - // 鎻愬彇浜笢鎵�鏈夌殑閾炬帴 - List<String> pddShortLink = PinDuoDuoUtil.getPDDShortLinksFromText(text); - Set<String> linkSet = new HashSet<>(); - Map<String, String> linkMap = new HashMap<>(); - if (pddShortLink != null && pddShortLink.size() > 0) { - linkSet.addAll(pddShortLink); - for (String shortLink : linkSet) { - String goodsId = PinDuoDuoUtil.getPDDGoodsId(shortLink); - if (!StringUtil.isNullOrEmpty(goodsId)) { - PDDPromotionUrl pddUrl = PinDuoDuoApiUtil.convert(Long.parseLong(goodsId), - share ? PinDuoDuoApiUtil.PID_SHARE : PinDuoDuoApiUtil.PID_FANLI, uid + "",false); - if (pddUrl != null) { - linkMap.put(shortLink, pddUrl.getShortUrl()); - } - } + StringBuilder builder = new StringBuilder(text); + String newText = builder.toString(); + // 浜笢杞摼 + // 鎻愬彇浜笢鎵�鏈夌殑閾炬帴 + List<String> pddShortLink = PinDuoDuoUtil.getPDDShortLinksFromText(text); + Set<String> linkSet = new HashSet<>(); + Map<String, String> linkMap = new HashMap<>(); + if (pddShortLink != null && pddShortLink.size() > 0) { + linkSet.addAll(pddShortLink); + for (String shortLink : linkSet) { + String goodsId = PinDuoDuoUtil.getPDDGoodsId(shortLink); + if (!StringUtil.isNullOrEmpty(goodsId)) { + PDDPromotionUrl pddUrl = PinDuoDuoApiUtil.convert(Long.parseLong(goodsId), + share ? PinDuoDuoApiUtil.PID_SHARE : PinDuoDuoApiUtil.PID_FANLI, uid + "", false); + if (pddUrl != null) { + linkMap.put(shortLink, pddUrl.getShortUrl()); + } + } - } - } + } + } - for (String link : linkSet) { - if (linkMap.get(link) == null) - throw new Exception("鎷煎澶氳浆閾惧け璐�"); - newText = newText.replace(link, linkMap.get(link)); - } + for (String link : linkSet) { + if (linkMap.get(link) == null) + throw new Exception("鎷煎澶氳浆閾惧け璐�"); + newText = newText.replace(link, linkMap.get(link)); + } - return newText; - } + return newText; + } - // 鑾峰彇娣樺疂瀹樻柟娲诲姩ID鍐呭 - public List<String> getTaoBaoOfficialActivityId(String text) { + // 鑾峰彇娣樺疂瀹樻柟娲诲姩ID鍐呭 + public List<String> getTaoBaoOfficialActivityId(String text) { - // {娣樺疂娲诲姩:13123123} - String regex = "(\\{\u6dd8\u5b9d\u6d3b\u52a8\\:){1}+[0-9]{5,20}\\}{1}"; - Pattern pattern = Pattern.compile(regex); - Matcher m = pattern.matcher(text); - List<String> urlList = new ArrayList<>(); - while (m.find()) { - urlList.add(m.group()); - } + // {娣樺疂娲诲姩:13123123} + String regex = "(\\{\u6dd8\u5b9d\u6d3b\u52a8\\:){1}+[0-9]{5,20}\\}{1}"; + Pattern pattern = Pattern.compile(regex); + Matcher m = pattern.matcher(text); + List<String> urlList = new ArrayList<>(); + while (m.find()) { + urlList.add(m.group()); + } - return urlList; + return urlList; - } + } - /** - * - * @Title: convertTBLinkFromText @Description: 杞摼鏂囨湰涓殑娣樺彛浠� @param text @param - * uid @param share @return @throws Exception String 杩斿洖绫诲瀷 @throws - */ - private String convertTBLinkFromText(String text, Long uid, boolean share) throws Exception { + /** + * + * @Title: convertTBLinkFromText @Description: 杞摼鏂囨湰涓殑娣樺彛浠� @param text @param + * uid @param share @return @throws Exception String 杩斿洖绫诲瀷 @throws + */ + /** + * @param text + * @param uid + * @param share + * @param simpleToken -鏄惁閲囩敤绠�鐗堟窐鍙d护 + * @return + * @throws Exception + */ + private String convertTBLinkFromText(String text, Long uid, boolean share, boolean simpleToken) throws Exception { - StringBuilder builder = new StringBuilder(text); - String newText = builder.toString(); + StringBuilder builder = new StringBuilder(text); + String newText = builder.toString(); - // 瑙f瀽娣樺彛浠� - List<String> tokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(newText); - if (tokenList != null && tokenList.size() > 0) { + // 瑙f瀽娣樺彛浠� + List<String> tokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(newText); + if (tokenList != null && tokenList.size() > 0) { - String relationId = null; - if (share) { - relationId = userExtraTaoBaoInfoService.getRelationIdByUid(uid); - if (StringUtil.isNullOrEmpty(relationId)) - throw new Exception("娣樺疂鏈巿鏉�"); - } + String relationId = null; + if (share) { + relationId = userExtraTaoBaoInfoService.getRelationIdByUid(uid); + if (StringUtil.isNullOrEmpty(relationId)) + throw new Exception("娣樺疂鏈巿鏉�"); + } - Set<String> tokenSet = new HashSet<>(); - tokenSet.addAll(tokenList); - Map<String, String> tokenMap = new HashMap<>(); - for (String token : tokenSet) { - Long auctionId = taoBaoTokenAuctionIdMapManager.getAuctionIdByToken(token); - if (auctionId == null) - throw new Exception("浠庢窐鍙d护鑾峰彇鍟嗗搧澶辫触"); - TaoBaoGoodsBrief simpleGoods = TaoKeApiUtil.getSimpleGoodsInfo(auctionId); - if (simpleGoods == null) - throw new Exception("鍟嗗搧淇℃伅鑾峰彇澶辫触"); - String link = null; - if (share) { - //鏆傛椂璋冪敤濂藉崟搴揂PI - link=HaoDanKuApiUtil.getInstance().convertLink(auctionId, TaoBaoConstant.TAOBAO_RELATION_PID_DEFAULT, null,null,relationId); + Set<String> tokenSet = new HashSet<>(); + tokenSet.addAll(tokenList); + Map<String, String> tokenMap = new HashMap<>(); + for (String token : tokenSet) { + Long auctionId = taoBaoTokenAuctionIdMapManager.getAuctionIdByToken(token); + if (auctionId == null) + throw new Exception("浠庢窐鍙d护鑾峰彇鍟嗗搧澶辫触"); + TaoBaoGoodsBrief simpleGoods = TaoKeApiUtil.getSimpleGoodsInfo(auctionId); + if (simpleGoods == null) + throw new Exception("鍟嗗搧淇℃伅鑾峰彇澶辫触"); + String link = null; + if (share) { + //鏆傛椂璋冪敤濂藉崟搴揂PI + link = HaoDanKuApiUtil.getInstance().convertLink(auctionId, TaoBaoConstant.TAOBAO_RELATION_PID_DEFAULT, null, null, relationId); // TaoBaoGoodsBrief goods = TaoKeApiUtil.specialConvertCoupon(auctionId, // new TaoKeAppInfo(TaoBaoConstant.TAOBAO_AUTH_APPKEY, TaoBaoConstant.TAOBAO_AUTH_APPSECRET, // TaoBaoConstant.TAOBAO_RELATION_PID_DEFAULT)); @@ -188,121 +195,131 @@ // if (StringUtil.isNullOrEmpty(link)) { // link = goods.getAuctionUrl(); // } - if (StringUtil.isNullOrEmpty(link)) - throw new Exception("鍟嗗搧杞摼澶辫触"); - link += "&relationId=" + relationId; - } else { - String specialId=userExtraTaoBaoInfoService.getSpecialIdByUid(uid); + if (StringUtil.isNullOrEmpty(link)) + throw new Exception("鍟嗗搧杞摼澶辫触"); + link += "&relationId=" + relationId; + } else { + String specialId = userExtraTaoBaoInfoService.getSpecialIdByUid(uid); - if (simpleGoods.getMaterialLibType() != null && simpleGoods.getMaterialLibType() == 1) { - TaoBaoGoodsBrief goods = TaoKeApiUtil.specialConvertCoupon(auctionId, - new TaoKeAppInfo(TaoBaoConstant.TAOBAO_AUTH_APPKEY, - TaoBaoConstant.TAOBAO_AUTH_APPSECRET, - TaoBaoConstant.TAOBAO_SPECIAL_PID_DEFAULT),specialId,null); - if (goods == null) - throw new Exception("鍟嗗搧杞摼澶辫触"); + if (simpleGoods.getMaterialLibType() != null && simpleGoods.getMaterialLibType() == 1) { + TaoBaoGoodsBrief goods = TaoKeApiUtil.specialConvertCoupon(auctionId, + new TaoKeAppInfo(TaoBaoConstant.TAOBAO_AUTH_APPKEY, + TaoBaoConstant.TAOBAO_AUTH_APPSECRET, + TaoBaoConstant.TAOBAO_SPECIAL_PID_DEFAULT), specialId, null); + if (goods == null) + throw new Exception("鍟嗗搧杞摼澶辫触"); - link = goods.getCouponLink(); - if (StringUtil.isNullOrEmpty(link)) { - link = goods.getAuctionUrl(); - } - } else { - String sRelationId = taoBaoBuyRelationMapService.getRelationId(uid); + link = goods.getCouponLink(); + if (StringUtil.isNullOrEmpty(link)) { + link = goods.getAuctionUrl(); + } + } else { + String sRelationId = taoBaoBuyRelationMapService.getRelationId(uid); - TaoBaoGoodsBrief goods = TaoKeApiUtil.specialConvertCoupon(auctionId, - new TaoKeAppInfo(TaoBaoConstant.TAOBAO_AUTH_APPKEY, - TaoBaoConstant.TAOBAO_AUTH_APPSECRET, - TaoBaoConstant.TAOBAO_RELATION_AS_SPECIAL_PID),specialId,null); - if (goods == null) - throw new Exception("鍟嗗搧杞摼澶辫触"); + TaoBaoGoodsBrief goods = TaoKeApiUtil.specialConvertCoupon(auctionId, + new TaoKeAppInfo(TaoBaoConstant.TAOBAO_AUTH_APPKEY, + TaoBaoConstant.TAOBAO_AUTH_APPSECRET, + TaoBaoConstant.TAOBAO_RELATION_AS_SPECIAL_PID), specialId, null); + if (goods == null) + throw new Exception("鍟嗗搧杞摼澶辫触"); - link = goods.getCouponLink(); - if (StringUtil.isNullOrEmpty(link)) { - link = goods.getAuctionUrl(); - } - if (StringUtil.isNullOrEmpty(link)) - throw new Exception("鍟嗗搧杞摼澶辫触"); - link += "&relationId=" + sRelationId; - } - } + link = goods.getCouponLink(); + if (StringUtil.isNullOrEmpty(link)) { + link = goods.getAuctionUrl(); + } + if (StringUtil.isNullOrEmpty(link)) + throw new Exception("鍟嗗搧杞摼澶辫触"); + link += "&relationId=" + sRelationId; + } + } - if (StringUtil.isNullOrEmpty(link)) { - throw new Exception("鍟嗗搧杞摼澶辫触"); - } + if (StringUtil.isNullOrEmpty(link)) { + throw new Exception("鍟嗗搧杞摼澶辫触"); + } - String newToken = TaoKeApiUtil.getTKToken(simpleGoods.getPictUrl(), simpleGoods.getTitle(), link); - if (StringUtil.isNullOrEmpty(newToken)) { - throw new Exception("鍙d护杞摼澶辫触"); - } - tokenMap.put(token, TaoBaoUtil.filterTaoToken(newToken)); - } + String newToken = TaoKeApiUtil.getTKToken(simpleGoods.getPictUrl(), simpleGoods.getTitle(), link, simpleToken); + if (StringUtil.isNullOrEmpty(newToken)) { + throw new Exception("鍙d护杞摼澶辫触"); + } + tokenMap.put(token, TaoBaoUtil.filterTaoToken(newToken)); + } - for (String token : tokenSet) { - if (tokenMap.get(token) == null) - throw new Exception("娣樺疂閮ㄥ垎杞摼澶辫触"); - newText = newText.replace(token, tokenMap.get(token)); - } - } + for (String token : tokenSet) { + if (tokenMap.get(token) == null) + throw new Exception("娣樺疂閮ㄥ垎杞摼澶辫触"); + newText = newText.replace(token, tokenMap.get(token)); + } + } - // 瑙f瀽娣樺疂瀹樻柟娲诲姩ID - List<String> list = getTaoBaoOfficialActivityId(newText); - if (list.size() > 0) { - String relationId = null; - if (share) { - relationId = userExtraTaoBaoInfoService.getRelationIdByUid(uid); - if (StringUtil.isNullOrEmpty(relationId)) - throw new Exception("娣樺疂鏈巿鏉�"); - } - for (String st : list) { - String activityid = st.replace("{娣樺疂娲诲姩:", "").replace("}", ""); - if (!StringUtil.isNullOrEmpty(activityid)) { - String adzoneId = TaoBaoConstant.TAOBAO_RELATION_PID_DEFAULT.split("_")[3]; - TaoKeOfficialActivityConvertResultDTO dto = TaoKeApiUtil.officialActivityConvert(adzoneId, - activityid, relationId); - String link = dto.getShort_click_url(); - if (StringUtil.isNullOrEmpty(link)) { - link = dto.getClick_url(); - } + // 瑙f瀽娣樺疂瀹樻柟娲诲姩ID + List<String> list = getTaoBaoOfficialActivityId(newText); + if (list.size() > 0) { + String relationId = null; + if (share) { + relationId = userExtraTaoBaoInfoService.getRelationIdByUid(uid); + if (StringUtil.isNullOrEmpty(relationId)) + throw new Exception("娣樺疂鏈巿鏉�"); + } + for (String st : list) { + String activityid = st.replace("{娣樺疂娲诲姩:", "").replace("}", ""); + if (!StringUtil.isNullOrEmpty(activityid)) { + String adzoneId = TaoBaoConstant.TAOBAO_RELATION_PID_DEFAULT.split("_")[3]; + TaoKeOfficialActivityConvertResultDTO dto = TaoKeApiUtil.officialActivityConvert(adzoneId, + activityid, relationId); + String link = dto.getShort_click_url(); + if (StringUtil.isNullOrEmpty(link)) { + link = dto.getClick_url(); + } - String token = TaoKeApiUtil.getTKToken("http://", "娣樺疂瀹樻柟娲诲姩", link); - newText = newText.replace(st, TaoBaoUtil.filterTaoToken(token)); - } - } - } + String token = TaoKeApiUtil.getTKToken("http://", "娣樺疂瀹樻柟娲诲姩", link, simpleToken); + newText = newText.replace(st, TaoBaoUtil.filterTaoToken(token)); + } + } + } - return newText; - } + return newText; + } - /** - * - * @Title: convertLinkFromText @Description: 浠庢枃鏈浆閾� @param text @return - * String 杩斿洖绫诲瀷 @throws - */ - public String convertLinkFromText(String text, Long uid, boolean share) - throws ConvertLinkExceptionException, Exception { - String newText = convertJDLinkFromText(text, uid, share); - newText = convertPDDLinkFromText(newText, uid, share); - newText = convertTBLinkFromText(newText, uid, share); - if (text.equalsIgnoreCase(newText)) - throw new ConvertLinkExceptionException(ConvertLinkExceptionException.CODE_NONE, "涓嶅寘鍚彲杞摼鐨勫彛浠や笌閾炬帴"); - return newText; - } + /** + * @Title: convertLinkFromText @Description: 浠庢枃鏈浆閾� @param text @return + * String 杩斿洖绫诲瀷 @throws + */ + public String convertLinkFromText(String text, Long uid, boolean share, boolean simpleToken) + throws ConvertLinkExceptionException, Exception { + String newText = convertJDLinkFromText(text, uid, share, simpleToken); + newText = convertPDDLinkFromText(newText, uid, share); + newText = convertTBLinkFromText(newText, uid, share, simpleToken); - /** - * 鍒涘缓鍙d护 @Title: createTaoBaoToken @Description: @param uid 鐢ㄦ埛ID @param - * picturl @param title @param link @return String 杩斿洖绫诲瀷 @throws - */ - public String createTaoBaoToken(Long uid, String picturl, String title, String link) { + List<String> urls = UrlUtils.parseUrlsFromText(text); + if (urls != null) + for (String url : urls) { + if (url.contains("{relationId}")) { + String relationId = userExtraTaoBaoInfoService.getRelationIdByUid(uid); + if (StringUtil.isNullOrEmpty(relationId)) { + throw new Exception("娣樺疂鏈巿鏉�"); + } + newText = newText.replace(url, url.replace("{relationId}", relationId)); + } + } + if (text.equalsIgnoreCase(newText)) + throw new ConvertLinkExceptionException(ConvertLinkExceptionException.CODE_NONE, "涓嶅寘鍚彲杞摼鐨勫彛浠や笌閾炬帴"); + return newText; + } - String token = TaoKeApiUtil.getTKToken(picturl, title, link); - if (!StringUtil.isNullOrEmpty(token)) { - // 缂撳瓨30鍒嗛挓 - redisManager.cacheCommonString( - RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoUserTKToken, uid + "-" + StringUtil.Md5(token)), "1", - 60 * 30); - } - return token; - } + /** + * 鍒涘缓鍙d护 @Title: createTaoBaoToken @Description: @param uid 鐢ㄦ埛ID @param + * picturl @param title @param link @return String 杩斿洖绫诲瀷 @throws + */ + public String createTaoBaoToken(Long uid, String picturl, String title, String link, boolean simpleToken) { + String token = TaoKeApiUtil.getTKToken(picturl, title, link, simpleToken); + if (!StringUtil.isNullOrEmpty(token)) { + // 缂撳瓨30鍒嗛挓 + redisManager.cacheCommonString( + RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoUserTKToken, uid + "-" + StringUtil.Md5(token)), "1", + 60 * 30); + } + return token; + } } -- Gitblit v1.8.0