html2Img.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_cygx/models"
  7. "hongze/hongze_cygx/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. // MakeYanxuanSpecialMomentsImg(185)
  65. //}
  66. // 生成研选专栏分享到朋友圈的图片
  67. func MakeYanxuanSpecialMomentsImg(specialId 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("生成研选专栏分享到朋友圈的图片,MakeYanxuanSpecialMomentsImg Err:"+err.Error()+"专栏ID"+strconv.Itoa(specialId), 3)
  74. }
  75. }()
  76. detail, e := models.GetYanxuanSpecialById(specialId, 0)
  77. if e != nil {
  78. err = errors.New("GetYanxuanSpecialById, Err: " + e.Error())
  79. return
  80. }
  81. go GetYanxuanSpecialAuthoListMomentsImg() //有专栏生成的时候,最新专栏列表封面图
  82. configCode := "yanxuan_special_moments_img_html"
  83. detailConfig, e := models.GetConfigByCode(configCode)
  84. if e != nil {
  85. err = errors.New("GetCygxConfigDetailByCode 获取生成研选专栏分享到朋友圈的图片格式信息失败, Err: " + e.Error())
  86. return
  87. }
  88. var typeName, content string
  89. if detail.Type == 1 {
  90. typeName = "笔记"
  91. } else {
  92. typeName = "观点"
  93. }
  94. content, _ = GetReportContentTextSubNew(detail.Content)
  95. configValue := detailConfig.ConfigValue
  96. configValue = strings.Replace(configValue, "{{HeadImg}}", detail.HeadImg, -1)
  97. configValue = strings.Replace(configValue, "{{NickName}}", detail.NickName, -1)
  98. configValue = strings.Replace(configValue, "{{TypeName}}", typeName, -1)
  99. configValue = strings.Replace(configValue, "{{PublishTime}}", detail.PublishTime, -1)
  100. configValue = strings.Replace(configValue, "{{Title}}", detail.Title, -1)
  101. configValue = strings.Replace(configValue, "{{Content}}", content, -1)
  102. htm2ImgReq := make(map[string]interface{})
  103. htm2ImgReq["html_content"] = configValue
  104. htm2ImgReq["width"] = 2250
  105. htm2ImgReq["height"] = 3813
  106. res, err := postHtml2Img(htm2ImgReq)
  107. if err != nil || res == nil {
  108. err = errors.New("html转图片失败: " + res.Msg)
  109. return
  110. }
  111. if res.Code != 200 {
  112. err = errors.New("html转图片失败: " + res.Msg)
  113. return
  114. }
  115. imgUrl = res.Data
  116. err = models.UpdateYanxuanSpecialMomentsImg(imgUrl, specialId)
  117. return
  118. }
  119. // 生成研选专栏列表分享到朋友圈的图片
  120. func GetYanxuanSpecialAuthoListMomentsImg() (imgUrl string) {
  121. var err error
  122. //time.Sleep(3*time.Second) // 有时候同时添加多个活动,延迟三秒
  123. defer func() {
  124. if err != nil {
  125. fmt.Println("err:", err)
  126. go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeArticleMomentsImg Err:"+err.Error(), 3)
  127. }
  128. }()
  129. var condition string
  130. var pars []interface{}
  131. condition += ` AND a.nick_name <> '' `
  132. condition += ` ORDER BY latest_publish_time DESC`
  133. list, e := models.GetYanxuanSpecialAuthorList(condition, pars, 0, 1)
  134. if e != nil {
  135. err = errors.New("GetYanxuanSpecialAuthorList, Err: " + e.Error())
  136. return
  137. }
  138. var SpecialName, HeadImg, NickName, Introduction, PublishTime, Title string
  139. var userIds []int
  140. for _, v := range list {
  141. SpecialName = v.SpecialName
  142. HeadImg = v.HeadImg
  143. NickName = v.NickName
  144. Introduction = v.Introduction
  145. userIds = append(userIds, v.UserId)
  146. }
  147. bestNew := GetBestNewYanxuanSpecialByUserId(userIds)
  148. for _, v := range list {
  149. if bestNew[v.UserId] != nil {
  150. PublishTime = bestNew[v.UserId].PublishTime
  151. Title = bestNew[v.UserId].Title
  152. }
  153. }
  154. configCode := "special_author_list_moments_img_html"
  155. detailConfig, e := models.GetConfigByCode(configCode)
  156. if e != nil {
  157. err = errors.New("GetCygxConfigDetailByCode 获取生成研选专栏分享到朋友圈的图片格式信息失败, Err: " + e.Error())
  158. return
  159. }
  160. configValue := detailConfig.ConfigValue
  161. configValue = strings.Replace(configValue, "{{SpecialName}}", SpecialName, -1)
  162. configValue = strings.Replace(configValue, "{{HeadImg}}", HeadImg, -1)
  163. configValue = strings.Replace(configValue, "{{NickName}}", NickName, -1)
  164. configValue = strings.Replace(configValue, "{{Introduction}}", Introduction, -1)
  165. configValue = strings.Replace(configValue, "{{PublishTime}}", PublishTime, -1)
  166. configValue = strings.Replace(configValue, "{{Title}}", Title, -1)
  167. htm2ImgReq := make(map[string]interface{})
  168. htm2ImgReq["html_content"] = configValue
  169. htm2ImgReq["width"] = 2250
  170. htm2ImgReq["height"] = 3813
  171. res, err := postHtml2Img(htm2ImgReq)
  172. if err != nil || res == nil {
  173. err = errors.New("html转图片失败: " + res.Msg)
  174. return
  175. }
  176. if res.Code != 200 {
  177. err = errors.New("html转图片失败: " + res.Msg)
  178. return
  179. }
  180. imgUrl = res.Data
  181. configCodeUpdate := "special_author_list_moments_img"
  182. err = models.UpdateConfigByCode(imgUrl, configCodeUpdate)
  183. return
  184. }
  185. // 生成研选专栏分享到朋友圈的图片
  186. func GetYanxuanSpecialAuthoMomentsImg(userId int) (imgUrl string) {
  187. var err error
  188. //time.Sleep(3*time.Second) // 有时候同时添加多个活动,延迟三秒
  189. defer func() {
  190. if err != nil {
  191. fmt.Println("err:", err)
  192. go utils.SendAlarmMsg("生成研选专栏分享到朋友圈的图片,MakeArticleMomentsImg Err:"+err.Error()+"用户ID"+strconv.Itoa(userId), 3)
  193. }
  194. }()
  195. articleInfo, e := models.GetYanxuanSpecialAuthor(userId, 0, "")
  196. if e != nil {
  197. err = errors.New("GetYanxuanSpecialAuthor, Err: " + e.Error())
  198. return
  199. }
  200. configCode := "special_author_moments_img_html"
  201. detailConfig, e := models.GetConfigByCode(configCode)
  202. if e != nil {
  203. err = errors.New("GetCygxConfigDetailByCode 获取生成研选专栏分享到朋友圈的图片格式信息失败, Err: " + e.Error())
  204. return
  205. }
  206. configValue := detailConfig.ConfigValue
  207. configValue = strings.Replace(configValue, "{{HeadImg}}", articleInfo.HeadImg, -1)
  208. configValue = strings.Replace(configValue, "{{SpecialName}}", articleInfo.SpecialName, -1)
  209. configValue = strings.Replace(configValue, "{{NickName}}", articleInfo.NickName, -1)
  210. configValue = strings.Replace(configValue, "{{SpecialArticleNum}}", strconv.Itoa(articleInfo.SpecialArticleNum), -1)
  211. configValue = strings.Replace(configValue, "{{CollectNum}}", strconv.Itoa(articleInfo.CollectNum), -1)
  212. configValue = strings.Replace(configValue, "{{FollowNum}}", strconv.Itoa(articleInfo.FollowNum), -1)
  213. configValue = strings.Replace(configValue, "{{Introduction}}", articleInfo.Introduction, -1)
  214. htm2ImgReq := make(map[string]interface{})
  215. htm2ImgReq["html_content"] = configValue
  216. htm2ImgReq["width"] = 2250
  217. htm2ImgReq["height"] = 3813
  218. res, err := postHtml2Img(htm2ImgReq)
  219. if err != nil || res == nil {
  220. err = errors.New("html转图片失败: " + res.Msg)
  221. return
  222. }
  223. if res.Code != 200 {
  224. err = errors.New("html转图片失败: " + res.Msg)
  225. return
  226. }
  227. imgUrl = res.Data
  228. err = models.UpdateYanxuanSpecialauthorMomentsImg(imgUrl, userId)
  229. fmt.Println(imgUrl)
  230. return
  231. }