send.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package utility
  2. import (
  3. "app.yhyue.com/moapp/jybase/date"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/gogf/gf/v2/frame/g"
  8. "io/ioutil"
  9. "net/http"
  10. "net/url"
  11. )
  12. type StationMessage struct {
  13. Action string
  14. Addr string
  15. CallPlatform string
  16. }
  17. type MessageParam struct {
  18. UserIds string // 用户mongo库id 用户userId用英文逗号拼接
  19. SendTime string // 发送时间 暂时无用 (2022-03-31 09:08:08 仅发送模式为定时时有效)
  20. Title string // 通知消息 消息标题
  21. Content string // 消息内容
  22. Link string // pc消息链接
  23. AndroidUrl string // 安卓消息链接
  24. IosUrl string // IOS消息链接
  25. WeChatUrl string // 微信消息链接
  26. SendMode int // 发送模式 1- 定时 2-实时 (所调接口未支持定时)
  27. PositionIds string // 职位id 与userIds 对应
  28. MsgType int // message_class 里的消息分类
  29. }
  30. type MessageRet struct {
  31. Status string `json:"status"`
  32. Info string `json:"info"`
  33. Data struct {
  34. Status int `json:"status"`
  35. } `json:"data"`
  36. }
  37. func NewStationMessage(addr string, action string, callPlatform string) *StationMessage {
  38. return &StationMessage{
  39. Addr: addr,
  40. Action: action,
  41. CallPlatform: callPlatform,
  42. }
  43. }
  44. func (s *StationMessage) SendStationMessages(ctx context.Context, p MessageParam) {
  45. var (
  46. ret MessageRet
  47. cont []byte
  48. )
  49. parms := fmt.Sprintf("_action=%s&userIds=%s&msgType=%v&title=%s&content=%s&link=%s&sendMode=2&sendTime=%s&androidUrl=%s&iosUrl=%s&weChatUrl=%s&_token=12311&reqSource=1&callPlatform=%s&positionIds=%s&menuname=message",
  50. s.Action, p.UserIds, p.MsgType, p.Title, p.Content, p.Link, date.NowFormat(date.Date_Short_Layout), p.AndroidUrl, p.IosUrl, p.WeChatUrl, s.CallPlatform, p.PositionIds)
  51. paramsEncode := url.PathEscape(parms)
  52. url := fmt.Sprintf("%s?%s", s.Addr, paramsEncode)
  53. resp, err := http.Get(url)
  54. if err != nil {
  55. g.Log().Error(ctx, "发送消息失败:", err)
  56. return
  57. }
  58. defer resp.Body.Close()
  59. cont, err = ioutil.ReadAll(resp.Body)
  60. if err != nil {
  61. g.Log().Error(ctx, "发送消息错误信息:", string(cont), err)
  62. return
  63. }
  64. err = json.Unmarshal(cont, &ret)
  65. if err != nil {
  66. g.Log().Error(ctx, "发送消息反序列化错误信息:", err)
  67. return
  68. }
  69. if ret.Data.Status != 1 {
  70. g.Log().Error(ctx, "发送消息失败:Status != 1", ret)
  71. }
  72. }