From 1572f45e72cc0fa15c029f9ee2a08474104435e6 Mon Sep 17 00:00:00 2001 From: selfcheer <selfcheer@gmail.com> Date: 星期五, 19 七月 2024 00:48:43 +0800 Subject: [PATCH] 采购单产品列表去掉过滤重复产品的逻辑 --- api/v1/test/contract.go | 24 ++++++++++++++++++------ 1 files changed, 18 insertions(+), 6 deletions(-) diff --git a/api/v1/test/contract.go b/api/v1/test/contract.go index a3411c5..ed952bb 100644 --- a/api/v1/test/contract.go +++ b/api/v1/test/contract.go @@ -4,6 +4,7 @@ "bytes" "fmt" "github.com/gin-gonic/gin" + "github.com/h2non/filetype" "go.uber.org/zap" "net/http" "srm/global" @@ -32,7 +33,7 @@ // @Success 200 {string} string "{"success":true,"data":{},"msg":"鑾峰彇鎴愬姛"}" // @Router /con/createContract [post] func (conApi *ContractApi) CreateContract(c *gin.Context) { - name := c.Param("name") + name := c.PostForm("name") file, err := c.FormFile("file") if err != nil { c.JSON(400, gin.H{"error": err.Error()}) @@ -44,9 +45,20 @@ defer f.Close() f.Read(fileContent) + // 閲嶇疆鏂囦欢鎸囬拡 + f.Seek(0, 0) + + // 浣跨敤 filetype 搴撴娴嬫枃浠剁被鍨� + kind, _ := filetype.Match(fileContent) + if kind == filetype.Unknown { + c.JSON(http.StatusBadRequest, gin.H{"error": "Unknown file type"}) + return + } + contract := test.Contract{ FileName: name, FileContent: fileContent, + FileType: kind.MIME.Value, } if err, id := conService.CreateContract(&contract); err != nil { @@ -54,7 +66,7 @@ response.FailWithMessage("鍒涘缓澶辫触", c) } else { //response.OkWithMessage("鍒涘缓鎴愬姛", c) - response.OkWithData(gin.H{"id": id}, c) + response.OkWithData(gin.H{"id": id, "name": name}, c) } } @@ -191,7 +203,7 @@ // @Produce application/json // @Param data query test.Contract true "鐢╥d鏌ヨContract" // @Success 200 {string} string "{"success":true,"data":{},"msg":"棰勮鎴愬姛"}" -// @Router /con/previewContract [get] +// @Router /previewContract [get] func (conApi *ContractApi) PreviewContract(c *gin.Context) { var con test.Contract err := c.ShouldBindQuery(&con) @@ -228,7 +240,7 @@ //reader := bytes.NewReader(contract.FileContent) //c.DataFromReader(http.StatusOK, int64(len(contract.FileContent)), "application/pdf", reader, map[string]string{"Content-Disposition": fmt.Sprintf("attachment; filename=%s", contract.FileName)}) reader := bytes.NewReader(contract.FileContent) - c.Writer.Header().Set("Content-Type", "application/pdf") + c.Header("Content-Type", contract.FileType) c.Writer.Header().Set("Content-Disposition", "inline; filename="+contract.FileName) http.ServeContent(c.Writer, c.Request, contract.FileName, time.Now(), reader) } @@ -242,7 +254,7 @@ // @Produce application/json // @Param data query test.Contract true "鐢╥d鏌ヨContract" // @Success 200 {string} string "{"success":true,"data":{},"msg":"涓嬭浇鎴愬姛"}" -// @Router /con/downloadContract [get] +// @Router /downloadContract [get] func (conApi *ContractApi) DownloadContract(c *gin.Context) { var con test.Contract err := c.ShouldBindQuery(&con) @@ -277,7 +289,7 @@ c.Header("Content-Description", "File Transfer") c.Header("Content-Transfer-Encoding", "binary") c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", contract.FileName)) - c.Header("Content-Type", "application/pdf") + c.Header("Content-Type", contract.FileType) // 灏嗘枃浠朵綔涓哄搷搴斾綋鍙戦�� http.ServeContent(c.Writer, c.Request, contract.FileName, time.Now(), reader) -- Gitblit v1.8.0