chart_permission_sync.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/services/alarm_msg"
  5. "eta_gn/eta_api/utils"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. )
  11. type ChartPermissionResp struct {
  12. Code int `json:"code" description:"状态码"`
  13. Msg string `json:"msg" description:"提示信息"`
  14. ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
  15. }
  16. func crmEtaPost(url string, pars interface{}) (respBody []byte, err error) {
  17. params, e := json.Marshal(pars)
  18. if e != nil {
  19. err = fmt.Errorf("data json marshal err: %s", e.Error())
  20. return
  21. }
  22. body := ioutil.NopCloser(strings.NewReader(string(params)))
  23. client := &http.Client{}
  24. req, e := http.NewRequest("POST", url, body)
  25. if e != nil {
  26. err = fmt.Errorf("http create request err: %s", e.Error())
  27. return
  28. }
  29. contentType := "application/json;charset=utf-8"
  30. req.Header.Set("Content-Type", contentType)
  31. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  32. resp, e := client.Do(req)
  33. if e != nil {
  34. err = fmt.Errorf("http client do err: %s", e.Error())
  35. return
  36. }
  37. defer func() {
  38. _ = resp.Body.Close()
  39. }()
  40. b, e := ioutil.ReadAll(resp.Body)
  41. if e != nil {
  42. err = fmt.Errorf("resp body read err: %s", e.Error())
  43. return
  44. }
  45. if len(b) == 0 {
  46. err = fmt.Errorf("resp body is empty")
  47. return
  48. }
  49. if utils.RunMode == "release" {
  50. str := string(b)
  51. str = strings.Trim(str, `"`)
  52. b = utils.DesBase64Decrypt([]byte(str), utils.CrmEtaServerDes3Key)
  53. }
  54. respBody = b
  55. return
  56. }
  57. func ChartFiccPermissionSync() (err error, errMsg string) {
  58. defer func() {
  59. if err != nil {
  60. utils.FileLog.Info("同步品种权限数据失败, Err: " + err.Error() + errMsg)
  61. alarm_msg.SendAlarmMsg("同步品种权限数据失败,Err:"+err.Error(), 3)
  62. }
  63. }()
  64. if utils.CrmEtaServerUrl == "" {
  65. return
  66. }
  67. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chart_permission/sync")
  68. pars := make(map[string]interface{})
  69. b, err := crmEtaPost(url, pars)
  70. if err != nil {
  71. errMsg = "同步品种失败"
  72. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  73. return
  74. }
  75. result := new(ChartPermissionResp)
  76. if e := json.Unmarshal(b, &result); e != nil {
  77. errMsg = "同步品种失败"
  78. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  79. return
  80. }
  81. utils.FileLog.Info("%s", string(b))
  82. if result.Code != 200 {
  83. err = fmt.Errorf("result: %s, err: %s", string(b), result.ErrMsg)
  84. errMsg = result.Msg
  85. return
  86. }
  87. return
  88. }
  89. type EditClassifyPermissionReq struct {
  90. Keyword string
  91. ClassifyId int
  92. }
  93. func EditClassifyChartPermissionSync(keyword string, classifyId int) (err error) {
  94. defer func() {
  95. if err != nil {
  96. utils.FileLog.Info("同步设置报告分类权限失败, Err: " + err.Error())
  97. alarm_msg.SendAlarmMsg("同步设置报告分类权限失败,Err:"+err.Error(), 3)
  98. }
  99. }()
  100. if utils.CrmEtaServerUrl == "" {
  101. return
  102. }
  103. req := &EditClassifyPermissionReq{Keyword: keyword, ClassifyId: classifyId}
  104. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chart_permission/classify/sync")
  105. b, err := crmEtaPost(url, req)
  106. if err != nil {
  107. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  108. return
  109. }
  110. result := new(ChartPermissionResp)
  111. if e := json.Unmarshal(b, &result); e != nil {
  112. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  113. return
  114. }
  115. utils.FileLog.Info("%s", string(b))
  116. if result.Code != 200 {
  117. err = fmt.Errorf("result: %s", string(b))
  118. return
  119. }
  120. return
  121. }
  122. type EditReportPermissionSyncReq struct {
  123. ReportId int64 `description:"报告id"`
  124. ClassifyNameSecond string `description:"二级分类名称"`
  125. ClassifyIdSecond int `description:"二级分类ID"`
  126. }
  127. func EditReportPermissionSync(reportId int64, classifyIdSecond int) (err error) {
  128. defer func() {
  129. if err != nil {
  130. utils.FileLog.Info("同步设置报告权限失败, Err: " + err.Error())
  131. alarm_msg.SendAlarmMsg("同步设置报告权限失败,Err:"+err.Error(), 3)
  132. }
  133. }()
  134. if utils.CrmEtaServerUrl == "" {
  135. return
  136. }
  137. req := &EditReportPermissionSyncReq{ReportId: reportId, ClassifyIdSecond: classifyIdSecond}
  138. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chart_permission/report/sync")
  139. b, err := crmEtaPost(url, req)
  140. if err != nil {
  141. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  142. return
  143. }
  144. result := new(ChartPermissionResp)
  145. if e := json.Unmarshal(b, &result); e != nil {
  146. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  147. return
  148. }
  149. utils.FileLog.Info("%s", string(b))
  150. if result.Code != 200 {
  151. err = fmt.Errorf("result: %s", string(b))
  152. return
  153. }
  154. return
  155. }
  156. type EditKeywordPermissionSyncReq struct {
  157. NewKeyword string
  158. ClassifyId int
  159. }
  160. func EditKeywordPermissionSync(newKeyword string, classifyId int) (err error) {
  161. defer func() {
  162. if err != nil {
  163. utils.FileLog.Info("同步设置报告权限分类名称失败, Err: " + err.Error())
  164. alarm_msg.SendAlarmMsg("同步设置报告权限分类名称失败,Err:"+err.Error(), 3)
  165. }
  166. }()
  167. if utils.CrmEtaServerUrl == "" {
  168. return
  169. }
  170. req := &EditKeywordPermissionSyncReq{NewKeyword: newKeyword, ClassifyId: classifyId}
  171. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chart_permission/keyword/sync")
  172. b, err := crmEtaPost(url, req)
  173. if err != nil {
  174. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  175. return
  176. }
  177. result := new(ChartPermissionResp)
  178. if e := json.Unmarshal(b, &result); e != nil {
  179. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  180. return
  181. }
  182. utils.FileLog.Info("%s", string(b))
  183. if result.Code != 200 {
  184. err = fmt.Errorf("result: %s", string(b))
  185. return
  186. }
  187. return
  188. }