logo search

This commit is contained in:
eson 2023-10-20 15:59:03 +08:00
parent 615032a0a8
commit 956cbde1bc
5 changed files with 38 additions and 9 deletions

View File

@ -25,8 +25,33 @@ type PreLogoSearchResult struct {
// TODO: 使用model的属性做你想做的
// 搜索建议
// 搜索
func (p *FsPreprocessLogoModel) PreLogoSearch(ctx context.Context, zipcode string, keywordsStr string, count int) (resp []PreLogoSearchResult, err error) {
keywords := regexp.MustCompile(`\s+`).Split(keywordsStr, -1)
for i, v := range keywords {
keywords[i] = "+" + v + "*"
}
sqlstr := fmt.Sprintf("SELECT id,restaurant_name,resource_url,address,zip_code,phone,website,is_branch FROM fs_preprocess_logo WHERE MATCH(restaurant_name) AGAINST(? IN BOOLEAN MODE) and zipcode = ? limit %d;", count)
tx := p.db.WithContext(ctx).Model(&FsPreprocessLogo{}).Raw(sqlstr, strings.Join(keywords, " "), zipcode)
err = tx.Scan(&resp).Error
if err != nil {
logx.Error(err)
return nil, err
}
for i := range resp {
if resp[i].RestaurantType == nil {
resp[i].RestaurantType = FsString("")
}
resp[i].ResourceUrl = FsString(testData[rand.Uint64()%uint64(len(testData))])
}
return resp, nil
}
// 搜索建议
func (p *FsPreprocessLogoModel) PreLogoSearchSuggestions(ctx context.Context, keywordsStr string, count int) (resp []PreLogoSearchResult, err error) {
keywords := regexp.MustCompile(`\s+`).Split(keywordsStr, -1)
for i, v := range keywords {
keywords[i] = "+" + v + "*"

View File

@ -14,7 +14,7 @@ import (
func PreLogoSearchSuggestionsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PreLogoSearchRequest
var req types.PreLogoSearchSuggestionsRequest
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return

View File

@ -30,18 +30,14 @@ func NewPreLogoSearchSuggestionsLogic(ctx context.Context, svcCtx *svc.ServiceCo
// func (l *PreLogoSearchSuggestionsLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *PreLogoSearchSuggestionsLogic) PreLogoSearchSuggestions(req *types.PreLogoSearchRequest, userinfo *auth.UserInfo) (resp *basic.Response) {
func (l *PreLogoSearchSuggestionsLogic) PreLogoSearchSuggestions(req *types.PreLogoSearchSuggestionsRequest, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
if !userinfo.IsOnlooker() {
return resp.SetStatus(basic.CodeSearchAuthErr)
}
if len(req.ZipCode) < 4 {
return resp.SetStatus(basic.CodeSearchZipCodeErr)
}
result, err := l.svcCtx.AllModels.FsPreprocessLogo.PreLogoSearch(l.ctx, req.ZipCode, req.Keywords, 5)
result, err := l.svcCtx.AllModels.FsPreprocessLogo.PreLogoSearchSuggestions(l.ctx, req.Keywords, 5)
if err != nil {
return resp.SetStatus(basic.CodeApiErr, err)
}

View File

@ -12,6 +12,10 @@ type ContactUsRequest struct {
Message string `json:"message"`
}
type PreLogoSearchSuggestionsRequest struct {
Keywords string `json:"keywords"` // 关键字
}
type PreLogoSearchRequest struct {
ZipCode string `json:"zip_code"` // 邮编
Keywords string `json:"keywords"` // 关键字

View File

@ -42,7 +42,7 @@ service info {
// 搜索建议
@handler PreLogoSearchSuggestionsHandler
post /api/info/prelogo/search/suggestions(PreLogoSearchRequest) returns (response);
post /api/info/prelogo/search/suggestions(PreLogoSearchSuggestionsRequest) returns (response);
// 搜索
@handler PreLogoSearchHandler
@ -60,6 +60,10 @@ type (
Message string `json:"message"`
}
PreLogoSearchSuggestionsRequest {
Keywords string `json:"keywords"` // 关键字
}
PreLogoSearchRequest {
ZipCode string `json:"zip_code"` // 邮编
Keywords string `json:"keywords"` // 关键字