Merge branch 'develop' of gitee.com:fusenpack/fusenapi into develop

This commit is contained in:
momo 2023-10-26 15:36:39 +08:00
commit 5f1fbe977d
5 changed files with 99 additions and 3 deletions

View File

@ -50,7 +50,7 @@ func (m *FsUserInfoModel) MergeMetadata(userId int64, meta any) error {
return fssql.MetadataModulePATCH(m.db, "profile", FsUserInfo{}, meta, "user_id = ?", userId) 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 var baseinfo map[string]any
condUser := "user_id = 0 and guest_id = 0" 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) 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) v, ok := baseinfo["query"].(string)
if !ok { if !ok {
return m.GetDefaultProfile(ctx, tname) return m.getDefaultProfile(ctx, tname)
} }
var info map[string]any var info map[string]any
@ -108,11 +108,17 @@ func (m *FsUserInfoModel) GetProfile(ctx context.Context, pkey string, userId in
} }
if len(info) == 0 { if len(info) == 0 {
return m.GetDefaultProfile(ctx, tname) return m.getDefaultProfile(ctx, tname)
} }
return info, nil 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) { func (m *FsUserInfoModel) FindOneByUser(ctx context.Context, userId, guestId int64, module string) (resp *FsUserInfo, err error) {
if userId > 0 { if userId > 0 {
err = m.db.WithContext(ctx).Model(&FsUserInfo{}).Where("user_id = ? and module = ?", userId, module).Take(&resp).Error err = m.db.WithContext(ctx).Model(&FsUserInfo{}).Where("user_id = ? and module = ?", userId, module).Take(&resp).Error

View File

@ -22,6 +22,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/api/info/user/profile", Path: "/api/info/user/profile",
Handler: UserGetProfileHandler(serverCtx), Handler: UserGetProfileHandler(serverCtx),
}, },
{
Method: http.MethodGet,
Path: "/api/info/user/profile/default",
Handler: UserGetDefaultProfileHandler(serverCtx),
},
{ {
Method: http.MethodPost, Method: http.MethodPost,
Path: "/api/info/user/profile/base/update", Path: "/api/info/user/profile/base/update",

View File

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

View File

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

View File

@ -16,6 +16,9 @@ service info {
@handler UserGetProfileHandler @handler UserGetProfileHandler
post /api/info/user/profile(QueryProfileRequest) returns (response); post /api/info/user/profile(QueryProfileRequest) returns (response);
@handler UserGetDefaultProfileHandler
get /api/info/user/profile/default(request) returns (response);
@handler UpdateProfileBaseHandler @handler UpdateProfileBaseHandler
post /api/info/user/profile/base/update(ProfileRequest) returns (response); post /api/info/user/profile/base/update(ProfileRequest) returns (response);