message.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package message
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "go-common/app/job/main/reply/conf"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. xhttp "go-common/library/net/http/blademaster"
  13. "go-common/library/xstr"
  14. )
  15. const (
  16. // 1 main site related
  17. // 1_1 reply related
  18. // 1_2 at related
  19. // 1_3 report related
  20. //_codeReplyGet = "1_1_1"
  21. _codeReplyDelete = "1_1_2"
  22. _codeReplyLike = "1_1_3"
  23. _codeAt = "1_2_1"
  24. _codeReport = "1_3_1"
  25. _dataTypeReply = 1
  26. _dataTypeAt = 2
  27. _dataTypeLike = 3
  28. _dataTypeSystem = 4
  29. _notifyTypeCnt = 2
  30. )
  31. // Dao message dao.
  32. type Dao struct {
  33. httpCli *xhttp.Client
  34. apiURL string
  35. }
  36. // NewMessageDao new a message dao and return.
  37. func NewMessageDao(c *conf.Config) *Dao {
  38. return &Dao{
  39. httpCli: xhttp.NewClient(c.HTTPClient),
  40. apiURL: c.Host.Message + "/api/notify/send.user.notify.do",
  41. }
  42. }
  43. // Like send a like message.
  44. func (dao *Dao) Like(c context.Context, mid, tomid int64, title, msg, extraInfo string, now time.Time) (err error) {
  45. return dao.send(c, _codeReplyLike, "", title, msg, _dataTypeLike, mid, []int64{tomid}, extraInfo, now.Unix())
  46. }
  47. // Reply send a reply message.
  48. func (dao *Dao) Reply(c context.Context, mc, resID string, mid, tomid int64, title, msg, extraInfo string, now time.Time) (err error) {
  49. return dao.send(c, mc, resID, title, msg, _dataTypeReply, mid, []int64{tomid}, extraInfo, now.Unix())
  50. }
  51. // DeleteReply send delete reply message.
  52. func (dao *Dao) DeleteReply(c context.Context, mid int64, title, msg string, now time.Time) (err error) {
  53. return dao.send(c, _codeReplyDelete, "", title, msg, _dataTypeSystem, 0, []int64{mid}, "", now.Unix())
  54. }
  55. // At send a at message.
  56. func (dao *Dao) At(c context.Context, mid int64, mids []int64, title, msg, extraInfo string, now time.Time) (err error) {
  57. if len(mids) == 0 {
  58. return
  59. }
  60. return dao.send(c, _codeAt, "", title, msg, _dataTypeAt, mid, mids, extraInfo, now.Unix())
  61. }
  62. // AcceptReport send accept report message.
  63. func (dao *Dao) AcceptReport(c context.Context, mid int64, title, msg string, now time.Time) (err error) {
  64. return dao.send(c, _codeReport, "", title, msg, _dataTypeSystem, 0, []int64{mid}, "", now.Unix())
  65. }
  66. // System send a system message.
  67. func (dao *Dao) System(c context.Context, mc, resID string, mid int64, title, msg, info string, now time.Time) (err error) {
  68. return dao.send(c, mc, resID, title, msg, _dataTypeSystem, 0, []int64{mid}, info, now.Unix())
  69. }
  70. func (dao *Dao) send(c context.Context, mc, resID, title, msg string, tp int, pub int64, mids []int64, info string, ts int64) (err error) {
  71. params := url.Values{}
  72. params.Set("type", "json")
  73. params.Set("source", "1")
  74. params.Set("mc", mc)
  75. params.Set("title", title)
  76. params.Set("data_type", strconv.Itoa(tp))
  77. params.Set("context", msg)
  78. params.Set("mid_list", xstr.JoinInts(mids))
  79. params.Set("publisher", strconv.FormatInt(pub, 10))
  80. params.Set("ext_info", info)
  81. if resID != "" {
  82. params.Set("notify_type", fmt.Sprint(_notifyTypeCnt))
  83. params.Set("res_id", resID)
  84. }
  85. var res struct {
  86. Code int `json:"code"`
  87. }
  88. if err = dao.httpCli.Post(c, dao.apiURL, "", params, &res); err != nil {
  89. log.Error("message url(%s) error(%v)", dao.apiURL+"?"+params.Encode(), err)
  90. return
  91. }
  92. if res.Code != ecode.OK.Code() {
  93. log.Error("message url(%s) error(%v)", dao.apiURL+"?"+params.Encode(), res.Code)
  94. err = fmt.Errorf("message send failed")
  95. return
  96. }
  97. log.Info("sendmessage success:%v;code:%d", params, res.Code)
  98. if tp != _dataTypeSystem {
  99. params.Set("mobi_app", "android_i")
  100. if tp == _dataTypeAt {
  101. params.Set("title", converAt(title))
  102. } else if tp == _dataTypeLike {
  103. params.Set("context", convertMsg(msg))
  104. } else if tp == _dataTypeReply {
  105. params.Set("title", convertMsg(title))
  106. params.Set("context", convertMsg(msg))
  107. }
  108. var res1 struct {
  109. Code int `json:"code"`
  110. }
  111. if err = dao.httpCli.Post(c, dao.apiURL, "", params, &res1); err != nil {
  112. log.Error("message url(%s) error(%v)", dao.apiURL+"?"+params.Encode(), err)
  113. return
  114. }
  115. if res1.Code != ecode.OK.Code() {
  116. log.Error("message url(%s) error(%v)", dao.apiURL+"?"+params.Encode(), res1.Code)
  117. err = fmt.Errorf("message send failed")
  118. return
  119. }
  120. log.Info("send international message success:%v;code:%d", params, res1.Code)
  121. }
  122. return
  123. }
  124. func converAt(title string) string {
  125. return strings.Replace(title, "评论中@了你", "評論中@了你", -1)
  126. }
  127. func convertMsg(msg string) string {
  128. rmsg := []rune(msg)
  129. for i, c := range rmsg {
  130. switch c {
  131. case '评':
  132. rmsg[i] = '評'
  133. case '论':
  134. rmsg[i] = '論'
  135. case '赞':
  136. rmsg[i] = '讚'
  137. case '条':
  138. rmsg[i] = '條'
  139. case '专':
  140. rmsg[i] = '專'
  141. case '栏':
  142. rmsg[i] = '欄'
  143. case '数':
  144. rmsg[i] = '數'
  145. case '达':
  146. rmsg[i] = '達'
  147. case '应':
  148. rmsg[i] = '應'
  149. case '点':
  150. rmsg[i] = '點'
  151. case '击':
  152. rmsg[i] = '擊'
  153. default:
  154. continue
  155. }
  156. }
  157. return string(rmsg)
  158. }