enhance(utils):将转换拼音所写的功能提取到公共工具函数。

This commit is contained in:
徐涛 2022-08-15 17:10:25 +08:00
parent 224ae9b07d
commit 83f19efecf
2 changed files with 17 additions and 10 deletions

View File

@ -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()

View File

@ -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
}