feat(user):增加生成用户名称的拼音缩写,并提供带有限制条数的企业检索。
This commit is contained in:
parent
3c61cd9e82
commit
c3d3167e62
|
@ -37,6 +37,7 @@ func InitializeUserController(router *gin.Engine) {
|
||||||
UserController.Router.GET("/account/:uid", security.MustAuthenticated, UserController.GetUserDetail)
|
UserController.Router.GET("/account/:uid", security.MustAuthenticated, UserController.GetUserDetail)
|
||||||
UserController.Router.POST("/enterprise", security.OPSAuthorize, UserController.CreateEnterpriseAccount)
|
UserController.Router.POST("/enterprise", security.OPSAuthorize, UserController.CreateEnterpriseAccount)
|
||||||
UserController.Router.PUT("/account/:uid", security.OPSAuthorize, UserController.ModifyAccountDetail)
|
UserController.Router.PUT("/account/:uid", security.OPSAuthorize, UserController.ModifyAccountDetail)
|
||||||
|
UserController.Router.GET("/enterprise/quick/search", security.OPSAuthorize, UserController.QuickSearchEnterprise)
|
||||||
}
|
}
|
||||||
|
|
||||||
type LoginFormData struct {
|
type LoginFormData struct {
|
||||||
|
@ -346,3 +347,14 @@ func (_UserController) ModifyAccountDetail(c *gin.Context) {
|
||||||
}
|
}
|
||||||
result.Success("指定用户的信息已经更新。")
|
result.Success("指定用户的信息已经更新。")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (_UserController) QuickSearchEnterprise(c *gin.Context) {
|
||||||
|
result := response.NewResult(c)
|
||||||
|
keyword := c.Query("keyword")
|
||||||
|
searchResult, err := service.UserService.SearchLimitUsers(keyword, 6)
|
||||||
|
if err != nil {
|
||||||
|
result.Error(http.StatusInternalServerError, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
result.Json(http.StatusOK, "已查询到存在符合条件的企业", gin.H{"users": searchResult})
|
||||||
|
}
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -35,6 +35,7 @@ require (
|
||||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
|
github.com/mozillazg/go-pinyin v0.19.0 // indirect
|
||||||
github.com/pelletier/go-toml v1.9.5 // indirect
|
github.com/pelletier/go-toml v1.9.5 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
|
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
|
||||||
github.com/spf13/afero v1.8.2 // indirect
|
github.com/spf13/afero v1.8.2 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -385,6 +385,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
|
||||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
|
github.com/mozillazg/go-pinyin v0.19.0 h1:p+J8/kjJ558KPvVGYLvqBhxf8jbZA2exSLCs2uUVN8c=
|
||||||
|
github.com/mozillazg/go-pinyin v0.19.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
||||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||||
github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg=
|
github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg=
|
||||||
github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU=
|
github.com/nats-io/jwt v0.3.2/go.mod h1:/euKqTS1ZD+zzjYrY7pseZrTtWQSjujC7xjPc8wL6eU=
|
||||||
|
|
|
@ -10,9 +10,12 @@ import (
|
||||||
"electricity_bill_calc/repository"
|
"electricity_bill_calc/repository"
|
||||||
"electricity_bill_calc/utils"
|
"electricity_bill_calc/utils"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/mozillazg/go-pinyin"
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -193,6 +196,17 @@ func (u _UserService) CreateUser(user *model.User, detail *model.UserDetail) (st
|
||||||
user.Password = fmt.Sprintf("%x", hash.Sum(nil))
|
user.Password = fmt.Sprintf("%x", hash.Sum(nil))
|
||||||
user.ResetNeeded = true
|
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, "")
|
||||||
|
detail.Abbr = &finalAbbr
|
||||||
|
log.Printf("[service] [debug] detail: %v", detail)
|
||||||
|
}
|
||||||
|
|
||||||
tx := global.DBConn.NewSession()
|
tx := global.DBConn.NewSession()
|
||||||
defer tx.Close()
|
defer tx.Close()
|
||||||
if err := tx.Begin(); err != nil {
|
if err := tx.Begin(); err != nil {
|
||||||
|
@ -229,3 +243,23 @@ func (u _UserService) SwitchUserState(uid string, enabled bool) error {
|
||||||
_, err = global.DBConn.ID(uid).Cols("enabled").Update(newStateUser)
|
_, err = global.DBConn.ID(uid).Cols("enabled").Update(newStateUser)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (_UserService) SearchLimitUsers(keyword string, limit int) ([]model.JoinedUserDetail, error) {
|
||||||
|
var users = make([]model.JoinedUserDetail, 0)
|
||||||
|
err := global.DBConn.
|
||||||
|
Table("user_detail").Alias("d").
|
||||||
|
Join("INNER", []string{"user", "u"}, "d.id=u.id").
|
||||||
|
Where(
|
||||||
|
builder.NewCond().
|
||||||
|
Or(builder.Like{"u.username", keyword}).
|
||||||
|
Or(builder.Like{"d.name", keyword}).
|
||||||
|
Or(builder.Like{"d.abbr", keyword})).
|
||||||
|
And(builder.Eq{"u.type": 0}).
|
||||||
|
Asc("u.created_at").
|
||||||
|
Limit(limit).
|
||||||
|
Find(&users)
|
||||||
|
if err != nil {
|
||||||
|
return make([]model.JoinedUserDetail, 0), err
|
||||||
|
}
|
||||||
|
return users, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user