From 31debda97ffdb370f76d5477d95966a8869b6636 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 15:02:09 +0800 Subject: [PATCH 1/2] info update profile --- model/gmodel/fs_user_info_logic.go | 7 +++ model/gmodel/fs_user_logic.go | 22 -------- model/gmodel/var.go | 22 ++++++++ server/info/internal/handler/routes.go | 5 ++ .../internal/handler/updateprofilehandler.go | 35 +++++++++++++ server/info/internal/logic/infologic_test.go | 15 ++++++ .../info/internal/logic/updateprofilelogic.go | 52 +++++++++++++++++++ server/info/internal/types/types.go | 9 ++++ server_api/info.api | 12 +++++ 9 files changed, 157 insertions(+), 22 deletions(-) create mode 100644 server/info/internal/handler/updateprofilehandler.go create mode 100644 server/info/internal/logic/updateprofilelogic.go diff --git a/model/gmodel/fs_user_info_logic.go b/model/gmodel/fs_user_info_logic.go index ad7ae9d6..1debc806 100644 --- a/model/gmodel/fs_user_info_logic.go +++ b/model/gmodel/fs_user_info_logic.go @@ -4,6 +4,7 @@ package gmodel import ( "context" + "fusenapi/utils/fssql" "fusenapi/utils/handlers" "gorm.io/gorm" @@ -42,3 +43,9 @@ func (p *FsUserInfoModel) CreateOrUpdate(gormDB *gorm.DB, req *FsUserInfo) (resp } return req, err } + +func (m *FsUserInfoModel) MergeMetadata(userId int64, meta any) error { + return fssql.MetadataModulePATCH(m.db, "profile", FsUserInfo{}, map[string]any{ + "base": meta, + }, "user_id = ?", userId) +} diff --git a/model/gmodel/fs_user_logic.go b/model/gmodel/fs_user_logic.go index 6f360323..1dbe2d0d 100644 --- a/model/gmodel/fs_user_logic.go +++ b/model/gmodel/fs_user_logic.go @@ -145,28 +145,6 @@ func (u *FsUserModel) RegisterByGoogleOAuth(ctx context.Context, token *auth.Reg return user, nil } -// SubscriptionStatus 订阅状态 -type SubscriptionStatus struct { - SubEmail bool `json:"all_emails"` - ItemMap *struct { - } `json:"item_map"` -} - -type UserAddress struct { -} - -type UserProfile struct { - ProfileBase UserProfileBase `json:"base"` - SubStatus SubscriptionStatus `json:"sub_status"` -} - -// UserProfileBase 个人信息 -type UserProfileBase struct { - FirstName string `json:"first_name"` - LastName string `json:"last_name"` - Resetaurant string `json:"resetaurant"` -} - // 自平台的注册流程 func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterToken) (user *FsUser, err error) { diff --git a/model/gmodel/var.go b/model/gmodel/var.go index 3bd8af16..b471b204 100644 --- a/model/gmodel/var.go +++ b/model/gmodel/var.go @@ -35,3 +35,25 @@ func FsFloat(v float64) *float64 { func FsBool(v bool) *bool { return &v } + +// SubscriptionStatus 订阅状态 +type SubscriptionStatus struct { + SubEmail bool `json:"all_emails"` + ItemMap *struct { + } `json:"item_map"` +} + +type UserProfile struct { + ProfileBase UserProfileBase `json:"base"` + SubStatus SubscriptionStatus `json:"sub_status"` +} + +// UserProfileBase 个人信息 +type UserProfileBase struct { + FirstName string `json:"first_name"` // 首名 + LastName string `json:"last_name"` // 后名 + UserName string `json:"user_name"` // 用户名 + Mobile string `json:"mobile"` // 电话 + Resetaurant string `json:"resetaurant"` // 不知道干什么 + Company string `json:"company"` // 公司 +} diff --git a/server/info/internal/handler/routes.go b/server/info/internal/handler/routes.go index 8ab5d58b..1b34893c 100644 --- a/server/info/internal/handler/routes.go +++ b/server/info/internal/handler/routes.go @@ -17,6 +17,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/info/user", Handler: InfoHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/info/user/profile/update", + Handler: UpdateProfileHandler(serverCtx), + }, { Method: http.MethodPost, Path: "/api/info/address/default", diff --git a/server/info/internal/handler/updateprofilehandler.go b/server/info/internal/handler/updateprofilehandler.go new file mode 100644 index 00000000..6e06b51f --- /dev/null +++ b/server/info/internal/handler/updateprofilehandler.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 UpdateProfileHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.ProfileRequest + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewUpdateProfileLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.UpdateProfile(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/info/internal/logic/infologic_test.go b/server/info/internal/logic/infologic_test.go index afc00177..776c7945 100644 --- a/server/info/internal/logic/infologic_test.go +++ b/server/info/internal/logic/infologic_test.go @@ -5,6 +5,7 @@ import ( "fmt" "fusenapi/initalize" "fusenapi/model/gmodel" + "fusenapi/server/info/internal/types" "fusenapi/utils/check" "log" "strings" @@ -175,6 +176,20 @@ func TestCaseJSON_EXTRACT(t *testing.T) { conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen") // err = conn.Exec(updatesql, 6).Error + fname := "asd" + r := &types.ProfileRequest{ + FirstName: &fname, + } + + m := map[string]any{ + "base": r, + } + + data, err := json.Marshal(m) + if err != nil { + panic(err) + } + log.Println(string(data)) var result []gmodel.FsAddress conn.Model(&gmodel.FsAddress{}).Find(&result) diff --git a/server/info/internal/logic/updateprofilelogic.go b/server/info/internal/logic/updateprofilelogic.go new file mode 100644 index 00000000..49be7b52 --- /dev/null +++ b/server/info/internal/logic/updateprofilelogic.go @@ -0,0 +1,52 @@ +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 UpdateProfileLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUpdateProfileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateProfileLogic { + return &UpdateProfileLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *UpdateProfileLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *UpdateProfileLogic) UpdateProfile(req *types.ProfileRequest, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + if !userinfo.IsUser() { + return resp.SetStatus(basic.CodeUnAuth) + } + + err := l.svcCtx.AllModels.FsUserInfo.MergeMetadata(userinfo.UserId, req) + if err != nil { + logx.Error(err) // 日志记录错误 + return resp.SetStatus(basic.CodeDbSqlErr, err.Error()) // 返回数据库创建错误 + } + + return resp.SetStatus(basic.CodeOK) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *UpdateProfileLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/info/internal/types/types.go b/server/info/internal/types/types.go index 2b740955..bb86a1b6 100644 --- a/server/info/internal/types/types.go +++ b/server/info/internal/types/types.go @@ -36,6 +36,15 @@ type AddressRequest struct { State string `json:"state"` //州 } +type ProfileRequest struct { + FirstName *string `json:"first_name,optional,omitempty"` // 首名 + LastName *string `json:"last_name,optional,omitempty"` // 后名 + UserName *string `json:"user_name,optional,omitempty"` // 用户名 + Mobile *string `json:"mobile,optional,omitempty"` // 电话 + Resetaurant *string `json:"resetaurant,optional,omitempty"` // 不知道干什么 + Company *string `json:"company,optional,omitempty"` // 公司 +} + type Request struct { } diff --git a/server_api/info.api b/server_api/info.api index 5f876f16..b4514b82 100644 --- a/server_api/info.api +++ b/server_api/info.api @@ -13,6 +13,9 @@ service info { @handler InfoHandler post /api/info/user(UserInfoRequest) returns (response); + @handler UpdateProfileHandler + post /api/info/user/profile/update(ProfileRequest) returns (response); + @handler AddressDefaultHandler post /api/info/address/default(AddressIdRequest) returns (response); @@ -60,4 +63,13 @@ type ( City string `json:"city"` //城市 State string `json:"state"` //州 } + + ProfileRequest { + FirstName *string `json:"first_name,optional,omitempty"` // 首名 + LastName *string `json:"last_name,optional,omitempty"` // 后名 + UserName *string `json:"user_name,optional,omitempty"` // 用户名 + Mobile *string `json:"mobile,optional,omitempty"` // 电话 + Resetaurant *string `json:"resetaurant,optional,omitempty"` // 不知道干什么 + Company *string `json:"company,optional,omitempty"` // 公司 + } ) \ No newline at end of file From f387e46977e04e7651ab87c930b7d52f62e7aa9b Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 17:16:10 +0800 Subject: [PATCH 2/2] info get profile --- model/gmodel/fs_guest_gen.go | 16 +++--- model/gmodel/fs_user_gen.go | 2 +- model/gmodel/fs_user_info_logic.go | 32 ++++++++++++ server/info/internal/handler/routes.go | 5 ++ .../internal/handler/usergetprofilehandler.go | 35 +++++++++++++ server/info/internal/logic/infologic_test.go | 26 +++------- .../internal/logic/usergetprofilelogic.go | 52 +++++++++++++++++++ server/info/internal/types/types.go | 4 ++ server_api/info.api | 7 +++ utils/fssql/metadata.go | 9 ++++ 10 files changed, 159 insertions(+), 29 deletions(-) create mode 100644 server/info/internal/handler/usergetprofilehandler.go create mode 100644 server/info/internal/logic/usergetprofilelogic.go diff --git a/model/gmodel/fs_guest_gen.go b/model/gmodel/fs_guest_gen.go index c273ea95..8a2361e8 100644 --- a/model/gmodel/fs_guest_gen.go +++ b/model/gmodel/fs_guest_gen.go @@ -7,16 +7,12 @@ import ( // fs_guest 游客表 type FsGuest struct { - GuestId int64 `gorm:"primary_key;default:0;auto_increment;" json:"guest_id"` // ID - AuthKey *string `gorm:"default:'';" json:"auth_key"` // jwt token - Status *int64 `gorm:"index;default:1;" json:"status"` // 1正常 0不正常 - IsDel *int64 `gorm:"index;default:0;" json:"is_del"` // 是否删除 1删除 - Ctime *time.Time `gorm:"index;default:'0000-00-00 00:00:00';" json:"ctime"` // - Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` // - IsOpenRender *int64 `gorm:"default:0;" json:"is_open_render"` // 是否打开个性化渲染(1:开启,0:关闭) - IsThousandFace *int64 `gorm:"default:0;" json:"is_thousand_face"` // 是否已经存在千人千面(1:存在,0:不存在) - IsLowRendering *int64 `gorm:"default:0;" json:"is_low_rendering"` // 是否开启低渲染模型渲染 - IsRemoveBg *int64 `gorm:"default:1;" json:"is_remove_bg"` // 用户上传logo是否去除背景 + GuestId int64 `gorm:"primary_key;default:0;auto_increment;" json:"guest_id"` // ID + AuthKey *string `gorm:"default:'';" json:"auth_key"` // jwt token + Status *int64 `gorm:"index;default:1;" json:"status"` // 1正常 0不正常 + IsDel *int64 `gorm:"index;default:0;" json:"is_del"` // 是否删除 1删除 + Ctime *time.Time `gorm:"index;default:'0000-00-00 00:00:00';" json:"ctime"` // + Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` // } type FsGuestModel struct { db *gorm.DB diff --git a/model/gmodel/fs_user_gen.go b/model/gmodel/fs_user_gen.go index 75f1e5a1..b3166fb2 100644 --- a/model/gmodel/fs_user_gen.go +++ b/model/gmodel/fs_user_gen.go @@ -14,7 +14,7 @@ type FsUser struct { LastName *string `gorm:"default:'';" json:"last_name"` // LastName Username *string `gorm:"index;default:'';" json:"username"` // Company *string `gorm:"default:'';" json:"company"` // 公司名称 - Mobile *string `gorm:"default:'';" json:"mobile"` // 手机号码 + Mobile *string `gorm:"default:'';" json:"mobile"` // PasswordHash *string `gorm:"default:'';" json:"password_hash"` // VerificationToken *string `gorm:"default:'';" json:"verification_token"` // PasswordResetToken *string `gorm:"default:'';" json:"password_reset_token"` // diff --git a/model/gmodel/fs_user_info_logic.go b/model/gmodel/fs_user_info_logic.go index 1debc806..aab9a4f3 100644 --- a/model/gmodel/fs_user_info_logic.go +++ b/model/gmodel/fs_user_info_logic.go @@ -4,6 +4,8 @@ package gmodel import ( "context" + "encoding/json" + "fmt" "fusenapi/utils/fssql" "fusenapi/utils/handlers" @@ -49,3 +51,33 @@ func (m *FsUserInfoModel) MergeMetadata(userId int64, meta any) error { "base": meta, }, "user_id = ?", userId) } + +func (m *FsUserInfoModel) GetProfile(ctx context.Context, pkey string, userId int64) (map[string]any, error) { + + var baseinfo map[string]any + tname := fssql.GetGormTableName(m.db, FsUserInfo{}) + + if pkey == "." { + pkey = "" + } else { + pkey = "." + pkey + } + + rawsql := fmt.Sprintf("select JSON_EXTRACT(metadata,'$%s') as query from %s where user_id = ? and module = 'profile' order by ctime DESC limit 1", pkey, tname) + err := m.db.Raw(rawsql, userId).Take(&baseinfo).Error + if err != nil { + return nil, err + } + + v, ok := baseinfo["query"].(string) + if !ok { + return nil, nil + } + + var info map[string]any + err = json.Unmarshal([]byte(v), &info) + if err != nil { + return nil, err + } + return info, nil +} diff --git a/server/info/internal/handler/routes.go b/server/info/internal/handler/routes.go index 1b34893c..a3c1fc4d 100644 --- a/server/info/internal/handler/routes.go +++ b/server/info/internal/handler/routes.go @@ -17,6 +17,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/info/user", Handler: InfoHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/info/user/profile", + Handler: UserGetProfileHandler(serverCtx), + }, { Method: http.MethodPost, Path: "/api/info/user/profile/update", diff --git a/server/info/internal/handler/usergetprofilehandler.go b/server/info/internal/handler/usergetprofilehandler.go new file mode 100644 index 00000000..28dafeca --- /dev/null +++ b/server/info/internal/handler/usergetprofilehandler.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 UserGetProfileHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.QueryProfileRequest + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewUserGetProfileLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.UserGetProfile(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/info/internal/logic/infologic_test.go b/server/info/internal/logic/infologic_test.go index 776c7945..85ac3b41 100644 --- a/server/info/internal/logic/infologic_test.go +++ b/server/info/internal/logic/infologic_test.go @@ -5,8 +5,8 @@ import ( "fmt" "fusenapi/initalize" "fusenapi/model/gmodel" - "fusenapi/server/info/internal/types" "fusenapi/utils/check" + "fusenapi/utils/fssql" "log" "strings" "testing" @@ -176,24 +176,14 @@ func TestCaseJSON_EXTRACT(t *testing.T) { conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen") // err = conn.Exec(updatesql, 6).Error - fname := "asd" - r := &types.ProfileRequest{ - FirstName: &fname, - } + info := gmodel.UserProfileBase{} - m := map[string]any{ - "base": r, - } + var baseinfo map[string]any + tname := fssql.GetGormTableName(conn, gmodel.FsUserInfo{}) + rawsql := fmt.Sprintf("select JSON_EXTRACT(metadata,'$.base') as base from %s where user_id = ? and module = ? order by ctime DESC limit 1", tname) + tx := conn.Raw(rawsql, 162, "profile").Take(&baseinfo) + log.Println(tx.Error) + json.Unmarshal([]byte(baseinfo["base"].(string)), &info) - data, err := json.Marshal(m) - if err != nil { - panic(err) - } - log.Println(string(data)) - - var result []gmodel.FsAddress - conn.Model(&gmodel.FsAddress{}).Find(&result) - - log.Println(conn.Model(&gmodel.FsChangeCode{}).Select("id").Where("id = 5").Take(nil).Error) log.Println(err) } diff --git a/server/info/internal/logic/usergetprofilelogic.go b/server/info/internal/logic/usergetprofilelogic.go new file mode 100644 index 00000000..be64152d --- /dev/null +++ b/server/info/internal/logic/usergetprofilelogic.go @@ -0,0 +1,52 @@ +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 UserGetProfileLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUserGetProfileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserGetProfileLogic { + return &UserGetProfileLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *UserGetProfileLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *UserGetProfileLogic) UserGetProfile(req *types.QueryProfileRequest, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + if !userinfo.IsUser() { + return resp.SetStatus(basic.CodeUnAuth) + } + + profileBase, err := l.svcCtx.AllModels.FsUserInfo.GetProfile(l.ctx, req.TopKey, userinfo.UserId) + if err != nil { + return resp.SetStatusWithMessage(basic.CodeApiErr, err.Error()) + } + + return resp.SetStatus(basic.CodeOK, profileBase) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *UserGetProfileLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/info/internal/types/types.go b/server/info/internal/types/types.go index bb86a1b6..5a2df57e 100644 --- a/server/info/internal/types/types.go +++ b/server/info/internal/types/types.go @@ -45,6 +45,10 @@ type ProfileRequest struct { Company *string `json:"company,optional,omitempty"` // 公司 } +type QueryProfileRequest struct { + TopKey string `json:"top_key"` // 首名 +} + type Request struct { } diff --git a/server_api/info.api b/server_api/info.api index b4514b82..ef645d7a 100644 --- a/server_api/info.api +++ b/server_api/info.api @@ -13,6 +13,9 @@ service info { @handler InfoHandler post /api/info/user(UserInfoRequest) returns (response); + @handler UserGetProfileHandler + get /api/info/user/profile(QueryProfileRequest) returns (response); + @handler UpdateProfileHandler post /api/info/user/profile/update(ProfileRequest) returns (response); @@ -72,4 +75,8 @@ type ( Resetaurant *string `json:"resetaurant,optional,omitempty"` // 不知道干什么 Company *string `json:"company,optional,omitempty"` // 公司 } + + QueryProfileRequest { + TopKey string `json:"top_key"` // 首名 + } ) \ No newline at end of file diff --git a/utils/fssql/metadata.go b/utils/fssql/metadata.go index 8c25b343..a9146ad2 100644 --- a/utils/fssql/metadata.go +++ b/utils/fssql/metadata.go @@ -191,3 +191,12 @@ func MetadataOrderPATCH(tx *gorm.DB, sql string, orderSn string, tableStructPoin return nil } + +func GetGormTableName(tx *gorm.DB, tableStructPointer any) string { + stype := reflect.TypeOf(tableStructPointer) + if stype.Kind() == reflect.Pointer { + stype = stype.Elem() + } + tname := tx.NamingStrategy.TableName(stype.Name()) + return tname +}