You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.7 KiB

  1. package jg_push
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_jg_push.git/md"
  4. jg_push_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_jg_push.git/utils"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "github.com/syyongx/php2go"
  9. "github.com/tidwall/gjson"
  10. )
  11. func Send(appKey, appSecret string, param md.PushParam) (string, string, error) {
  12. if appKey == "" || appSecret == "" {
  13. return "", "", errors.New("配置未设置")
  14. }
  15. url := "https://api.jpush.cn/v3/push"
  16. var iosAlert = md.PushIosAlert{Title: param.Title, Body: param.Content}
  17. req := md.PushRequest{
  18. Platform: param.Platform,
  19. Audience: param.Audience,
  20. Notification: struct {
  21. Android md.PushAndroid `json:"android"`
  22. Ios md.PushIos `json:"ios"`
  23. }{
  24. Android: md.PushAndroid{
  25. Alert: param.Content,
  26. Extras: param.Extras,
  27. Title: param.Title,
  28. },
  29. Ios: md.PushIos{
  30. Alert: iosAlert,
  31. Extras: param.Extras,
  32. Sound: "default",
  33. },
  34. },
  35. Message: struct {
  36. Extras interface{} `json:"extras"`
  37. MsgContent string `json:"msg_content"`
  38. }{
  39. Extras: param.Extras,
  40. MsgContent: param.Content,
  41. },
  42. Options: struct {
  43. TimeToLive int `json:"time_to_live"`
  44. }{
  45. TimeToLive: 86400,
  46. },
  47. }
  48. key := php2go.Base64Encode(appKey + ":" + appSecret)
  49. headers := map[string]string{
  50. "Content-Type": "application/json",
  51. "Authorization": "Basic " + key,
  52. }
  53. b, err := json.Marshal(req)
  54. if err != nil {
  55. return "", "", err
  56. }
  57. res, err := jg_push_utils.CurlPost(url, b, headers)
  58. if err != nil {
  59. return "", "", err
  60. }
  61. fmt.Println(string(res))
  62. msgId := gjson.Get(string(res), "msg_id").String()
  63. if msgId == "0" || msgId == "" {
  64. return "", string(res), err
  65. }
  66. return msgId, string(res), nil
  67. }