New file |
| | |
| | | package com.yeshi.fanli.service.manger.goods;
|
| | |
|
| | | import java.util.HashMap;
|
| | | import java.util.HashSet;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.Set;
|
| | |
|
| | | import javax.annotation.Resource;
|
| | |
|
| | | import org.springframework.stereotype.Component;
|
| | | import org.yeshi.utils.StringUtil;
|
| | |
|
| | | import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
|
| | | import com.yeshi.fanli.entity.taobao.TaoKeAppInfo;
|
| | | import com.yeshi.fanli.service.inter.taobao.TaoBaoBuyRelationMapService;
|
| | | import com.yeshi.fanli.service.inter.user.tb.UserExtraTaoBaoInfoService;
|
| | | import com.yeshi.fanli.util.TaoBaoConstant;
|
| | | import com.yeshi.fanli.util.jd.JDApiUtil;
|
| | | import com.yeshi.fanli.util.jd.JDUtil;
|
| | | import com.yeshi.fanli.util.taobao.TaoBaoUtil;
|
| | | import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
|
| | |
|
| | | /**
|
| | | * 转链组件
|
| | | * |
| | | * @author Administrator
|
| | | *
|
| | | */
|
| | | @Component
|
| | | public class ConvertLinkManager {
|
| | |
|
| | | @Resource
|
| | | private UserExtraTaoBaoInfoService userExtraTaoBaoInfoService;
|
| | |
|
| | | @Resource
|
| | | private TaoBaoBuyRelationMapService taoBaoBuyRelationMapService;
|
| | |
|
| | | /**
|
| | | * |
| | | * @Title: convertJDLinkFromText
|
| | | * @Description:转链文本中的京东短链 |
| | | * @param text
|
| | | * @param uid
|
| | | * @param share
|
| | | * @return
|
| | | * @throws Exception |
| | | * String 返回类型
|
| | | * @throws
|
| | | */
|
| | |
|
| | | private String convertJDLinkFromText(String text, Long uid, boolean share) 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);
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | for (String link : linkSet) {
|
| | | if (linkMap.get(link) == null)
|
| | | throw new Exception("京东转链失败");
|
| | | newText = newText.replace(link, linkMap.get(link));
|
| | | }
|
| | |
|
| | | return newText;
|
| | | }
|
| | |
|
| | | /**
|
| | | * |
| | | * @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 {
|
| | |
|
| | | StringBuilder builder = new StringBuilder(text);
|
| | | String newText = builder.toString();
|
| | | List<String> tokenList = TaoBaoUtil.getTokenListFromText(newText);
|
| | | if (tokenList != null && tokenList.size() > 0) {
|
| | |
|
| | | 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 = TaoKeApiUtil.tokenConvertAuctionId(token);
|
| | | if (auctionId == null)
|
| | | throw new Exception("从淘口令获取商品失败");
|
| | | TaoBaoGoodsBrief simpleGoods = TaoKeApiUtil.getSimpleGoodsInfo(auctionId);
|
| | | if (simpleGoods == null)
|
| | | throw new Exception("商品信息获取失败");
|
| | | String link = null;
|
| | | if (share) {
|
| | | TaoBaoGoodsBrief goods = TaoKeApiUtil.specialConvertCoupon(auctionId,
|
| | | new TaoKeAppInfo(TaoBaoConstant.TAOBAO_AUTH_APPKEY, TaoBaoConstant.TAOBAO_AUTH_APPSECRET,
|
| | | TaoBaoConstant.TAOBAO_RELATION_PID_DEFAULT));
|
| | | 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=" + relationId;
|
| | | } else {
|
| | | 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));
|
| | | if (goods == null)
|
| | | throw new Exception("商品转链失败");
|
| | |
|
| | | 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));
|
| | | 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;
|
| | | }
|
| | | }
|
| | |
|
| | | if (StringUtil.isNullOrEmpty(link)) {
|
| | | throw new Exception("商品转链失败");
|
| | | }
|
| | |
|
| | | String newToken = TaoKeApiUtil.getTKToken(simpleGoods.getPictUrl(), simpleGoods.getTitle(), link);
|
| | | if (StringUtil.isNullOrEmpty(newToken)) {
|
| | | throw new Exception("口令转链失败");
|
| | | }
|
| | | tokenMap.put(token, newToken);
|
| | | }
|
| | |
|
| | | for (String token : tokenSet) {
|
| | | if (tokenMap.get(token) == null)
|
| | | throw new Exception("淘宝部分转链失败");
|
| | | newText = newText.replace(token, tokenMap.get(token));
|
| | | }
|
| | | }
|
| | | return newText;
|
| | | }
|
| | |
|
| | | /**
|
| | | * |
| | | * @Title: convertLinkFromText
|
| | | * @Description: 从文本转链
|
| | | * @param text
|
| | | * @return |
| | | * String 返回类型
|
| | | * @throws
|
| | | */
|
| | | public String convertLinkFromText(String text, Long uid, boolean share) throws Exception {
|
| | | String newText = convertJDLinkFromText(text, uid, share);
|
| | | newText = convertTBLinkFromText(newText, uid, share);
|
| | | return newText;
|
| | | }
|
| | |
|
| | | }
|