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

141 lines
3.4 KiB
Go
Raw Normal View History

2023-07-26 16:03:38 +00:00
package logic
import (
2023-08-25 07:37:35 +00:00
"fmt"
2023-08-27 15:54:28 +00:00
"fusenapi/model/gmodel"
2023-07-26 16:03:38 +00:00
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-08-25 07:37:35 +00:00
"fusenapi/utils/wevent"
2023-08-29 02:53:03 +00:00
"log"
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"
2023-08-25 07:37:35 +00:00
"github.com/474420502/requests"
2023-07-26 16:03:38 +00:00
"github.com/zeromicro/go-zero/core/logx"
)
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) {
// }
2023-08-27 15:54:28 +00:00
func FinishRegister(svcCtx *svc.ServiceContext, user *gmodel.FsUser, token *auth.RegisterToken) error {
// 创建签证
jwtToken, err := auth.GenerateJwtTokenUint64(
auth.StringToHash(*user.PasswordHash),
svcCtx.Config.Auth.AccessExpire,
time.Now().UTC().Unix(),
user.Id,
0,
)
if err != nil {
return err
}
event := wevent.NewWebsocketEventSuccess(wevent.UserEmailRegister, token.TraceId)
event.Data = wevent.DataEmailRegister{
JwtToken: jwtToken,
}
err = CommonNotify(svcCtx.Config.MainAddress, token.Wid, event)
if err != nil {
// logx.Error(err, token.TraceId)
return err
}
return nil
}
func CommonNotify(MainAddress, wid string, event *wevent.WebsocketEvent) error {
tp := requests.Post(fmt.Sprintf("%s/api/websocket/common_notify", MainAddress))
tp.SetBodyJson(requests.M{
"wid": wid,
"data": event,
})
wresp, err := tp.Execute()
if err != nil {
// logx.Error(err, token.TraceId)
return err
}
result := wresp.Json()
if result.Get("code").Int() != 200 {
// logx.Error(result.Get("message"))
return fmt.Errorf("%s", result.Get("message").Str)
}
return nil
}
2023-07-26 16:03:38 +00:00
func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEmailConfirmation, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
2023-08-11 09:39:18 +00:00
token, err := l.svcCtx.RegisterTokenManger.Decrypt(req.Token)
2023-07-27 08:48:43 +00:00
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":
2023-07-27 11:47:52 +00:00
// 谷歌平台的注册流程
user, err := l.svcCtx.AllModels.FsUser.RegisterByGoogleOAuth(l.ctx, token)
2023-07-27 08:48:43 +00:00
if err != nil {
2023-08-25 07:37:35 +00:00
logx.Error(err, token.TraceId)
2023-07-27 08:48:43 +00:00
return resp.SetStatus(basic.CodeDbSqlErr)
}
2023-08-27 15:54:28 +00:00
FinishRegister(l.svcCtx, user, token)
logx.Info("success", token.TraceId)
2023-07-27 11:47:52 +00:00
2023-07-27 08:48:43 +00:00
case "facebook":
2023-08-24 03:47:22 +00:00
case "fusen":
2023-08-29 02:53:03 +00:00
log.Println("aaaa", token)
2023-08-24 10:28:01 +00:00
user, err := l.svcCtx.AllModels.FsUser.RegisterByFusen(l.ctx, token)
if err != nil {
2023-08-25 07:37:35 +00:00
logx.Error(err, token.TraceId)
2023-08-24 10:28:01 +00:00
return resp.SetStatus(basic.CodeDbSqlErr)
}
2023-08-27 15:54:28 +00:00
FinishRegister(l.svcCtx, user, token)
logx.Info("success", token.TraceId)
2023-07-27 08:48:43 +00:00
}
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)
// }