|
@@ -8,16 +8,16 @@ import (
|
|
|
"hongze/hongze_yb/utils"
|
|
|
"io/ioutil"
|
|
|
"net/http"
|
|
|
- "os"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
// SharePosterReq 分享海报请求体
|
|
|
type SharePosterReq struct {
|
|
|
- Source string `json:"source" description:"来源"`
|
|
|
- Pars string `json:"pars" description:"海报动态信息"`
|
|
|
- Scene string `json:"scene" description:"太阳码scene"`
|
|
|
- Path string `json:"path" description:"太阳码path"`
|
|
|
+ Source string `json:"source" description:"来源"`
|
|
|
+ Pars string `json:"pars" description:"海报动态信息"`
|
|
|
+ CodeScene string `json:"code_scene" description:"太阳码scene"`
|
|
|
+ CodePath string `json:"code_path" description:"太阳码path"`
|
|
|
+ Version string `json:"version" description:"版本号" `
|
|
|
}
|
|
|
|
|
|
// CreatePosterFromSource 生成分享海报
|
|
@@ -25,52 +25,56 @@ func CreatePosterFromSource(source string, pars string) (imgUrl string, err erro
|
|
|
var errMsg string
|
|
|
defer func() {
|
|
|
if err != nil {
|
|
|
- if global.CONFIG.Serve.RunMode == "release" {
|
|
|
- go SendEmail(utils.APPNAME+"【"+global.CONFIG.Serve.RunMode+"】"+"失败提醒", "生成分享海报失败, "+errMsg+", CreatePosterFromSource: "+err.Error(), utils.EmailSendToUsers)
|
|
|
- }
|
|
|
+ global.LOG.Critical(fmt.Sprintf("CreatePosterFromSource: source=%s, pars:%s, errMsg:%s", source, pars, errMsg))
|
|
|
+ go SendEmail(utils.APPNAME+"【"+global.CONFIG.Serve.RunMode+"】"+"失败提醒", "生成分享海报失败, "+errMsg+", CreatePosterFromSource: "+err.Error(), utils.EmailSendToUsers)
|
|
|
}
|
|
|
}()
|
|
|
-
|
|
|
- // 根据请求路径及版本号获取本地图片, 无对应图片则生成图片
|
|
|
-
|
|
|
- // 根据来源生成相应的html文件, 确定生成图片的长宽
|
|
|
- resourceUrl := ""
|
|
|
- filePath := ""
|
|
|
+ // TODO:根据请求路径及版本号获取图片, 无对应图片则生成图片
|
|
|
+
|
|
|
+ // 根据来源选择指定模板并填充html内容, 及生成图片的长宽
|
|
|
+ heightMap := map[string]int{
|
|
|
+ "chapter_detail": 370,
|
|
|
+ "chapter_list": 369,
|
|
|
+ "chart_detail": 381,
|
|
|
+ "chart_list": 338,
|
|
|
+ "report_detail": 355,
|
|
|
+ "report_list": 369,
|
|
|
+ }
|
|
|
+ width := 294
|
|
|
+ height := heightMap[source]
|
|
|
+ if height == 0 {
|
|
|
+ errMsg = "图片长度有误"
|
|
|
+ err = errors.New(errMsg)
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ reqContent := ""
|
|
|
switch source {
|
|
|
case "chart_detail":
|
|
|
- filePath, err = CreateChartDetailHtml(pars)
|
|
|
- if err != nil {
|
|
|
+ if reqContent, err = FillContent2ChartDetailHtml(pars); err != nil {
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
- if filePath == "" {
|
|
|
- errMsg = "html资源生成失败"
|
|
|
+ if reqContent == "" {
|
|
|
+ errMsg = "html内容有误"
|
|
|
err = errors.New(errMsg)
|
|
|
return
|
|
|
}
|
|
|
- resourceUrl = "http://127.0.0.1:8612/" + filePath
|
|
|
- fmt.Println(resourceUrl)
|
|
|
-
|
|
|
// 请求python服务htm2img
|
|
|
htm2ImgReq := make(map[string]interface{})
|
|
|
- htm2ImgReq["html_url"] = resourceUrl
|
|
|
- res, err := postHtml2Img(htm2ImgReq)
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
- if res == nil {
|
|
|
- err = errors.New("html转图片请求失败, 未知错误")
|
|
|
- return
|
|
|
- }
|
|
|
- if res.Code != 200 {
|
|
|
- err = errors.New("html转图片失败: " + res.Msg)
|
|
|
+ htm2ImgReq["html_content"] = reqContent
|
|
|
+ htm2ImgReq["width"] = width
|
|
|
+ htm2ImgReq["height"] = height
|
|
|
+ if res, err := postHtml2Img(htm2ImgReq); err != nil || res == nil {
|
|
|
+ err = errors.New("html转图片请求失败")
|
|
|
return
|
|
|
+ } else {
|
|
|
+ if res.Code != 200 {
|
|
|
+ err = errors.New("html转图片失败: " + res.Msg)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ imgUrl = res.Data
|
|
|
}
|
|
|
- imgUrl = res.Data
|
|
|
- fmt.Println(imgUrl)
|
|
|
- // 清除本地html文件
|
|
|
-
|
|
|
- // 记录请求路径对应的图片信息
|
|
|
+ // TODO:记录请求路径对应的图片信息
|
|
|
|
|
|
return
|
|
|
}
|
|
@@ -81,53 +85,26 @@ type ChartDetailPosterReq struct {
|
|
|
ChartImage string `json:"chart_image"`
|
|
|
}
|
|
|
|
|
|
-// 生成html-图表详情页
|
|
|
-func CreateChartDetailHtml(pars string) (filePath string, err error) {
|
|
|
+// 生成html文本-图表详情页
|
|
|
+func FillContent2ChartDetailHtml(pars string) (contentStr string, err error) {
|
|
|
// 解析html填充的参数
|
|
|
params := new(ChartDetailPosterReq)
|
|
|
if err = json.Unmarshal([]byte(pars), ¶ms); err != nil {
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
// 获取模板内容
|
|
|
demoPath := "static/htm2img/chart_detail.html"
|
|
|
contentByte, err := ioutil.ReadFile(demoPath)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
- // 获取动态太阳码
|
|
|
+ // TODO:获取太阳码
|
|
|
|
|
|
// 替换模板内容
|
|
|
- contentStr := string(contentByte)
|
|
|
+ contentStr = string(contentByte)
|
|
|
contentStr = strings.ReplaceAll(contentStr, "{{CHART_NAME}}", params.ChartName)
|
|
|
contentStr = strings.ReplaceAll(contentStr, "{{CHART_IMAGE}}", params.ChartImage)
|
|
|
- fmt.Println("ChartName:", params.ChartName)
|
|
|
- fmt.Println("ChartImage:", params.ChartImage)
|
|
|
-
|
|
|
- // 生成静态html文件
|
|
|
- filePath = "static/htm2img/" + utils.GetRandStringNoSpecialChar(28) + ".html"
|
|
|
- f, err := os.Create(filePath)
|
|
|
- defer f.Close()
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
- if _, err = f.Write([]byte(contentStr)); err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
-// 生成静态html文件
|
|
|
-func PutHtmlIntoFile(content []byte) (filePath string, err error) {
|
|
|
- filePath = "static/htm2img/" + utils.GetRandStringNoSpecialChar(28) + ".html"
|
|
|
- f, err := os.Create(filePath)
|
|
|
- defer f.Close()
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
- _, err = f.Write(content)
|
|
|
+ contentStr = strings.ReplaceAll(contentStr, "{{SUN_CODE}}", "")
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -141,7 +118,7 @@ type Html2ImgResp struct {
|
|
|
func postHtml2Img(param map[string]interface{}) (resp *Html2ImgResp, err error) {
|
|
|
pythonUrl := ""
|
|
|
if global.CONFIG.Serve.RunMode == "release" {
|
|
|
- //edbLibUrl = "http://172.19.173.232:8300/"
|
|
|
+
|
|
|
} else {
|
|
|
pythonUrl = "http://127.0.0.1:8888/"
|
|
|
}
|
|
@@ -160,7 +137,7 @@ func postHtml2Img(param map[string]interface{}) (resp *Html2ImgResp, err error)
|
|
|
return resp, nil
|
|
|
}
|
|
|
|
|
|
-// Html2ImgHttpPost post请求edb库
|
|
|
+// Html2ImgHttpPost post请求
|
|
|
func Html2ImgHttpPost(url, postData string, params ...string) ([]byte, error) {
|
|
|
body := ioutil.NopCloser(strings.NewReader(postData))
|
|
|
client := &http.Client{}
|