html2Img.go 8.3 KB

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