chart.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package cygx
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hz_crm_api/models/cygx"
  6. "hongze/hz_crm_api/services/alarm_msg"
  7. "hongze/hz_crm_api/utils"
  8. "io/ioutil"
  9. nhttp "net/http"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // get公共请求方法
  15. func PublicGetDate(url, authorization string) (body []byte, err error) {
  16. defer func() {
  17. if err != nil {
  18. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", url+"Get ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  19. }
  20. }()
  21. method := "GET"
  22. client := &nhttp.Client{}
  23. req, err := nhttp.NewRequest(method, url, nil)
  24. if err != nil {
  25. return
  26. }
  27. req.Header.Add("Authorization", authorization)
  28. res, err := client.Do(req)
  29. if err != nil {
  30. return
  31. }
  32. defer res.Body.Close()
  33. body, err = ioutil.ReadAll(res.Body)
  34. if err != nil {
  35. return
  36. }
  37. return
  38. }
  39. // 获取用户的Token
  40. func GetUserTokenByMobile(mobile string) (token string, err error) {
  41. //缓存校验
  42. cacheKey := fmt.Sprint("xygx_chart:chart_token:add:", "Mobile", mobile)
  43. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  44. if ttlTime > 0 {
  45. token, _ = utils.Rc.RedisString(cacheKey)
  46. }
  47. if token == "" {
  48. url := utils.HZ_CELUE_API_Url + "auth/login"
  49. method := "POST"
  50. payload := strings.NewReader(`{
  51. "phone_number":"` + mobile + `",
  52. "password":"hz123456"}`)
  53. client := &nhttp.Client{}
  54. req, errReq := nhttp.NewRequest(method, url, payload)
  55. if errReq != nil {
  56. err = errReq
  57. return
  58. }
  59. req.Header.Add("Content-Type", "application/json")
  60. req.Header.Add("Cookie", "sessionid=naj5j5kl1jjynh7og1rsaxkl1vrsl829")
  61. res, errReq := client.Do(req)
  62. if errReq != nil {
  63. err = errReq
  64. return
  65. }
  66. defer res.Body.Close()
  67. body, errReq := ioutil.ReadAll(res.Body)
  68. if errReq != nil {
  69. err = errReq
  70. return
  71. }
  72. var chartResult cygx.ChartUserTokenResultApi
  73. errReq = json.Unmarshal(body, &chartResult)
  74. if errReq != nil {
  75. err = errReq
  76. return
  77. }
  78. token = chartResult.Data.AccessToken
  79. utils.Rc.Put(cacheKey, token, time.Hour*24)
  80. }
  81. return
  82. }
  83. // 获取图表收藏
  84. func GetChartCollectionByApi(mobile string, take, skip int) (items []*cygx.HomeChartListResp, err error, total int) {
  85. defer func() {
  86. if err != nil {
  87. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "GetChartPtagByApi ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  88. }
  89. }()
  90. url := utils.HZ_CELUE_API_Url + "charts/favorites?take=" + strconv.Itoa(take) + "&skip=" + strconv.Itoa(skip)
  91. authorization, err := GetUserTokenByMobile(mobile)
  92. if err != nil {
  93. return
  94. }
  95. authorization = "bearer " + authorization
  96. body, err := PublicGetDate(url, authorization)
  97. if err != nil {
  98. return
  99. }
  100. var chartResult cygx.ChartFavoritesResultApi
  101. err = json.Unmarshal(body, &chartResult)
  102. if err != nil {
  103. return
  104. }
  105. for _, v := range chartResult.Data {
  106. item := new(cygx.HomeChartListResp)
  107. item.ChartId = v.ChartId
  108. item.Title = v.ChartInfo.Title
  109. item.TitleEn = v.ChartInfo.TitleEn
  110. t1, _ := time.Parse("2006-01-02T15:04:05Z", v.CreateDate)
  111. item.CreateDate = t1.Add(+time.Hour * 8).Format(utils.FormatDateTime)
  112. item.PtagName = v.ChartInfo.Ptag.Name
  113. item.CtagName = v.ChartInfo.Ctag.Name
  114. item.BodyHtml = v.ChartInfo.Cover
  115. item.HttpUrl = "https://vmp.hzinsights.com/v2/charts/" + strconv.Itoa(v.ChartId)
  116. item.IsNeedJump = true
  117. items = append(items, item)
  118. }
  119. total = chartResult.Pagination.Total
  120. return
  121. }
  122. // 判断策略平台是否已经添加过收藏
  123. func GetIsCollectionChart(mobile string, chartId int) (isCollection bool, err error) {
  124. //获取所有的收藏列表,进行比对看看是否收藏,调用三方接口详情没有是否收藏的字段
  125. list, err, _ := GetChartCollectionByApi(mobile, 9999, 0)
  126. if err != nil {
  127. return
  128. }
  129. for _, v := range list {
  130. if v.ChartId == chartId {
  131. isCollection = true
  132. }
  133. }
  134. return
  135. }
  136. // GetChartTitleMapByid 通过图表ID获取文章标题 map
  137. func GetChartTitleMapByid(chartIds []int) (mapResp map[int]string) {
  138. var err error
  139. defer func() {
  140. if err != nil {
  141. go alarm_msg.SendAlarmMsg(" 通过图表ID获取文章标题 map,信息失败,Err:"+err.Error(), 3)
  142. }
  143. }()
  144. lenchartIds := len(chartIds)
  145. if lenchartIds == 0 {
  146. return
  147. }
  148. var condition string
  149. var pars []interface{}
  150. condition = ` AND chart_id IN (` + utils.GetOrmInReplace(lenchartIds) + `) `
  151. pars = append(pars, chartIds)
  152. chartList, err := cygx.GetChartList(condition, pars, 0, lenchartIds)
  153. if err != nil {
  154. return
  155. }
  156. mapResp = make(map[int]string, 0)
  157. for _, v := range chartList {
  158. mapResp[v.ChartId] = v.Title
  159. }
  160. return
  161. }