first commit

This commit is contained in:
2024-10-02 00:57:17 +08:00
commit 6773f86bc5
312 changed files with 19169 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
package auth
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"tianyuan-api/apps/gateway/internal/logic/auth"
"tianyuan-api/apps/gateway/internal/svc"
"tianyuan-api/apps/gateway/internal/types"
xhttp "github.com/zeromicro/x/http"
)
func GetVerifyCodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetVerifyCodeReq
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := auth.NewGetVerifyCodeLogic(r.Context(), svcCtx)
err := l.GetVerifyCode(&req)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
}
}
}

View File

@@ -0,0 +1,40 @@
package auth
import (
"net/http"
"time"
"github.com/zeromicro/go-zero/rest/httpx"
"tianyuan-api/apps/gateway/internal/logic/auth"
"tianyuan-api/apps/gateway/internal/svc"
"tianyuan-api/apps/gateway/internal/types"
xhttp "github.com/zeromicro/x/http"
)
func LoginUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.LoginReq
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := auth.NewLoginUserLogic(r.Context(), svcCtx)
token, err := l.LoginUser(&req)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
http.SetCookie(w, &http.Cookie{
Name: "Authorization",
Value: token, // JWT 令牌
HttpOnly: true, // 防止 JavaScript 访问
Secure: false, // HTTPS 使用
SameSite: http.SameSiteLaxMode, // 防止 CSRF 攻击
Path: "/",
Expires: time.Now().Add(time.Duration(svcCtx.Config.AuthJWT.AccessExpire) * time.Second), // 过期时间
})
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
}
}
}

View File

@@ -0,0 +1,23 @@
package auth
import (
"net/http"
"time"
xhttp "github.com/zeromicro/x/http"
"tianyuan-api/apps/gateway/internal/svc"
)
func LogoutHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 设置空的JWT Cookie覆盖之前的JWT
http.SetCookie(w, &http.Cookie{
Name: "Authorization", // 你的JWT cookie名
Value: "", // 清空cookies
Path: "/",
HttpOnly: true,
Expires: time.Unix(0, 0), // 过期时间设置为过去
})
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
}
}

View File

@@ -0,0 +1,40 @@
package auth
import (
"net/http"
"time"
"github.com/zeromicro/go-zero/rest/httpx"
"tianyuan-api/apps/gateway/internal/logic/auth"
"tianyuan-api/apps/gateway/internal/svc"
"tianyuan-api/apps/gateway/internal/types"
xhttp "github.com/zeromicro/x/http"
)
func PhoneLoginUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PhoneLoginReq
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := auth.NewPhoneLoginUserLogic(r.Context(), svcCtx)
token, err := l.PhoneLoginUser(&req)
http.SetCookie(w, &http.Cookie{
Name: "Authorization",
Value: token, // JWT 令牌
HttpOnly: true, // 防止 JavaScript 访问
Secure: false, // HTTPS 使用
SameSite: http.SameSiteLaxMode, // 防止 CSRF 攻击
Path: "/",
Expires: time.Now().Add(time.Duration(svcCtx.Config.AuthJWT.AccessExpire) * time.Second), // 过期时间
})
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
}
}
}

View File

@@ -0,0 +1,30 @@
package auth
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"tianyuan-api/apps/gateway/internal/logic/auth"
"tianyuan-api/apps/gateway/internal/svc"
"tianyuan-api/apps/gateway/internal/types"
xhttp "github.com/zeromicro/x/http"
)
func RegisterUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.RegisterReq
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := auth.NewRegisterUserLogic(r.Context(), svcCtx)
err := l.RegisterUser(&req)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
}
}
}

View File

@@ -0,0 +1,21 @@
package base
import (
"net/http"
xhttp "github.com/zeromicro/x/http"
"tianyuan-api/apps/gateway/internal/logic/base"
"tianyuan-api/apps/gateway/internal/svc"
)
func HealthHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := base.NewHealthLogic(r.Context(), svcCtx)
resp, err := l.Health()
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, resp)
}
}
}

View File

@@ -0,0 +1,30 @@
package product
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"tianyuan-api/apps/gateway/internal/logic/product"
"tianyuan-api/apps/gateway/internal/svc"
"tianyuan-api/apps/gateway/internal/types"
xhttp "github.com/zeromicro/x/http"
)
func GetProductByIdHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetProductByIdReq
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := product.NewGetProductByIdLogic(r.Context(), svcCtx)
resp, err := l.GetProductById(&req)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, resp)
}
}
}

View File

@@ -0,0 +1,30 @@
package product
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"tianyuan-api/apps/gateway/internal/logic/product"
"tianyuan-api/apps/gateway/internal/svc"
"tianyuan-api/apps/gateway/internal/types"
xhttp "github.com/zeromicro/x/http"
)
func GetProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetProductListReq
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := product.NewGetProductListLogic(r.Context(), svcCtx)
resp, err := l.GetProductList(&req)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, resp)
}
}
}

View File

@@ -0,0 +1,167 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.7.2
package handler
import (
"net/http"
auth "tianyuan-api/apps/gateway/internal/handler/auth"
base "tianyuan-api/apps/gateway/internal/handler/base"
product "tianyuan-api/apps/gateway/internal/handler/product"
user "tianyuan-api/apps/gateway/internal/handler/user"
userProduct "tianyuan-api/apps/gateway/internal/handler/userProduct"
whitelistr "tianyuan-api/apps/gateway/internal/handler/whitelistr"
"tianyuan-api/apps/gateway/internal/svc"
"github.com/zeromicro/go-zero/rest"
)
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Path: "/getVerifyCode",
Handler: auth.GetVerifyCodeHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/login",
Handler: auth.LoginUserHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/logout",
Handler: auth.LogoutHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/phoneLogin",
Handler: auth.PhoneLoginUserHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/register",
Handler: auth.RegisterUserHandler(serverCtx),
},
},
rest.WithPrefix("/api/console/auth"),
)
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodGet,
Path: "/health",
Handler: base.HealthHandler(serverCtx),
},
},
rest.WithPrefix("/api/console/base"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.AuthInterceptor},
[]rest.Route{
{
Method: http.MethodGet,
Path: "/:productId",
Handler: product.GetProductByIdHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/list",
Handler: product.GetProductListHandler(serverCtx),
},
}...,
),
rest.WithPrefix("/api/console/product"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.AuthInterceptor},
[]rest.Route{
{
Method: http.MethodPost,
Path: "/businessLicenseUpload",
Handler: user.UploadBusinessLicenseHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/enterpriseAuth",
Handler: user.EnterpriseAuthHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/getSecretInfo",
Handler: user.GetSecretInfoHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/info",
Handler: user.GetUserInfoHandler(serverCtx),
},
}...,
),
rest.WithPrefix("/api/console/user"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.AuthInterceptor},
[]rest.Route{
{
Method: http.MethodPost,
Path: "/userProductAdd",
Handler: userProduct.AddUserProductHandler(serverCtx),
},
{
Method: http.MethodDelete,
Path: "/userProductDel/:id",
Handler: userProduct.DeleteUserProductHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/userProductList",
Handler: userProduct.GetUserProductListHandler(serverCtx),
},
}...,
),
rest.WithPrefix("/api/console/user-product"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.AuthInterceptor},
[]rest.Route{
{
Method: http.MethodGet,
Path: "/getWhitelistList",
Handler: whitelistr.GetWhitelistListHandler(serverCtx),
},
}...,
),
rest.WithPrefix("/api/console/whitelist"),
)
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.AuthInterceptor, serverCtx.EntAuthInterceptor},
[]rest.Route{
{
Method: http.MethodPost,
Path: "/addWhitelist",
Handler: whitelistr.AddWhitelistHandler(serverCtx),
},
{
Method: http.MethodDelete,
Path: "/delWhitelist",
Handler: whitelistr.DeleteWhitelistHandler(serverCtx),
},
}...,
),
rest.WithPrefix("/api/console/whitelist"),
)
}

View File

@@ -0,0 +1,30 @@
package user
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"tianyuan-api/apps/gateway/internal/logic/user"
"tianyuan-api/apps/gateway/internal/svc"
"tianyuan-api/apps/gateway/internal/types"
xhttp "github.com/zeromicro/x/http"
)
func EnterpriseAuthHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.EnterpriseAuthReq
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := user.NewEnterpriseAuthLogic(r.Context(), svcCtx)
err := l.EnterpriseAuth(&req)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
}
}
}

View File

@@ -0,0 +1,21 @@
package user
import (
"net/http"
xhttp "github.com/zeromicro/x/http"
"tianyuan-api/apps/gateway/internal/logic/user"
"tianyuan-api/apps/gateway/internal/svc"
)
func GetSecretInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := user.NewGetSecretInfoLogic(r.Context(), svcCtx)
resp, err := l.GetSecretInfo()
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, resp)
}
}
}

View File

@@ -0,0 +1,21 @@
package user
import (
"net/http"
xhttp "github.com/zeromicro/x/http"
"tianyuan-api/apps/gateway/internal/logic/user"
"tianyuan-api/apps/gateway/internal/svc"
)
func GetUserInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := user.NewGetUserInfoLogic(r.Context(), svcCtx)
resp, err := l.GetUserInfo()
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, resp)
}
}
}

View File

@@ -0,0 +1,21 @@
package user
import (
"net/http"
xhttp "github.com/zeromicro/x/http"
"tianyuan-api/apps/gateway/internal/logic/user"
"tianyuan-api/apps/gateway/internal/svc"
)
func UploadBusinessLicenseHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := user.NewUploadBusinessLicenseLogic(r.Context(), svcCtx)
resp, err := l.UploadBusinessLicense(r)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, resp)
}
}
}

View File

@@ -0,0 +1,30 @@
package userProduct
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"tianyuan-api/apps/gateway/internal/logic/userProduct"
"tianyuan-api/apps/gateway/internal/svc"
"tianyuan-api/apps/gateway/internal/types"
xhttp "github.com/zeromicro/x/http"
)
func AddUserProductHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.AddUserProductReq
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := userProduct.NewAddUserProductLogic(r.Context(), svcCtx)
err := l.AddUserProduct(&req)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
}
}
}

View File

@@ -0,0 +1,30 @@
package userProduct
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"tianyuan-api/apps/gateway/internal/logic/userProduct"
"tianyuan-api/apps/gateway/internal/svc"
"tianyuan-api/apps/gateway/internal/types"
xhttp "github.com/zeromicro/x/http"
)
func DeleteUserProductHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.DeleteUserProductReq
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := userProduct.NewDeleteUserProductLogic(r.Context(), svcCtx)
err := l.DeleteUserProduct(&req)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
}
}
}

View File

@@ -0,0 +1,30 @@
package userProduct
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"tianyuan-api/apps/gateway/internal/logic/userProduct"
"tianyuan-api/apps/gateway/internal/svc"
"tianyuan-api/apps/gateway/internal/types"
xhttp "github.com/zeromicro/x/http"
)
func GetUserProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetUserProductListReq
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := userProduct.NewGetUserProductListLogic(r.Context(), svcCtx)
resp, err := l.GetUserProductList(&req)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, resp)
}
}
}

View File

@@ -0,0 +1,30 @@
package whitelistr
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"tianyuan-api/apps/gateway/internal/logic/whitelistr"
"tianyuan-api/apps/gateway/internal/svc"
"tianyuan-api/apps/gateway/internal/types"
xhttp "github.com/zeromicro/x/http"
)
func AddWhitelistHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.AddWhitelistReq
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := whitelistr.NewAddWhitelistLogic(r.Context(), svcCtx)
err := l.AddWhitelist(&req)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
}
}
}

View File

@@ -0,0 +1,30 @@
package whitelistr
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"tianyuan-api/apps/gateway/internal/logic/whitelistr"
"tianyuan-api/apps/gateway/internal/svc"
"tianyuan-api/apps/gateway/internal/types"
xhttp "github.com/zeromicro/x/http"
)
func DeleteWhitelistHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.DeleteWhitelistReq
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := whitelistr.NewDeleteWhitelistLogic(r.Context(), svcCtx)
err := l.DeleteWhitelist(&req)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
}
}
}

View File

@@ -0,0 +1,30 @@
package whitelistr
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"tianyuan-api/apps/gateway/internal/logic/whitelistr"
"tianyuan-api/apps/gateway/internal/svc"
"tianyuan-api/apps/gateway/internal/types"
xhttp "github.com/zeromicro/x/http"
)
func GetWhitelistListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetWhitelistListReq
if err := httpx.Parse(r, &req); err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := whitelistr.NewGetWhitelistListLogic(r.Context(), svcCtx)
resp, err := l.GetWhitelistList(&req)
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
} else {
xhttp.JsonBaseResponseCtx(r.Context(), w, resp)
}
}
}