package com.yeshi.fanli.util.goods;
|
|
import com.yeshi.fanli.util.StringUtil;
|
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
|
import org.jsoup.Jsoup;
|
|
import java.util.List;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
/**
|
* 商品文案
|
*/
|
public class GoodsTextUtil {
|
|
|
/**
|
* APP内推荐语编码
|
*
|
* @param content
|
* @param linkIconPath
|
* @param linkName
|
* @return
|
*/
|
public static String encodeAppHtmlText(String content, String linkIconPath, String linkName) {
|
//将链接替换成客户端可展示的内容
|
//提取链接
|
String result = content;
|
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(content);
|
while (matcher.find()) {
|
String url = matcher.group();
|
if (!StringUtil.isNullOrEmpty(url)) {
|
//将链接替换成APP输出的标准模式
|
result = result.replace(url, String.format("<img src='%s'><a href=\"%s\">%s</a>", linkIconPath, url, linkName));
|
}
|
}
|
//提取淘口令;
|
List<String> taoBaoTokenList = TaoBaoUtil.getTokenListFromTextWithKuoHao(result);
|
if (taoBaoTokenList != null)
|
for (String token : taoBaoTokenList) {
|
//替换淘口令
|
result = result.replace(token, String.format("<img src='%s'><a href=\"%s\">%s</a>", linkIconPath, token, linkName));
|
}
|
//替换换行符
|
result = result.replace("\n", "<br>");
|
return result;
|
}
|
|
/**
|
* APP内推荐语反编码
|
*
|
* @param decodeContent
|
* @return
|
*/
|
public static String decodeAppHtmlText(String decodeContent) {
|
//替换img标签
|
String result = decodeContent.replaceAll("<img[^>]*>", "");
|
//替换a标签
|
Pattern p = Pattern.compile("<a[^>]*>([^<]*)</a>");
|
Matcher m = p.matcher(result);
|
while (m.find()) {
|
String r = m.group(0);
|
String href = Jsoup.parse(r).getElementsByTag("a").get(0).attr("href");
|
result = result.replace(r, href);
|
}
|
//替换换行符
|
result = result.replace("<br>", "\n");
|
return result;
|
}
|
|
|
}
|