html2Img.go 8.6 KB

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