From a4901a848d7f9f2d5f1f6b41c96a7780055c5be7 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 30 Apr 2026 15:22:35 +0800 Subject: [PATCH] ✨ feat(im): 增加 pinyin 功能 --- yudao-dependencies/pom.xml | 7 +++++++ yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/string/StrUtils.java | 20 ++++++++++++++++++++ yudao-module-im/pom.xml | 6 ++++++ yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/friend/ImFriendController.java | 4 ++++ yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/friend/vo/ImFriendRespVO.java | 8 +++++++- 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index 07be435..7fdec84 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -51,6 +51,7 @@ 1.4.0 1.21.2 0.29.5 + 2.5.1 1.18.42 1.6.3 5.8.42 @@ -565,6 +566,12 @@ ${sensitive-word.version} + + com.belerweb + pinyin4j + ${pinyin4j.version} + + io.vertx diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/string/StrUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/string/StrUtils.java index 47a53a5..f0e423e 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/string/StrUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/string/StrUtils.java @@ -3,6 +3,7 @@ package cn.iocoder.yudao.framework.common.util.string; import cn.hutool.core.text.StrPool; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.StrUtil; +import cn.hutool.extra.pinyin.PinyinUtil; import org.aspectj.lang.JoinPoint; import java.util.Arrays; @@ -79,6 +80,25 @@ public class StrUtils { } /** + * 转小写拼音,字之间以空格分隔,便于调用方按需拼接 / 取首字母 / 拼音搜索 + * + * 例:「老张」→ "lao zhang"、「ZhangSan」→ "zhangsan" + * 英文 / 数字 / 符号原样返回,空值返回 null + * + * 注意:底层依赖 hutool-extra 的 {@link PinyinUtil},需要业务模块自行引入拼音引擎依赖 + * (pinyin4j / TinyPinyin / Bopomofo4j 任选其一),否则运行时会抛 NoClassDefFoundError + * + * @param str 字符串 + * @return 拼音串(保留空格分隔) + */ + public static String toPinyin(String str) { + if (StrUtil.isBlank(str)) { + return null; + } + return PinyinUtil.getPinyin(str); + } + + /** * 拼接方法的参数 * * 特殊:排除一些无法序列化的参数,如 ServletRequest、ServletResponse、MultipartFile diff --git a/yudao-module-im/pom.xml b/yudao-module-im/pom.xml index 18354f6..c64c05e 100644 --- a/yudao-module-im/pom.xml +++ b/yudao-module-im/pom.xml @@ -68,6 +68,12 @@ sensitive-word + + + com.belerweb + pinyin4j + + cn.iocoder.boot diff --git a/yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/friend/ImFriendController.java b/yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/friend/ImFriendController.java index b526ad6..7f885df 100644 --- a/yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/friend/ImFriendController.java +++ b/yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/friend/ImFriendController.java @@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.util.collection.MapUtils; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; +import cn.iocoder.yudao.framework.common.util.string.StrUtils; import cn.iocoder.yudao.module.im.controller.admin.friend.vo.ImFriendRespVO; import cn.iocoder.yudao.module.im.controller.admin.friend.vo.ImFriendUpdateReqVO; import cn.iocoder.yudao.module.im.dal.dataobject.friend.ImFriendDO; @@ -94,6 +95,9 @@ public class ImFriendController { ImFriendRespVO vo = BeanUtils.toBean(f, ImFriendRespVO.class); MapUtils.findAndThen(userMap, f.getFriendUserId(), user -> vo.setNickname(user.getNickname()).setAvatar(user.getAvatar())); + // 备注 / 昵称的拼音,给前端做字母分桶 + 拼音搜索 + vo.setDisplayNamePinyin(StrUtils.toPinyin(vo.getDisplayName())) + .setNicknamePinyin(StrUtils.toPinyin(vo.getNickname())); return vo; }); } diff --git a/yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/friend/vo/ImFriendRespVO.java b/yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/friend/vo/ImFriendRespVO.java index ebefe1e..b3ff24c 100644 --- a/yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/friend/vo/ImFriendRespVO.java +++ b/yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/friend/vo/ImFriendRespVO.java @@ -26,7 +26,10 @@ public class ImFriendRespVO { @Schema(description = "好友展示备注(仅自己可见)", example = "老张") private String displayName; - @Schema(description = "好友状态(0=正常,1=已删除)", example = "0") + @Schema(description = "好友展示备注的拼音(小写无空格)", example = "laozhang") + private String displayNamePinyin; + + @Schema(description = "好友状态", example = "0") private Integer status; @Schema(description = "添加好友时间") @@ -40,6 +43,9 @@ public class ImFriendRespVO { @Schema(description = "好友昵称(实时聚合自 AdminUser)", example = "芋道") private String nickname; + @Schema(description = "好友昵称的拼音(小写无空格)", example = "yudao") + private String nicknamePinyin; + @Schema(description = "好友头像(实时聚合自 AdminUser)") private String avatar; -- libgit2 0.21.4