first commit
This commit is contained in:
40
apps/admin/internal/handler/auth/loginhandler.go
Normal file
40
apps/admin/internal/handler/auth/loginhandler.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tianyuan-api/apps/admin/internal/logic/auth"
|
||||
"tianyuan-api/apps/admin/internal/svc"
|
||||
"tianyuan-api/apps/admin/internal/types"
|
||||
|
||||
xhttp "github.com/zeromicro/x/http"
|
||||
)
|
||||
|
||||
func LoginHandler(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.NewLoginLogic(r.Context(), svcCtx)
|
||||
token, err := l.Login(&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)
|
||||
}
|
||||
}
|
||||
}
|
||||
30
apps/admin/internal/handler/product/createproducthandler.go
Normal file
30
apps/admin/internal/handler/product/createproducthandler.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package product
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tianyuan-api/apps/admin/internal/logic/product"
|
||||
"tianyuan-api/apps/admin/internal/svc"
|
||||
"tianyuan-api/apps/admin/internal/types"
|
||||
|
||||
xhttp "github.com/zeromicro/x/http"
|
||||
)
|
||||
|
||||
func CreateProductHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.CreateProductReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := product.NewCreateProductLogic(r.Context(), svcCtx)
|
||||
err := l.CreateProduct(&req)
|
||||
if err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
} else {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
30
apps/admin/internal/handler/product/deleteproducthandler.go
Normal file
30
apps/admin/internal/handler/product/deleteproducthandler.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package product
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tianyuan-api/apps/admin/internal/logic/product"
|
||||
"tianyuan-api/apps/admin/internal/svc"
|
||||
"tianyuan-api/apps/admin/internal/types"
|
||||
|
||||
xhttp "github.com/zeromicro/x/http"
|
||||
)
|
||||
|
||||
func DeleteProductHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.DeleteProductReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := product.NewDeleteProductLogic(r.Context(), svcCtx)
|
||||
err := l.DeleteProduct(&req)
|
||||
if err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
} else {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
30
apps/admin/internal/handler/product/getproductbyidhandler.go
Normal file
30
apps/admin/internal/handler/product/getproductbyidhandler.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package product
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tianyuan-api/apps/admin/internal/logic/product"
|
||||
"tianyuan-api/apps/admin/internal/svc"
|
||||
"tianyuan-api/apps/admin/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)
|
||||
}
|
||||
}
|
||||
}
|
||||
30
apps/admin/internal/handler/product/getproductlisthandler.go
Normal file
30
apps/admin/internal/handler/product/getproductlisthandler.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package product
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tianyuan-api/apps/admin/internal/logic/product"
|
||||
"tianyuan-api/apps/admin/internal/svc"
|
||||
"tianyuan-api/apps/admin/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)
|
||||
}
|
||||
}
|
||||
}
|
||||
30
apps/admin/internal/handler/product/updateproducthandler.go
Normal file
30
apps/admin/internal/handler/product/updateproducthandler.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package product
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tianyuan-api/apps/admin/internal/logic/product"
|
||||
"tianyuan-api/apps/admin/internal/svc"
|
||||
"tianyuan-api/apps/admin/internal/types"
|
||||
|
||||
xhttp "github.com/zeromicro/x/http"
|
||||
)
|
||||
|
||||
func UpdateProductHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.UpdateProductReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := product.NewUpdateProductLogic(r.Context(), svcCtx)
|
||||
err := l.UpdateProduct(&req)
|
||||
if err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
} else {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package review
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tianyuan-api/apps/admin/internal/logic/review"
|
||||
"tianyuan-api/apps/admin/internal/svc"
|
||||
"tianyuan-api/apps/admin/internal/types"
|
||||
|
||||
xhttp "github.com/zeromicro/x/http"
|
||||
)
|
||||
|
||||
func GetPendingEnterpriseHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.GetPendingEnterpriseReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := review.NewGetPendingEnterpriseLogic(r.Context(), svcCtx)
|
||||
resp, err := l.GetPendingEnterprise(&req)
|
||||
if err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
} else {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package review
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"tianyuan-api/apps/admin/internal/logic/review"
|
||||
"tianyuan-api/apps/admin/internal/svc"
|
||||
"tianyuan-api/apps/admin/internal/types"
|
||||
|
||||
xhttp "github.com/zeromicro/x/http"
|
||||
)
|
||||
|
||||
func ReviewEnterpriseHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.ReviewEnterpriseReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := review.NewReviewEnterpriseLogic(r.Context(), svcCtx)
|
||||
resp, err := l.ReviewEnterprise(&req)
|
||||
if err != nil {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
|
||||
} else {
|
||||
xhttp.JsonBaseResponseCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
96
apps/admin/internal/handler/routes.go
Normal file
96
apps/admin/internal/handler/routes.go
Normal file
@@ -0,0 +1,96 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.7.2
|
||||
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
auth "tianyuan-api/apps/admin/internal/handler/auth"
|
||||
product "tianyuan-api/apps/admin/internal/handler/product"
|
||||
review "tianyuan-api/apps/admin/internal/handler/review"
|
||||
user "tianyuan-api/apps/admin/internal/handler/user"
|
||||
"tianyuan-api/apps/admin/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/login",
|
||||
Handler: auth.LoginHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
rest.WithPrefix("/api/admin/auth"),
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
rest.WithMiddlewares(
|
||||
[]rest.Middleware{serverCtx.AuthInterceptor},
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/:productId",
|
||||
Handler: product.GetProductByIdHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/create",
|
||||
Handler: product.CreateProductHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/delete",
|
||||
Handler: product.DeleteProductHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/list",
|
||||
Handler: product.GetProductListHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/update",
|
||||
Handler: product.UpdateProductHandler(serverCtx),
|
||||
},
|
||||
}...,
|
||||
),
|
||||
rest.WithPrefix("/api/admin/product"),
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
rest.WithMiddlewares(
|
||||
[]rest.Middleware{serverCtx.AuthInterceptor},
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/pending",
|
||||
Handler: review.GetPendingEnterpriseHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/review",
|
||||
Handler: review.ReviewEnterpriseHandler(serverCtx),
|
||||
},
|
||||
}...,
|
||||
),
|
||||
rest.WithPrefix("/api/admin/enterprise"),
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
rest.WithMiddlewares(
|
||||
[]rest.Middleware{serverCtx.AuthInterceptor},
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/info",
|
||||
Handler: user.GetUserInfoHandler(serverCtx),
|
||||
},
|
||||
}...,
|
||||
),
|
||||
rest.WithPrefix("/api/admin/user"),
|
||||
)
|
||||
}
|
||||
21
apps/admin/internal/handler/user/getuserinfohandler.go
Normal file
21
apps/admin/internal/handler/user/getuserinfohandler.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
xhttp "github.com/zeromicro/x/http"
|
||||
"tianyuan-api/apps/admin/internal/logic/user"
|
||||
"tianyuan-api/apps/admin/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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user