sync_edb_lib.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongqi_watch/global"
  6. "hongze/hongqi_watch/utils"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. )
  11. type BaseResponse struct {
  12. Ret int
  13. Msg string
  14. ErrMsg string
  15. ErrCode string
  16. Data interface{}
  17. Success bool `description:"true 执行成功,false 执行失败"`
  18. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  19. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  20. }
  21. // postRefreshEdbData 刷新指标数据
  22. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *BaseResponse, err error) {
  23. postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
  24. postData, err := json.Marshal(param)
  25. if err != nil {
  26. return
  27. }
  28. result, err := HttpPost(postUrl, string(postData), "application/json")
  29. if err != nil {
  30. return
  31. }
  32. if result != nil {
  33. global.LOG.Info(" Refresh Result: " + string(result))
  34. err = json.Unmarshal(result, &resp)
  35. if err != nil {
  36. return
  37. }
  38. return resp, nil
  39. }
  40. return nil, err
  41. }
  42. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  43. body := ioutil.NopCloser(strings.NewReader(postData))
  44. client := &http.Client{}
  45. req, err := http.NewRequest("POST", url, body)
  46. if err != nil {
  47. return nil, err
  48. }
  49. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  50. if len(params) > 0 && params[0] != "" {
  51. contentType = params[0]
  52. }
  53. req.Header.Set("Content-Type", contentType)
  54. req.Header.Set("authorization", utils.MD5(global.CONFIG.Serve.AppEdbLibNameEn+global.CONFIG.Serve.EdbLibMd5Key))
  55. resp, err := client.Do(req)
  56. if resp != nil {
  57. defer resp.Body.Close()
  58. b, err := ioutil.ReadAll(resp.Body)
  59. fmt.Println("HttpPost:" + string(b))
  60. return b, err
  61. }
  62. return nil, err
  63. }
  64. // 调用指标库公共服务处理卓创红期指标
  65. func HandleHqExcelDataByEdbLib(indexDataList []*SciHqExcel) (resp *BaseResponse, err error) {
  66. urlStr := "sci_hq/handle/excel_data"
  67. postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
  68. postData, err := json.Marshal(indexDataList)
  69. if err != nil {
  70. return
  71. }
  72. result, err := HttpPost(postUrl, string(postData), "application/json")
  73. if err != nil {
  74. return
  75. }
  76. if result != nil {
  77. global.LOG.Info(" Refresh Result: " + string(result))
  78. err = json.Unmarshal(result, &resp)
  79. if err != nil {
  80. return
  81. }
  82. return resp, nil
  83. }
  84. return
  85. }