修复json的类型影响

This commit is contained in:
eson
2023-08-24 11:47:22 +08:00
parent 500611236e
commit d8ddad6fe9
18 changed files with 190 additions and 56 deletions

View File

@@ -17,6 +17,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/api/auth/login",
Handler: UserLoginHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/api/auth/register",
Handler: UserRegisterHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/api/auth/accept-cookie",

View File

@@ -0,0 +1,35 @@
package handler
import (
"net/http"
"reflect"
"fusenapi/utils/basic"
"fusenapi/server/auth/internal/logic"
"fusenapi/server/auth/internal/svc"
"fusenapi/server/auth/internal/types"
)
func UserRegisterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.RequestUserRegister
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewUserRegisterLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.UserRegister(&req, userinfo)
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)
}
}
}

View File

@@ -74,6 +74,7 @@ func (l *UserEmailConfirmationLogic) UserEmailConfirmation(req *types.RequestEma
log.Println(jwtToken) // 通过websocket去, 送回通道
case "facebook":
case "fusen":
}

View File

@@ -60,7 +60,7 @@ func (l *UserEmailRegisterLogic) UserEmailRegister(req *types.RequestEmailRegist
// 确认email 重新序列化
token.Email = req.Email
token.Wid = req.Wid
token.GuestId = req.GuestId
token.GuestId = userinfo.GuestId
clurl, err := l.svcCtx.RegisterTokenManger.Generate(token)
if err != nil {

View File

@@ -19,7 +19,6 @@ import (
"github.com/474420502/requests"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"golang.org/x/net/proxy"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"gorm.io/gorm"
@@ -53,19 +52,6 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:1080", nil, proxy.Direct)
if err != nil {
log.Fatal(err)
}
customClient := &http.Client{
Transport: &http.Transport{
Dial: dialer.Dial,
},
}
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, customClient)
var googleOauthConfig = &oauth2.Config{
RedirectURL: fmt.Sprintf("http://%s/api/user/oauth2/login/google", l.svcCtx.Config.MainAddress),
ClientID: l.svcCtx.Config.OAuth.Google.Appid,
@@ -74,15 +60,13 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us
Endpoint: google.Endpoint,
}
token, err := googleOauthConfig.Exchange(ctx, req.Code)
token, err := googleOauthConfig.Exchange(l.ctx, req.Code)
if err != nil {
logx.Error(err)
resp.SetStatus(basic.CodeApiErr)
}
ses := requests.NewSession()
ses.Config().SetProxy("socks5://127.0.0.1:1080") // 代理 为了测试功能
r, err := ses.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken).Execute()
if err != nil {
logx.Error(err)

View File

@@ -0,0 +1,75 @@
package logic
import (
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"time"
"context"
"fusenapi/server/auth/internal/svc"
"fusenapi/server/auth/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UserRegisterLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRegisterLogic {
return &UserRegisterLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *UserRegisterLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *UserRegisterLogic) UserRegister(req *types.RequestUserRegister, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
// 邮箱验证格式错误
if !auth.ValidateEmail(req.Email) {
return resp.SetStatus(basic.CodeOAuthEmailErr)
}
token := &auth.RegisterToken{
OperateType: auth.OpTypeRegister,
Id: 0,
GuestId: userinfo.GuestId,
Wid: req.Wid,
Email: req.Email,
Password: req.Password,
Platform: "fusen",
CreateAt: time.Now(),
}
clurl, err := l.svcCtx.RegisterTokenManger.Generate(token)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeOAuthRegisterTokenErr)
}
// 进入发送邮箱的系统
EmailManager.EmailTasks <- &EmailFormat{
TargetEmail: req.Email,
CompanyName: "fusen",
ConfirmationLink: clurl,
SenderName: "support@fusenpack.com",
SenderTitle: "register-valid",
} // email进入队
return resp.SetStatus(basic.CodeOK)
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *UserRegisterLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }

View File

@@ -47,7 +47,7 @@ func (l *UserResetTokenLogic) UserResetToken(req *types.RequestUserResetToken, u
token := &auth.ResetToken{
// 操作的类型, 验证的token 必须要继承这个
OperateType: auth.OpTypeResetToken,
UserId: uint64(userinfo.UserId),
UserId: userinfo.UserId,
Wid: req.Wid,
Email: *user.Email,
OldPassword: *user.PasswordHash,

View File

@@ -14,6 +14,16 @@ type DataUserLogin struct {
Token string `json:"token"` // 登录jwt token
}
type RequestUserRegister struct {
Wid string `json:"wid"` // websocket的id
GuestId int64 `json:"guest_id"` // 游客id
FirstName string `json:"first_name"` // 首名
LastName string `json:"last_name"` // 名
Resetaurant string `json:"resetaurant"` // 餐厅类型
Email string `json:"email"` // email
Password string `json:"password"` // 密码
}
type RequestUserResetToken struct {
Wid string `json:"wid"`
}
@@ -39,9 +49,8 @@ type RequestEmailConfirmation struct {
}
type RequestEmailRegister struct {
Email string `json:"email"`
Wid string `json:"wid"`
GuestId uint64 `json:"guest_id"`
Email string `json:"email"`
RegisterToken string `json:"register_token"`
}

View File

@@ -76,13 +76,15 @@ func (l *UserInfoSetLogic) UserInfoSet(req *types.UserInfoSetReq, userinfo *auth
}
var nowTime = time.Now().UTC()
if userInfo.Id != 0 {
userInfo.Metadata = &req.Metadata
v := []byte(req.Metadata)
userInfo.Metadata = &v
userInfo.Utime = &nowTime
} else {
userInfo.GuestId = &guestId
userInfo.UserId = &userId
userInfo.Module = &req.Module
userInfo.Metadata = &req.Metadata
v := []byte(req.Metadata)
userInfo.Metadata = &v
userInfo.Ctime = &nowTime
}
_, err = fsUserInfoModel.CreateOrUpdate(BuilderDB, userInfo)