Merge branch 'develop' of gitee.com:fusenpack/fusenapi into develop
This commit is contained in:
commit
60255b5c45
@ -6,9 +6,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"fusenapi/utils/auth"
|
"fusenapi/utils/auth"
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -52,36 +52,37 @@ func (u *FsUserModel) Transaction(ctx context.Context, fc func(tx *gorm.DB) erro
|
|||||||
// 继承guest_id的资源表
|
// 继承guest_id的资源表
|
||||||
func InheritGuestIdResource(tx *gorm.DB, userId, guestId int64, afterDo func(txResouce *gorm.DB, txUserMaterial *gorm.DB, txUserInfo *gorm.DB) error) error {
|
func InheritGuestIdResource(tx *gorm.DB, userId, guestId int64, afterDo func(txResouce *gorm.DB, txUserMaterial *gorm.DB, txUserInfo *gorm.DB) error) error {
|
||||||
var err error
|
var err error
|
||||||
|
txRes := tx.Model(&FsResource{})
|
||||||
|
txUserMaterial := tx.Model(&FsUserMaterial{})
|
||||||
|
txUserInfo := tx.Model(&FsUserInfo{})
|
||||||
if guestId != 0 {
|
if guestId != 0 {
|
||||||
// 继承guest_id的资源表
|
// 继承guest_id的资源表
|
||||||
txRes := tx.Model(&FsResource{})
|
|
||||||
err = txRes.
|
err = txRes.
|
||||||
Where("guest_id = ?", guestId).
|
Where("guest_id = ?", guestId).
|
||||||
UpdateColumn("user_id", userId).Error
|
UpdateColumn("user_id", userId).Error
|
||||||
|
|
||||||
if err != nil {
|
if err != nil && err != gorm.ErrRecordNotFound {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
txUserMaterial := tx.Model(&FsUserMaterial{})
|
|
||||||
err = txUserMaterial.
|
err = txUserMaterial.
|
||||||
Where("guest_id = ?", guestId).
|
Where("guest_id = ?", guestId).
|
||||||
UpdateColumn("user_id", userId).Error
|
UpdateColumn("user_id", userId).Error
|
||||||
|
|
||||||
if err != nil {
|
if err != nil && err != gorm.ErrRecordNotFound {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
txUserInfo := tx.Model(&FsUserInfo{})
|
|
||||||
err = txUserInfo.
|
err = txUserInfo.
|
||||||
Where("guest_id = ?", guestId).
|
Where("guest_id = ?", guestId).
|
||||||
UpdateColumn("user_id", userId).Error
|
UpdateColumn("user_id", userId).Error
|
||||||
if err != nil {
|
if err != nil && err != gorm.ErrRecordNotFound {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if afterDo != nil {
|
if afterDo != nil {
|
||||||
return afterDo(txRes, txUserMaterial, txUserInfo)
|
return afterDo(txRes, txUserMaterial, txUserInfo)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return fmt.Errorf("guest_id must not be 0")
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if token.GuestId != 0 {
|
// 继承guest_id的资源表
|
||||||
// 继承guest_id的资源表
|
return InheritGuestIdResource(tx, user.Id, token.GuestId, nil)
|
||||||
return InheritGuestIdResource(tx, user.Id, token.GuestId, nil)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
@ -140,67 +138,73 @@ type UserProfile struct {
|
|||||||
func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterToken) (*FsUser, error) {
|
func (u *FsUserModel) RegisterByFusen(ctx context.Context, token *auth.RegisterToken) (*FsUser, error) {
|
||||||
user := &FsUser{}
|
user := &FsUser{}
|
||||||
|
|
||||||
|
logcontent := ""
|
||||||
|
defer func() {
|
||||||
|
logx.Info("aaaa:", logcontent)
|
||||||
|
}()
|
||||||
|
|
||||||
err := u.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
err := u.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||||
|
var err error
|
||||||
UserTx := tx.Model(user)
|
UserTx := tx.Model(user)
|
||||||
err := UserTx.Where("email = ?", token.Email).Take(user).Error
|
err = UserTx.Where("email = ?", token.Email).Take(user).Error
|
||||||
if err != nil {
|
if err == gorm.ErrRecordNotFound {
|
||||||
// 没有找到在数据库就创建注册
|
|
||||||
if err == gorm.ErrRecordNotFound {
|
|
||||||
FirstName := token.Extend["first_name"].(string)
|
|
||||||
LastName := token.Extend["last_name"].(string)
|
|
||||||
Resetaurant := token.Extend["resetaurant"].(string)
|
|
||||||
|
|
||||||
createAt := time.Now().UTC().Unix()
|
FirstName := token.Extend["first_name"].(string)
|
||||||
user.Email = &token.Email
|
LastName := token.Extend["last_name"].(string)
|
||||||
user.CreatedAt = &createAt
|
Resetaurant := token.Extend["resetaurant"].(string)
|
||||||
user.PasswordHash = &token.Password
|
|
||||||
user.FirstName = &FirstName
|
|
||||||
user.LastName = &LastName
|
|
||||||
|
|
||||||
err = UserTx.Create(user).Error
|
createAt := time.Now().UTC().Unix()
|
||||||
log.Println("create")
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
now := time.Now()
|
||||||
if token.GuestId != 0 {
|
uinfo := &FsUserInfo{
|
||||||
|
Module: FsString("profile"),
|
||||||
// 继承guest_id的资源表
|
UserId: &user.Id,
|
||||||
return InheritGuestIdResource(tx, user.Id, token.GuestId, func(txResouce, txUserMaterial, txUserInfo *gorm.DB) error {
|
GuestId: &token.GuestId,
|
||||||
userProfile := &UserProfile{
|
Metadata: &metadata,
|
||||||
FirstName: FirstName,
|
Ctime: &now,
|
||||||
LastName: LastName,
|
Utime: &now,
|
||||||
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
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return err
|
logcontent += "profile."
|
||||||
|
return txUserInfo.Create(uinfo).Error
|
||||||
|
})
|
||||||
|
|
||||||
|
if err == gorm.ErrRecordNotFound {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == nil {
|
return fmt.Errorf("the email had registered")
|
||||||
return fmt.Errorf("the email had registered")
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil && err != gorm.ErrRecordNotFound {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,12 +85,13 @@ func main() {
|
|||||||
indexHtmlPath := vueBuild + "/index.html"
|
indexHtmlPath := vueBuild + "/index.html"
|
||||||
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
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/") {
|
if strings.HasPrefix(r.URL.Path, "/api/") {
|
||||||
|
|
||||||
|
err := r.ParseMultipartForm(100 << 20)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
// 对/api开头的请求进行反向代理
|
// 对/api开头的请求进行反向代理
|
||||||
proxy := httputil.NewSingleHostReverseProxy(apiURL)
|
proxy := httputil.NewSingleHostReverseProxy(apiURL)
|
||||||
proxy.ServeHTTP(w, r)
|
proxy.ServeHTTP(w, r)
|
||||||
|
@ -118,7 +118,7 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma
|
|||||||
// log.Println("aaaa", token)
|
// log.Println("aaaa", token)
|
||||||
user, err := l.svcCtx.AllModels.FsUser.RegisterByFusen(l.ctx, token)
|
user, err := l.svcCtx.AllModels.FsUser.RegisterByFusen(l.ctx, token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logx.Error(err, token.TraceId)
|
logx.Error(err, ":", token.TraceId)
|
||||||
return resp.SetStatus(basic.CodeDbSqlErr)
|
return resp.SetStatus(basic.CodeDbSqlErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user