Roc 2 years ago
parent
commit
d0e9436727
2 changed files with 78 additions and 96 deletions
  1. 27 21
      models/tables/yb_poster_config/yb_poster_config.go
  2. 51 75
      services/share_poster.go

+ 27 - 21
models/tables/yb_poster_config/yb_poster_config.go

@@ -6,13 +6,15 @@ import (
 
 // YbPosterConfig 研报海报生成配置
 type YbPosterConfig struct {
-	ID                uint32    `gorm:"primaryKey;column:id;type:int(9) unsigned;not null" json:"-"`
-	Source            string    `gorm:"index:idx_source;column:source;type:varchar(64);default:''" json:"source"` // 来源
-	Hight             float64   `gorm:"column:hight;type:double(9,2) unsigned;default:0.00" json:"hight"`         // 海报高度
-	Width             float64   `gorm:"column:width;type:double(9,2) unsigned;default:0.00" json:"width"`         // 海报宽度
-	HTMLTemplate      string    `gorm:"column:html_template;type:text" json:"htmlTemplate"`                       // html代码模板
-	HTMLReplaceConfig string    `gorm:"column:html_replace_config;type:text" json:"htmlReplaceConfig"`            // 模板中的变量替换规则
-	CreateTime        time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP" json:"createTime"`
+	ID                 uint32    `gorm:"primaryKey;column:id;type:int(9) unsigned;not null" json:"-"`
+	Source             string    `gorm:"index:idx_source;column:source;type:varchar(64);default:''" json:"source"` // 来源
+	Hight              float64   `gorm:"column:hight;type:double(9,2) unsigned;default:0.00" json:"hight"`         // 海报高度
+	Width              float64   `gorm:"column:width;type:double(9,2) unsigned;default:0.00" json:"width"`         // 海报宽度
+	HTMLTemplate       string    `gorm:"column:html_template;type:text" json:"htmlTemplate"`                       // html代码模板
+	HTMLReplaceConfig  string    `gorm:"column:html_replace_config;type:text" json:"htmlReplaceConfig"`            // 模板中的变量替换规则
+	DefaultValueConfig string    `gorm:"column:default_value_config;type:text" json:"defaultValueConfig"`          // 默认值的配置
+	Remark             string    `gorm:"column:remark;type:varchar(255);default:''" json:"remark"`                 // 备注
+	CreateTime         time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP" json:"createTime"`
 }
 
 // TableName get sql table name.获取数据库表名
@@ -22,19 +24,23 @@ func (m *YbPosterConfig) TableName() string {
 
 // YbPosterConfigColumns get sql column name.获取数据库列名
 var YbPosterConfigColumns = struct {
-	ID                string
-	Source            string
-	Hight             string
-	Width             string
-	HTMLTemplate      string
-	HTMLReplaceConfig string
-	CreateTime        string
+	ID                 string
+	Source             string
+	Hight              string
+	Width              string
+	HTMLTemplate       string
+	HTMLReplaceConfig  string
+	DefaultValueConfig string
+	Remark             string
+	CreateTime         string
 }{
-	ID:                "id",
-	Source:            "source",
-	Hight:             "hight",
-	Width:             "width",
-	HTMLTemplate:      "html_template",
-	HTMLReplaceConfig: "html_replace_config",
-	CreateTime:        "create_time",
+	ID:                 "id",
+	Source:             "source",
+	Hight:              "hight",
+	Width:              "width",
+	HTMLTemplate:       "html_template",
+	HTMLReplaceConfig:  "html_replace_config",
+	DefaultValueConfig: "default_value_config",
+	Remark:             "remark",
+	CreateTime:         "create_time",
 }

+ 51 - 75
services/share_poster.go

@@ -527,18 +527,20 @@ func CreatePosterFromSourceV2(codePage, codeScene, source, version, pars string)
 
 	width := ybPosterConfig.Width
 	height := ybPosterConfig.Hight
-	// 生成太阳码
+	//生成太阳码
 	sunCodeUrl, err := CreateAndUploadSunCode(codePage, codeScene, version)
 	if err != nil {
 		return
 	}
+	//sunCodeUrl := ``
 	// 填充html内容
 	contentStr, newHeight, err := fillContent2HtmlV2(source, pars, sunCodeUrl, height, *ybPosterConfig)
 	if err != nil {
 		errMsg = "html内容有误"
 		return
 	}
-	return
+	//global.LOG.Critical(contentStr)
+	//return
 	// 请求python服务htm2img
 	htm2ImgReq := make(map[string]interface{})
 	htm2ImgReq["html_content"] = contentStr
@@ -573,21 +575,22 @@ type HtmlReplaceConfig struct {
 	ReplaceStr  string `json:"replace_str"`
 }
 
+// DefaultValueConfig 默认值的配置
+type DefaultValueConfig struct {
+	Key          string `json:"key"`
+	UseOtherKey  string `json:"use_other_key"`
+	Value        string `json:"value"`
+	ConditionKey string `json:"condition_key"`
+}
+
 // fillContent2Html 填充HTML动态内容
 func fillContent2HtmlV2(source, pars, sunCodeUrl string, height float64, ybPosterConfig yb_poster_config.YbPosterConfig) (contentStr string, newHeight float64, err error) {
-	params := new(PosterParsReq)
-	if err = json.Unmarshal([]byte(pars), &params); err != nil {
-		return
-	}
-
-	fmt.Println(params)
-
 	paramsMap := make(map[string]string)
 	if err = json.Unmarshal([]byte(pars), &paramsMap); err != nil {
 		return
 	}
 
-	fmt.Println(paramsMap)
+	//fmt.Println(paramsMap)
 
 	//html替换规则
 	htmlReplaceConfigList := make([]HtmlReplaceConfig, 0)
@@ -597,66 +600,34 @@ func fillContent2HtmlV2(source, pars, sunCodeUrl string, height float64, ybPoste
 
 	newHeight = height
 	contentStr = ybPosterConfig.HTMLTemplate
+
+	// 默认数据替换
+	defaultValueConfigMap := make([]DefaultValueConfig, 0)
+	if ybPosterConfig.DefaultValueConfig != `` {
+		if err = json.Unmarshal([]byte(ybPosterConfig.DefaultValueConfig), &defaultValueConfigMap); err != nil {
+			return
+		}
+	}
 	// 列表的动态内容不完整的用默认内容的填充
-	var emptyTime1, emptyTime2 bool
-	if strings.Contains(source, "list") {
-		title1, ok := paramsMap["title_1"]
-		if !ok {
-			title1 = ``
+	//var emptyTime1, emptyTime2 bool
+	conditionKeyValMap := make(map[string]string)
+	for _, v := range defaultValueConfigMap {
+		if v.ConditionKey == `` {
+			continue
 		}
-		title2, ok := paramsMap["title_2"]
+		conditionKeyVal, ok := conditionKeyValMap[v.ConditionKey]
 		if !ok {
-			title2 = ``
+			conditionKeyVal = paramsMap[v.ConditionKey]
+			conditionKeyValMap[v.ConditionKey] = conditionKeyVal
 		}
-		if title1 == "" || title2 == "" {
-			defaultAvatar := "https://hzstatic.hzinsights.com/static/images/202112/20211210/wn6c3oYKTfT4NbTZgRGflRuIBZaa.png"
-			switch source {
-			case "activity_list":
-				if title1 == "" {
-					paramsMap["list_title"] = "线下沙龙"
-					paramsMap["status_1"] = "未开始"
-					paramsMap["avatar_1"] = defaultAvatar
-					paramsMap["title_1"] = "周度报告"
-					paramsMap["speaker_1"] = "FICC研究员"
-					paramsMap["time_1"] = "2022-5-10"
-					emptyTime1 = true
-				}
-				params.Status2 = params.Status1
-				params.Avatar2 = defaultAvatar
-				params.Title2 = "周度报告"
-				params.Speaker2 = "FICC研究员"
-				params.Time2 = "2022-5-10"
-				emptyTime2 = true
-			case "special_column_list":
-				if params.Title1 == "" {
-					params.Stage1 = "第1期"
-					params.Avatar1 = defaultAvatar
-					params.Title1 = "弘则FICC专栏"
-					params.Author1 = "弘则研究"
-					params.Tag1 = "FICC研究员"
-				}
-				params.Stage2 = "第2期"
-				params.Avatar2 = defaultAvatar
-				params.Title2 = "弘则FICC专栏"
-				params.Author2 = "弘则研究"
-				params.Tag2 = "FICC研究员"
-			case "chart_list":
-				if params.Title1 == "" {
-					params.Title1 = "螺纹仓单-热卷仓单季节性"
-					params.Img1 = "https://hzstatic.hzinsights.com/static/images/202112/20211227/8VBIH1l6VraYpLCoBS6qOIXA5Zoq.png"
+		if conditionKeyVal == `` {
+			paramsMap[v.Key] = v.Value
+			if v.UseOtherKey != `` {
+				if tmpVal, ok := paramsMap[v.UseOtherKey]; ok {
+					paramsMap[v.Key] = tmpVal
 				}
-				params.Title2 = "卷螺期货现货价差"
-				params.Img2 = "https://hzstatic.hzinsights.com/static/images/202204/20220427/d8GRfdR3Xfrvk397SnYudcwVs9pV.png"
-			case "report_list":
-				if params.Title1 == "" {
-					params.Img1 = defaultAvatar
-					params.Title1 = "弘则FICC研报"
-					params.Time1 = "1期"
-				}
-				params.Img2 = defaultAvatar
-				params.Title2 = "弘则FICC研报"
-				params.Time2 = "2期"
 			}
+
 		}
 	}
 
@@ -701,27 +672,32 @@ func fillContent2HtmlV2(source, pars, sunCodeUrl string, height float64, ybPoste
 			"线上会议": "block",
 			"线下沙龙": "none",
 		}
-		if params.Status1 != "未开始" {
+
+		listTitle := paramsMap["list_title"]
+		status1 := paramsMap["status_1"]
+		if status1 != "未开始" {
 			newHeight = 1715
 		}
+		status2 := paramsMap["status_2"]
 		paramsMap["list_title"] = "弘则FICC周度电话会安排"
-		paramsMap["bg_color_1"] = bgColorMap[params.Status1]
-		paramsMap["show_item_1"] = statusItemMap[params.Status1]
-		paramsMap["show_offline_1"] = offlineMap[params.ListTitle]
-		paramsMap["show_online_1"] = onlineMap[params.ListTitle]
+		paramsMap["bg_color_1"] = bgColorMap[status1]
+		paramsMap["show_item_1"] = statusItemMap[status1]
+		paramsMap["show_offline_1"] = offlineMap[listTitle]
+		paramsMap["show_online_1"] = onlineMap[listTitle]
 
-		paramsMap["bg_color_2"] = bgColorMap[params.Status2]
-		paramsMap["show_item_2"] = statusItemMap[params.Status2]
-		paramsMap["show_offline_2"] = offlineMap[params.ListTitle]
-		paramsMap["show_online_2"] = onlineMap[params.ListTitle]
+		paramsMap["bg_color_2"] = bgColorMap[status2]
+		paramsMap["show_item_2"] = statusItemMap[status2]
+		paramsMap["show_offline_2"] = offlineMap[listTitle]
+		paramsMap["show_online_2"] = onlineMap[listTitle]
 
 		// 用默认内容填充的活动时间字体颜色调至看不见
 		color1 := "#999"
 		color2 := "#999"
-		if emptyTime1 {
+
+		if paramsMap["empty_time_1"] == "true" {
 			color1 = "#fff"
 		}
-		if emptyTime2 {
+		if paramsMap["empty_time_2"] == "true" {
 			color2 = "#fff"
 		}
 		paramsMap["time_color_1"] = color1