This commit is contained in:
eson 2023-07-27 19:47:52 +08:00
parent 64c6c19918
commit 3be161a011
5 changed files with 91 additions and 45 deletions

View File

@ -0,0 +1,27 @@
package gmodel
import (
"gorm.io/gorm"
"time"
)
// fs_resource 资源表
type FsResource struct {
ResourceId string `gorm:"primary_key;default:'';" json:"resource_id"` // 资源 ID
UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户 ID
GuestId *int64 `gorm:"index;default:0;" json:"guest_id"` // 访客 ID
ResourceType *string `gorm:"index;default:'';" json:"resource_type"` // 资源类型
ResourceUrl *string `gorm:"default:'';" json:"resource_url"` // 资源 URL
Version *string `gorm:"index;default:'0';" json:"version"` // 版本信息
UploadedAt *time.Time `gorm:"index;default:'0000-00-00 00:00:00';" json:"uploaded_at"` // 上传时间
Metadata *string `gorm:"default:'';" json:"metadata"` // 元数据,json格式,存储图像分率
MetaKey1 *string `gorm:"index;default:'';" json:"meta_key1"` // 需要关键信息查询的自定义属性1,可以动态增加
}
type FsResourceModel struct {
db *gorm.DB
name string
}
func NewFsResourceModel(db *gorm.DB) *FsResourceModel {
return &FsResourceModel{db: db, name: "fs_resource"}
}

View File

@ -0,0 +1,3 @@
package gmodel
// TODO: 使用model的属性做你想做的

View File

@ -2,6 +2,8 @@ package gmodel
import (
"context"
"fusenapi/utils/auth"
"time"
"gorm.io/gorm"
)
@ -43,6 +45,51 @@ func (u *FsUserModel) Transaction(ctx context.Context, fc func(tx *gorm.DB) erro
return u.db.WithContext(ctx).Transaction(fc)
}
// 谷歌平台的注册流程
func (u *FsUserModel) RegisterByGoogleOAuth(ctx context.Context, token *auth.RegisterToken) (*FsUser, error) {
user := &FsUser{}
err := u.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
err := tx.Model(user).Where("email = ?", token.Email).Take(user).Error
if err != nil {
// 没有找到在数据库就创建注册
if err == gorm.ErrRecordNotFound {
createAt := time.Now().Unix()
user.Email = &token.Email
user.CreatedAt = &createAt
user.GoogleId = &token.Id
user.PasswordHash = &token.Password
err = tx.Model(user).Create(user).Error
if err != nil {
return err
}
if token.GuestId != 0 {
// 继承guest_id的资源表
return tx.Model(&FsResource{}).
Where("guest_id = ?", token.GuestId).
UpdateColumn("user_id", user.Id).Error
}
return err
}
return err
}
// 如果已经存在,把谷歌id 加入到用户信息里
user.GoogleId = &token.Id
return tx.Model(user).Update("google_id", user).Error
})
if err != nil {
return nil, err
}
return user, nil
}
func (u *FsUserModel) UpdateUserBasicInfoById(ctx context.Context, Id int64, user *UserBasicInfoForSave) (err error) {
err = u.db.WithContext(ctx).Model(&FsUser{}).

View File

@ -79,7 +79,7 @@ type AllModelsGen struct {
FsQuotationRemarkTemplate *FsQuotationRemarkTemplateModel // fs_quotation_remark_template 报价单备注模板
FsQuotationSaler *FsQuotationSalerModel // fs_quotation_saler 报价单业务员表
FsRefundReason *FsRefundReasonModel // fs_refund_reason
FsResources *FsResourcesModel // fs_resources 资源表
FsResource *FsResourceModel // fs_resource 资源表
FsStandardLogo *FsStandardLogoModel // fs_standard_logo 标准logo
FsTags *FsTagsModel // fs_tags 产品分类表
FsToolLogs *FsToolLogsModel // fs_tool_logs 3d设计工具日志表
@ -170,7 +170,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen {
FsQuotationRemarkTemplate: NewFsQuotationRemarkTemplateModel(gdb),
FsQuotationSaler: NewFsQuotationSalerModel(gdb),
FsRefundReason: NewFsRefundReasonModel(gdb),
FsResources: NewFsResourcesModel(gdb),
FsResource: NewFsResourceModel(gdb),
FsStandardLogo: NewFsStandardLogoModel(gdb),
FsTags: NewFsTagsModel(gdb),
FsToolLogs: NewFsToolLogsModel(gdb),

View File

@ -1,9 +1,9 @@
package logic
import (
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"log"
"time"
"context"
@ -12,7 +12,6 @@ import (
"fusenapi/server/auth/internal/types"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
type UserEmailConfirmationLogic struct {
@ -51,13 +50,14 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma
switch token.Platform {
case "google":
user, err := l.OpGoogleRegister(token)
// 谷歌平台的注册流程
user, err := l.svcCtx.AllModels.FsUser.RegisterByGoogleOAuth(l.ctx, token)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr)
}
// 创建签证
jwtToken, err := auth.GenerateJwtTokenUint64(
auth.StringToHash(*user.PasswordHash),
l.svcCtx.Config.Auth.AccessExpire,
@ -66,8 +66,15 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma
0,
)
if err != nil {
logx.Error(err)
return
}
log.Println(jwtToken)
case "facebook":
l.OpGoogleRegister(token)
}
default:
@ -81,41 +88,3 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma
// func (l *UserEmailConfirmationLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
// OpGoogleRegister 谷歌平台的注册流程
func (l *UserEmailConfirmationLogic) OpGoogleRegister(token *auth.RegisterToken) (*gmodel.FsUser, error) {
user := &gmodel.FsUser{}
err := l.svcCtx.AllModels.FsUser.Transaction(context.TODO(), func(tx *gorm.DB) error {
err := tx.Model(user).Where("email = ?", token.Email).Take(user).Error
if err != nil {
// 没有找到在数据库就创建注册
if err == gorm.ErrRecordNotFound {
createAt := time.Now().Unix()
user.Email = &token.Email
user.CreatedAt = &createAt
user.GoogleId = &token.Id
user.PasswordHash = &token.Password
err = tx.Model(user).Create(user).Error
if err != nil {
return err
}
// 继承guest_id的资源表
// tx.Table("fs_resources").Model()
return nil
}
return err
}
user.GoogleId = &token.Id
return tx.Model(user).Update("google_id", user).Error
})
if err != nil {
return nil, err
}
return user, nil
}