Pārlūkot izejas kodu

报告转图兼容地址替换

hsun 1 mēnesi atpakaļ
vecāks
revīzija
a177cc99d2
2 mainītis faili ar 47 papildinājumiem un 0 dzēšanām
  1. 38 0
      controllers/report_share.go
  2. 9 0
      models/business_conf.go

+ 38 - 0
controllers/report_share.go

@@ -1,8 +1,11 @@
 package controllers
 
 import (
+	"encoding/json"
 	"eta/eta_report/models"
+	"fmt"
 	"html"
+	"strings"
 )
 
 // ReportShareController 报告分享
@@ -14,6 +17,7 @@ type ReportShareController struct {
 // @Title 日评详情
 // @Description 日评详情接口
 // @Param   ReportCode   query   string  true       "报告唯一编码"
+// @Param   IsReplace   query   int  false       "是否报告转长图的场景:1-是"
 // @Success 200 {object} models.ReportShareDetailResp
 // @router /share/detail [get]
 func (this *ReportShareController) Detail() {
@@ -24,6 +28,7 @@ func (this *ReportShareController) Detail() {
 	}()
 
 	reportCode := this.GetString("ReportCode")
+	isReplace, _ := this.GetInt("IsReplace", 0)
 	if reportCode == "" {
 		br.Msg = "参数错误"
 		br.ErrMsg = "参数错误,reportCode 为空"
@@ -69,6 +74,15 @@ func (this *ReportShareController) Detail() {
 		br.ErrMsg = "获取免责声明失败, Err: " + e.Error()
 		return
 	}
+	// 是否需要替换资源地址
+	var urlReplace models.Report2ImgReplace
+	if isReplace == 1 && conf[models.BusinessConfReport2ImgReplace] != "" {
+		if e := json.Unmarshal([]byte(conf[models.BusinessConfReport2ImgReplace]), &urlReplace); e != nil {
+			br.Msg = "获取失败"
+			br.ErrMsg = fmt.Sprintf("获取报告替换配置失败, %v", e)
+			return
+		}
+	}
 	if conf[models.BusinessConfDisclaimer] != "" {
 		resp.Disclaimer = conf[models.BusinessConfDisclaimer]
 	}
@@ -110,12 +124,36 @@ func (this *ReportShareController) Detail() {
 			for _, chapter := range chapters {
 				chapter.Content = html.UnescapeString(chapter.Content)
 				chapter.ContentSub = html.UnescapeString(chapter.ContentSub)
+				if urlReplace.IsReplace {
+					if urlReplace.OssUrlOrigin != "" {
+						chapter.Content = strings.ReplaceAll(chapter.Content, urlReplace.OssUrlOrigin, urlReplace.OssUrlNew)
+						chapter.ContentSub = strings.ReplaceAll(chapter.ContentSub, urlReplace.OssUrlOrigin, urlReplace.OssUrlNew)
+					}
+					if urlReplace.ChartUrlOrigin != "" {
+						chapter.Content = strings.ReplaceAll(chapter.Content, urlReplace.ChartUrlOrigin, urlReplace.ChartUrlNew)
+						chapter.ContentSub = strings.ReplaceAll(chapter.ContentSub, urlReplace.ChartUrlOrigin, urlReplace.ChartUrlNew)
+					}
+				}
 				reportChapters = append(reportChapters, chapter)
 			}
 		}
 		//report.Abstract = report.Title
 	}
 
+	// 替换地址
+	if urlReplace.IsReplace {
+		if urlReplace.OssUrlOrigin != "" {
+			resp.H5ReportShareImg = strings.ReplaceAll(resp.H5ReportShareImg, urlReplace.OssUrlOrigin, urlReplace.OssUrlNew)
+			resp.ReportLogo = strings.ReplaceAll(resp.ReportLogo, urlReplace.OssUrlOrigin, urlReplace.OssUrlNew)
+			report.Content = strings.ReplaceAll(report.Content, urlReplace.OssUrlOrigin, urlReplace.OssUrlNew)
+			report.ContentSub = strings.ReplaceAll(report.ContentSub, urlReplace.OssUrlOrigin, urlReplace.OssUrlNew)
+		}
+		if urlReplace.ChartUrlOrigin != "" {
+			report.Content = strings.ReplaceAll(report.Content, urlReplace.ChartUrlOrigin, urlReplace.ChartUrlNew)
+			report.ContentSub = strings.ReplaceAll(report.ContentSub, urlReplace.ChartUrlOrigin, urlReplace.ChartUrlNew)
+		}
+	}
+
 	// 更新pv
 	if e = models.UpdateReportPv(report.Id); e != nil {
 		br.Msg = "获取失败"

+ 9 - 0
models/business_conf.go

@@ -21,6 +21,7 @@ const (
 	BusinessConfReportCenterLogoShow = "ReportCenterLogoShow" // 报告logo
 	BusinessConfReportEnLogoShow     = "ReportEnLogoShow"     // 报告logo
 	BusinessConfDisclaimerEn         = "DisclaimerEn"         // 英文免责声明
+	BusinessConfReport2ImgReplace    = "Report2ImgReplace"    // 报告转图替换地址
 )
 
 // BusinessConf 商户配置表
@@ -55,3 +56,11 @@ func GetBusinessConf() (list map[string]string, err error) {
 	}
 	return
 }
+
+type Report2ImgReplace struct {
+	IsReplace      bool   `description:"是否替换"`
+	OssUrlOrigin   string `description:"被替换的资源地址"`
+	OssUrlNew      string `description:"新的资源地址"`
+	ChartUrlOrigin string `description:"被替换的图表地址"`
+	ChartUrlNew    string `description:"新的图表地址"`
+}