From 03bade2c1fd46f5cd806de5f565c46b543f483db Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com>
Date: Sat, 26 Aug 2023 11:27:40 +0800
Subject: [PATCH] =?UTF-8?q?fix:=E6=B5=8B=E8=AF=95=E6=8B=BC=E9=9F=B3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 5 ++
.../springbootkwan/filter/CorsFilter.java | 9 +++-
.../kwan/springbootkwan/filter/MyFilter.java | 31 ++++++++++++
.../kwan/springbootkwan/utils/PinyinUtil.java | 49 +++++++++++++++++++
.../springbootkwan/UserServiceImplTest.java | 4 +-
.../springbootkwan/utils/PinyinUtilTest.java | 19 +++++++
6 files changed, 113 insertions(+), 4 deletions(-)
create mode 100644 src/main/java/com/kwan/springbootkwan/filter/MyFilter.java
create mode 100644 src/main/java/com/kwan/springbootkwan/utils/PinyinUtil.java
create mode 100644 src/test/java/com/kwan/springbootkwan/utils/PinyinUtilTest.java
diff --git a/pom.xml b/pom.xml
index aea2df3..4bf3d66 100644
--- a/pom.xml
+++ b/pom.xml
@@ -187,6 +187,11 @@
poi-ooxml
4.1.2
+
+ com.hankcs
+ hanlp
+ portable-1.8.3
+
diff --git a/src/main/java/com/kwan/springbootkwan/filter/CorsFilter.java b/src/main/java/com/kwan/springbootkwan/filter/CorsFilter.java
index 1f95cb8..c4f8d6e 100644
--- a/src/main/java/com/kwan/springbootkwan/filter/CorsFilter.java
+++ b/src/main/java/com/kwan/springbootkwan/filter/CorsFilter.java
@@ -1,8 +1,13 @@
package com.kwan.springbootkwan.filter;
+import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
-import javax.servlet.*;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@@ -14,6 +19,7 @@ import java.io.IOException;
* @version : 2.2.0
* @date : 2023/7/9 22:54
*/
+@Order(1)
@Component
public class CorsFilter implements Filter {
@@ -25,7 +31,6 @@ public class CorsFilter implements Filter {
httpResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
-
chain.doFilter(request, response);
}
}
diff --git a/src/main/java/com/kwan/springbootkwan/filter/MyFilter.java b/src/main/java/com/kwan/springbootkwan/filter/MyFilter.java
new file mode 100644
index 0000000..74c4a13
--- /dev/null
+++ b/src/main/java/com/kwan/springbootkwan/filter/MyFilter.java
@@ -0,0 +1,31 @@
+package com.kwan.springbootkwan.filter;
+
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import java.io.IOException;
+
+
+/**
+ * 添加过滤器
+ *
+ * @author : qinyingjie
+ * @version : 2.2.0
+ * @date : 2023/8/26 11:06
+ */
+@Component
+@Order(2) // 设置过滤器的优先级为1,值越小,优先级越高
+public class MyFilter implements Filter {
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+ throws IOException, ServletException {
+ // 过滤器逻辑
+ chain.doFilter(request, response);
+ }
+}
+
diff --git a/src/main/java/com/kwan/springbootkwan/utils/PinyinUtil.java b/src/main/java/com/kwan/springbootkwan/utils/PinyinUtil.java
new file mode 100644
index 0000000..9d7ac0b
--- /dev/null
+++ b/src/main/java/com/kwan/springbootkwan/utils/PinyinUtil.java
@@ -0,0 +1,49 @@
+package com.kwan.springbootkwan.utils;
+
+import com.hankcs.hanlp.dictionary.py.Pinyin;
+import com.hankcs.hanlp.dictionary.py.PinyinDictionary;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.util.CollectionUtils;
+
+import java.util.List;
+
+
+/**
+ * 拼音工具类
+ *
+ * @author : qinyingjie
+ * @version : 2.2.0
+ * @date : 2023/8/26 11:16
+ */
+public class PinyinUtil {
+
+ /**
+ * 获取中文完整拼音
+ *
+ * @param chineseStr
+ * @return
+ */
+ public static String getPinyin(String chineseStr) {
+ List pinyins = PinyinDictionary.convertToPinyin(chineseStr);
+ StringBuilder stringBuilder = new StringBuilder();
+ for (Pinyin pinyin : pinyins) {
+ stringBuilder.append(pinyin.getPinyinWithoutTone());
+ }
+ return stringBuilder.toString();
+ }
+
+
+ /**
+ * 获取中文拼音首字母
+ *
+ * @param chineseStr
+ * @return
+ */
+ public static String getInitial(String chineseStr) {
+ List pinyins = PinyinDictionary.convertToPinyin(chineseStr);
+ if (CollectionUtils.isEmpty(pinyins)) {
+ return StringUtils.EMPTY;
+ }
+ return String.valueOf(pinyins.get(0).getPinyinWithoutTone().charAt(0));
+ }
+}
diff --git a/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java b/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java
index b721ae1..c202ae8 100644
--- a/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java
+++ b/src/test/java/com/kwan/springbootkwan/UserServiceImplTest.java
@@ -5,7 +5,7 @@ import com.kwan.springbootkwan.entity.User;
import com.kwan.springbootkwan.mapper.UserMapper;
import com.kwan.springbootkwan.service.ISendMsgHandle;
import com.kwan.springbootkwan.utils.RedisIdWorker;
-import org.junit.jupiter.api.Test;
+import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@@ -129,7 +129,7 @@ public class UserServiceImplTest {
}
latch.await();
long end = System.currentTimeMillis();
- System.out.println("times = " + (end- begin));
+ System.out.println("times = " + (end - begin));
}
diff --git a/src/test/java/com/kwan/springbootkwan/utils/PinyinUtilTest.java b/src/test/java/com/kwan/springbootkwan/utils/PinyinUtilTest.java
new file mode 100644
index 0000000..4526860
--- /dev/null
+++ b/src/test/java/com/kwan/springbootkwan/utils/PinyinUtilTest.java
@@ -0,0 +1,19 @@
+package com.kwan.springbootkwan.utils;
+
+import com.kwan.springbootkwan.SpringBootKwanApplication;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+@ContextConfiguration(classes = SpringBootKwanApplication.class)
+public class PinyinUtilTest {
+
+ @Test
+ public void testName() {
+ assert "ceshi".equals(PinyinUtil.getPinyin("测试"));
+ }
+}
\ No newline at end of file
--
GitLab