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

This commit is contained in:
laodaming 2023-08-29 15:04:20 +08:00
commit 60255b5c45
3 changed files with 73 additions and 68 deletions

View File

@ -6,9 +6,9 @@ import (
"fmt"
"fusenapi/utils/auth"
"log"
"time"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
@ -52,36 +52,37 @@ func (u *FsUserModel) Transaction(ctx context.Context, fc func(tx *gorm.DB) erro
// 继承guest_id的资源表
func InheritGuestIdResource(tx *gorm.DB, userId, guestId int64, afterDo func(txResouce *gorm.DB, txUserMaterial *gorm.DB, txUserInfo *gorm.DB) error) error {
var err error
txRes := tx.Model(&FsResource{})
txUserMaterial := tx.Model(&FsUserMaterial{})
txUserInfo := tx.Model(&FsUserInfo{})
if guestId != 0 {
// 继承guest_id的资源表
txRes := tx.Model(&FsResource{})
err = txRes.
Where("guest_id = ?", guestId).
UpdateColumn("user_id", userId).Error
if err != nil {
if err != nil && err != gorm.ErrRecordNotFound {
return err
}
txUserMaterial := tx.Model(&FsUserMaterial{})
err = txUserMaterial.
Where("guest_id = ?", guestId).
UpdateColumn("user_id", userId).Error
if err != nil {
if err != nil && err != gorm.ErrRecordNotFound {
return err
}
txUserInfo := tx.Model(&FsUserInfo{})
err = txUserInfo.
Where("guest_id = ?", guestId).
UpdateColumn("user_id", userId).Error
if err != nil {
if err != nil && err != gorm.ErrRecordNotFound {
return err
}
}
if afterDo != nil {
return afterDo(txRes, txUserMaterial, txUserInfo)
}
if afterDo != nil {
return afterDo(txRes, txUserMaterial, txUserInfo)
}
return fmt.Errorf("guest_id must not be 0")
}
@ -108,11 +109,8 @@ func (u *FsUserModel) RegisterByGoogleOAuth(ctx context.Context, token *auth.Reg
return err
}
if token.GuestId != 0 {
// 继承guest_id的资源表
return InheritGuestIdResource(tx, user.Id, token.GuestId, nil)
}
return err
// 继承guest_id的资源表
return InheritGuestIdResource(tx, user.Id, token.GuestId, nil)
}
return err
@ -140,67 +138,73 @@ type UserProfile struct {
func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterToken) (*FsUser, error) {
user := &FsUser{}
logcontent := ""
defer func() {
logx.Info("aaaa:", logcontent)
}()
err := u.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var err error
UserTx := tx.Model(user)
err := UserTx.Where("email = ?", token.Email).Take(user).Error
if err != nil {
// 没有找到在数据库就创建注册
if err == gorm.ErrRecordNotFound {
FirstName := token.Extend["first_name"].(string)
LastName := token.Extend["last_name"].(string)
Resetaurant := token.Extend["resetaurant"].(string)
err = UserTx.Where("email = ?", token.Email).Take(user).Error
if err == gorm.ErrRecordNotFound {
createAt := time.Now().UTC().Unix()
user.Email = &token.Email
user.CreatedAt = &createAt
user.PasswordHash = &token.Password
user.FirstName = &FirstName
user.LastName = &LastName
FirstName := token.Extend["first_name"].(string)
LastName := token.Extend["last_name"].(string)
Resetaurant := token.Extend["resetaurant"].(string)
err = UserTx.Create(user).Error
log.Println("create")
createAt := time.Now().UTC().Unix()
user.Email = &token.Email
user.CreatedAt = &createAt
user.PasswordHash = &token.Password
user.FirstName = &FirstName
user.LastName = &LastName
err = UserTx.Create(user).Error
if err != nil {
logx.Error(err)
return err
}
logcontent += "create."
// 继承guest_id的资源表
err = InheritGuestIdResource(tx, user.Id, token.GuestId, func(txResouce, txUserMaterial, txUserInfo *gorm.DB) error {
userProfile := &UserProfile{
FirstName: FirstName,
LastName: LastName,
Resetaurant: Resetaurant,
}
metadata, err := json.Marshal(userProfile)
if err != nil {
return err
}
if token.GuestId != 0 {
// 继承guest_id的资源表
return InheritGuestIdResource(tx, user.Id, token.GuestId, func(txResouce, txUserMaterial, txUserInfo *gorm.DB) error {
userProfile := &UserProfile{
FirstName: FirstName,
LastName: LastName,
Resetaurant: Resetaurant,
}
metadata, err := json.Marshal(userProfile)
if err != nil {
return err
}
now := time.Now()
uinfo := &FsUserInfo{
Module: FsString("profile"),
UserId: &user.Id,
GuestId: &token.GuestId,
Metadata: &metadata,
Ctime: &now,
Utime: &now,
}
return txUserInfo.Create(uinfo).Error
})
now := time.Now()
uinfo := &FsUserInfo{
Module: FsString("profile"),
UserId: &user.Id,
GuestId: &token.GuestId,
Metadata: &metadata,
Ctime: &now,
Utime: &now,
}
return err
logcontent += "profile."
return txUserInfo.Create(uinfo).Error
})
if err == gorm.ErrRecordNotFound {
return nil
}
return err
}
if err == nil {
return fmt.Errorf("the email had registered")
}
return err
return fmt.Errorf("the email had registered")
})
if err != nil {
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}

View File

@ -85,12 +85,13 @@ func main() {
indexHtmlPath := vueBuild + "/index.html"
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(100 << 20)
if err != nil {
log.Println(err)
}
if strings.HasPrefix(r.URL.Path, "/api/") {
err := r.ParseMultipartForm(100 << 20)
if err != nil {
log.Println(err)
}
// 对/api开头的请求进行反向代理
proxy := httputil.NewSingleHostReverseProxy(apiURL)
proxy.ServeHTTP(w, r)

View File

@ -118,7 +118,7 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma
// log.Println("aaaa", token)
user, err := l.svcCtx.AllModels.FsUser.RegisterByFusen(l.ctx, token)
if err != nil {
logx.Error(err, token.TraceId)
logx.Error(err, ":", token.TraceId)
return resp.SetStatus(basic.CodeDbSqlErr)
}