ht_mini_facade.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package instance
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_chart_lib/facade"
  6. "eta/eta_chart_lib/models"
  7. "eta/eta_chart_lib/utils"
  8. "fmt"
  9. )
  10. const (
  11. bookMarkUrl = "user/bookMark"
  12. unBookMarkUrl = "user/unBookMark"
  13. checkBookMarkUrl = "user/checkBookMark"
  14. target = "ht"
  15. )
  16. type HTCollectionReq struct {
  17. UniqueCode string `json:"uniqueCode"`
  18. }
  19. type HTMiniFacade struct {
  20. facade.BaseMiniFacade
  21. }
  22. type HTResponse struct {
  23. Ret int
  24. Msg string
  25. Data interface{} `json:"data"`
  26. ErrMsg string
  27. ErrCode int
  28. Success bool
  29. }
  30. type HTRequest struct {
  31. facade.BaseRequest
  32. ChartInfoId int
  33. ChartName string
  34. ChartImage string
  35. SourceType string
  36. SourceId int
  37. }
  38. func dealChartInfo(baseReq facade.BaseRequest) (request HTRequest, err error) {
  39. chartInfo, err := models.GetChartInfoByUniqueCode(baseReq.UniqueCode)
  40. if err != nil {
  41. utils.FileLog.Error("收藏失败,获取图表信息失败:%v", err.Error())
  42. return
  43. }
  44. request = HTRequest{
  45. BaseRequest: baseReq,
  46. ChartImage: chartInfo.ChartImage,
  47. ChartInfoId: chartInfo.ChartInfoId,
  48. ChartName: chartInfo.ChartName,
  49. SourceType: "chart",
  50. SourceId: chartInfo.ChartInfoId,
  51. }
  52. return
  53. }
  54. func parseResult(resp string) (response HTResponse, err error) {
  55. err = json.Unmarshal([]byte(resp), &response)
  56. if err != nil {
  57. utils.FileLog.Error("收藏失败,解析应答失败:%v,应答结果:%s", err.Error(), resp)
  58. }
  59. if !response.Success {
  60. utils.FileLog.Error("收藏失败,应答结果:%d[%s]", response.ErrCode, response.ErrMsg)
  61. err = errors.New(response.Msg + response.ErrMsg)
  62. }
  63. return
  64. }
  65. func (ht *HTMiniFacade) Collect(req facade.BaseRequest) (err error) {
  66. param, err := dealChartInfo(req)
  67. if err != nil {
  68. utils.FileLog.Error("收藏失败,获取图表信息失败:%v", err.Error())
  69. return err
  70. }
  71. url, err := generateUrl(bookMarkUrl)
  72. if err != nil {
  73. return
  74. }
  75. resp, err := ht.Post(url, param, req.Auth)
  76. if err != nil {
  77. utils.FileLog.Error("收藏失败,err:%v,resp:%v", err, resp)
  78. return
  79. }
  80. _, err = parseResult(resp)
  81. return
  82. }
  83. func (ht *HTMiniFacade) UnCollect(req facade.BaseRequest) (err error) {
  84. param, err := dealChartInfo(req)
  85. if err != nil {
  86. utils.FileLog.Error("收藏失败,获取图表信息失败:%v", err.Error())
  87. return err
  88. }
  89. url, err := generateUrl(unBookMarkUrl)
  90. if err != nil {
  91. return
  92. }
  93. resp, err := ht.Post(url, param, req.Auth)
  94. if err != nil {
  95. utils.FileLog.Error("取消收藏失败,err:%v,resp:%v", err, resp)
  96. return
  97. }
  98. _, err = parseResult(resp)
  99. return
  100. }
  101. func generateUrl(path string) (url string, err error) {
  102. configPath := utils.GetMiniUrl(target)
  103. if configPath == "" {
  104. utils.FileLog.Error("获取mini接口地址失败", err)
  105. err = errors.New("获取mini接口地址失败")
  106. return
  107. }
  108. url = fmt.Sprintf("%s%s", configPath, path)
  109. return
  110. }
  111. func (ht *HTMiniFacade) IsCollect(req facade.BaseRequest) bool {
  112. param, err := dealChartInfo(req)
  113. if err != nil {
  114. utils.FileLog.Error("获取是否收藏失败,获取图表信息失败:%v", err.Error())
  115. return false
  116. }
  117. url, err := generateUrl(checkBookMarkUrl)
  118. if err != nil {
  119. return false
  120. }
  121. resp, err := ht.Post(url, param, req.Auth)
  122. if err != nil {
  123. utils.FileLog.Error("获取是否收藏失败,err:%v,resp:%v", err, resp)
  124. return false
  125. }
  126. response, err := parseResult(resp)
  127. if err != nil {
  128. utils.FileLog.Error("获取是否收藏失败,解析应答失败:%v,应答结果:%s", err, resp)
  129. return false
  130. }
  131. var resMap = response.Data.(map[string]interface{})
  132. bookMarked := resMap["isBookMarked"]
  133. if bookMarked == nil {
  134. return false
  135. }
  136. return bookMarked.(bool)
  137. }
  138. func init() {
  139. facade.RegisterMiniFacade(target, &HTMiniFacade{
  140. BaseMiniFacade: facade.FacadeClient,
  141. })
  142. }