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: 解析文本的类型
|
* @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();
|
// 自有口令
|
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: 解析
|
* @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);
|
|
// 系统口令
|
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)) {// 纯链接
|
// 解析商品
|
CommonGoods goods = parseLink(text);
|
if (goods == null) {
|
resultListener.none();
|
} else {
|
resultListener.onResult(goods);
|
}
|
|
} else if (typeSet.contains(ClipboardContentType.taoBaoToken)) {// 纯淘口令
|
|
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)) {// 链接+口令
|
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 {// 口令+文本
|
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) {// 口令+链接+文本
|
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: 解析单链接
|
* @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")) { // 手机端天猫APP分享
|
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())) {// 抖音解析到淘宝商品链接
|
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;
|
|
// 淘宝商品解析结束
|
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 {
|
// 解析其他商品
|
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: 抖音解析
|
* @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
|
*
|
*/
|