html2Img.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_mfyx/models"
  7. "hongze/hongze_mfyx/utils"
  8. "io/ioutil"
  9. "net/http"
  10. "strconv"
  11. "strings"
  12. )
  13. var (
  14. ServerUrl = "http://127.0.0.1:5008/"
  15. Cygx_activity_sigin_html = "cygx_activity_sigin_html"
  16. Cygx_mp3_html = "cygx_mp3_html"
  17. Cygx_mp4_html = "cygx_mp4_html"
  18. )
  19. type Html2ImgResp struct {
  20. Code int `json:"code"`
  21. Msg string `json:"msg"`
  22. Data string `json:"data"`
  23. }
  24. // postHtml2Img 请求htm2img接口
  25. func postHtml2Img(param map[string]interface{}) (resp *Html2ImgResp, err error) {
  26. // 目前仅此处调用该接口,暂不加授权、校验等
  27. postUrl := ServerUrl + "htm2img"
  28. postData, err := json.Marshal(param)
  29. if err != nil {
  30. return
  31. }
  32. result, err := Html2ImgHttpPost(postUrl, string(postData), "application/json")
  33. if err != nil {
  34. return
  35. }
  36. if err = json.Unmarshal(result, &resp); err != nil {
  37. return
  38. }
  39. return resp, nil
  40. }
  41. // Html2ImgHttpPost post请求
  42. func Html2ImgHttpPost(url, postData string, params ...string) ([]byte, error) {
  43. body := ioutil.NopCloser(strings.NewReader(postData))
  44. client := &http.Client{}
  45. req, err := http.NewRequest("POST", url, body)
  46. if err != nil {
  47. return nil, err
  48. }
  49. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  50. if len(params) > 0 && params[0] != "" {
  51. contentType = params[0]
  52. }
  53. req.Header.Set("Content-Type", contentType)
  54. resp, err := client.Do(req)
  55. if err != nil {
  56. return nil, err
  57. }
  58. defer resp.Body.Close()
  59. b, err := ioutil.ReadAll(resp.Body)
  60. fmt.Println("HttpPost:" + string(b))
  61. return b, err
  62. }
  63. //func init() {
  64. // GetYanxuanSpecialAuthoMomentsImg(53559)
  65. //}
  66. // 生成研选专栏分享到朋友圈的图片
  67. func GetYanxuanSpecialAuthoMomentsImg(userId int) (imgUrl string) {
  68. var err error
  69. //time.Sleep(3*time.Second) // 有时候同时添加多个活动,延迟三秒
  70. defer func() {
  71. if err != nil {
  72. fmt.Println("err:", err)
  73. go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeArticleMomentsImg Err:"+err.Error()+"用户ID"+strconv.Itoa(userId), 3)
  74. }
  75. }()
  76. articleInfo, e := models.GetYanxuanSpecialAuthor(userId, 0, "")
  77. if e != nil {
  78. err = errors.New("GetYanxuanSpecialAuthor, Err: " + e.Error())
  79. return
  80. }
  81. configCode := "special_author_moments_img_html"
  82. detailConfig, e := models.GetConfigByCode(configCode)
  83. if e != nil {
  84. err = errors.New("GetCygxConfigDetailByCode 获取生成研选专栏分享到朋友圈的图片格式信息失败, Err: " + e.Error())
  85. return
  86. }
  87. configValue := detailConfig.ConfigValue
  88. configValue = strings.Replace(configValue, "{{HeadImg}}", articleInfo.HeadImg, -1)
  89. configValue = strings.Replace(configValue, "{{SpecialName}}", articleInfo.SpecialName, -1)
  90. configValue = strings.Replace(configValue, "{{NickName}}", articleInfo.NickName, -1)
  91. configValue = strings.Replace(configValue, "{{SpecialArticleNum}}", strconv.Itoa(articleInfo.SpecialArticleNum), -1)
  92. configValue = strings.Replace(configValue, "{{CollectNum}}", strconv.Itoa(articleInfo.CollectNum), -1)
  93. configValue = strings.Replace(configValue, "{{FollowNum}}", strconv.Itoa(articleInfo.FollowNum), -1)
  94. configValue = strings.Replace(configValue, "{{Introduction}}", articleInfo.Introduction, -1)
  95. htm2ImgReq := make(map[string]interface{})
  96. htm2ImgReq["html_content"] = configValue
  97. htm2ImgReq["width"] = 2250
  98. htm2ImgReq["height"] = 3813
  99. res, err := postHtml2Img(htm2ImgReq)
  100. if err != nil || res == nil {
  101. err = errors.New("html转图片失败: " + res.Msg)
  102. return
  103. }
  104. if res.Code != 200 {
  105. err = errors.New("html转图片失败: " + res.Msg)
  106. return
  107. }
  108. imgUrl = res.Data
  109. fmt.Println(imgUrl)
  110. return
  111. }
  112. // 生成研选专栏分享到朋友圈的图片
  113. func GetYanxuanSpecialAuthoListMomentsImg() (imgUrl string) {
  114. var err error
  115. //time.Sleep(3*time.Second) // 有时候同时添加多个活动,延迟三秒
  116. defer func() {
  117. if err != nil {
  118. fmt.Println("err:", err)
  119. go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeArticleMomentsImg Err:"+err.Error(), 3)
  120. }
  121. }()
  122. var condition string
  123. var pars []interface{}
  124. condition += ` AND a.nick_name <> '' `
  125. condition += ` ORDER BY latest_publish_time DESC`
  126. list, e := models.GetYanxuanSpecialAuthorList(condition, pars, 0, 1)
  127. if e != nil {
  128. err = errors.New("GetYanxuanSpecialAuthor, Err: " + e.Error())
  129. return
  130. }
  131. var SpecialName, HeadImg, NickName, Introduction, PublishTime, Title string
  132. var userIds []int
  133. for _, v := range list {
  134. SpecialName = v.SpecialName
  135. HeadImg = v.HeadImg
  136. NickName = v.NickName
  137. Introduction = v.Introduction
  138. userIds = append(userIds, v.UserId)
  139. }
  140. bestNew := GetBestNewYanxuanSpecialByUserId(userIds)
  141. for _, v := range list {
  142. if bestNew[v.UserId] != nil {
  143. PublishTime = bestNew[v.UserId].PublishTime
  144. Title = bestNew[v.UserId].Title
  145. }
  146. }
  147. configCode := "special_author_list_moments_img_html"
  148. detailConfig, e := models.GetConfigByCode(configCode)
  149. if e != nil {
  150. err = errors.New("GetCygxConfigDetailByCode 获取生成研选专栏分享到朋友圈的图片格式信息失败, Err: " + e.Error())
  151. return
  152. }
  153. configValue := detailConfig.ConfigValue
  154. configValue = strings.Replace(configValue, "{{SpecialName}}", SpecialName, -1)
  155. configValue = strings.Replace(configValue, "{{HeadImg}}", HeadImg, -1)
  156. configValue = strings.Replace(configValue, "{{NickName}}", NickName, -1)
  157. configValue = strings.Replace(configValue, "{{Introduction}}", Introduction, -1)
  158. configValue = strings.Replace(configValue, "{{PublishTime}}", PublishTime, -1)
  159. configValue = strings.Replace(configValue, "{{Title}}", Title, -1)
  160. htm2ImgReq := make(map[string]interface{})
  161. htm2ImgReq["html_content"] = configValue
  162. htm2ImgReq["width"] = 2250
  163. htm2ImgReq["height"] = 3813
  164. res, err := postHtml2Img(htm2ImgReq)
  165. if err != nil || res == nil {
  166. err = errors.New("html转图片失败: " + res.Msg)
  167. return
  168. }
  169. if res.Code != 200 {
  170. err = errors.New("html转图片失败: " + res.Msg)
  171. return
  172. }
  173. imgUrl = res.Data
  174. return
  175. }