chart_permission_sync.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/services/alarm_msg"
  5. "eta/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. // 生产环境解密, 注意有个坑前后的双引号
  50. if utils.RunMode == "release" {
  51. str := string(b)
  52. str = strings.Trim(str, `"`)
  53. b = utils.DesBase64Decrypt([]byte(str), utils.CrmEtaServerDes3Key)
  54. }
  55. respBody = b
  56. return
  57. }
  58. func ChartFiccPermissionSync() (err error, errMsg string) {
  59. defer func() {
  60. if err != nil {
  61. utils.FileLog.Info("同步品种权限数据失败, Err: " + err.Error() + errMsg)
  62. alarm_msg.SendAlarmMsg("同步品种权限数据失败,Err:"+err.Error(), 3)
  63. }
  64. }()
  65. if utils.CrmEtaServerUrl == "" {
  66. return
  67. }
  68. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chart_permission/sync")
  69. pars := make(map[string]interface{})
  70. b, err := crmEtaPost(url, pars)
  71. if err != nil {
  72. errMsg = "同步品种失败"
  73. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  74. return
  75. }
  76. //result := new(models.ResultData)
  77. result := new(ChartPermissionResp)
  78. if e := json.Unmarshal(b, &result); e != nil {
  79. errMsg = "同步品种失败"
  80. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  81. return
  82. }
  83. utils.FileLog.Info("%s", string(b))
  84. if result.Code != 200 {
  85. err = fmt.Errorf("result: %s, err: %s", string(b), result.ErrMsg)
  86. errMsg = result.Msg
  87. return
  88. }
  89. return
  90. }
  91. type EditClassifyPermissionReq struct {
  92. Keyword string
  93. }
  94. // EditClassifyChartPermissionSync 设置报告分类权限
  95. func EditClassifyChartPermissionSync(keyword string) (err error) {
  96. defer func() {
  97. if err != nil {
  98. utils.FileLog.Info("同步设置报告分类权限失败, Err: " + err.Error())
  99. alarm_msg.SendAlarmMsg("同步设置报告分类权限失败,Err:"+err.Error(), 3)
  100. }
  101. }()
  102. if utils.CrmEtaServerUrl == "" {
  103. return
  104. }
  105. req := &EditClassifyPermissionReq{Keyword: keyword}
  106. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chart_permission/classify/sync")
  107. b, err := crmEtaPost(url, req)
  108. if err != nil {
  109. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  110. return
  111. }
  112. //result := new(models.ResultData)
  113. result := new(ChartPermissionResp)
  114. if e := json.Unmarshal(b, &result); e != nil {
  115. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  116. return
  117. }
  118. utils.FileLog.Info("%s", string(b))
  119. if result.Code != 200 {
  120. err = fmt.Errorf("result: %s", string(b))
  121. return
  122. }
  123. return
  124. }
  125. type EditReportPermissionSyncReq struct {
  126. ReportId int64 `description:"报告id"`
  127. ClassifyNameSecond string `description:"二级分类名称"`
  128. }
  129. // EditReportPermissionSync 设置报告权限
  130. func EditReportPermissionSync(reportId int64, classifyNameSecond string) (err error) {
  131. defer func() {
  132. if err != nil {
  133. utils.FileLog.Info("同步设置报告权限失败, Err: " + err.Error())
  134. alarm_msg.SendAlarmMsg("同步设置报告权限失败,Err:"+err.Error(), 3)
  135. }
  136. }()
  137. if utils.CrmEtaServerUrl == "" {
  138. return
  139. }
  140. req := &EditReportPermissionSyncReq{ReportId: reportId, ClassifyNameSecond: classifyNameSecond}
  141. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chart_permission/report/sync")
  142. b, err := crmEtaPost(url, req)
  143. if err != nil {
  144. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  145. return
  146. }
  147. //result := new(models.ResultData)
  148. result := new(ChartPermissionResp)
  149. if e := json.Unmarshal(b, &result); e != nil {
  150. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  151. return
  152. }
  153. utils.FileLog.Info("%s", string(b))
  154. if result.Code != 200 {
  155. err = fmt.Errorf("result: %s", string(b))
  156. return
  157. }
  158. return
  159. }
  160. type EditKeywordPermissionSyncReq struct {
  161. NewKeyword string
  162. Keyword string
  163. }
  164. // EditKeywordPermissionSync 设置报告权限分类名称
  165. func EditKeywordPermissionSync(newKeyword, keyword string) (err error) {
  166. defer func() {
  167. if err != nil {
  168. utils.FileLog.Info("同步设置报告权限分类名称失败, Err: " + err.Error())
  169. alarm_msg.SendAlarmMsg("同步设置报告权限分类名称失败,Err:"+err.Error(), 3)
  170. }
  171. }()
  172. if utils.CrmEtaServerUrl == "" {
  173. return
  174. }
  175. req := &EditKeywordPermissionSyncReq{NewKeyword: newKeyword, Keyword: keyword}
  176. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chart_permission/keyword/sync")
  177. b, err := crmEtaPost(url, req)
  178. if err != nil {
  179. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  180. return
  181. }
  182. //result := new(models.ResultData)
  183. result := new(ChartPermissionResp)
  184. if e := json.Unmarshal(b, &result); e != nil {
  185. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  186. return
  187. }
  188. utils.FileLog.Info("%s", string(b))
  189. if result.Code != 200 {
  190. err = fmt.Errorf("result: %s", string(b))
  191. return
  192. }
  193. return
  194. }