perf(novel-front): 优化昵称显示

This commit is contained in:
xiongxiaoyang committed 2026-10-21 11:49:50 +08:00
1 parent 8ed4f0551b
commit 62b2507eed
11 files changed
+56 -17

No files matched your search

@@ -3,7 +3,7 @@ package com.java2nb.novel.core.serialize;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.apache.commons.lang3.StringUtils;
import com.java2nb.novel.core.utils.NameMaskUtil;
import java.io.IOException;
@@ -11,10 +11,6 @@ public class CommentUserNameSerialize extends JsonSerializer<String> {
@Override
public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
if(StringUtils.isNotBlank(s)){
jsonGenerator.writeString(s.substring(0, 4) + "****" + s.substring(s.length() - 3));
}
jsonGenerator.writeString(NameMaskUtil.maskIfNeeded(s));
}
}
@@ -0,0 +1,46 @@
package com.java2nb.novel.core.utils;
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Pattern;
/**
* 用户名/昵称展示脱敏工具
* <p>
* 若昵称是手机号格式,隐藏中间四位;其它情况原样返回。
*
* @author 11797
*/
public final class NameMaskUtil {
/**
* 中国大陆手机号格式:1开头,第二位3-9,共11位
*/
private static final Pattern PHONE_PATTERN = Pattern.compile("^1[3-9]\\d{9}$");
private static final String DEFAULT_DISPLAY = "匿名用户";
private NameMaskUtil() {
}
/**
* 判断字符串是否为手机号格式
*/
public static boolean isPhone(String s) {
return StringUtils.isNotBlank(s) && PHONE_PATTERN.matcher(s).matches();
}
/**
* 展示用脱敏:空返回「匿名用户」;手机号中间四位打码;其它原样
*/
public static String maskIfNeeded(String s) {
if (StringUtils.isBlank(s)) {
return DEFAULT_DISPLAY;
}
String trim = s.trim();
if (isPhone(trim)) {
return trim.substring(0, 3) + "****" + trim.substring(trim.length() - 4);
}
return trim;
}
}
@@ -4,7 +4,7 @@
<mapper namespace="com.java2nb.novel.mapper.FrontBookCommentMapper">
<select id="listCommentByPage" resultType="com.java2nb.novel.vo.BookCommentVO">
select t1.id,t1.book_id,t1.comment_content,t1.location,t1.reply_count,t1.create_time,t2.username create_user_name,t2.user_photo create_user_photo,
select t1.id,t1.book_id,t1.comment_content,t1.location,t1.reply_count,t1.create_time,IFNULL(NULLIF(t2.nick_name, ''), t2.username) create_user_name,t2.user_photo create_user_photo,
t3.book_name,t3.pic_url
from book_comment t1 inner join user t2 on t1.create_user_id = t2.id
left join book t3 on t1.book_id = t3.id
@@ -4,7 +4,7 @@
<mapper namespace="com.java2nb.novel.mapper.FrontBookCommentReplyMapper">
<select id="listCommentReplyByPage" resultType="com.java2nb.novel.vo.BookCommentReplyVO">
select t1.id,t1.reply_content,t1.location,t1.create_time,t2.username create_user_name,t2.user_photo create_user_photo
select t1.id,t1.reply_content,t1.location,t1.create_time,IFNULL(NULLIF(t2.nick_name, ''), t2.username) create_user_name,t2.user_photo create_user_photo
from book_comment_reply t1 inner join user t2 on t1.create_user_id = t2.id
<trim>
<if test="commentId != null">
@@ -14,7 +14,7 @@
<select id="listByParagraph" resultType="com.java2nb.novel.vo.BookParagraphCommentVO">
select t1.id, t1.book_id, t1.book_index_id, t1.paragraph_idx, t1.root_id, t1.reply_user_name,
t1.reply_count, t1.comment_content, t1.location, t1.create_user_id, t1.create_time,
t2.username create_user_name, t2.user_photo create_user_photo
IFNULL(NULLIF(t2.nick_name, ''), t2.username) create_user_name, t2.user_photo create_user_photo
from book_paragraph_comment t1
inner join user t2 on t1.create_user_id = t2.id
where t1.book_index_id = #{bookIndexId}
@@ -26,7 +26,7 @@
<select id="getById" resultType="com.java2nb.novel.vo.BookParagraphCommentVO">
select t1.id, t1.book_id, t1.book_index_id, t1.paragraph_idx, t1.root_id, t1.reply_user_name,
t1.reply_count, t1.comment_content, t1.location, t1.create_user_id, t1.create_time,
t2.username create_user_name, t2.user_photo create_user_photo
IFNULL(NULLIF(t2.nick_name, ''), t2.username) create_user_name, t2.user_photo create_user_photo
from book_paragraph_comment t1
inner join user t2 on t1.create_user_id = t2.id
where t1.id = #{id}
@@ -10,7 +10,7 @@
<select id="listByPage" resultType="com.java2nb.novel.vo.UserMessageVO">
select t1.id, t1.user_id, t1.send_user_id, t1.msg_type, t1.book_id, t1.book_index_id, t1.target_id,
t1.content, t1.read_flag, t1.create_time,
t2.username send_user_name, t2.user_photo send_user_photo,
IFNULL(NULLIF(t2.nick_name, ''), t2.username) send_user_name, t2.user_photo send_user_photo,
t3.book_name, t4.index_name
from user_message t1
left join user t2 on t1.send_user_id = t2.id
@@ -155,4 +155,3 @@ function checkPicUpload(file) {
}
return true;
}
@@ -197,5 +197,3 @@ String.prototype.isNickName = function () {
}
return false;
};
@@ -109,7 +109,7 @@
style="display: none;">见习</span></div>
<ul class="pl_bar fr">
<li class="name"><span
th:text="${#strings.substring(comment.createUserName,0,4)}+'****'+${#strings.substring(comment.createUserName,#strings.length(comment.createUserName)-3,#strings.length(comment.createUserName))}"></span><span
th:text="${T(com.java2nb.novel.core.utils.NameMaskUtil).maskIfNeeded(comment.createUserName)}"></span><span
style="padding-left: 10px" class="other" th:if="${comment.location}"
th:text="${comment.location} + '读者'"></span></span></li>
<li class="dec" th:utext="${comment.commentContent}"></li>
@@ -424,7 +424,7 @@
<div style="margin-left:52px;">
<div style="font-size:14px;">
<span style="color:#444;"
th:text="${#strings.length(comment.createUserName) > 4} ? ${#strings.substring(comment.createUserName,0,4)}+'****'+${#strings.substring(comment.createUserName,#strings.length(comment.createUserName)-3,#strings.length(comment.createUserName))} : ${comment.createUserName}"></span>
th:text="${T(com.java2nb.novel.core.utils.NameMaskUtil).maskIfNeeded(comment.createUserName)}"></span>
<span th:if="${comment.location}" style="color:#999;margin-left:8px;"
th:text="${comment.location}+'读者'"></span>
<span style="color:#999;float:right;font-size:12px;"
@@ -69,7 +69,7 @@
"<div class=\"user_heads fl\" vals=\"389\">" +
"<img src=\"" + (comment.createUserPhoto ? comment.createUserPhoto : '/images/man.png') + "\" class=\"user_head\" alt=\"\">" +
"<span class=\"user_level1\" style=\"display: none;\">见习</span></div>" +
"<ul class=\"pl_bar fr\">\t\t\t<li class=\"name\">" + (comment.createUserName.substr(0, 4) + "****" + comment.createUserName.substr(comment.createUserName.length - 3, 3)) + "</li><li class=\"dec\">" +
"<ul class=\"pl_bar fr\">\t\t\t<li class=\"name\">" + (comment.createUserName) + "</li><li class=\"dec\">" +
comment.commentContent +
"</li><li class=\"other cf\">" +
"<span class=\"time fl\">" + comment.createTime + "</span>" +