feat: 添加搜索收藏微服务

This commit is contained in:
zetaloop
2026-04-24 13:24:58 +08:00
parent 91fdd2a498
commit 53d0e791b4
58 changed files with 6239 additions and 57 deletions
+11
View File
@@ -0,0 +1,11 @@
package config
import (
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/zrpc"
)
type Config struct {
rest.RestConf
SearchRpcConf zrpc.RpcClientConf
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package favorites
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/search/api/internal/logic/favorites"
"juwan-backend/app/search/api/internal/svc"
"juwan-backend/app/search/api/internal/types"
)
// 添加收藏
func AddFavoriteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.FavoriteReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := favorites.NewAddFavoriteLogic(r.Context(), svcCtx)
resp, err := l.AddFavorite(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package favorites
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/search/api/internal/logic/favorites"
"juwan-backend/app/search/api/internal/svc"
"juwan-backend/app/search/api/internal/types"
)
// 检查收藏状态
func CheckFavoriteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.FavoriteCheckReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := favorites.NewCheckFavoriteLogic(r.Context(), svcCtx)
resp, err := l.CheckFavorite(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package favorites
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/search/api/internal/logic/favorites"
"juwan-backend/app/search/api/internal/svc"
"juwan-backend/app/search/api/internal/types"
)
// 获取收藏列表
func ListFavoritesHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PageReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := favorites.NewListFavoritesLogic(r.Context(), svcCtx)
resp, err := l.ListFavorites(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package favorites
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/search/api/internal/logic/favorites"
"juwan-backend/app/search/api/internal/svc"
"juwan-backend/app/search/api/internal/types"
)
// 取消收藏
func RemoveFavoriteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PathIDReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := favorites.NewRemoveFavoriteLogic(r.Context(), svcCtx)
resp, err := l.RemoveFavorite(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
+64
View File
@@ -0,0 +1,64 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.10.1
package handler
import (
"net/http"
favorites "juwan-backend/app/search/api/internal/handler/favorites"
search "juwan-backend/app/search/api/internal/handler/search"
"juwan-backend/app/search/api/internal/svc"
"github.com/zeromicro/go-zero/rest"
)
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
[]rest.Route{
{
// 获取收藏列表
Method: http.MethodGet,
Path: "/favorites",
Handler: favorites.ListFavoritesHandler(serverCtx),
},
{
// 添加收藏
Method: http.MethodPost,
Path: "/favorites",
Handler: favorites.AddFavoriteHandler(serverCtx),
},
{
// 取消收藏
Method: http.MethodDelete,
Path: "/favorites/:id",
Handler: favorites.RemoveFavoriteHandler(serverCtx),
},
{
// 检查收藏状态
Method: http.MethodGet,
Path: "/users/:id/favorites/check",
Handler: favorites.CheckFavoriteHandler(serverCtx),
},
},
rest.WithPrefix("/api/v1"),
)
server.AddRoutes(
[]rest.Route{
{
// 首页推荐
Method: http.MethodGet,
Path: "/recommendations/home",
Handler: search.RecommendationsHandler(serverCtx),
},
{
// 统一搜索
Method: http.MethodGet,
Path: "/search",
Handler: search.SearchHandler(serverCtx),
},
},
rest.WithPrefix("/api/v1"),
)
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package search
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/search/api/internal/logic/search"
"juwan-backend/app/search/api/internal/svc"
"juwan-backend/app/search/api/internal/types"
)
// 首页推荐
func RecommendationsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PageReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := search.NewRecommendationsLogic(r.Context(), svcCtx)
resp, err := l.Recommendations(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package search
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/search/api/internal/logic/search"
"juwan-backend/app/search/api/internal/svc"
"juwan-backend/app/search/api/internal/types"
)
// 统一搜索
func SearchHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.SearchReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := search.NewSearchLogic(r.Context(), svcCtx)
resp, err := l.Search(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,52 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package favorites
import (
"context"
"juwan-backend/app/search/api/internal/svc"
"juwan-backend/app/search/api/internal/types"
"juwan-backend/app/search/rpc/searchservice"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
type AddFavoriteLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 添加收藏
func NewAddFavoriteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddFavoriteLogic {
return &AddFavoriteLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *AddFavoriteLogic) AddFavorite(req *types.FavoriteReq) (resp *types.EmptyResp, err error) {
uid, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
targetID, err := parseSnowflakeID(req.TargetId)
if err != nil {
return nil, err
}
_, err = l.svcCtx.SearchRpc.AddFavorites(l.ctx, &searchservice.AddFavoritesReq{
UserId: uid,
TargetType: req.TargetType,
TargetId: targetID,
})
if err != nil {
return nil, err
}
return &types.EmptyResp{}, nil
}
@@ -0,0 +1,58 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package favorites
import (
"context"
"errors"
"juwan-backend/app/search/api/internal/svc"
"juwan-backend/app/search/api/internal/types"
"juwan-backend/app/search/rpc/searchservice"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
type CheckFavoriteLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 检查收藏状态
func NewCheckFavoriteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckFavoriteLogic {
return &CheckFavoriteLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CheckFavoriteLogic) CheckFavorite(req *types.FavoriteCheckReq) (resp *types.FavoriteCheckResp, err error) {
uid, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
if req.Id != uid {
return nil, errors.New("user mismatch")
}
targetID, err := parseSnowflakeID(req.TargetId)
if err != nil {
return nil, err
}
out, err := l.svcCtx.SearchRpc.SearchFavorites(l.ctx, &searchservice.SearchFavoritesReq{
Offset: 0,
Limit: 1,
UserId: &uid,
TargetType: &req.TargetType,
TargetId: &targetID,
})
if err != nil {
return nil, err
}
return &types.FavoriteCheckResp{Favorited: len(out.GetFavorites()) > 0}, nil
}
@@ -0,0 +1,23 @@
package favorites
import (
"strconv"
"time"
"juwan-backend/app/search/api/internal/types"
"juwan-backend/app/search/rpc/searchservice"
)
func toAPIFavorite(f *searchservice.Favorites) types.Favorite {
return types.Favorite{
Id: strconv.FormatInt(f.GetId(), 10),
UserId: strconv.FormatInt(f.GetUserId(), 10),
TargetType: f.GetTargetType(),
TargetId: strconv.FormatInt(f.GetTargetId(), 10),
CreatedAt: time.Unix(f.GetCreatedAt(), 0).Format(time.RFC3339),
}
}
func parseSnowflakeID(value string) (int64, error) {
return strconv.ParseInt(value, 10, 64)
}
@@ -0,0 +1,63 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package favorites
import (
"context"
"juwan-backend/app/search/api/internal/svc"
"juwan-backend/app/search/api/internal/types"
"juwan-backend/app/search/rpc/searchservice"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
type ListFavoritesLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取收藏列表
func NewListFavoritesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListFavoritesLogic {
return &ListFavoritesLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ListFavoritesLogic) ListFavorites(req *types.PageReq) (resp *types.FavoriteListResp, err error) {
uid, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
limit := req.Limit
if limit <= 0 {
limit = 20
}
out, err := l.svcCtx.SearchRpc.SearchFavorites(l.ctx, &searchservice.SearchFavoritesReq{
Offset: req.Offset,
Limit: limit,
UserId: &uid,
})
if err != nil {
return nil, err
}
items := make([]types.Favorite, 0, len(out.GetFavorites()))
for _, item := range out.GetFavorites() {
items = append(items, toAPIFavorite(item))
}
return &types.FavoriteListResp{
Items: items,
Meta: types.PageMeta{
Total: int64(len(items)),
Offset: req.Offset,
Limit: limit,
},
}, nil
}
@@ -0,0 +1,53 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package favorites
import (
"context"
"errors"
"juwan-backend/app/search/api/internal/svc"
"juwan-backend/app/search/api/internal/types"
"juwan-backend/app/search/rpc/searchservice"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
type RemoveFavoriteLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 取消收藏
func NewRemoveFavoriteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RemoveFavoriteLogic {
return &RemoveFavoriteLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *RemoveFavoriteLogic) RemoveFavorite(req *types.PathIDReq) (resp *types.EmptyResp, err error) {
uid, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
current, err := l.svcCtx.SearchRpc.GetFavoritesById(l.ctx, &searchservice.GetFavoritesByIdReq{Id: req.Id})
if err != nil {
return nil, err
}
if current.GetFavorites() == nil || current.GetFavorites().GetUserId() != uid {
return nil, errors.New("favorite not found")
}
_, err = l.svcCtx.SearchRpc.DelFavorites(l.ctx, &searchservice.DelFavoritesReq{Id: req.Id})
if err != nil {
return nil, err
}
return &types.EmptyResp{}, nil
}
@@ -0,0 +1,17 @@
package search
import "juwan-backend/app/search/api/internal/types"
func emptySearchResp(offset, limit int64) *types.SearchResp {
if limit <= 0 {
limit = 20
}
return &types.SearchResp{
Items: make([]interface{}, 0),
Meta: types.PageMeta{
Total: 0,
Offset: offset,
Limit: limit,
},
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package search
import (
"context"
"juwan-backend/app/search/api/internal/svc"
"juwan-backend/app/search/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type RecommendationsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 首页推荐
func NewRecommendationsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RecommendationsLogic {
return &RecommendationsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *RecommendationsLogic) Recommendations(req *types.PageReq) (resp *types.SearchResp, err error) {
return emptySearchResp(req.Offset, req.Limit), nil
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1
package search
import (
"context"
"juwan-backend/app/search/api/internal/svc"
"juwan-backend/app/search/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type SearchLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 统一搜索
func NewSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchLogic {
return &SearchLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SearchLogic) Search(req *types.SearchReq) (resp *types.SearchResp, err error) {
return emptySearchResp(req.Offset, req.Limit), nil
}
@@ -0,0 +1,20 @@
package svc
import (
"juwan-backend/app/search/api/internal/config"
"juwan-backend/app/search/rpc/searchservice"
"github.com/zeromicro/go-zero/zrpc"
)
type ServiceContext struct {
Config config.Config
SearchRpc searchservice.SearchService
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
SearchRpc: searchservice.NewSearchService(zrpc.MustNewClient(c.SearchRpcConf)),
}
}
+83
View File
@@ -0,0 +1,83 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.10.1
package types
type EmptyResp struct {
}
type Favorite struct {
Id string `json:"id"`
UserId string `json:"userId"`
TargetType string `json:"targetType"`
TargetId string `json:"targetId"`
CreatedAt string `json:"createdAt"`
}
type FavoriteCheckReq struct {
PathIDReq
TargetType string `form:"targetType"`
TargetId string `form:"targetId"`
}
type FavoriteCheckResp struct {
Favorited bool `json:"favorited"`
}
type FavoriteListResp struct {
Items []Favorite `json:"items"`
Meta PageMeta `json:"meta"`
}
type FavoriteReq struct {
TargetType string `json:"targetType"` // player, shop
TargetId string `json:"targetId"`
}
type PageMeta struct {
Total int64 `json:"total"`
Offset int64 `json:"offset"`
Limit int64 `json:"limit"`
}
type PageReq struct {
Offset int64 `form:"offset,default=0"`
Limit int64 `form:"limit,default=20"`
}
type PathIDReq struct {
Id int64 `path:"id"`
}
type SearchReq struct {
PageReq
Q string `form:"q,optional"`
MinPrice float64 `form:"min,optional"`
MaxPrice float64 `form:"max,optional"`
OnlyOnline bool `form:"onlyOnline,optional"`
Sort string `form:"sort,optional"`
}
type SearchResp struct {
Items []interface{} `json:"items"` // Mixed items
Meta PageMeta `json:"meta"`
}
type SimpleUser struct {
Id string `json:"id"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
}
type UserProfile struct {
Id string `json:"id"`
Username string `json:"username"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Role string `json:"role"` // consumer, player, owner, admin
VerifiedRoles []string `json:"verifiedRoles"`
VerificationStatus map[string]string `json:"verificationStatus"`
Phone string `json:"phone,optional"`
Bio string `json:"bio,optional"`
CreatedAt string `json:"createdAt"`
}