浏览代码

合并master

xingzai 3 年之前
父节点
当前提交
b260b67c0c
共有 2 个文件被更改,包括 61 次插入65 次删除
  1. 2 1
      .gitignore
  2. 59 64
      controllers/article.go

+ 2 - 1
.gitignore

@@ -9,4 +9,5 @@
 /lastupdate.tmp
 /*.zip
 /riot-index
-/hongze_cygx
+/hongze_cygx
+/static

+ 59 - 64
controllers/article.go

@@ -2,18 +2,26 @@ package controllers
 
 import (
 	"bufio"
+	"github.com/pdfcpu/pdfcpu/pkg/api"
+	"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
+	"io"
+
+	//"bufio"
 	"encoding/json"
 	"fmt"
 	"github.com/medivhzhan/weapp/v2"
+	"os"
+
+	//"github.com/pdfcpu/pdfcpu/pkg/api"
+	//"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
 	"hongze/hongze_cygx/models"
 	"hongze/hongze_cygx/services"
 	"hongze/hongze_cygx/utils"
 	"html"
-	"io"
+	//"io"
 	"io/ioutil"
 	nhttp "net/http"
-	"os"
-	"rdluck_tools/http"
+	//"os"
 	"regexp"
 	"strconv"
 	"strings"
@@ -792,7 +800,6 @@ func (this *ArticleController) Pdfwatermark() {
 		br.Ret = 408
 		return
 	}
-
 	articleId, err := this.GetInt("ArticleId")
 	if articleId <= 0 {
 		br.Msg = "文章不存在"
@@ -816,80 +823,75 @@ func (this *ArticleController) Pdfwatermark() {
 	if mobile == "" {
 		mobile = user.Email
 	}
-	dataMap := make(map[string]interface{})
-	dataMap["Mobile"] = mobile
-	//dataMap["Name"] = user.RealName
-	dataMap["Name"] = ""
-	pdfPath := "static/"
-	pdfUrl := fileLink
-	fileName := utils.GetRandStringNoSpecialChar(28) + ".pdf"
-	res, err := nhttp.Get(pdfUrl)
-	if err != nil {
-		fmt.Println("A error occurred!")
-		return
-	}
-	defer res.Body.Close()
-	// 获得get请求响应的reader对象
-	reader := bufio.NewReaderSize(res.Body, 32*1024)
-	file, err := os.Create(pdfPath + fileName)
-	if err != nil {
-		panic(err)
-	}
-	//获得文件的writer对象
-	writer := bufio.NewWriter(file)
-	written, _ := io.Copy(writer, reader)
-	fmt.Printf("Total length: %d", written)
-	str, _ := os.Getwd()
-	fileLink = str + "/" + pdfPath + fileName
-	dataMap["FileLink"] = fileLink
-	postUrl := "http://172.19.173.227:5007/pdfwatermark/" //
-	fmt.Println("postUrl:", postUrl)
-	postData, err := json.Marshal(dataMap)
-	if err != nil {
-		br.Msg = "下载失败"
-		br.ErrMsg = "解析,Marshal,Err:" + err.Error()
-		return
+	sliceLink := strings.Split(fileLink, "/")
+	uploadDir := "static/pdf/"
+	//判断文件夹是否存在,不存在则创建
+	if !utils.FileIsExist(uploadDir) {
+		err = os.MkdirAll(uploadDir, 0755)
+		if err != nil {
+			br.Msg = "下载失败"
+			br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
+			return
+		}
 	}
-	fmt.Println("publish ppt param:", string(postData))
-	result, err := http.Post(postUrl, string(postData))
+	var oldFile string
+	var newFile string
+	//获取PDF源文件名称
+	pdfName := sliceLink[len(sliceLink)-1]
+	pdfName = utils.MD5(pdfName) + ".pdf"
+	oldFile = uploadDir + pdfName
+	//判断PDF本地是否存在,不存在则保存到本地
+	if !utils.FileIsExist(oldFile) {
+		res, err := nhttp.Get(fileLink)
+		if err != nil {
+			br.Msg = "下载失败"
+			br.ErrMsg = "获取源文件失败,Err:" + err.Error()
+			return
+		}
+		defer res.Body.Close()
+		// 获得get请求响应的reader对象
+		reader := bufio.NewReaderSize(res.Body, 32*1024)
+		file, err := os.Create(oldFile)
+		if err != nil {
+			br.Msg = "下载失败"
+			br.ErrMsg = "保存源文件到本地失败,Err:" + err.Error()
+			return
+		}
+		//获得文件的writer对象
+		writer := bufio.NewWriter(file)
+		written, _ := io.Copy(writer, reader)
+		fmt.Printf("Total length: %d", written)
+	}
+	newFile = uploadDir + "new_" + pdfName
+	onTop := true
+	wm, err := pdfcpu.ParseTextWatermarkDetails(mobile, " op:.4, pos:c ,points:16 ", onTop, 1)
 	if err != nil {
 		br.Msg = "下载失败"
-		br.ErrMsg = "Marshal,Err:" + err.Error()
+		br.ErrMsg = "生成水印文件失败,Err:" + err.Error()
 		return
 	}
-	fmt.Println("publish pdf result:", string(result))
-	var pdfResult PublishPdfResult
-	err = json.Unmarshal(result, &pdfResult)
+	err = api.AddWatermarksFile(oldFile, newFile, nil, wm, nil)
 	if err != nil {
 		br.Msg = "下载失败"
-		br.ErrMsg = "Unmarshal,Err:" + err.Error()
+		br.ErrMsg = "生成水印PDF失败,Err:" + err.Error()
 		return
 	}
-	//fmt.Println(pdfResult)
 	resp := new(models.ArticleDetailFileLink)
-	filePathName := strings.TrimLeft(pdfResult.FilePath, ".")
-	//filePathOldName := strings.TrimLeft(pdfResult.FileOldPath, ".")
-	filePath := pdfResult.Pdfpath + filePathName
-	//filePathOld := pdfResult.Pdfpath + filePathOldName
 	randStr := utils.GetRandStringNoSpecialChar(28)
-	fileName = randStr + ".pdf"
-	hzUploadDir := "static/pdf/"
-	savePath := hzUploadDir + time.Now().Format("200601/20060102/")
+	fileName := randStr + ".pdf"
+	savePath := uploadDir + time.Now().Format("200601/20060102/")
 	savePath += fileName
 	//上传到阿里云
-	err = services.UploadFileToAliyun(fileName, filePath, savePath)
+	err = services.UploadFileToAliyun(fileName, newFile, savePath)
 	if err != nil {
 		br.Msg = "下载失败"
 		br.ErrMsg = "文件上传失败,Err:" + err.Error()
 		return
 	}
-	utils.FileLog.Info("%s:", time.Now().Format(utils.FormatDateTime))
-	utils.FileLog.Info("end update oss ")
 	fileHost := "https://hzstatic.hzinsights.com/"
 	resourceUrl := fileHost + savePath
 	defer func() {
-		os.Remove(filePath)
-		os.Remove(fileLink)
+		os.Remove(newFile)
 	}()
 	resp.FileLink = resourceUrl
 	br.Ret = 200
@@ -898,13 +900,6 @@ func (this *ArticleController) Pdfwatermark() {
 	br.Data = resp
 }
 
-type PublishPdfResult struct {
-	Result      string `json:"result"`
-	FilePath    string `json:"file_path"`
-	FileOldPath string `json:"file_old_path"`
-	Pdfpath     string `json:"pdfpath"`
-}
-
 // @Title 同步策略报告内容
 // @Description 同步策略报告内容接口
 // @Param   ArticleIdMd5   query   int  true       "报告ID"