浏览代码

PDF打水印方式修改

xingzai 3 年之前
父节点
当前提交
0f8c39ca2a
共有 3 个文件被更改,包括 67 次插入15 次删除
  1. 51 14
      controllers/article.go
  2. 15 0
      models/company.go
  3. 1 1
      utils/config.go

+ 51 - 14
controllers/article.go

@@ -1,6 +1,15 @@
 package controllers
 
 import (
+	//"bufio"
+	//"io"
+	//"path"
+	//"rdluck_tools/http"
+
+	"bufio"
+	"io"
+
+	//"bufio"
 	"encoding/json"
 	"fmt"
 	"github.com/medivhzhan/weapp/v2"
@@ -8,8 +17,17 @@ import (
 	"hongze/hongze_cygx/services"
 	"hongze/hongze_cygx/utils"
 	"html"
-	"os"
 	"rdluck_tools/http"
+
+	//"rdluck_tools/http"
+
+	//"io"
+	nhttp "net/http"
+	"os"
+	//"rdluck_tools/http"
+
+	//"path"
+	//"rdluck_tools/http"
 	"regexp"
 	"strconv"
 	"strings"
@@ -710,14 +728,13 @@ func (this *ArticleController) AskAdd() {
 		br.ErrMsg = "提交失败,Err:" + err.Error()
 		return
 	}
-	companyItem, err := models.GetCompanyDetailById(user.CompanyId)
+	companyItem, err := models.GetSellerDetailAllByCompanyId(user.CompanyId)
 	if err != nil {
 		br.Msg = "获取信息失败"
 		br.ErrMsg = "获取所属销售信息失败,Err:" + err.Error()
 		return
 	}
 	var mobile string
-	companyItem.Mobile = "123"
 	if utils.WxMsgTemplateIdAskMsgMobile == "" {
 		mobile = utils.WxMsgTemplateIdAskMsgMobileAll + "," + companyItem.Mobile
 	} else {
@@ -788,19 +805,40 @@ func (this *ArticleController) Pdfwatermark() {
 	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://127.0.0.1:5007/pdfwatermark/" //
 	fmt.Println("postUrl:", postUrl)
 	postData, err := json.Marshal(dataMap)
 	if err != nil {
-		br.Msg = "获取postData数据失败"
+		br.Msg = "下载失败"
 		br.ErrMsg = "解析,Marshal,Err:" + err.Error()
 		return
 	}
 	fmt.Println("publish ppt param:", string(postData))
 	result, err := http.Post(postUrl, string(postData))
 	if err != nil {
-		br.Msg = "发布失败"
+		br.Msg = "下载失败"
 		br.ErrMsg = "Marshal,Err:" + err.Error()
 		return
 	}
@@ -808,26 +846,25 @@ func (this *ArticleController) Pdfwatermark() {
 	var pdfResult PublishPdfResult
 	err = json.Unmarshal(result, &pdfResult)
 	if err != nil {
-		br.Msg = "获取数据失败"
+		br.Msg = "下载失败"
 		br.ErrMsg = "Unmarshal,Err:" + err.Error()
 		return
 	}
+	//fmt.Println(pdfResult)
 	resp := new(models.ArticleDetailFileLink)
 	filePathName := strings.TrimLeft(pdfResult.FilePath, ".")
-	filePathOldName := strings.TrimLeft(pdfResult.FileOldPath, ".")
+	//filePathOldName := strings.TrimLeft(pdfResult.FileOldPath, ".")
 	filePath := pdfResult.Pdfpath + filePathName
-	filePathOld := pdfResult.Pdfpath + filePathOldName
-	fmt.Println(filePath)
-	fmt.Println(filePathOld)
+	//filePathOld := pdfResult.Pdfpath + filePathOldName
 	randStr := utils.GetRandStringNoSpecialChar(28)
-	fileName := randStr + ".pdf"
+	fileName = randStr + ".pdf"
 	hzUploadDir := "static/pdf/"
 	savePath := hzUploadDir + time.Now().Format("200601/20060102/")
 	savePath += fileName
 	//上传到阿里云
-	err = services.UploadFileToAliyun(fileName, filePath, savePath)
+	err = services.UploadFileToAliyun(fileName, fileLink, savePath)
 	if err != nil {
-		br.Msg = "文件上传失败"
+		br.Msg = "下载失败"
 		br.ErrMsg = "文件上传失败,Err:" + err.Error()
 		return
 	}
@@ -837,7 +874,7 @@ func (this *ArticleController) Pdfwatermark() {
 	resourceUrl := fileHost + savePath
 	defer func() {
 		os.Remove(filePath)
-		os.Remove(filePathOld)
+		os.Remove(fileLink)
 	}()
 	resp.FileLink = resourceUrl
 	br.Ret = 200

+ 15 - 0
models/company.go

@@ -116,3 +116,18 @@ func GetCompanyDetailAllById(companyId int) (item *CompanyDetail, err error) {
 	err = o.Raw(sql, companyId, companyId).QueryRow(&item)
 	return
 }
+
+//获取对应销售以及销售的手机号
+func GetSellerDetailAllByCompanyId(companyId int) (item *CompanyDetail, err error) {
+	sql := ` SELECT c.mobile ,c.real_name as seller_name
+			FROM
+			company AS a
+			INNER JOIN company_product AS b ON a.company_id = b.company_id
+			INNER JOIN admin AS c ON b.seller_id = c.admin_id 
+			WHERE
+			b.product_id = 2 
+			AND a.company_id = ? LIMIT 1`
+	o := orm.NewOrm()
+	err = o.Raw(sql, companyId).QueryRow(&item)
+	return
+}

+ 1 - 1
utils/config.go

@@ -75,7 +75,7 @@ func init() {
 	}
 	OnlineTime = "2021-06-01 00:00:01" //上线时间
 	SummaryArticleId = 1000000         //手动添加的纪要库开始ID
-	WxMsgTemplateIdAskMsgMobileAll = "15557270714"
+	WxMsgTemplateIdAskMsgMobileAll = "15557270714,18767183922"
 	if RunMode == "release" {
 		WxAppId = "wxcc32b61f96720d2f"
 		WxAppSecret = "06894933fafb24dafead7eaae09c08e0"