123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- package services
- import (
- "encoding/json"
- "errors"
- "fmt"
- "hongze/hongze_yb/global"
- "hongze/hongze_yb/utils"
- "io/ioutil"
- "net/http"
- "strings"
- )
- // SharePosterReq 分享海报请求体
- type SharePosterReq struct {
- 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 生成分享海报
- func CreatePosterFromSource(source string, 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))
- go SendEmail(utils.APPNAME+"【"+global.CONFIG.Serve.RunMode+"】"+"失败提醒", "生成分享海报失败, "+errMsg+", CreatePosterFromSource: "+err.Error(), utils.EmailSendToUsers)
- }
- }()
- // 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":
- if reqContent, err = FillContent2ChartDetailHtml(pars); err != nil {
- return
- }
- }
- if reqContent == "" {
- errMsg = "html内容有误"
- err = errors.New(errMsg)
- return
- }
- // 请求python服务htm2img
- htm2ImgReq := make(map[string]interface{})
- 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
- }
- // TODO:记录请求路径对应的图片信息
- return
- }
- type ChartDetailPosterReq struct {
- ChartInfoId int `json:"chart_info_id"`
- ChartName string `json:"chart_name"`
- ChartImage string `json:"chart_image"`
- }
- // 生成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 = strings.ReplaceAll(contentStr, "{{CHART_NAME}}", params.ChartName)
- contentStr = strings.ReplaceAll(contentStr, "{{CHART_IMAGE}}", params.ChartImage)
- contentStr = strings.ReplaceAll(contentStr, "{{SUN_CODE}}", "")
- return
- }
- type Html2ImgResp struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- Data string `json:"data"`
- }
- // postHtml2Img 请求htm2img接口
- func postHtml2Img(param map[string]interface{}) (resp *Html2ImgResp, err error) {
- pythonUrl := ""
- if global.CONFIG.Serve.RunMode == "release" {
- } else {
- pythonUrl = "http://127.0.0.1:8888/"
- }
- postUrl := pythonUrl + "htm2img"
- postData, err := json.Marshal(param)
- if err != nil {
- return
- }
- result, err := Html2ImgHttpPost(postUrl, string(postData), "application/json")
- if err != nil {
- return
- }
- if err = json.Unmarshal(result, &resp); err != nil {
- return
- }
- return resp, nil
- }
- // Html2ImgHttpPost post请求
- func Html2ImgHttpPost(url, postData string, params ...string) ([]byte, error) {
- body := ioutil.NopCloser(strings.NewReader(postData))
- client := &http.Client{}
- req, err := http.NewRequest("POST", url, body)
- if err != nil {
- return nil, err
- }
- contentType := "application/x-www-form-urlencoded;charset=utf-8"
- if len(params) > 0 && params[0] != "" {
- contentType = params[0]
- }
- req.Header.Set("Content-Type", contentType)
- resp, err := client.Do(req)
- if err != nil {
- return nil, err
- }
- defer resp.Body.Close()
- b, err := ioutil.ReadAll(resp.Body)
- fmt.Println("HttpPost:" + string(b))
- return b, err
- }
|