From 3e3c9097de3b82afa472d85b6bea7ab5111cacc5 Mon Sep 17 00:00:00 2001 From: songchuang <192749120@qq.com> Date: Sat, 29 Mar 2025 02:49:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=90=88=E5=B9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/service/bundle/bundleOrder.go | 8 +- pkg/service/bundle/logic/signContractV2.go | 117 +++++++++++++++++++++ 2 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 pkg/service/bundle/logic/signContractV2.go diff --git a/pkg/service/bundle/bundleOrder.go b/pkg/service/bundle/bundleOrder.go index 35009a3..2bfd43a 100644 --- a/pkg/service/bundle/bundleOrder.go +++ b/pkg/service/bundle/bundleOrder.go @@ -30,10 +30,12 @@ func CreateBundleOrderSignature(c *gin.Context) { service.Error(c, errors.New(common.MissBundleUUID)) return } - if req.Signature == "" { + + // 不去校验 签名 + /*if req.Signature == "" { service.Error(c, errors.New(common.MissOrderSignature)) return - } + }*/ // 获取 用户信息 userInfo := login.GetUserInfoFromC(c) @@ -126,7 +128,7 @@ func CreateBundleOrderSignature(c *gin.Context) { req.ContractNo = common.GenerateContractNo(lastContractNo) // 当前 未将 签名 写入合同中 - signContract, signContractErr := logic.SignContract(req.CustomerNum, bundleDetail.Bundle.Contract, req.Signature, bundleDetail.Bundle.CompanySign, req.ContractNo, userInfo.Name, userInfo.SubscriberNumber, userInfo.TelNum, userInfo.PlaceOfResidence, req.TotalAmount, bundleDetail.Bundle.ContractDuration) + signContract, signContractErr := logic.SignContractV2(req.CustomerNum, bundleDetail.Bundle.Contract, req.TotalAmount, bundleDetail.Bundle.ContractDuration) if signContractErr != nil { service.Error(c, signContractErr) return diff --git a/pkg/service/bundle/logic/signContractV2.go b/pkg/service/bundle/logic/signContractV2.go new file mode 100644 index 0000000..7af5b84 --- /dev/null +++ b/pkg/service/bundle/logic/signContractV2.go @@ -0,0 +1,117 @@ +package logic + +import ( + "errors" + "fmt" + "fonchain-fiee/pkg/model" + "fonchain-fiee/pkg/service/bundle/common" + "fonchain-fiee/pkg/service/upload" + "github.com/signintech/gopdf" + "go.uber.org/zap" + "log" + "os" + "strconv" + "time" +) + +func SignContractV2(customerNum, contract string, price float32, contractDuration int64) (outputUrl string, err error) { + filePath := model.MediaPath + customerNum + time.Now().Format("20060102150405") + ".pdf" + downloadFileErr := DownloadFile(filePath, contract) + if downloadFileErr != nil { + zap.L().Error("download file error: ", zap.Error(downloadFileErr)) + //service.Error(c, errors.New(common.ErrorDownloadFile)) + return outputUrl, errors.New(common.ErrorDownloadFile) + } + + signFile := model.MediaPath + customerNum + "signed" + time.Now().Format("20060102150405") + ".pdf" + + /*signErr := InsertSignature(filePath, signFile, signImgPath, contractNo, idNo, telNum, address, price) + if signErr != nil { + zap.L().Error("insert signature error: ", zap.Error(signErr)) + return outputUrl, errors.New(common.ErrorInsertSignature) + }*/ + + signErr := InsertSignatureV2(filePath, signFile, price, contractDuration) + if signErr != nil { + zap.L().Error("insert signature error: ", zap.Error(signErr)) + return outputUrl, errors.New(common.ErrorInsertSignature) + } + + os.Remove(filePath) + + outputUrl, ossErr := upload.PutBos(signFile, upload.PdfType, true) + if ossErr != nil { + return "", errors.New(common.ErrorUploadFile) + } + return outputUrl, nil +} + +func InsertSignatureV2(templatePath, outputPath string, price float32, contractDuration int64) error { + pdf := gopdf.GoPdf{} + pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4}) + + // 导入模板文件中的页面 + err := pdf.ImportPagesFromSource(templatePath, "/MediaBox") + if err != nil { + log.Fatalf("无法导入页面: %v", err) + } + + // 获取模板文件的总页数 + totalPages := pdf.GetNumberOfPages() + fmt.Printf("模板文件的总页数: %d\n", totalPages) + + pricePage := 3 + + limitTimePage := 4 + + tffErr := pdf.AddTTFFont("simfang", "./data/simfang.ttf") + if tffErr != nil { + fmt.Printf("加载中文字体失败: %v\n", tffErr) + log.Fatalf("加载中文字体失败: %v", tffErr) + } + + // 设置字体和字号 + err = pdf.SetFont("simfang", "", 14) + if err != nil { + fmt.Printf("设置字体失败: %v\n", err) + log.Fatalf("设置字体失败: %v", err) + } + + // 填 金额 + pdf.SetPage(pricePage) + pdf.SetX(383) + pdf.SetY(351) + pdf.Cell(nil, strconv.FormatFloat(float64(price), 'f', 2, 64)) + + pdf.SetX(345) + pdf.SetY(383) + pdf.Cell(nil, strconv.FormatFloat(float64(price), 'f', 2, 64)) + // 写 有效期 + pdf.SetPage(limitTimePage) + + // 英文格式的时间 + t := time.Now().AddDate(int(contractDuration), 0, 0) + pdf.SetX(160) + pdf.SetY(387) + pdf.Cell(nil, t.Format("2006-01-02")) + + pdf.SetX(330) + pdf.SetY(403) + pdf.Cell(nil, t.Format("2006")) + + pdf.SetX(396) + pdf.SetY(403) + pdf.Cell(nil, t.Format("01")) + + pdf.SetX(443) + pdf.SetY(403) + pdf.Cell(nil, t.Format("02")) + + // 生成新的 PDF + if err = pdf.WritePdf(outputPath); err != nil { + //zap.L().Error("WritePdf err", zap.Error(err)) + return errors.New("error writing final PDF") + } + + return nil +}