浏览代码

fix:ppt转报告的表格也要继承原先的样式

Roc 3 月之前
父节点
当前提交
a3f8b4eeea
共有 3 个文件被更改,包括 113 次插入26 次删除
  1. 21 14
      services/ppt.go
  2. 2 2
      services/report.go
  3. 90 10
      services/report_v2.go

+ 21 - 14
services/ppt.go

@@ -11,6 +11,7 @@ import (
 	"eta/eta_api/services/ppt2img"
 	"eta/eta_api/utils"
 	"fmt"
+	"html"
 	"sort"
 	"time"
 )
@@ -39,6 +40,7 @@ type PPTContentElements struct {
 	SheetId     string `json:"sheetId"`
 	SheetHeight string `json:"sheetHeight"`
 	Src         string `json:"src"`
+	Uid         string `json:"uid"`
 }
 
 // SavePPTReport 保存PPT报告
@@ -161,17 +163,17 @@ func SavePPTReport(pptId, classifyId int, title string, adminInfo *system.Admin)
 		reportReq.IsPublicPublish = 1
 	}
 
-	newReportId, newCode, _, e := CreateNewReport(*reportReq, adminInfo)
+	newReportInfo, _, e := CreateNewReport(*reportReq, adminInfo)
 	if e != nil {
 		errMsg = "转换失败"
 		err = errors.New("新增报告失败, Err: " + e.Error())
 		return
 	}
-	reportId = int(newReportId)
-	reportCode = newCode
+	reportId = newReportInfo.Id
+	reportCode = newReportInfo.ReportCode
 
 	// ppt转报告后,将ppt的表格关系做处理
-	handlerPptToReportTableReferenced(pptId, int(newReportId))
+	handlerPptToReportTableReferenced(pptId, newReportInfo.Id)
 
 	// 更新报告中的ppt图片
 	go saveReportPptImg(pptId, reportId, item.PptxUrl)
@@ -240,12 +242,12 @@ func pptContent2Html(content string, isEnglish bool) (htm string, err error) {
 				case ElementsTypeChart:
 					if isEnglish {
 						// 英文研报图表src多加一个fromPage=en, 表格暂时没有区分
-						htmlContent += fmt.Sprintf(`<iframe src="%s/chartshow?code=%s&fromPage=en" width="100%%" height="350" style="border-width:0px; min-height:350px;"></iframe>`, chartRoot, v.ChartId)
+						htmlContent += fmt.Sprintf(`<iframe src="%s/chartshow?code=%s&fromPage=en&uid=%s" width="100%%" height="350" style="border-width:0px; min-height:350px;"></iframe>`, chartRoot, v.ChartId, v.Uid)
 						break
 					}
-					htmlContent += fmt.Sprintf(`<iframe src="%s/chartshow?code=%s" width="100%%" height="350" style="border-width:0px; min-height:350px;"></iframe>`, chartRoot, v.ChartId)
+					htmlContent += fmt.Sprintf(`<iframe src="%s/chartshow?code=%s&uid=%s" width="100%%" height="350" style="border-width:0px; min-height:350px;"></iframe>`, chartRoot, v.ChartId, v.Uid)
 				case ElementsTypeSheet:
-					htmlContent += fmt.Sprintf(`<iframe src="%s/sheetshow?code=%s" class="iframe%s" width="100%%" height="%s" style="border-width:0px;"></iframe>`, chartRoot, v.SheetId, v.SheetId, v.SheetHeight)
+					htmlContent += fmt.Sprintf(`<iframe src="%s/sheetshow?code=%s&uid=%s" class="iframe%s" width="100%%" height="%s" style="border-width:0px;"></iframe>`, chartRoot, v.SheetId, v.Uid, v.SheetId, v.SheetHeight)
 				}
 				htmlContent += htmlSuffix
 			}
@@ -511,15 +513,17 @@ func handlerPptToReportTableReferenced(pptId, reportId int) {
 		return
 	}
 
-	addList, err := excel.CopyReferencedExcelConfigByReferencedIdAndFromScene(pptId, utils.TableReferencedByPPT, reportInfo.Id, utils.TableReferencedByReport, reportInfo.AdminId, reportInfo.AdminRealName)
+	newFromScene := utils.TableReferencedByReport
+	addList, err := excel.CopyReferencedExcelConfigByReferencedIdAndFromScene(pptId, utils.TableReferencedByPPT, reportInfo.Id, newFromScene, reportInfo.AdminId, reportInfo.AdminRealName)
 	if err != nil {
 		return
 	}
 
 	if len(addList) > 0 {
 		// 修改内容
-		reportInfo.Content = HandleReportContentTable(reportInfo.Id, reportInfo.Content)
-		reportInfo.ContentStruct = HandleReportContentStructTable(reportInfo.Id, reportInfo.ContentStruct)
+		content := HandleReportContentTableAndScene(reportInfo.Id, newFromScene, html.UnescapeString(reportInfo.Content))
+		reportInfo.Content = html.EscapeString(content)
+		reportInfo.ContentStruct = HandleReportContentStructTableAndScene(reportInfo.Id, newFromScene, reportInfo.ContentStruct)
 		err = reportInfo.Update([]string{"Content", "ContentStruct"})
 		if err != nil {
 			return
@@ -532,8 +536,9 @@ func handlerPptToReportTableReferenced(pptId, reportId int) {
 				return
 			}
 			for _, v := range chapterList {
-				v.Content = HandleReportContentTable(reportInfo.Id, v.Content)
-				v.ContentStruct = HandleReportContentStructTable(reportInfo.Id, v.ContentStruct)
+				chapterContent := HandleReportContentTableAndScene(reportInfo.Id, newFromScene, html.UnescapeString(v.Content))
+				v.Content = html.EscapeString(chapterContent)
+				v.ContentStruct = HandleReportContentStructTableAndScene(reportInfo.Id, newFromScene, v.ContentStruct)
 				err = v.Update([]string{"Content", "ContentStruct"})
 				if err != nil {
 					return
@@ -608,14 +613,16 @@ func handlerPptToEnReportTableReferenced(enPptId, enReportId int) {
 		return
 	}
 
-	addList, err := excel.CopyReferencedExcelConfigByReferencedIdAndFromScene(enPptId, utils.TableReferencedByEnPPT, enReportId, utils.TableReferencedByEnReport, reportInfo.AdminId, reportInfo.AdminRealName)
+	newFromScene := utils.TableReferencedByEnReport
+	addList, err := excel.CopyReferencedExcelConfigByReferencedIdAndFromScene(enPptId, utils.TableReferencedByEnPPT, enReportId, newFromScene, reportInfo.AdminId, reportInfo.AdminRealName)
 	if err != nil {
 		return
 	}
 
 	if len(addList) > 0 {
 		// 修改内容
-		reportInfo.Content = HandleReportContentTable(reportInfo.Id, reportInfo.Content)
+		content := HandleReportContentTableAndScene(reportInfo.Id, newFromScene, html.UnescapeString(reportInfo.Content))
+		reportInfo.Content = html.EscapeString(content)
 		err = reportInfo.Update([]string{"Content"})
 		if err != nil {
 			return

+ 2 - 2
services/report.go

@@ -659,7 +659,7 @@ func PcCreateAndUploadSunCode(scene, page string) (imgUrl string, err error) {
 }
 
 // CreateNewReport 创建新报告
-func CreateNewReport(req models.AddReq, adminInfo *system.Admin) (newReportId int64, reportCode, errMsg string, err error) {
+func CreateNewReport(req models.AddReq, adminInfo *system.Admin) (item *models.Report, errMsg string, err error) {
 	contentSub := ""
 	if req.Content != "" {
 		e := utils.ContentXssCheck(req.Content)
@@ -689,7 +689,7 @@ func CreateNewReport(req models.AddReq, adminInfo *system.Admin) (newReportId in
 		return
 	}
 
-	item := new(models.Report)
+	item = new(models.Report)
 	item.AddType = req.AddType
 	item.ClassifyIdFirst = req.ClassifyIdFirst
 	item.ClassifyNameFirst = req.ClassifyNameFirst

+ 90 - 10
services/report_v2.go

@@ -1608,7 +1608,7 @@ func HandleReportContentTable(reportId int, body string) (newBody string) {
 		return
 	}
 
-	replaceIframeSrcTable(doc, reportId)
+	replaceIframeSrcTable(doc, reportId, 0)
 
 	// 输出修改后的HTML
 	var modifiedHtml strings.Builder
@@ -1624,12 +1624,48 @@ func HandleReportContentTable(reportId int, body string) (newBody string) {
 	return
 }
 
+// HandleReportContentTableAndScene
+// @Description: 处理报告内容(动态表格添加报告来源id)(跟另一个需求代码冲突了,不好处理,只能额外写个方法出来使用了)
+// @author: Roc
+// @datetime 2025-01-10 10:15:39
+// @param reportId int
+// @param fromScene int
+// @param body string
+// @return newBody string
+func HandleReportContentTableAndScene(reportId, fromScene int, body string) (newBody string) {
+	if body == `` {
+		return
+	}
+	newBody = body
+
+	// 解析HTML
+	doc, err := html2.Parse(strings.NewReader(body))
+	if err != nil {
+		fmt.Println("Error parsing HTML:", err)
+		return
+	}
+
+	replaceIframeSrcTable(doc, reportId, fromScene)
+
+	// 输出修改后的HTML
+	var modifiedHtml strings.Builder
+	err = html2.Render(&modifiedHtml, doc)
+	if err != nil {
+		fmt.Println("Error rendering HTML:", err)
+		return
+	}
+
+	newBody = modifiedHtml.String()
+
+	return
+}
+
 // replaceIframeSrc 遍历HTML节点,替换iframe的src属性
-func replaceIframeSrcTable(n *html2.Node, reportId int) {
+func replaceIframeSrcTable(n *html2.Node, reportId, fromScene int) {
 	if n.Type == html2.ElementNode && n.Data == "iframe" {
 		for i, attr := range n.Attr {
 			if attr.Key == "src" {
-				newLink := handleTableLinkReportId(attr.Val, reportId)
+				newLink := handleTableLinkReportId(attr.Val, reportId, fromScene)
 				// 替换原来的链接
 				n.Attr[i].Val = newLink
 				break
@@ -1638,12 +1674,12 @@ func replaceIframeSrcTable(n *html2.Node, reportId int) {
 	}
 	// 递归处理子节点
 	for c := n.FirstChild; c != nil; c = c.NextSibling {
-		replaceIframeSrcTable(c, reportId)
+		replaceIframeSrcTable(c, reportId, fromScene)
 	}
 }
 
 // handleTableLinkReportId 链接添加token
-func handleTableLinkReportId(link string, reportId int) string {
+func handleTableLinkReportId(link string, reportId, fromScene int) string {
 	var err error
 	defer func() {
 		if err != nil {
@@ -1663,6 +1699,11 @@ func handleTableLinkReportId(link string, reportId int) string {
 
 	queryParams.Add("sourceId", fmt.Sprint(reportId))
 
+	if fromScene > 0 {
+		queryParams.Del("fromScene")
+		queryParams.Add("fromScene", fmt.Sprint(fromScene))
+	}
+
 	// 更新URL的查询参数
 	parsedURL.RawQuery = queryParams.Encode()
 
@@ -1692,7 +1733,46 @@ func HandleReportContentStructTable(reportId int, body string) (newBody string)
 
 	// 处理每个组件
 	for i := range jsonData {
-		if err := processMapTable(jsonData[i], reportId); err != nil {
+		if err := processMapTable(jsonData[i], reportId, 0); err != nil {
+			fmt.Println("Error processing component:", err)
+			return
+		}
+	}
+
+	// 将处理后的数据转换回JSON字符串
+	modifiedJSON, err := json.MarshalIndent(jsonData, "", "  ")
+	if err != nil {
+		fmt.Println("Error marshaling JSON:", err)
+		return
+	}
+	newBody = string(modifiedJSON)
+
+	return
+}
+
+// HandleReportContentStructTableAndScene
+// @Description: 处理内容组件的表格链接,需要处理来源(跟另一个需求代码冲突了,不好处理,只能额外写个方法出来使用了)
+// @author: Roc
+// @datetime 2025-01-09 13:40:35
+// @param reportId int
+// @param body string
+// @return newBody string
+func HandleReportContentStructTableAndScene(reportId, fromScene int, body string) (newBody string) {
+	if body == `` {
+		return
+	}
+	newBody = body
+
+	// 解析JSON数据到map[string]interface{}
+	var jsonData []map[string]interface{}
+	if err := json.Unmarshal([]byte(body), &jsonData); err != nil {
+		fmt.Println("Error parsing JSON:", err)
+		return
+	}
+
+	// 处理每个组件
+	for i := range jsonData {
+		if err := processMapTable(jsonData[i], reportId, 0); err != nil {
 			fmt.Println("Error processing component:", err)
 			return
 		}
@@ -1710,22 +1790,22 @@ func HandleReportContentStructTable(reportId int, body string) (newBody string)
 }
 
 // processMapTable 递归处理map中的content字段
-func processMapTable(data map[string]interface{}, reportId int) error {
+func processMapTable(data map[string]interface{}, reportId, fromScene int) error {
 	for key, value := range data {
 		switch v := value.(type) {
 		case string:
 			if key == "content" {
-				newContent := handleTableLinkReportId(v, reportId)
+				newContent := handleTableLinkReportId(v, reportId, fromScene)
 				data[key] = newContent
 			}
 		case map[string]interface{}:
-			if err := processMapTable(v, reportId); err != nil {
+			if err := processMapTable(v, reportId, fromScene); err != nil {
 				return err
 			}
 		case []interface{}:
 			for i := range v {
 				if m, ok := v[i].(map[string]interface{}); ok {
-					if err := processMapTable(m, reportId); err != nil {
+					if err := processMapTable(m, reportId, fromScene); err != nil {
 						return err
 					}
 				}