base_public_api.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package public_api
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "eta/eta_api/models"
  7. "eta/eta_api/services/alarm_msg"
  8. "eta/eta_api/utils"
  9. "io/ioutil"
  10. "net/http"
  11. "strings"
  12. )
  13. // SendTemplateMsg 发送微信模板消息
  14. func SendTemplateMsg(postData string) (err error) {
  15. _, err = post(utils.SendWxTemplateMsgUrl, postData)
  16. if err != nil {
  17. alarm_msg.SendAlarmMsg("SendTemplateMsg http.NewRequest Err:"+err.Error(), 1)
  18. return
  19. }
  20. return
  21. }
  22. // ReportChapterReq 报告章节id
  23. type ReportChapterReq struct {
  24. ReportChapterId int `description:"报告章节ID"`
  25. }
  26. // HandleVideoDecibel 处理音频
  27. func HandleVideoDecibel(reportChapterId int) (err error) {
  28. if utils.HandleVideoDecibelUrl == `` {
  29. // 处理音频的地址未配置的话,直接返回
  30. return
  31. }
  32. postData := ReportChapterReq{
  33. ReportChapterId: reportChapterId,
  34. }
  35. postDataByte, err := json.Marshal(postData)
  36. if err != nil {
  37. alarm_msg.SendAlarmMsg("HandleVideoDecibel json.Marshal Err:"+err.Error(), 1)
  38. return err
  39. }
  40. _, err = post(utils.HandleVideoDecibelUrl, string(postDataByte))
  41. if err != nil {
  42. alarm_msg.SendAlarmMsg("HandleVideoDecibel json.Marshal Err:"+err.Error(), 1)
  43. return err
  44. }
  45. return
  46. }
  47. type BaseResponse struct {
  48. Ret int
  49. Msg string
  50. ErrMsg string
  51. ErrCode string
  52. Data interface{}
  53. Success bool `description:"true 执行成功,false 执行失败"`
  54. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  55. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  56. }
  57. func post(postUrl, postData string) (result *models.BaseResponse, err error) {
  58. body := ioutil.NopCloser(strings.NewReader(postData))
  59. client := &http.Client{}
  60. req, err := http.NewRequest("POST", postUrl, body)
  61. if err != nil {
  62. alarm_msg.SendAlarmMsg("post public_api http.NewRequest Err:"+err.Error(), 1)
  63. return
  64. }
  65. contentType := "application/json;charset=utf-8"
  66. req.Header.Set("Content-Type", contentType)
  67. req.Header.Set("Authorization", utils.SendTemplateMsgAuthorization)
  68. resp, err := client.Do(req)
  69. if err != nil {
  70. fmt.Println("http client.Do Err:" + err.Error())
  71. return
  72. }
  73. defer resp.Body.Close()
  74. b, err := ioutil.ReadAll(resp.Body)
  75. if err != nil {
  76. return
  77. }
  78. result = new(models.BaseResponse)
  79. err = json.Unmarshal(b, &result)
  80. if err != nil {
  81. return
  82. }
  83. if result.Ret != 200 {
  84. err = errors.New(string(b))
  85. }
  86. return
  87. }