154 lines
3.8 KiB
Go
154 lines
3.8 KiB
Go
package logic
|
|
|
|
import (
|
|
"fmt"
|
|
"fusenapi/utils/auth"
|
|
"fusenapi/utils/basic"
|
|
"fusenapi/utils/wevent"
|
|
"time"
|
|
|
|
"context"
|
|
|
|
"fusenapi/server/auth/internal/svc"
|
|
"fusenapi/server/auth/internal/types"
|
|
|
|
"github.com/474420502/requests"
|
|
"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) {
|
|
// }
|
|
|
|
func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEmailConfirmation, userinfo *auth.UserInfo) (resp *basic.Response) {
|
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
|
// userinfo 传入值时, 一定不为null
|
|
|
|
token, err := l.svcCtx.RegisterTokenManger.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.svcCtx.AllModels.FsUser.RegisterByGoogleOAuth(l.ctx, token)
|
|
if err != nil {
|
|
logx.Error(err, token.TraceId)
|
|
return resp.SetStatus(basic.CodeDbSqlErr)
|
|
}
|
|
|
|
// 创建签证
|
|
jwtToken, err := auth.GenerateJwtTokenUint64(
|
|
auth.StringToHash(*user.PasswordHash),
|
|
l.svcCtx.Config.Auth.AccessExpire,
|
|
time.Now().Unix(),
|
|
user.Id,
|
|
0,
|
|
)
|
|
|
|
if err != nil {
|
|
logx.Error(err, token.TraceId)
|
|
return
|
|
}
|
|
|
|
event := wevent.NewWebsocketEventSuccess(wevent.UserEmailRegister, token.TraceId)
|
|
event.Data = wevent.DataEmailRegister{
|
|
JwtToken: jwtToken,
|
|
}
|
|
|
|
tp := requests.Post(fmt.Sprintf("%s/api/websocket/common_notify", l.svcCtx.Config.MainAddress))
|
|
tp.SetBodyJson(requests.M{
|
|
"wid": token.Wid,
|
|
"data": event,
|
|
})
|
|
|
|
wresp, err := tp.Execute()
|
|
if err != nil {
|
|
logx.Error(err, token.TraceId)
|
|
return
|
|
}
|
|
|
|
result := wresp.Json()
|
|
if result.Get("code").Int() != 200 {
|
|
logx.Error(result.Get("message"))
|
|
return
|
|
}
|
|
logx.Info("success", token.TraceId, jwtToken)
|
|
|
|
case "facebook":
|
|
case "fusen":
|
|
|
|
user, err := l.svcCtx.AllModels.FsUser.RegisterByFusen(l.ctx, token)
|
|
if err != nil {
|
|
logx.Error(err, token.TraceId)
|
|
return resp.SetStatus(basic.CodeDbSqlErr)
|
|
}
|
|
|
|
// 创建签证
|
|
jwtToken, err := auth.GenerateJwtTokenUint64(
|
|
auth.StringToHash(*user.PasswordHash),
|
|
l.svcCtx.Config.Auth.AccessExpire,
|
|
time.Now().Unix(),
|
|
user.Id,
|
|
0,
|
|
)
|
|
|
|
if err != nil {
|
|
logx.Error(err, token.TraceId)
|
|
return
|
|
}
|
|
|
|
event := wevent.NewWebsocketEventSuccess(wevent.UserEmailRegister, token.TraceId)
|
|
event.Data = wevent.DataEmailRegister{
|
|
JwtToken: jwtToken,
|
|
}
|
|
tp := requests.Post(fmt.Sprintf("%s/api/websocket/common_notify", l.svcCtx.Config.MainAddress))
|
|
tp.SetBodyJson(requests.M{
|
|
"wid": token.Wid,
|
|
"data": event,
|
|
})
|
|
wresp, err := tp.Execute()
|
|
if err != nil {
|
|
logx.Error(err, token.TraceId)
|
|
}
|
|
result := wresp.Json()
|
|
if result.Get("code").Int() != 200 {
|
|
logx.Error(result.Get("message"))
|
|
}
|
|
logx.Info("success", token.TraceId, jwtToken)
|
|
}
|
|
|
|
default:
|
|
return resp.SetStatus(basic.CodeOAuthRegisterTokenErr)
|
|
}
|
|
|
|
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)
|
|
// }
|