修改表单
This commit is contained in:
parent
72a6412eed
commit
900d5bf24b
|
@ -1,6 +1,7 @@
|
|||
Name: home-user-auth
|
||||
Host: 0.0.0.0
|
||||
Port: 9904
|
||||
MainAddress: "http://localhost:9900"
|
||||
SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest
|
||||
|
||||
Auth:
|
||||
|
|
|
@ -11,6 +11,8 @@ type Config struct {
|
|||
SourceMysql string
|
||||
Auth types.Auth
|
||||
|
||||
MainAddress string
|
||||
|
||||
OAuth struct {
|
||||
Google struct {
|
||||
Appid string
|
||||
|
|
|
@ -72,6 +72,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||
Path: "/api/user/oauth2/login/google",
|
||||
Handler: UserGoogleLoginHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/user/oauth2/login/register",
|
||||
Handler: UserEmailRegisterHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/user/order-list",
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/home-user-auth/internal/logic"
|
||||
"fusenapi/server/home-user-auth/internal/svc"
|
||||
"fusenapi/server/home-user-auth/internal/types"
|
||||
)
|
||||
|
||||
func UserEmailRegisterHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.RequestEmailRegister
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewUserEmailRegisterLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.UserEmailRegister(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,40 +1,35 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/home-user-auth/internal/logic"
|
||||
"fusenapi/server/home-user-auth/internal/svc"
|
||||
"fusenapi/server/home-user-auth/internal/types"
|
||||
"fusenapi/utils/basic"
|
||||
)
|
||||
|
||||
// UserLoginHandler 特殊的登录获取jwt token处理
|
||||
func UserLoginHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.RequestUserLogin
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewUserLoginLogic(r.Context(), svcCtx)
|
||||
resp, token := l.UserLogin(&req)
|
||||
if resp.Code == basic.CodeOK.Code {
|
||||
w.Header().Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
}
|
||||
|
||||
// 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应;
|
||||
// 否则,发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
|
||||
if resp != nil {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
} else {
|
||||
err := errors.New("server logic is error, resp must not be nil")
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
logx.Error(err)
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.UserLogin(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/home-user-auth/internal/svc"
|
||||
"fusenapi/server/home-user-auth/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UserEmailRegisterLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUserEmailRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserEmailRegisterLogic {
|
||||
return &UserEmailRegisterLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *UserEmailRegisterLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向
|
||||
// func (l *UserEmailRegisterLogic) AfterLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
func (l *UserEmailRegisterLogic) UserEmailRegister(req *types.RequestEmailRegister, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
|
@ -27,7 +27,10 @@ type UserGoogleLoginLogic struct {
|
|||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
|
||||
redirectUrl string
|
||||
token string // 登录 token
|
||||
|
||||
isRegistered bool // 是否注册
|
||||
registerToken string // 注册邮箱的token
|
||||
}
|
||||
|
||||
func NewUserGoogleLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserGoogleLoginLogic {
|
||||
|
@ -44,6 +47,13 @@ func NewUserGoogleLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *U
|
|||
|
||||
func (l *UserGoogleLoginLogic) AfterLogic(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
rurl := fmt.Sprintf(
|
||||
l.svcCtx.Config.MainAddress+"/oauth?token=%s&is_registered=%t®ister_token=%s",
|
||||
l.token,
|
||||
l.isRegistered,
|
||||
l.registerToken,
|
||||
)
|
||||
|
||||
html := fmt.Sprintf(`
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
@ -58,7 +68,7 @@ func (l *UserGoogleLoginLogic) AfterLogic(w http.ResponseWriter, r *http.Request
|
|||
<body>
|
||||
</body>
|
||||
</html>
|
||||
`, l.redirectUrl)
|
||||
`, rurl)
|
||||
fmt.Fprintln(w, html)
|
||||
}
|
||||
|
||||
|
@ -104,17 +114,21 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us
|
|||
log.Println(r.Json())
|
||||
|
||||
googleId := r.Json().Get("id").Int()
|
||||
// l.redirectUrl = "http://localhost:9900/oauth?token=21321123"
|
||||
|
||||
// l.redirectUrl = "http://localhost:9900/oauth?token=21321123&is_registered"
|
||||
// return resp.Set(304, "21321321")
|
||||
user, err := l.svcCtx.AllModels.FsUser.FindUserByGoogleId(context.TODO(), googleId)
|
||||
log.Println(user)
|
||||
if err != nil {
|
||||
if err != gorm.ErrRecordNotFound {
|
||||
logx.Error(err)
|
||||
|
||||
return resp.SetStatus(basic.CodeDbSqlErr)
|
||||
}
|
||||
|
||||
if req.Email == "" {
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
|
||||
// 如果密码匹配,则生成 JWT Token。
|
||||
nowSec := time.Now().Unix()
|
||||
jwtToken, err := auth.GenerateJwtToken(&l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, nowSec, 0, 0)
|
||||
|
|
|
@ -3,6 +3,8 @@ package logic
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"fusenapi/server/home-user-auth/internal/svc"
|
||||
|
@ -18,6 +20,8 @@ type UserLoginLogic struct {
|
|||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
|
||||
token string
|
||||
}
|
||||
|
||||
func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
|
||||
|
@ -28,35 +32,41 @@ func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLog
|
|||
}
|
||||
}
|
||||
|
||||
func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *basic.Response, jwtToken string) {
|
||||
func (l *UserLoginLogic) AfterLogic(w http.ResponseWriter, r *http.Request) {
|
||||
if l.token != "" {
|
||||
w.Header().Add("Authorization", fmt.Sprintf("Bearer %s", l.token))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 创建一个 FsUserModel 对象 m 并实例化之,该对象用于操作 MySQL 数据库中的用户数据表。
|
||||
m := l.svcCtx.AllModels.FsUser
|
||||
|
||||
// 在用户数据表中根据登录名(email)查找用户记录,并返回 UserModel 类型的结构体对象 userModel。
|
||||
user, err := m.FindUserByEmail(l.ctx, req.Email)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatus(basic.CodeEmailNotFoundErr), ""
|
||||
return resp.SetStatus(basic.CodeEmailNotFoundErr)
|
||||
}
|
||||
|
||||
// 如果在用户数据表中找到了登录名匹配的用户记录,则判断密码是否匹配。
|
||||
if *user.PasswordHash != req.Password {
|
||||
logx.Info("密码错误")
|
||||
return resp.SetStatus(basic.CodePasswordErr), jwtToken
|
||||
return resp.SetStatus(basic.CodePasswordErr)
|
||||
}
|
||||
|
||||
// 如果密码匹配,则生成 JWT Token。
|
||||
nowSec := time.Now().Unix()
|
||||
jwtToken, err = auth.GenerateJwtToken(&l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, nowSec, user.Id, 0)
|
||||
jwtToken, err := auth.GenerateJwtToken(&l.svcCtx.Config.Auth.AccessSecret, l.svcCtx.Config.Auth.AccessExpire, nowSec, user.Id, 0)
|
||||
|
||||
// 如果生成 JWT Token 失败,则抛出错误并返回未认证的状态码。
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatus(basic.CodeUnAuth), jwtToken
|
||||
return resp.SetStatus(basic.CodeUnAuth)
|
||||
}
|
||||
|
||||
// 如果更新 VerificationToken 字段失败,则返回未认证的状态码。
|
||||
if err != nil {
|
||||
return resp.SetStatus(basic.CodeUnAuth), jwtToken
|
||||
return resp.SetStatus(basic.CodeUnAuth)
|
||||
}
|
||||
|
||||
// 构造 DataUserLogin 类型的数据对象 data 并设置其属性值为生成的 JWT Token。
|
||||
|
@ -64,6 +74,8 @@ func (l *UserLoginLogic) UserLogin(req *types.RequestUserLogin) (resp *basic.Res
|
|||
Token: jwtToken,
|
||||
}
|
||||
|
||||
l.token = jwtToken
|
||||
|
||||
// 返回认证成功的状态码以及数据对象 data 和 JWT Token。
|
||||
return resp.SetStatus(basic.CodeOK, data), jwtToken
|
||||
return resp.SetStatus(basic.CodeOK, data)
|
||||
}
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/home-user-auth/internal/svc"
|
||||
"fusenapi/server/home-user-auth/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UserOAuth2LoginLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUserOAuth2LoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserOAuth2LoginLogic {
|
||||
|
||||
return &UserOAuth2LoginLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UserOAuth2LoginLogic) UserOAuth2Login(req *types.RequestOAuth, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
|
@ -75,11 +75,12 @@ type RequestGoogleLogin struct {
|
|||
Scope string `form:"scope"`
|
||||
AuthUser string `form:"authuser"`
|
||||
Prompt string `form:"prompt"`
|
||||
Email string `form:"email,optional"`
|
||||
}
|
||||
|
||||
type RequestOAuth struct {
|
||||
Platform string `json:"platform"`
|
||||
Code string `json:"code"`
|
||||
type RequestEmailRegister struct {
|
||||
Email string `json:"email"`
|
||||
RegisterToken string `json:"register_token"`
|
||||
}
|
||||
|
||||
type RequestContactService struct {
|
||||
|
@ -108,8 +109,8 @@ type RequestBasicInfoForm struct {
|
|||
}
|
||||
|
||||
type RequestUserLogin struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
Email string `form:"email"`
|
||||
Password string `form:"password"`
|
||||
}
|
||||
|
||||
type RequestAddAddress struct {
|
||||
|
|
|
@ -10,57 +10,60 @@ info (
|
|||
import "basic.api"
|
||||
|
||||
service home-user-auth {
|
||||
|
||||
|
||||
// @handler UserRegisterHandler
|
||||
// post /api/user/register(RequestUserRegister) returns (response);
|
||||
|
||||
|
||||
@handler UserLoginHandler
|
||||
post /api/user/login(RequestUserLogin) returns (response);
|
||||
|
||||
|
||||
@handler AcceptCookieHandler
|
||||
post /api/user/accept-cookie(request) returns (response);
|
||||
|
||||
|
||||
@handler UserFontsHandler
|
||||
get /api/user/fonts(request) returns (response);
|
||||
|
||||
|
||||
@handler UserGetTypeHandler
|
||||
get /api/user/get-type(request) returns (response);
|
||||
|
||||
|
||||
@handler UserSaveBasicInfoHandler
|
||||
post /api/user/basic-info(RequestBasicInfoForm) returns (response);
|
||||
|
||||
|
||||
@handler UserStatusConfigHandler
|
||||
get /api/user/status-config(request) returns (response);
|
||||
|
||||
|
||||
@handler UserBasicInfoHandler
|
||||
get /api/user/basic-info(request) returns (response);
|
||||
|
||||
|
||||
@handler UserAddressListHandler
|
||||
get /api/user/address-list(request) returns (response);
|
||||
|
||||
|
||||
@handler UserAddAddressHandler
|
||||
post /api/user/add-address(RequestAddAddress) returns (response);
|
||||
|
||||
|
||||
@handler UserContactServiceHandler
|
||||
post /api/user/contact-service (RequestContactService) returns (response);
|
||||
|
||||
|
||||
// @handler UserOderListHandler
|
||||
// get /api/user/order-list(RequestOrderId) returns (response);
|
||||
|
||||
|
||||
@handler UserOderDeleteHandler
|
||||
post /api/user/order-delete(RequestOrderId) returns (response);
|
||||
|
||||
|
||||
@handler UserGoogleLoginHandler
|
||||
get /api/user/oauth2/login/google(RequestGoogleLogin) returns (response);
|
||||
|
||||
|
||||
@handler UserEmailRegisterHandler
|
||||
get /api/user/oauth2/login/register(RequestEmailRegister) returns (response);
|
||||
|
||||
//订单列表
|
||||
@handler UserOrderListHandler
|
||||
get /api/user/order-list (UserOrderListReq) returns (response);
|
||||
|
||||
|
||||
//取消订单
|
||||
@handler UserOrderCancelHandler
|
||||
get /api/user/order-cancel (UserOrderCancelReq) returns (response);
|
||||
|
||||
|
||||
}
|
||||
|
||||
//取消订单
|
||||
|
@ -138,11 +141,12 @@ type RequestGoogleLogin {
|
|||
Scope string `form:"scope"`
|
||||
AuthUser string `form:"authuser"`
|
||||
Prompt string `form:"prompt"`
|
||||
Email string `form:"email,optional"`
|
||||
}
|
||||
|
||||
type RequestOAuth {
|
||||
Platform string `json:"platform"`
|
||||
Code string `json:"code"`
|
||||
type RequestEmailRegister {
|
||||
Email string `json:"email"`
|
||||
RegisterToken string `json:"register_token"`
|
||||
}
|
||||
|
||||
type RequestContactService {
|
||||
|
@ -174,8 +178,8 @@ type RequestBasicInfoForm {
|
|||
|
||||
// UserAddAddressHandler 用户登录请求结构
|
||||
type RequestUserLogin {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
Email string `form:"email"`
|
||||
Password string `form:"password"`
|
||||
}
|
||||
|
||||
// RequestAddAddress 增加地址结构
|
||||
|
|
Loading…
Reference in New Issue
Block a user