fix comment list emoji charset nickName

main
wangqing 5 months ago
parent 25fe4dba55
commit 25ed14277d

@ -12,6 +12,7 @@ import com.luoo.comment.service.CommentService;
import com.luoo.comment.service.ComplaintService;
import com.luoo.comment.service.LikeService;
import com.luoo.comment.service.UserInfoService;
import com.luoo.comment.util.EmojiConverterUtil;
import com.luoo.comment.util.IpUtil;
import com.luoo.comment.util.MySensitiveWordReplaceUtils;
import constants.Constants;
@ -199,7 +200,7 @@ public class CommentController extends BaseController {
commentResp.setNickName("已注销");
commentResp.setAvatar(Constants.RESOURCE_PREFIX + Constants.DEFAULT_USER_THUMBNAIL);
if(null != commentUserInfo) {
commentResp.setNickName(commentUserInfo.getNickName());
commentResp.setNickName(EmojiConverterUtil.encode(commentUserInfo.getNickName()));
commentResp.setAvatar(getAvatar(commentUserInfo));
if(StringUtils.isNotEmpty(comment.getReplyToUserId())){
UserInfo replyToUserInfo = userInfoService.findById(comment.getReplyToUserId());
@ -227,7 +228,7 @@ public class CommentController extends BaseController {
topComment.setNickName("已注销");
topComment.setAvatar(Constants.RESOURCE_PREFIX + Constants.DEFAULT_USER_THUMBNAIL);
if(null != topCommentUserInfo) {
topComment.setNickName(topCommentUserInfo.getNickName());
topComment.setNickName(EmojiConverterUtil.encode(topCommentUserInfo.getNickName()));
topComment.setAvatar(getAvatar(topCommentUserInfo));
}
if(StringUtils.isNotEmpty(topComment.getReplyToUserId())){
@ -270,7 +271,7 @@ public class CommentController extends BaseController {
commentResp.setNickName("已注销");
commentResp.setAvatar(Constants.RESOURCE_PREFIX + Constants.DEFAULT_USER_THUMBNAIL);
if (null != commentUserInfo) {
commentResp.setNickName(commentUserInfo.getNickName());
commentResp.setNickName(EmojiConverterUtil.encode(commentUserInfo.getNickName()));
commentResp.setAvatar(getAvatar(commentUserInfo));
if (StringUtils.isNotEmpty(commentUserInfo.getBadges())){
Set<Integer> badgeSet = new HashSet<>();
@ -289,7 +290,7 @@ public class CommentController extends BaseController {
topComment.setNickName("已注销");
topComment.setAvatar(Constants.RESOURCE_PREFIX + Constants.DEFAULT_USER_THUMBNAIL);
if(null != topCommentUserInfo) {
topComment.setNickName(topCommentUserInfo.getNickName());
topComment.setNickName(EmojiConverterUtil.encode(topCommentUserInfo.getNickName()));
topComment.setAvatar(getAvatar(topCommentUserInfo));
}

@ -0,0 +1,60 @@
package com.luoo.comment.util;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmojiConverterUtil {
private static final String encodePatternString = "([\\x{10000}-\\x{10ffff}\ud800-\udfff])";
private static final Pattern encodePattern = Pattern.compile(encodePatternString);
private static final String decodePatternString = "\\[\\[(.*?)\\]\\]";
private static final Pattern decodePattern = Pattern.compile(decodePatternString);
/**
* @Description emojiutf-84utf8mb4
* @param str
* @return
* @throws UnsupportedEncodingException exception
*/
public static String encode(String str) {
if (null == str) {
return null;
}
Matcher matcher = encodePattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
try {
matcher.appendReplacement(sb, "[[" + URLEncoder.encode(matcher.group(1), "UTF-8") + "]]");
} catch (UnsupportedEncodingException e) {
}
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* @Description utf8emoji
* @param str
* @return exception
*/
public static String decode(String str) {
if (null == str) {
return null;
}
Matcher matcher = decodePattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
try {
matcher.appendReplacement(sb, URLDecoder.decode(matcher.group(1), "UTF-8"));
} catch (UnsupportedEncodingException e) {
}
}
matcher.appendTail(sb);
return sb.toString();
}
}
Loading…
Cancel
Save