diff --git a/model/gmodel/fs_user_logic.go b/model/gmodel/fs_user_logic.go index 1bae52d5..440eeaac 100644 --- a/model/gmodel/fs_user_logic.go +++ b/model/gmodel/fs_user_logic.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" + "fusenapi/server/fssql" "fusenapi/utils/auth" "time" @@ -213,7 +214,7 @@ func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterT Utime: &now, } - err = txUserInfo.Where("user_id = ?", uinfo.UserId).Take(nil).Error + err = txUserInfo.Where("module = 'profile' and user_id = ?", uinfo.UserId).Take(nil).Error // txUserInfo.Statement.Table if err != nil { @@ -224,19 +225,14 @@ func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterT return nil } } + } else { - updatesql := `UPDATE %s - SET metadata = CASE - WHEN metadata IS NULL THEN ? - ELSE JSON_MERGE_PATCH(metadata, ?) - END - WHERE id = ?;` - updatesql = fmt.Sprintf(updatesql, txUserInfo.Statement.Table) - logx.Error(updatesql) - err = txUserInfo.Raw(updatesql, metadata, metadata, uinfo.UserId).Error + + err = fssql.MetadataPATCH(txUserInfo, "profile", FsUser{}, metadata, "user_id = ?", *uinfo.UserId) if err != nil { return err } + } return err }) diff --git a/server/auth/internal/logic/useremailconfirmationlogic.go b/server/auth/internal/logic/useremailconfirmationlogic.go index 3bfb83c0..3cb86bc8 100644 --- a/server/auth/internal/logic/useremailconfirmationlogic.go +++ b/server/auth/internal/logic/useremailconfirmationlogic.go @@ -111,7 +111,7 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma return resp.SetStatusWithMessage(basic.CodeOAuthConfirmationTimeoutErr, "Verification links expire after 30 minute.") } - logx.Error(token.Platform) + // logx.Error(token.Platform) switch token.Platform { case string(auth.PLATFORM_GOOGLE): // 谷歌平台的注册流程 diff --git a/server/auth/internal/logic/websocket_test.go b/server/auth/internal/logic/websocket_test.go index 639674ea..c74a5736 100644 --- a/server/auth/internal/logic/websocket_test.go +++ b/server/auth/internal/logic/websocket_test.go @@ -2,12 +2,16 @@ package logic import ( "bytes" + "encoding/json" "fmt" + "fusenapi/initalize" + "fusenapi/model/gmodel" "fusenapi/utils/wevent" "io" "log" "net/http" "net/smtp" + "reflect" "testing" "github.com/474420502/requests" @@ -130,3 +134,41 @@ func TestSender(t *testing.T) { log.Println(result) } + +func TestSql(t *testing.T) { + + type MetaData struct { + Key string `gorm:"key" json:"key"` + Test string `gorm:"test" json:"test"` + } + + conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen") + log.Println(conn.Model(&gmodel.FsUser{})) + + log.Println(conn.NamingStrategy.TableName(reflect.TypeOf(gmodel.FsUser{}).Name())) + updatesql := `UPDATE %s + SET metadata = CASE + WHEN metadata IS NULL THEN ? + ELSE JSON_MERGE_PATCH(metadata, ?) + END + WHERE id = ?;` + + metadata := &MetaData{ + Key: "mkey", + Test: "testv", + } + data, err := json.Marshal(metadata) + if err != nil { + logx.Error(err) + panic(err) + } + + updatesql = fmt.Sprintf(updatesql, "fusen.fs_change_code") + log.Println(updatesql) + err = conn.Exec(updatesql, data, data, 6).Error + + if err != nil { + logx.Error(err) + panic(err) + } +} diff --git a/server/fssql/metadata.go b/server/fssql/metadata.go new file mode 100644 index 00000000..b3342ee7 --- /dev/null +++ b/server/fssql/metadata.go @@ -0,0 +1,91 @@ +package fssql + +import ( + "encoding/json" + "fmt" + "reflect" + + "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" +) + +// func MetadataPATCH(tx *gorm.DB, module string, tableStructPointer any, updateMetadata any, rawID int64) error { +// stype := reflect.TypeOf(tableStructPointer) +// if stype.Kind() == reflect.Pointer { +// stype = stype.Elem() +// } + +// updatesql := `UPDATE %s +// SET metadata = CASE +// WHEN metadata IS NULL THEN ? +// ELSE JSON_MERGE_PATCH(metadata, ?) +// END +// WHERE module = '%s' and id = ?;` + +// var err error +// var metadata []byte + +// switch mdata := updateMetadata.(type) { +// case []byte: +// metadata = mdata +// case string: +// metadata = []byte(mdata) +// default: +// metadata, err = json.Marshal(updateMetadata) +// if err != nil { +// return err +// } +// } + +// updatesql = fmt.Sprintf(updatesql, tx.NamingStrategy.TableName(stype.Name()), module) +// logx.Error(updatesql) +// err = tx.Exec(updatesql, metadata, metadata, rawID).Error +// if err != nil { +// return err +// } + +// return nil +// } + +func MetadataPATCH(tx *gorm.DB, module string, tableStructPointer any, updateMetadata any, WhereKeysCond string, value ...any) error { + stype := reflect.TypeOf(tableStructPointer) + if stype.Kind() == reflect.Pointer { + stype = stype.Elem() + } + + updatesql := `UPDATE %s + SET metadata = CASE + WHEN metadata IS NULL THEN ? + ELSE JSON_MERGE_PATCH(metadata, ?) + END + WHERE module = '%s' and %s;` + + var err error + var metadata []byte + + switch mdata := updateMetadata.(type) { + case []byte: + metadata = mdata + case string: + metadata = []byte(mdata) + default: + metadata, err = json.Marshal(updateMetadata) + if err != nil { + return err + } + } + + var values []any + + values = append(values, metadata, metadata) + values = append(values, value...) + + updatesql = fmt.Sprintf(updatesql, tx.NamingStrategy.TableName(stype.Name()), module, WhereKeysCond) + logx.Error(updatesql) + err = tx.Exec(updatesql, values).Error + if err != nil { + return err + } + + return nil +}