questionnaire.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package eta_trial
  2. import (
  3. "encoding/json"
  4. "eta/eta_mobile/models"
  5. "eta/eta_mobile/models/eta_trial"
  6. "eta/eta_mobile/utils"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. )
  12. type EtaTrialQuestionnaireListResp struct {
  13. Code int `json:"code" description:"状态码"`
  14. Msg string `json:"msg" description:"提示信息"`
  15. Data eta_trial.EtaTrialQuestionnaireResp `json:"data" description:"返回数据"`
  16. ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
  17. }
  18. // GetEtaTrialQuestionnaireList CRM_ETA服务-获取问卷列表
  19. func GetEtaTrialQuestionnaireList() (res eta_trial.EtaTrialQuestionnaireResp, err error) {
  20. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta_trial/questionnaire/list")
  21. body := ioutil.NopCloser(strings.NewReader(""))
  22. client := &http.Client{}
  23. req, e := http.NewRequest("POST", url, body)
  24. if e != nil {
  25. err = fmt.Errorf("http create request err: %s", e.Error())
  26. return
  27. }
  28. contentType := "application/json;charset=utf-8"
  29. req.Header.Set("Content-Type", contentType)
  30. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  31. resp, e := client.Do(req)
  32. if e != nil {
  33. err = fmt.Errorf("http client do err: %s", e.Error())
  34. return
  35. }
  36. defer func() {
  37. _ = resp.Body.Close()
  38. }()
  39. b, e := ioutil.ReadAll(resp.Body)
  40. if e != nil {
  41. err = fmt.Errorf("resp body read err: %s", e.Error())
  42. return
  43. }
  44. if len(b) == 0 {
  45. err = fmt.Errorf("resp body is empty")
  46. return
  47. }
  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. result := new(EtaTrialQuestionnaireListResp)
  55. if e = json.Unmarshal(b, &result); e != nil {
  56. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  57. return
  58. }
  59. if result.Code != 200 {
  60. err = fmt.Errorf("result: %s", string(b))
  61. return
  62. }
  63. res = result.Data
  64. return
  65. }
  66. type EtaTrialQuestionnaireCommitReq struct {
  67. List []eta_trial.EtaTrialQuestionnaireRecord
  68. }
  69. // EtaTrialQuestionnaireCommit CRM_ETA服务-提交问卷调查
  70. func EtaTrialQuestionnaireCommit(pars EtaTrialQuestionnaireCommitReq) (res bool, err error) {
  71. url := fmt.Sprint(utils.CrmEtaServerUrl, "/api/eta_trial/questionnaire/commit")
  72. params, e := json.Marshal(pars)
  73. if e != nil {
  74. err = fmt.Errorf("data json marshal err: %s", e.Error())
  75. return
  76. }
  77. body := ioutil.NopCloser(strings.NewReader(string(params)))
  78. client := &http.Client{}
  79. req, e := http.NewRequest("POST", url, body)
  80. if e != nil {
  81. err = fmt.Errorf("http create request err: %s", e.Error())
  82. return
  83. }
  84. contentType := "application/json;charset=utf-8"
  85. req.Header.Set("Content-Type", contentType)
  86. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  87. resp, e := client.Do(req)
  88. if e != nil {
  89. err = fmt.Errorf("http client do err: %s", e.Error())
  90. return
  91. }
  92. defer func() {
  93. _ = resp.Body.Close()
  94. }()
  95. b, e := ioutil.ReadAll(resp.Body)
  96. if e != nil {
  97. err = fmt.Errorf("resp body read err: %s", e.Error())
  98. return
  99. }
  100. if len(b) == 0 {
  101. err = fmt.Errorf("resp body is empty")
  102. return
  103. }
  104. // 生产环境解密, 注意有个坑前后的双引号
  105. if utils.RunMode == "release" {
  106. str := string(b)
  107. str = strings.Trim(str, `"`)
  108. b = utils.DesBase64Decrypt([]byte(str), utils.CrmEtaServerDes3Key)
  109. }
  110. result := new(models.ResultData)
  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. if result.Code != 200 {
  116. err = fmt.Errorf("result: %s", string(b))
  117. return
  118. }
  119. res = true
  120. return
  121. }