base_public_api.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package public_api
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/services/alarm_msg"
  8. "hongze/hz_crm_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. postData := ReportChapterReq{
  29. ReportChapterId: reportChapterId,
  30. }
  31. postDataByte, err := json.Marshal(postData)
  32. if err != nil {
  33. alarm_msg.SendAlarmMsg("HandleVideoDecibel json.Marshal Err:"+err.Error(), 1)
  34. return err
  35. }
  36. _, err = post(utils.HandleVideoDecibelUrl, string(postDataByte))
  37. if err != nil {
  38. alarm_msg.SendAlarmMsg("HandleVideoDecibel json.Marshal Err:"+err.Error(), 1)
  39. return err
  40. }
  41. return
  42. }
  43. type BaseResponse struct {
  44. Ret int
  45. Msg string
  46. ErrMsg string
  47. ErrCode string
  48. Data interface{}
  49. Success bool `description:"true 执行成功,false 执行失败"`
  50. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  51. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  52. }
  53. func post(postUrl, postData string) (result *models.BaseResponse, err error) {
  54. body := ioutil.NopCloser(strings.NewReader(postData))
  55. client := &http.Client{}
  56. req, err := http.NewRequest("POST", postUrl, body)
  57. if err != nil {
  58. alarm_msg.SendAlarmMsg("post public_api http.NewRequest Err:"+err.Error(), 1)
  59. return
  60. }
  61. contentType := "application/json;charset=utf-8"
  62. req.Header.Set("Content-Type", contentType)
  63. req.Header.Set("Authorization", utils.SendTemplateMsgAuthorization)
  64. resp, err := client.Do(req)
  65. if err != nil {
  66. fmt.Println("http client.Do Err:" + err.Error())
  67. return
  68. }
  69. defer resp.Body.Close()
  70. b, err := ioutil.ReadAll(resp.Body)
  71. if err != nil {
  72. return
  73. }
  74. result = new(models.BaseResponse)
  75. err = json.Unmarshal(b, &result)
  76. if err != nil {
  77. return
  78. }
  79. if result.Ret != 200 {
  80. err = errors.New(string(b))
  81. }
  82. return
  83. }