fusenapi/server/auth/internal/logic/useremailconfirmationlogic.go

122 lines
3.0 KiB
Go
Raw Normal View History

2023-07-26 16:03:38 +00:00
package logic
import (
2023-07-27 08:48:43 +00:00
"fusenapi/model/gmodel"
2023-07-26 16:03:38 +00:00
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-07-27 08:48:43 +00:00
"time"
2023-07-26 16:03:38 +00:00
"context"
"fusenapi/server/auth/internal/svc"
"fusenapi/server/auth/internal/types"
"github.com/zeromicro/go-zero/core/logx"
2023-07-27 08:48:43 +00:00
"gorm.io/gorm"
2023-07-26 16:03:38 +00:00
)
type UserEmailConfirmationLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserEmailConfirmationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserEmailConfirmationLogic {
return &UserEmailConfirmationLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *UserEmailConfirmationLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEmailConfirmation, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
2023-07-27 08:48:43 +00:00
token, err := l.svcCtx.TokenManger.Decrypt(req.Token)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeOAuthRegisterTokenErr)
}
switch token.OperateType {
case auth.OpTypeRegister:
if time.Since(token.CreateAt) >= 24*time.Hour {
return resp.SetStatus(basic.CodeOAuthConfirmationTimeoutErr)
}
switch token.Platform {
case "google":
user, err := l.OpGoogleRegister(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,
time.Now().Unix(),
user.Id,
0,
)
case "facebook":
l.OpGoogleRegister(token)
}
default:
return resp.SetStatus(basic.CodeOAuthRegisterTokenErr)
}
2023-07-26 16:03:38 +00:00
return resp.SetStatus(basic.CodeOK)
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *UserEmailConfirmationLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
2023-07-27 08:48:43 +00:00
// 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
}