Commit a4901a848d7f9f2d5f1f6b41c96a7780055c5be7
1 parent
fc90635a
✨ feat(im): 增加 pinyin 功能
Showing
5 changed files
with
44 additions
and
1 deletions
yudao-dependencies/pom.xml
| ... | ... | @@ -51,6 +51,7 @@ |
| 51 | 51 | <anji-plus-captcha.version>1.4.0</anji-plus-captcha.version> |
| 52 | 52 | <jsoup.version>1.21.2</jsoup.version> |
| 53 | 53 | <sensitive-word.version>0.29.5</sensitive-word.version> |
| 54 | + <pinyin4j.version>2.5.1</pinyin4j.version> | |
| 54 | 55 | <lombok.version>1.18.42</lombok.version> |
| 55 | 56 | <mapstruct.version>1.6.3</mapstruct.version> |
| 56 | 57 | <hutool-5.version>5.8.42</hutool-5.version> |
| ... | ... | @@ -565,6 +566,12 @@ |
| 565 | 566 | <version>${sensitive-word.version}</version> |
| 566 | 567 | </dependency> |
| 567 | 568 | |
| 569 | + <dependency> | |
| 570 | + <groupId>com.belerweb</groupId> | |
| 571 | + <artifactId>pinyin4j</artifactId> <!-- 汉字转拼音:作为 hutool PinyinUtil 的底层引擎 --> | |
| 572 | + <version>${pinyin4j.version}</version> | |
| 573 | + </dependency> | |
| 574 | + | |
| 568 | 575 | <!-- Vert.x --> |
| 569 | 576 | <dependency> |
| 570 | 577 | <groupId>io.vertx</groupId> | ... | ... |
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; |
| 3 | 3 | import cn.hutool.core.text.StrPool; |
| 4 | 4 | import cn.hutool.core.util.ArrayUtil; |
| 5 | 5 | import cn.hutool.core.util.StrUtil; |
| 6 | +import cn.hutool.extra.pinyin.PinyinUtil; | |
| 6 | 7 | import org.aspectj.lang.JoinPoint; |
| 7 | 8 | |
| 8 | 9 | import java.util.Arrays; |
| ... | ... | @@ -79,6 +80,25 @@ public class StrUtils { |
| 79 | 80 | } |
| 80 | 81 | |
| 81 | 82 | /** |
| 83 | + * 转小写拼音,字之间以空格分隔,便于调用方按需拼接 / 取首字母 / 拼音搜索 | |
| 84 | + * | |
| 85 | + * 例:「老张」→ "lao zhang"、「ZhangSan」→ "zhangsan" | |
| 86 | + * 英文 / 数字 / 符号原样返回,空值返回 null | |
| 87 | + * | |
| 88 | + * 注意:底层依赖 hutool-extra 的 {@link PinyinUtil},需要业务模块自行引入拼音引擎依赖 | |
| 89 | + * (pinyin4j / TinyPinyin / Bopomofo4j 任选其一),否则运行时会抛 NoClassDefFoundError | |
| 90 | + * | |
| 91 | + * @param str 字符串 | |
| 92 | + * @return 拼音串(保留空格分隔) | |
| 93 | + */ | |
| 94 | + public static String toPinyin(String str) { | |
| 95 | + if (StrUtil.isBlank(str)) { | |
| 96 | + return null; | |
| 97 | + } | |
| 98 | + return PinyinUtil.getPinyin(str); | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 82 | 102 | * 拼接方法的参数 |
| 83 | 103 | * |
| 84 | 104 | * 特殊:排除一些无法序列化的参数,如 ServletRequest、ServletResponse、MultipartFile | ... | ... |
yudao-module-im/pom.xml
| ... | ... | @@ -68,6 +68,12 @@ |
| 68 | 68 | <artifactId>sensitive-word</artifactId> |
| 69 | 69 | </dependency> |
| 70 | 70 | |
| 71 | + <!-- 汉字转拼音:作为 hutool PinyinUtil 的底层引擎,业务侧调用 StrUtils.toPinyin 时按需引入 --> | |
| 72 | + <dependency> | |
| 73 | + <groupId>com.belerweb</groupId> | |
| 74 | + <artifactId>pinyin4j</artifactId> | |
| 75 | + </dependency> | |
| 76 | + | |
| 71 | 77 | <!-- Test 测试相关 --> |
| 72 | 78 | <dependency> |
| 73 | 79 | <groupId>cn.iocoder.boot</groupId> | ... | ... |
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; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.util.collection.MapUtils; |
| 6 | 6 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 7 | +import cn.iocoder.yudao.framework.common.util.string.StrUtils; | |
| 7 | 8 | import cn.iocoder.yudao.module.im.controller.admin.friend.vo.ImFriendRespVO; |
| 8 | 9 | import cn.iocoder.yudao.module.im.controller.admin.friend.vo.ImFriendUpdateReqVO; |
| 9 | 10 | import cn.iocoder.yudao.module.im.dal.dataobject.friend.ImFriendDO; |
| ... | ... | @@ -94,6 +95,9 @@ public class ImFriendController { |
| 94 | 95 | ImFriendRespVO vo = BeanUtils.toBean(f, ImFriendRespVO.class); |
| 95 | 96 | MapUtils.findAndThen(userMap, f.getFriendUserId(), user -> |
| 96 | 97 | vo.setNickname(user.getNickname()).setAvatar(user.getAvatar())); |
| 98 | + // 备注 / 昵称的拼音,给前端做字母分桶 + 拼音搜索 | |
| 99 | + vo.setDisplayNamePinyin(StrUtils.toPinyin(vo.getDisplayName())) | |
| 100 | + .setNicknamePinyin(StrUtils.toPinyin(vo.getNickname())); | |
| 97 | 101 | return vo; |
| 98 | 102 | }); |
| 99 | 103 | } | ... | ... |
yudao-module-im/src/main/java/cn/iocoder/yudao/module/im/controller/admin/friend/vo/ImFriendRespVO.java
| ... | ... | @@ -26,7 +26,10 @@ public class ImFriendRespVO { |
| 26 | 26 | @Schema(description = "好友展示备注(仅自己可见)", example = "老张") |
| 27 | 27 | private String displayName; |
| 28 | 28 | |
| 29 | - @Schema(description = "好友状态(0=正常,1=已删除)", example = "0") | |
| 29 | + @Schema(description = "好友展示备注的拼音(小写无空格)", example = "laozhang") | |
| 30 | + private String displayNamePinyin; | |
| 31 | + | |
| 32 | + @Schema(description = "好友状态", example = "0") | |
| 30 | 33 | private Integer status; |
| 31 | 34 | |
| 32 | 35 | @Schema(description = "添加好友时间") |
| ... | ... | @@ -40,6 +43,9 @@ public class ImFriendRespVO { |
| 40 | 43 | @Schema(description = "好友昵称(实时聚合自 AdminUser)", example = "芋道") |
| 41 | 44 | private String nickname; |
| 42 | 45 | |
| 46 | + @Schema(description = "好友昵称的拼音(小写无空格)", example = "yudao") | |
| 47 | + private String nicknamePinyin; | |
| 48 | + | |
| 43 | 49 | @Schema(description = "好友头像(实时聚合自 AdminUser)") |
| 44 | 50 | private String avatar; |
| 45 | 51 | ... | ... |