From 17c00236cfb611c07745a6d5607a30cfbffdae95 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Thu, 26 Oct 2023 15:28:44 +0800 Subject: [PATCH] info --- model/gmodel/fs_user_info_logic.go | 12 +++-- server/info/internal/handler/routes.go | 5 ++ .../handler/usergetdefaultprofilehandler.go | 35 ++++++++++++++ .../logic/usergetdefaultprofilelogic.go | 47 +++++++++++++++++++ server_api/info.api | 3 ++ 5 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 server/info/internal/handler/usergetdefaultprofilehandler.go create mode 100644 server/info/internal/logic/usergetdefaultprofilelogic.go diff --git a/model/gmodel/fs_user_info_logic.go b/model/gmodel/fs_user_info_logic.go index 23304365..51b1d329 100644 --- a/model/gmodel/fs_user_info_logic.go +++ b/model/gmodel/fs_user_info_logic.go @@ -50,7 +50,7 @@ func (m *FsUserInfoModel) MergeMetadata(userId int64, meta any) error { return fssql.MetadataModulePATCH(m.db, "profile", FsUserInfo{}, meta, "user_id = ?", userId) } -func (m *FsUserInfoModel) GetDefaultProfile(ctx context.Context, tname string) (map[string]any, error) { +func (m *FsUserInfoModel) getDefaultProfile(ctx context.Context, tname string) (map[string]any, error) { var baseinfo map[string]any condUser := "user_id = 0 and guest_id = 0" rawsql := fmt.Sprintf("select JSON_EXTRACT(metadata,'$') as query from %s where %s and module = 'profile' order by ctime DESC limit 1", tname, condUser) @@ -98,7 +98,7 @@ func (m *FsUserInfoModel) GetProfile(ctx context.Context, pkey string, userId in v, ok := baseinfo["query"].(string) if !ok { - return m.GetDefaultProfile(ctx, tname) + return m.getDefaultProfile(ctx, tname) } var info map[string]any @@ -108,11 +108,17 @@ func (m *FsUserInfoModel) GetProfile(ctx context.Context, pkey string, userId in } if len(info) == 0 { - return m.GetDefaultProfile(ctx, tname) + return m.getDefaultProfile(ctx, tname) } return info, nil } + +func (m *FsUserInfoModel) GetDefaultProfile(ctx context.Context) (map[string]any, error) { + tname := fssql.GetGormTableName(m.db, FsUserInfo{}) + return m.getDefaultProfile(ctx, tname) +} + func (m *FsUserInfoModel) FindOneByUser(ctx context.Context, userId, guestId int64, module string) (resp *FsUserInfo, err error) { if userId > 0 { err = m.db.WithContext(ctx).Model(&FsUserInfo{}).Where("user_id = ? and module = ?", userId, module).Take(&resp).Error diff --git a/server/info/internal/handler/routes.go b/server/info/internal/handler/routes.go index 2f095f92..eda05c28 100644 --- a/server/info/internal/handler/routes.go +++ b/server/info/internal/handler/routes.go @@ -22,6 +22,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/info/user/profile", Handler: UserGetProfileHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/info/user/profile/default", + Handler: UserGetDefaultProfileHandler(serverCtx), + }, { Method: http.MethodPost, Path: "/api/info/user/profile/base/update", diff --git a/server/info/internal/handler/usergetdefaultprofilehandler.go b/server/info/internal/handler/usergetdefaultprofilehandler.go new file mode 100644 index 00000000..e3507db8 --- /dev/null +++ b/server/info/internal/handler/usergetdefaultprofilehandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/info/internal/logic" + "fusenapi/server/info/internal/svc" + "fusenapi/server/info/internal/types" +) + +func UserGetDefaultProfileHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.Request + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewUserGetDefaultProfileLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.UserGetDefaultProfile(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/info/internal/logic/usergetdefaultprofilelogic.go b/server/info/internal/logic/usergetdefaultprofilelogic.go new file mode 100644 index 00000000..36f4de89 --- /dev/null +++ b/server/info/internal/logic/usergetdefaultprofilelogic.go @@ -0,0 +1,47 @@ +package logic + +import ( + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/info/internal/svc" + "fusenapi/server/info/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type UserGetDefaultProfileLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUserGetDefaultProfileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserGetDefaultProfileLogic { + return &UserGetDefaultProfileLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *UserGetDefaultProfileLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *UserGetDefaultProfileLogic) UserGetDefaultProfile(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + defaultProfile, err := l.svcCtx.AllModels.FsUserInfo.GetDefaultProfile(l.ctx) + if err != nil { + return resp.SetStatusWithMessage(basic.CodeApiErr, err.Error()) + } + + return resp.SetStatus(basic.CodeOK, defaultProfile) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *UserGetDefaultProfileLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server_api/info.api b/server_api/info.api index 20e6d88c..ebc8fe54 100644 --- a/server_api/info.api +++ b/server_api/info.api @@ -16,6 +16,9 @@ service info { @handler UserGetProfileHandler post /api/info/user/profile(QueryProfileRequest) returns (response); + @handler UserGetDefaultProfileHandler + get /api/info/user/profile/default(request) returns (response); + @handler UpdateProfileBaseHandler post /api/info/user/profile/base/update(ProfileRequest) returns (response);