push.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package http
  2. import (
  3. "context"
  4. "strconv"
  5. "time"
  6. "go-common/app/service/main/push/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. xtime "go-common/library/time"
  11. "go-common/library/xstr"
  12. )
  13. func push(c *bm.Context) {
  14. params := c.Request.Form
  15. appID, _ := strconv.ParseInt(params.Get("app_id"), 10, 64)
  16. if appID < 1 {
  17. c.JSON(nil, ecode.RequestErr)
  18. log.Error("app_id is wrong: %s", params.Get("app_id"))
  19. return
  20. }
  21. platform := params.Get("platform")
  22. alertTitle := params.Get("alert_title")
  23. if alertTitle != "" {
  24. res, err := pushSrv.Filter(c, alertTitle)
  25. if err == nil && res != alertTitle {
  26. log.Error("alertTitle(%s) contains invalid content", alertTitle)
  27. c.JSON(nil, ecode.PushSensitiveWordsErr)
  28. return
  29. }
  30. }
  31. alertBody := params.Get("alert_body")
  32. if alertBody == "" {
  33. c.JSON(nil, ecode.RequestErr)
  34. log.Error("alert_body is empty")
  35. return
  36. }
  37. res, err := pushSrv.Filter(c, alertBody)
  38. if err == nil && res != alertBody {
  39. log.Error("alertBody(%s) contains invalid content", alertBody)
  40. c.JSON(nil, ecode.PushSensitiveWordsErr)
  41. return
  42. }
  43. linkType, _ := strconv.Atoi(params.Get("link_type"))
  44. if linkType < 1 {
  45. c.JSON(nil, ecode.RequestErr)
  46. log.Error("link_type is wrong: %s", params.Get("link_type"))
  47. return
  48. }
  49. linkValue := params.Get("link_value")
  50. expireTime, _ := strconv.ParseInt(params.Get("expire_time"), 10, 64)
  51. if expireTime == 0 {
  52. expireTime = time.Now().Add(7 * 24 * time.Hour).Unix()
  53. }
  54. builds := params.Get("builds")
  55. sound, vibration := model.SwitchOn, model.SwitchOn
  56. if params.Get("sound") != "" {
  57. if sd, _ := strconv.Atoi(params.Get("sound")); sd == model.SwitchOff {
  58. sound = model.SwitchOff
  59. }
  60. }
  61. if params.Get("vibration") != "" {
  62. if vr, _ := strconv.Atoi(params.Get("vibration")); vr == model.SwitchOff {
  63. vibration = model.SwitchOff
  64. }
  65. }
  66. passThrough, _ := strconv.Atoi(params.Get("pass_through"))
  67. if passThrough != model.SwitchOn {
  68. passThrough = model.SwitchOff
  69. }
  70. mid := params.Get("mid")
  71. if mid == "" {
  72. log.Error("mid is empty", mid)
  73. c.JSON(nil, ecode.RequestErr)
  74. return
  75. }
  76. mids, err := xstr.SplitInts(mid)
  77. if err != nil {
  78. log.Error("parse mid(%s) error(%v)", mid, err)
  79. c.JSON(nil, ecode.RequestErr)
  80. return
  81. }
  82. task := &model.Task{
  83. ID: model.TempTaskID(),
  84. Job: model.JobName(time.Now().UnixNano(), alertBody, linkValue, ""),
  85. APPID: appID,
  86. Platform: model.SplitInts(platform),
  87. Title: alertTitle,
  88. Summary: alertBody,
  89. LinkType: int8(linkType),
  90. LinkValue: linkValue,
  91. PushTime: xtime.Time(time.Now().Unix()),
  92. ExpireTime: xtime.Time(expireTime),
  93. Build: model.ParseBuild(builds),
  94. Sound: sound,
  95. Vibration: vibration,
  96. PassThrough: passThrough,
  97. Group: params.Get("group"),
  98. ImageURL: params.Get("image_url"),
  99. }
  100. go pushSrv.Pushs(context.Background(), task, mids)
  101. c.JSON(nil, nil)
  102. }
  103. func singlePush(c *bm.Context) {
  104. params := c.Request.Form
  105. appID, _ := strconv.ParseInt(params.Get("app_id"), 10, 64)
  106. if appID < 1 {
  107. c.JSON(nil, ecode.RequestErr)
  108. log.Error("app_id is wrong: %s", params.Get("app_id"))
  109. return
  110. }
  111. businessID, _ := strconv.ParseInt(params.Get("business_id"), 10, 64)
  112. if businessID < 1 {
  113. c.JSON(nil, ecode.RequestErr)
  114. log.Error("business_id is wrong: %s", params.Get("business_id"))
  115. return
  116. }
  117. token := params.Get("token")
  118. if token == "" {
  119. c.JSON(nil, ecode.RequestErr)
  120. log.Error("token is empty")
  121. return
  122. }
  123. platform := params.Get("platform")
  124. alertTitle := params.Get("alert_title")
  125. if alertTitle != "" {
  126. res, err := pushSrv.Filter(c, alertTitle)
  127. if err == nil && res != alertTitle {
  128. log.Error("alertTitle(%s) contains invalid content", alertTitle)
  129. c.JSON(nil, ecode.PushSensitiveWordsErr)
  130. return
  131. }
  132. }
  133. alertBody := params.Get("alert_body")
  134. if alertBody == "" {
  135. c.JSON(nil, ecode.RequestErr)
  136. log.Error("alert_body is empty")
  137. return
  138. }
  139. res, err := pushSrv.Filter(c, alertBody)
  140. if err == nil && res != alertBody {
  141. log.Error("alertBody(%s) contains invalid content", alertBody)
  142. c.JSON(nil, ecode.PushSensitiveWordsErr)
  143. return
  144. }
  145. linkType, _ := strconv.Atoi(params.Get("link_type"))
  146. if linkType < 1 {
  147. c.JSON(nil, ecode.RequestErr)
  148. log.Error("link_type is wrong: %s", params.Get("link_type"))
  149. return
  150. }
  151. linkValue := params.Get("link_value")
  152. expireTime, _ := strconv.ParseInt(params.Get("expire_time"), 10, 64)
  153. if expireTime == 0 {
  154. expireTime = time.Now().Add(7 * 24 * time.Hour).Unix()
  155. }
  156. builds := params.Get("builds")
  157. sound, vibration := model.SwitchOn, model.SwitchOn
  158. if params.Get("sound") != "" {
  159. if sd, _ := strconv.Atoi(params.Get("sound")); sd == model.SwitchOff {
  160. sound = model.SwitchOff
  161. }
  162. }
  163. if params.Get("vibration") != "" {
  164. if vr, _ := strconv.Atoi(params.Get("vibration")); vr == model.SwitchOff {
  165. vibration = model.SwitchOff
  166. }
  167. }
  168. passThrough, _ := strconv.Atoi(params.Get("pass_through"))
  169. if passThrough != model.SwitchOn {
  170. passThrough = model.SwitchOff
  171. }
  172. mid, _ := strconv.ParseInt(params.Get("mid"), 10, 64)
  173. if mid < 1 {
  174. c.JSON(nil, ecode.RequestErr)
  175. log.Error("mid is wrong (%d)", mid)
  176. return
  177. }
  178. task := &model.Task{
  179. ID: model.TempTaskID(),
  180. Job: model.JobName(time.Now().UnixNano(), alertBody, linkValue, ""),
  181. BusinessID: businessID,
  182. APPID: appID,
  183. Platform: model.SplitInts(platform),
  184. Title: alertTitle,
  185. Summary: alertBody,
  186. LinkType: int8(linkType),
  187. LinkValue: linkValue,
  188. PushTime: xtime.Time(time.Now().Unix()),
  189. ExpireTime: xtime.Time(expireTime),
  190. Build: model.ParseBuild(builds),
  191. Sound: sound,
  192. Vibration: vibration,
  193. PassThrough: passThrough,
  194. Group: params.Get("group"),
  195. ImageURL: params.Get("image_url"),
  196. }
  197. go pushSrv.SinglePush(context.Background(), token, task, mid)
  198. c.JSON(nil, nil)
  199. }
  200. func testToken(c *bm.Context) {
  201. params := c.Request.Form
  202. appID, _ := strconv.ParseInt(params.Get("app_id"), 10, 64)
  203. if appID < 1 {
  204. c.JSON(nil, ecode.RequestErr)
  205. log.Error("app_id is wrong: %s", params.Get("app_id"))
  206. return
  207. }
  208. alertTitle := params.Get("alert_title")
  209. if alertTitle == "" {
  210. alertTitle = model.DefaultMessageTitle
  211. }
  212. res, err := pushSrv.Filter(c, alertTitle)
  213. if err == nil && res != alertTitle {
  214. log.Error("alertTitle(%s) contains invalid content", alertTitle)
  215. c.JSON(nil, ecode.PushSensitiveWordsErr)
  216. return
  217. }
  218. alertBody := params.Get("alert_body")
  219. if alertBody == "" {
  220. c.JSON(nil, ecode.RequestErr)
  221. log.Error("alert_body is empty")
  222. return
  223. }
  224. res, err = pushSrv.Filter(c, alertBody)
  225. if err == nil && res != alertBody {
  226. log.Error("alertBody(%s) contains invalid content", alertBody)
  227. c.JSON(nil, ecode.PushSensitiveWordsErr)
  228. return
  229. }
  230. token := params.Get("token")
  231. if token == "" {
  232. c.JSON(nil, ecode.RequestErr)
  233. log.Error("token is empty")
  234. return
  235. }
  236. linkType, _ := strconv.Atoi(params.Get("link_type"))
  237. if linkType < 1 {
  238. c.JSON(nil, ecode.RequestErr)
  239. log.Error("link_type is wrong: %s", params.Get("link_type"))
  240. return
  241. }
  242. linkValue := params.Get("link_value")
  243. expireTime, _ := strconv.ParseInt(params.Get("expire_time"), 10, 64)
  244. if expireTime == 0 {
  245. expireTime = time.Now().Add(7 * 24 * time.Hour).Unix()
  246. }
  247. sound, vibration := model.SwitchOn, model.SwitchOn
  248. if params.Get("sound") != "" {
  249. if sd, _ := strconv.Atoi(params.Get("sound")); sd == model.SwitchOff {
  250. sound = model.SwitchOff
  251. }
  252. }
  253. if params.Get("vibration") != "" {
  254. if vr, _ := strconv.Atoi(params.Get("vibration")); vr == model.SwitchOff {
  255. vibration = model.SwitchOff
  256. }
  257. }
  258. passThrough, _ := strconv.Atoi(params.Get("pass_through"))
  259. if passThrough != model.SwitchOn {
  260. passThrough = model.SwitchOff
  261. }
  262. info := &model.PushInfo{
  263. TaskID: model.TempTaskID(),
  264. Job: model.JobName(time.Now().UnixNano(), alertBody, linkValue, ""),
  265. APPID: appID,
  266. Title: alertTitle,
  267. Summary: alertBody,
  268. LinkType: int8(linkType),
  269. LinkValue: linkValue,
  270. ExpireTime: xtime.Time(expireTime),
  271. PassThrough: passThrough,
  272. Sound: sound,
  273. Vibration: vibration,
  274. ImageURL: params.Get("image_url"),
  275. }
  276. go pushSrv.TestToken(context.Background(), info, token)
  277. c.JSON(nil, nil)
  278. }