Browse Source

Merge branch 'dev/v4.8.61_dx' of qmx/jy into feature/v4.8.61

duxin 1 year ago
parent
commit
b3ac54a313
3 changed files with 68 additions and 36 deletions
  1. 1 0
      src/go.mod
  2. 29 13
      src/jfw/config/config.go
  3. 38 23
      src/jfw/front/pcIndex.go

+ 1 - 0
src/go.mod

@@ -12,6 +12,7 @@ require (
 	github.com/bwmarrin/snowflake v0.3.0
 	github.com/fsnotify/fsnotify v1.6.0
 	github.com/gogf/gf/v2 v2.4.4
+	github.com/mozillazg/go-pinyin v0.20.0
 	github.com/robfig/cron v1.2.0
 	github.com/zeromicro/go-zero v1.5.3
 	go.mongodb.org/mongo-driver v1.11.7

+ 29 - 13
src/jfw/config/config.go

@@ -10,9 +10,9 @@ import (
 	"github.com/gogf/gf/v2/frame/g"
 	"github.com/gogf/gf/v2/os/gcfg"
 	"github.com/gogf/gf/v2/os/gctx"
+	"github.com/mozillazg/go-pinyin"
 	"strings"
 	"sync"
-	"unicode"
 )
 
 var (
@@ -126,6 +126,7 @@ func GetLetterMap(names string) []map[string]interface{} {
 					if v1 == v2["name"] {
 						isNormal = true
 						letter := util.ObjToString(v2["letter"])
+						//判断问题
 						id := util.Int64All(v2["id"])
 						v1 += util.ObjToString(Seoconfig["seoKeywordSuffix"])
 						data = append(data, map[string]interface{}{
@@ -139,7 +140,7 @@ func GetLetterMap(names string) []map[string]interface{} {
 		if !isNormal {
 			nameArr2 = append(nameArr2, v1)
 			nameArr3 = append(nameArr3, fmt.Sprintf(`"%s"`, v1))
-			letter := getFirstLetter(v1)
+			letter := getFirstChar(v1)
 			nameArr4 = append(nameArr4, fmt.Sprintf(`("%s", "%s", 2)`, v1, letter))
 		}
 	}
@@ -162,18 +163,33 @@ func GetLetterMap(names string) []map[string]interface{} {
 	return data
 }
 
-// 获取字符串的首字母大写形式
-func getFirstLetter(s string) string {
-	words := strings.Fields(s) // 将字符串分割成单词
-	var result string
-	for _, word := range words {
-		firstChar := []rune(word)[0] // 获取单词的第一个字符
+// 获取单词的首字母大写
+func getFirstChar(word string) string {
+	firstChar := string(word[0])
 
-		// 检查字符是否为汉字或英文字母
-		if unicode.Is(unicode.Han, firstChar) || unicode.IsLetter(firstChar) {
-			result += strings.ToUpper(string(firstChar))
-		}
+	// 检查第一个字符是否是英文字母
+	if isEnglishLetter(firstChar) {
+		return strings.ToUpper(firstChar)
 	}
 
-	return result
+	// 转换为拼音并获取首字母大写
+	py := convertToPinyin(word)
+	return strings.ToUpper(string(py[0]))
+}
+
+// 判断字符是否是英文字母
+func isEnglishLetter(char string) bool {
+	return len(char) == 1 && ((char >= "a" && char <= "z") || (char >= "A" && char <= "Z"))
+}
+
+// 将中文单词转换为拼音
+func convertToPinyin(word string) string {
+	p := pinyin.NewArgs()
+	p.Style = pinyin.FirstLetter
+	pys := pinyin.Pinyin(word, p)
+	pinyinStr := ""
+	for _, py := range pys {
+		pinyinStr += py[0]
+	}
+	return pinyinStr
 }

+ 38 - 23
src/jfw/front/pcIndex.go

@@ -1,9 +1,19 @@
 package front
 
 import (
+	util "app.yhyue.com/moapp/jybase/common"
+	"app.yhyue.com/moapp/jybase/encrypt"
+	elastic "app.yhyue.com/moapp/jybase/es"
+	"app.yhyue.com/moapp/jybase/go-xweb/xweb"
+	"app.yhyue.com/moapp/jybase/redis"
+	"app.yhyue.com/moapp/jypkg/common/src/qfw/util/bidsearch"
+	"app.yhyue.com/moapp/jypkg/common/src/qfw/util/jy"
+	"app.yhyue.com/moapp/jypkg/jylabutil"
+	"app.yhyue.com/moapp/jypkg/public"
 	"encoding/json"
 	"fmt"
 	"github.com/gogf/gf/v2/util/gconv"
+	"github.com/mozillazg/go-pinyin"
 	"jy/src/jfw/config"
 	"jy/src/jfw/site/jySchool"
 	"jy/src/jfw/wx"
@@ -13,17 +23,6 @@ import (
 	"strconv"
 	"strings"
 	"time"
-	"unicode"
-
-	util "app.yhyue.com/moapp/jybase/common"
-	"app.yhyue.com/moapp/jybase/encrypt"
-	elastic "app.yhyue.com/moapp/jybase/es"
-	"app.yhyue.com/moapp/jybase/go-xweb/xweb"
-	"app.yhyue.com/moapp/jybase/redis"
-	"app.yhyue.com/moapp/jypkg/common/src/qfw/util/bidsearch"
-	"app.yhyue.com/moapp/jypkg/common/src/qfw/util/jy"
-	"app.yhyue.com/moapp/jypkg/jylabutil"
-	"app.yhyue.com/moapp/jypkg/public"
 )
 
 const (
@@ -929,7 +928,7 @@ func getLetterMap(nameArr []string) []*hotKeyWord {
 		if !isNormal {
 			nameArr2 = append(nameArr2, v1)
 			nameArr3 = append(nameArr3, fmt.Sprintf(`"%s"`, v1))
-			letter := getFirstLetter(v1)
+			letter := getFirstChar(v1)
 			nameArr4 = append(nameArr4, fmt.Sprintf(`("%s", "%s", 2)`, v1, letter))
 		}
 	}
@@ -952,17 +951,33 @@ func getLetterMap(nameArr []string) []*hotKeyWord {
 	return data
 }
 
-// 获取字符串的首字母大写形式
-func getFirstLetter(s string) string {
-	words := strings.Fields(s) // 将字符串分割成单词
-	var result string
-	for _, word := range words {
-		firstChar := []rune(word)[0] // 获取单词的第一个字符
+// 获取单词的首字母大写
+func getFirstChar(word string) string {
+	firstChar := string(word[0])
 
-		// 检查字符是否为汉字或英文字母
-		if unicode.Is(unicode.Han, firstChar) || unicode.IsLetter(firstChar) {
-			result += strings.ToUpper(string(firstChar))
-		}
+	// 检查第一个字符是否是英文字母
+	if isEnglishLetter(firstChar) {
+		return strings.ToUpper(firstChar)
+	}
+
+	// 转换为拼音并获取首字母大写
+	py := convertToPinyin(word)
+	return strings.ToUpper(string(py[0]))
+}
+
+// 判断字符是否是英文字母
+func isEnglishLetter(char string) bool {
+	return len(char) == 1 && ((char >= "a" && char <= "z") || (char >= "A" && char <= "Z"))
+}
+
+// 将中文单词转换为拼音
+func convertToPinyin(word string) string {
+	p := pinyin.NewArgs()
+	p.Style = pinyin.FirstLetter
+	pys := pinyin.Pinyin(word, p)
+	pinyinStr := ""
+	for _, py := range pys {
+		pinyinStr += py[0]
 	}
-	return result
+	return pinyinStr
 }