chart_permission_sync.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_mobile/services/alarm_msg"
  5. "eta/eta_mobile/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. ClassifyId int
  94. }
  95. // EditClassifyChartPermissionSync 设置报告分类权限
  96. func EditClassifyChartPermissionSync(keyword string, classifyId int) (err error) {
  97. defer func() {
  98. if err != nil {
  99. utils.FileLog.Info("同步设置报告分类权限失败, Err: " + err.Error())
  100. alarm_msg.SendAlarmMsg("同步设置报告分类权限失败,Err:"+err.Error(), 3)
  101. }
  102. }()
  103. if utils.CrmEtaServerUrl == "" {
  104. return
  105. }
  106. req := &EditClassifyPermissionReq{Keyword: keyword, ClassifyId: classifyId}
  107. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chart_permission/classify/sync")
  108. b, err := crmEtaPost(url, req)
  109. if err != nil {
  110. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  111. return
  112. }
  113. //result := new(models.ResultData)
  114. result := new(ChartPermissionResp)
  115. if e := json.Unmarshal(b, &result); e != nil {
  116. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  117. return
  118. }
  119. utils.FileLog.Info("%s", string(b))
  120. if result.Code != 200 {
  121. err = fmt.Errorf("result: %s", string(b))
  122. return
  123. }
  124. return
  125. }
  126. type EditReportPermissionSyncReq struct {
  127. ReportId int64 `description:"报告id"`
  128. ClassifyNameSecond string `description:"二级分类名称"`
  129. ClassifyIdSecond int `description:"二级分类ID"`
  130. }
  131. // EditReportPermissionSync 设置报告权限
  132. func EditReportPermissionSync(reportId int64, classifyIdSecond int) (err error) {
  133. defer func() {
  134. if err != nil {
  135. utils.FileLog.Info("同步设置报告权限失败, Err: " + err.Error())
  136. alarm_msg.SendAlarmMsg("同步设置报告权限失败,Err:"+err.Error(), 3)
  137. }
  138. }()
  139. if utils.CrmEtaServerUrl == "" {
  140. return
  141. }
  142. req := &EditReportPermissionSyncReq{ReportId: reportId, ClassifyIdSecond: classifyIdSecond}
  143. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chart_permission/report/sync")
  144. b, err := crmEtaPost(url, req)
  145. if err != nil {
  146. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  147. return
  148. }
  149. //result := new(models.ResultData)
  150. result := new(ChartPermissionResp)
  151. if e := json.Unmarshal(b, &result); e != nil {
  152. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  153. return
  154. }
  155. utils.FileLog.Info("%s", string(b))
  156. if result.Code != 200 {
  157. err = fmt.Errorf("result: %s", string(b))
  158. return
  159. }
  160. return
  161. }
  162. type EditKeywordPermissionSyncReq struct {
  163. NewKeyword string
  164. ClassifyId int
  165. }
  166. // EditKeywordPermissionSync 设置报告权限分类名称
  167. func EditKeywordPermissionSync(newKeyword string, classifyId int) (err error) {
  168. defer func() {
  169. if err != nil {
  170. utils.FileLog.Info("同步设置报告权限分类名称失败, Err: " + err.Error())
  171. alarm_msg.SendAlarmMsg("同步设置报告权限分类名称失败,Err:"+err.Error(), 3)
  172. }
  173. }()
  174. if utils.CrmEtaServerUrl == "" {
  175. return
  176. }
  177. req := &EditKeywordPermissionSyncReq{NewKeyword: newKeyword, ClassifyId: classifyId}
  178. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/crm/chart_permission/keyword/sync")
  179. b, err := crmEtaPost(url, req)
  180. if err != nil {
  181. err = fmt.Errorf("url:%s err: %s", url, err.Error())
  182. return
  183. }
  184. //result := new(models.ResultData)
  185. result := new(ChartPermissionResp)
  186. if e := json.Unmarshal(b, &result); e != nil {
  187. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  188. return
  189. }
  190. utils.FileLog.Info("%s", string(b))
  191. if result.Code != 200 {
  192. err = fmt.Errorf("result: %s", string(b))
  193. return
  194. }
  195. return
  196. }