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,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)
}
}
}

View 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)
}
}
}

View 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)
}
}
}

View 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)
}
}
}

View 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)
}
}
}

View 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)
}
}
}

View File

@@ -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)
}
}
}

View File

@@ -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)
}
}
}

View 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"),
)
}

View 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)
}
}
}