|
@@ -6,6 +6,7 @@ import (
|
|
|
"fmt"
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
"hongze/hongze_yb/global"
|
|
|
+ "hongze/hongze_yb/models/tables/yb_poster_config"
|
|
|
"hongze/hongze_yb/models/tables/yb_poster_resource"
|
|
|
"hongze/hongze_yb/models/tables/yb_suncode_pars"
|
|
|
"hongze/hongze_yb/services/alarm_msg"
|
|
@@ -382,12 +383,12 @@ func fillContent2Html(source, pars, sunCodeUrl string, height int) (contentStr s
|
|
|
contentStr = strings.Replace(contentStr, "{{REPORT_ABSTRACT}}", abstract, 1)
|
|
|
case "special_column_list":
|
|
|
contentStr = strings.Replace(contentStr, "{{LIST_TITLE}}", params.ListTitle, 1)
|
|
|
- contentStr = strings.Replace(contentStr, "{{STAGE_1}}", "第" + params.Stage1 + "期", 1)
|
|
|
+ contentStr = strings.Replace(contentStr, "{{STAGE_1}}", "第"+params.Stage1+"期", 1)
|
|
|
contentStr = strings.Replace(contentStr, "{{AVATAR_1}}", params.Avatar1, 1)
|
|
|
contentStr = strings.Replace(contentStr, "{{TITLE_1}}", params.Title1, 1)
|
|
|
contentStr = strings.Replace(contentStr, "{{AUTHOR_1}}", params.Author1, 1)
|
|
|
contentStr = strings.Replace(contentStr, "{{TAG_1}}", params.Tag1, 1)
|
|
|
- contentStr = strings.Replace(contentStr, "{{STAGE_2}}", "第" + params.Stage2 + "期", 1)
|
|
|
+ contentStr = strings.Replace(contentStr, "{{STAGE_2}}", "第"+params.Stage2+"期", 1)
|
|
|
contentStr = strings.Replace(contentStr, "{{AVATAR_2}}", params.Avatar2, 1)
|
|
|
contentStr = strings.Replace(contentStr, "{{TITLE_2}}", params.Title2, 1)
|
|
|
contentStr = strings.Replace(contentStr, "{{AUTHOR_2}}", params.Author2, 1)
|
|
@@ -464,3 +465,277 @@ func fillContent2Html(source, pars, sunCodeUrl string, height int) (contentStr s
|
|
|
contentStr = strings.Replace(contentStr, "{{SUN_CODE}}", sunCodeUrl, 1)
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+// CreatePosterFromSourceV2 生成分享海报(通过配置获取相关信息)
|
|
|
+func CreatePosterFromSourceV2(codePage, codeScene, source, version, pars string) (imgUrl string, err error) {
|
|
|
+ var errMsg string
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ global.LOG.Critical(fmt.Sprintf("CreatePosterFromSource: source=%s, pars:%s, errMsg:%s", source, pars, errMsg))
|
|
|
+ reqSlice := make([]string, 0)
|
|
|
+ reqSlice = append(reqSlice, fmt.Sprint("CodePage:", codePage, "\n"))
|
|
|
+ reqSlice = append(reqSlice, fmt.Sprint("CodeScene:", codeScene, "\n"))
|
|
|
+ reqSlice = append(reqSlice, fmt.Sprint("Source:", source, "\n"))
|
|
|
+ reqSlice = append(reqSlice, fmt.Sprint("Version:", version, "\n"))
|
|
|
+ reqSlice = append(reqSlice, fmt.Sprint("Pars:", pars, "\n"))
|
|
|
+ go alarm_msg.SendAlarmMsg("CreatePosterFromSource生成分享海报失败, Msg:"+errMsg+";Err:"+err.Error()+"\n;Req:\n"+strings.Join(reqSlice, ";"), 3)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ if codePage == "" || source == "" || pars == "" {
|
|
|
+ errMsg = "参数有误"
|
|
|
+ err = errors.New(errMsg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ path := fmt.Sprint(codePage, "?", codeScene)
|
|
|
+ // 非列表来源获取历史图片,无则生成
|
|
|
+ if !strings.Contains(source, "list") {
|
|
|
+ poster, tmpErr := yb_poster_resource.GetPosterByCondition(path, "poster", version)
|
|
|
+ if tmpErr != nil && tmpErr != utils.ErrNoRow {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if poster != nil && poster.ImgURL != "" {
|
|
|
+ imgUrl = poster.ImgURL
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ybPosterConfig, err := yb_poster_config.GetBySource(source)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 图片长宽
|
|
|
+ //heightMap := map[string]int{
|
|
|
+ // "activity_detail": 1210,
|
|
|
+ // "activity_list": 1948,
|
|
|
+ // "special_column_detail": 1480,
|
|
|
+ // "special_column_list": 1548,
|
|
|
+ // "chart_detail": 1536,
|
|
|
+ // "chart_list": 1352,
|
|
|
+ // "report_detail": 1420,
|
|
|
+ // "report_list": 1344,
|
|
|
+ //}
|
|
|
+ //widthMap := map[string]int{
|
|
|
+ // "activity_detail": 1280,
|
|
|
+ // "activity_list": 1280,
|
|
|
+ // "special_column_detail": 1176,
|
|
|
+ // "special_column_list": 1176,
|
|
|
+ // "chart_detail": 1176,
|
|
|
+ // "chart_list": 1176,
|
|
|
+ // "report_detail": 1176,
|
|
|
+ // "report_list": 1176,
|
|
|
+ //}
|
|
|
+
|
|
|
+ width := ybPosterConfig.Width
|
|
|
+ height := ybPosterConfig.Hight
|
|
|
+ // 生成太阳码
|
|
|
+ sunCodeUrl, err := CreateAndUploadSunCode(codePage, codeScene, version)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 填充html内容
|
|
|
+ contentStr, newHeight, err := fillContent2HtmlV2(source, pars, sunCodeUrl, height, *ybPosterConfig)
|
|
|
+ if err != nil {
|
|
|
+ errMsg = "html内容有误"
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+ // 请求python服务htm2img
|
|
|
+ htm2ImgReq := make(map[string]interface{})
|
|
|
+ htm2ImgReq["html_content"] = contentStr
|
|
|
+ htm2ImgReq["width"] = width
|
|
|
+ htm2ImgReq["height"] = newHeight
|
|
|
+ res, err := postHtml2Img(htm2ImgReq)
|
|
|
+ if err != nil || res == nil {
|
|
|
+ errMsg = "html转图片请求失败"
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if res.Code != 200 {
|
|
|
+ errMsg = "html转图片请求失败"
|
|
|
+ err = errors.New("html转图片失败: " + res.Msg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ imgUrl = res.Data
|
|
|
+ // 记录海报信息
|
|
|
+ newPoster := &yb_poster_resource.YbPosterResource{
|
|
|
+ Path: path,
|
|
|
+ ImgURL: imgUrl,
|
|
|
+ Type: "poster",
|
|
|
+ Version: version,
|
|
|
+ CreateTime: time.Now(),
|
|
|
+ }
|
|
|
+ err = newPoster.Create()
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// HtmlReplaceConfig html替换配置
|
|
|
+type HtmlReplaceConfig struct {
|
|
|
+ TemplateStr string `json:"template_str"`
|
|
|
+ ReplaceStr string `json:"replace_str"`
|
|
|
+}
|
|
|
+
|
|
|
+// 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), ¶ms); err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Println(params)
|
|
|
+
|
|
|
+ paramsMap := make(map[string]string)
|
|
|
+ if err = json.Unmarshal([]byte(pars), ¶msMap); err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Println(paramsMap)
|
|
|
+
|
|
|
+ //html替换规则
|
|
|
+ htmlReplaceConfigList := make([]HtmlReplaceConfig, 0)
|
|
|
+ if err = json.Unmarshal([]byte(ybPosterConfig.HTMLReplaceConfig), &htmlReplaceConfigList); err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ newHeight = height
|
|
|
+ contentStr = ybPosterConfig.HTMLTemplate
|
|
|
+ // 列表的动态内容不完整的用默认内容的填充
|
|
|
+ var emptyTime1, emptyTime2 bool
|
|
|
+ if strings.Contains(source, "list") {
|
|
|
+ title1, ok := paramsMap["title_1"]
|
|
|
+ if !ok {
|
|
|
+ title1 = ``
|
|
|
+ }
|
|
|
+ title2, ok := paramsMap["title_2"]
|
|
|
+ if !ok {
|
|
|
+ title2 = ``
|
|
|
+ }
|
|
|
+ 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"
|
|
|
+ }
|
|
|
+ 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期"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 填充指定内容
|
|
|
+ switch source {
|
|
|
+ case "report_detail": //需要将简介处理下
|
|
|
+ reportAbstract := paramsMap["report_abstract"]
|
|
|
+ doc, tmpErr := goquery.NewDocumentFromReader(strings.NewReader(reportAbstract))
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ abstract := ""
|
|
|
+ doc.Find("p").Each(func(i int, s *goquery.Selection) {
|
|
|
+ phtml, tmpErr := s.Html()
|
|
|
+ if tmpErr != nil {
|
|
|
+ err = tmpErr
|
|
|
+ return
|
|
|
+ }
|
|
|
+ st := s.Text()
|
|
|
+ if st != "" && st != "<br>" && st != "<br style=\"max-width: 100%;\">" && !strings.Contains(phtml, "iframe") {
|
|
|
+ abstract = abstract + "<p>" + phtml + "</p>"
|
|
|
+ }
|
|
|
+ })
|
|
|
+ paramsMap["report_abstract"] = abstract
|
|
|
+ case "activity_list":
|
|
|
+ bgColorMap := map[string]string{
|
|
|
+ "未开始": "#E3B377",
|
|
|
+ "进行中": "#3385FF",
|
|
|
+ "已结束": "#A2A2A2",
|
|
|
+ }
|
|
|
+ statusItemMap := map[string]string{
|
|
|
+ "未开始": "block",
|
|
|
+ "进行中": "none",
|
|
|
+ "已结束": "none",
|
|
|
+ }
|
|
|
+ offlineMap := map[string]string{
|
|
|
+ "线上会议": "none",
|
|
|
+ "线下沙龙": "block",
|
|
|
+ }
|
|
|
+ onlineMap := map[string]string{
|
|
|
+ "线上会议": "block",
|
|
|
+ "线下沙龙": "none",
|
|
|
+ }
|
|
|
+ if params.Status1 != "未开始" {
|
|
|
+ newHeight = 1715
|
|
|
+ }
|
|
|
+ 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_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]
|
|
|
+
|
|
|
+ // 用默认内容填充的活动时间字体颜色调至看不见
|
|
|
+ color1 := "#999"
|
|
|
+ color2 := "#999"
|
|
|
+ if emptyTime1 {
|
|
|
+ color1 = "#fff"
|
|
|
+ }
|
|
|
+ if emptyTime2 {
|
|
|
+ color2 = "#fff"
|
|
|
+ }
|
|
|
+ paramsMap["time_color_1"] = color1
|
|
|
+ paramsMap["time_color_2"] = color2
|
|
|
+ }
|
|
|
+
|
|
|
+ contentStr = strings.Replace(contentStr, "{{SUN_CODE}}", sunCodeUrl, 1)
|
|
|
+
|
|
|
+ for _, v := range htmlReplaceConfigList {
|
|
|
+ tmpVal, ok := paramsMap[v.ReplaceStr]
|
|
|
+ if !ok {
|
|
|
+ tmpVal = ``
|
|
|
+ }
|
|
|
+ contentStr = strings.Replace(contentStr, v.TemplateStr, tmpVal, 1)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|