From 83f19efecf942fa0d6eeee7a3b6da1d2b25db9e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Mon, 15 Aug 2022 17:10:25 +0800 Subject: [PATCH] =?UTF-8?q?enhance(utils):=E5=B0=86=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E6=8B=BC=E9=9F=B3=E6=89=80=E5=86=99=E7=9A=84=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E6=8F=90=E5=8F=96=E5=88=B0=E5=85=AC=E5=85=B1=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/user.go | 11 +---------- utils/utils.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/service/user.go b/service/user.go index bbf3f91..d3accc5 100644 --- a/service/user.go +++ b/service/user.go @@ -10,12 +10,9 @@ import ( "electricity_bill_calc/repository" "electricity_bill_calc/utils" "fmt" - "log" - "strings" "time" "github.com/google/uuid" - "github.com/mozillazg/go-pinyin" "xorm.io/builder" ) @@ -197,14 +194,8 @@ func (u _UserService) CreateUser(user *model.User, detail *model.UserDetail) (st user.ResetNeeded = true if detail.Name != nil { - abbr := pinyin.Pinyin(*detail.Name, pinyin.NewArgs()) - var abbrCollect = make([]string, 0) - for _, a := range abbr { - abbrCollect = append(abbrCollect, a[0][0:1]) - } - finalAbbr := strings.Join(abbrCollect, "") + finalAbbr := utils.PinyinAbbr(*detail.Name) detail.Abbr = &finalAbbr - log.Printf("[service] [debug] detail: %v", detail) } tx := global.DBConn.NewSession() diff --git a/utils/utils.go b/utils/utils.go index 079af2f..66b76ba 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,5 +1,11 @@ package utils +import ( + "strings" + + "github.com/mozillazg/go-pinyin" +) + func Contains[T string | int | uint](element T, slice []T) bool { for _, v := range slice { if v == element { @@ -8,3 +14,13 @@ func Contains[T string | int | uint](element T, slice []T) bool { } return false } + +func PinyinAbbr(source string) string { + abbr := pinyin.Pinyin(source, pinyin.NewArgs()) + var abbrCollect = make([]string, 0) + for _, a := range abbr { + abbrCollect = append(abbrCollect, a[0][0:1]) + } + finalAbbr := strings.Join(abbrCollect, "") + return finalAbbr +}