From 8593eb2dd3336d8aff63d66bf20b20263c259347 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 10 Oct 2023 14:59:00 +0800 Subject: [PATCH 1/3] fix --- server/collection/internal/handler/routes.go | 5 +++++ server/collection/internal/types/types.go | 5 +++++ server_api/collection.api | 12 ++++++++++-- utils/pdf/html_to_pdf.go | 10 ++++++---- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/server/collection/internal/handler/routes.go b/server/collection/internal/handler/routes.go index ac4eade4..ca29fb08 100644 --- a/server/collection/internal/handler/routes.go +++ b/server/collection/internal/handler/routes.go @@ -32,6 +32,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/collection/test_ai", Handler: TestAiHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/collection/test_pdf", + Handler: TestPdfHandler(serverCtx), + }, }, ) } diff --git a/server/collection/internal/types/types.go b/server/collection/internal/types/types.go index f3707230..c6268e1d 100644 --- a/server/collection/internal/types/types.go +++ b/server/collection/internal/types/types.go @@ -44,6 +44,11 @@ type TestAiReq struct { Num int `form:"num"` } +type TestPdfReq struct { + Content string `json:"content"` + Type string `json:"type"` +} + type Request struct { } diff --git a/server_api/collection.api b/server_api/collection.api index 866a8d1a..31253735 100644 --- a/server_api/collection.api +++ b/server_api/collection.api @@ -18,9 +18,12 @@ service collection { //获取收藏列表 @handler GetCollectProductListHandler get /api/collection/get_collect_product_list(GetCollectProductListReq) returns (response); - //测试 + //测试算法合图并发 @handler TestAiHandler get /api/collection/test_ai(TestAiReq) returns (response); + //测试pdf + @handler TestPdfHandler + post /api/collection/test_pdf(TestPdfReq) returns (response); } //收藏产品 @@ -56,7 +59,12 @@ type GetCollectProductListRspItem { IsShelf int64 `json:"is_shelf"` IsDeleted int64 `json:"is_deleted"` } -//测试 +//测试算法 type TestAiReq { Num int `form:"num"` +} +//测试pdf +type TestPdfReq { + Content string `form:"content"` + Type string `form:"type"` } \ No newline at end of file diff --git a/utils/pdf/html_to_pdf.go b/utils/pdf/html_to_pdf.go index c81f9e10..2af6de15 100644 --- a/utils/pdf/html_to_pdf.go +++ b/utils/pdf/html_to_pdf.go @@ -2,25 +2,27 @@ package pdf import ( "encoding/base64" + "errors" "github.com/SebastiaanKlippert/go-wkhtmltopdf" "strings" ) /* html转 Pdf -dataType = 1 为网页url dataType = 2为网页内容 outFile为空则不保存(使用该方法需要安装工具 sudo apt-get install wkhtmltopdf) */ -func HtmlToPdfBase64(content string, dataType int, outFile ...string) (string, error) { +func HtmlToPdfBase64(content string, dataType string, outFile ...string) (string, error) { pdfg, err := wkhtmltopdf.NewPDFGenerator() if err != nil { return "", err } switch dataType { - case 1: //网页地址 + case "url": //网页地址 pdfg.AddPage(wkhtmltopdf.NewPage(content)) - case 2: //网页内容 + case "html": //网页内容 pdfg.AddPage(wkhtmltopdf.NewPageReader(strings.NewReader(content))) + default: + return "", errors.New("invalid type") } //模式 pdfg.Orientation.Set(wkhtmltopdf.OrientationPortrait) From 8f92776103083d64e547dc9ddebacdb5e38d7e9e Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 10 Oct 2023 15:08:41 +0800 Subject: [PATCH 2/3] fix --- server/collection/internal/handler/routes.go | 2 +- .../internal/handler/testpdfhandler.go | 35 +++++++++++++ .../collection/internal/logic/testpdflogic.go | 49 +++++++++++++++++++ server/collection/internal/types/types.go | 4 +- server_api/collection.api | 2 +- 5 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 server/collection/internal/handler/testpdfhandler.go create mode 100644 server/collection/internal/logic/testpdflogic.go diff --git a/server/collection/internal/handler/routes.go b/server/collection/internal/handler/routes.go index ca29fb08..4abd6190 100644 --- a/server/collection/internal/handler/routes.go +++ b/server/collection/internal/handler/routes.go @@ -33,7 +33,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Handler: TestAiHandler(serverCtx), }, { - Method: http.MethodPost, + Method: http.MethodGet, Path: "/api/collection/test_pdf", Handler: TestPdfHandler(serverCtx), }, diff --git a/server/collection/internal/handler/testpdfhandler.go b/server/collection/internal/handler/testpdfhandler.go new file mode 100644 index 00000000..6a40187c --- /dev/null +++ b/server/collection/internal/handler/testpdfhandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/collection/internal/logic" + "fusenapi/server/collection/internal/svc" + "fusenapi/server/collection/internal/types" +) + +func TestPdfHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.TestPdfReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewTestPdfLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.TestPdf(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/collection/internal/logic/testpdflogic.go b/server/collection/internal/logic/testpdflogic.go new file mode 100644 index 00000000..a80d2252 --- /dev/null +++ b/server/collection/internal/logic/testpdflogic.go @@ -0,0 +1,49 @@ +package logic + +import ( + "context" + "fusenapi/server/collection/internal/svc" + "fusenapi/server/collection/internal/types" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "fusenapi/utils/pdf" + + "github.com/zeromicro/go-zero/core/logx" +) + +type TestPdfLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewTestPdfLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TestPdfLogic { + return &TestPdfLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *TestPdfLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *TestPdfLogic) TestPdf(req *types.TestPdfReq, userinfo *auth.UserInfo) (resp *basic.Response) { + switch req.Type { + case "url": + case "html": + default: + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid type") + } + res, err := pdf.HtmlToPdfBase64(req.Content, req.Type) + if err != nil { + return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()) + } + return resp.SetStatus(basic.CodeOK, res) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *TestPdfLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/collection/internal/types/types.go b/server/collection/internal/types/types.go index c6268e1d..0b3cbda3 100644 --- a/server/collection/internal/types/types.go +++ b/server/collection/internal/types/types.go @@ -45,8 +45,8 @@ type TestAiReq struct { } type TestPdfReq struct { - Content string `json:"content"` - Type string `json:"type"` + Content string `form:"content"` + Type string `form:"type"` } type Request struct { diff --git a/server_api/collection.api b/server_api/collection.api index 31253735..22c8501c 100644 --- a/server_api/collection.api +++ b/server_api/collection.api @@ -23,7 +23,7 @@ service collection { get /api/collection/test_ai(TestAiReq) returns (response); //测试pdf @handler TestPdfHandler - post /api/collection/test_pdf(TestPdfReq) returns (response); + get /api/collection/test_pdf(TestPdfReq) returns (response); } //收藏产品 From ce31946e53e51b6160143d43cea0800365535478 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 10 Oct 2023 15:09:20 +0800 Subject: [PATCH 3/3] fix --- server/collection/internal/handler/routes.go | 2 +- server/collection/internal/types/types.go | 4 ++-- server_api/collection.api | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/server/collection/internal/handler/routes.go b/server/collection/internal/handler/routes.go index 4abd6190..ca29fb08 100644 --- a/server/collection/internal/handler/routes.go +++ b/server/collection/internal/handler/routes.go @@ -33,7 +33,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Handler: TestAiHandler(serverCtx), }, { - Method: http.MethodGet, + Method: http.MethodPost, Path: "/api/collection/test_pdf", Handler: TestPdfHandler(serverCtx), }, diff --git a/server/collection/internal/types/types.go b/server/collection/internal/types/types.go index 0b3cbda3..c6268e1d 100644 --- a/server/collection/internal/types/types.go +++ b/server/collection/internal/types/types.go @@ -45,8 +45,8 @@ type TestAiReq struct { } type TestPdfReq struct { - Content string `form:"content"` - Type string `form:"type"` + Content string `json:"content"` + Type string `json:"type"` } type Request struct { diff --git a/server_api/collection.api b/server_api/collection.api index 22c8501c..ea9fbabb 100644 --- a/server_api/collection.api +++ b/server_api/collection.api @@ -23,7 +23,7 @@ service collection { get /api/collection/test_ai(TestAiReq) returns (response); //测试pdf @handler TestPdfHandler - get /api/collection/test_pdf(TestPdfReq) returns (response); + post /api/collection/test_pdf(TestPdfReq) returns (response); } //收藏产品 @@ -65,6 +65,6 @@ type TestAiReq { } //测试pdf type TestPdfReq { - Content string `form:"content"` - Type string `form:"type"` + Content string `json:"content"` + Type string `json:"type"` } \ No newline at end of file