123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- package http
- import (
- "context"
- "strconv"
- "time"
- "go-common/app/service/main/push/model"
- "go-common/library/ecode"
- "go-common/library/log"
- bm "go-common/library/net/http/blademaster"
- xtime "go-common/library/time"
- "go-common/library/xstr"
- )
- func push(c *bm.Context) {
- params := c.Request.Form
- appID, _ := strconv.ParseInt(params.Get("app_id"), 10, 64)
- if appID < 1 {
- c.JSON(nil, ecode.RequestErr)
- log.Error("app_id is wrong: %s", params.Get("app_id"))
- return
- }
- platform := params.Get("platform")
- alertTitle := params.Get("alert_title")
- if alertTitle != "" {
- res, err := pushSrv.Filter(c, alertTitle)
- if err == nil && res != alertTitle {
- log.Error("alertTitle(%s) contains invalid content", alertTitle)
- c.JSON(nil, ecode.PushSensitiveWordsErr)
- return
- }
- }
- alertBody := params.Get("alert_body")
- if alertBody == "" {
- c.JSON(nil, ecode.RequestErr)
- log.Error("alert_body is empty")
- return
- }
- res, err := pushSrv.Filter(c, alertBody)
- if err == nil && res != alertBody {
- log.Error("alertBody(%s) contains invalid content", alertBody)
- c.JSON(nil, ecode.PushSensitiveWordsErr)
- return
- }
- linkType, _ := strconv.Atoi(params.Get("link_type"))
- if linkType < 1 {
- c.JSON(nil, ecode.RequestErr)
- log.Error("link_type is wrong: %s", params.Get("link_type"))
- return
- }
- linkValue := params.Get("link_value")
- expireTime, _ := strconv.ParseInt(params.Get("expire_time"), 10, 64)
- if expireTime == 0 {
- expireTime = time.Now().Add(7 * 24 * time.Hour).Unix()
- }
- builds := params.Get("builds")
- sound, vibration := model.SwitchOn, model.SwitchOn
- if params.Get("sound") != "" {
- if sd, _ := strconv.Atoi(params.Get("sound")); sd == model.SwitchOff {
- sound = model.SwitchOff
- }
- }
- if params.Get("vibration") != "" {
- if vr, _ := strconv.Atoi(params.Get("vibration")); vr == model.SwitchOff {
- vibration = model.SwitchOff
- }
- }
- passThrough, _ := strconv.Atoi(params.Get("pass_through"))
- if passThrough != model.SwitchOn {
- passThrough = model.SwitchOff
- }
- mid := params.Get("mid")
- if mid == "" {
- log.Error("mid is empty", mid)
- c.JSON(nil, ecode.RequestErr)
- return
- }
- mids, err := xstr.SplitInts(mid)
- if err != nil {
- log.Error("parse mid(%s) error(%v)", mid, err)
- c.JSON(nil, ecode.RequestErr)
- return
- }
- task := &model.Task{
- ID: model.TempTaskID(),
- Job: model.JobName(time.Now().UnixNano(), alertBody, linkValue, ""),
- APPID: appID,
- Platform: model.SplitInts(platform),
- Title: alertTitle,
- Summary: alertBody,
- LinkType: int8(linkType),
- LinkValue: linkValue,
- PushTime: xtime.Time(time.Now().Unix()),
- ExpireTime: xtime.Time(expireTime),
- Build: model.ParseBuild(builds),
- Sound: sound,
- Vibration: vibration,
- PassThrough: passThrough,
- Group: params.Get("group"),
- ImageURL: params.Get("image_url"),
- }
- go pushSrv.Pushs(context.Background(), task, mids)
- c.JSON(nil, nil)
- }
- func singlePush(c *bm.Context) {
- params := c.Request.Form
- appID, _ := strconv.ParseInt(params.Get("app_id"), 10, 64)
- if appID < 1 {
- c.JSON(nil, ecode.RequestErr)
- log.Error("app_id is wrong: %s", params.Get("app_id"))
- return
- }
- businessID, _ := strconv.ParseInt(params.Get("business_id"), 10, 64)
- if businessID < 1 {
- c.JSON(nil, ecode.RequestErr)
- log.Error("business_id is wrong: %s", params.Get("business_id"))
- return
- }
- token := params.Get("token")
- if token == "" {
- c.JSON(nil, ecode.RequestErr)
- log.Error("token is empty")
- return
- }
- platform := params.Get("platform")
- alertTitle := params.Get("alert_title")
- if alertTitle != "" {
- res, err := pushSrv.Filter(c, alertTitle)
- if err == nil && res != alertTitle {
- log.Error("alertTitle(%s) contains invalid content", alertTitle)
- c.JSON(nil, ecode.PushSensitiveWordsErr)
- return
- }
- }
- alertBody := params.Get("alert_body")
- if alertBody == "" {
- c.JSON(nil, ecode.RequestErr)
- log.Error("alert_body is empty")
- return
- }
- res, err := pushSrv.Filter(c, alertBody)
- if err == nil && res != alertBody {
- log.Error("alertBody(%s) contains invalid content", alertBody)
- c.JSON(nil, ecode.PushSensitiveWordsErr)
- return
- }
- linkType, _ := strconv.Atoi(params.Get("link_type"))
- if linkType < 1 {
- c.JSON(nil, ecode.RequestErr)
- log.Error("link_type is wrong: %s", params.Get("link_type"))
- return
- }
- linkValue := params.Get("link_value")
- expireTime, _ := strconv.ParseInt(params.Get("expire_time"), 10, 64)
- if expireTime == 0 {
- expireTime = time.Now().Add(7 * 24 * time.Hour).Unix()
- }
- builds := params.Get("builds")
- sound, vibration := model.SwitchOn, model.SwitchOn
- if params.Get("sound") != "" {
- if sd, _ := strconv.Atoi(params.Get("sound")); sd == model.SwitchOff {
- sound = model.SwitchOff
- }
- }
- if params.Get("vibration") != "" {
- if vr, _ := strconv.Atoi(params.Get("vibration")); vr == model.SwitchOff {
- vibration = model.SwitchOff
- }
- }
- passThrough, _ := strconv.Atoi(params.Get("pass_through"))
- if passThrough != model.SwitchOn {
- passThrough = model.SwitchOff
- }
- mid, _ := strconv.ParseInt(params.Get("mid"), 10, 64)
- if mid < 1 {
- c.JSON(nil, ecode.RequestErr)
- log.Error("mid is wrong (%d)", mid)
- return
- }
- task := &model.Task{
- ID: model.TempTaskID(),
- Job: model.JobName(time.Now().UnixNano(), alertBody, linkValue, ""),
- BusinessID: businessID,
- APPID: appID,
- Platform: model.SplitInts(platform),
- Title: alertTitle,
- Summary: alertBody,
- LinkType: int8(linkType),
- LinkValue: linkValue,
- PushTime: xtime.Time(time.Now().Unix()),
- ExpireTime: xtime.Time(expireTime),
- Build: model.ParseBuild(builds),
- Sound: sound,
- Vibration: vibration,
- PassThrough: passThrough,
- Group: params.Get("group"),
- ImageURL: params.Get("image_url"),
- }
- go pushSrv.SinglePush(context.Background(), token, task, mid)
- c.JSON(nil, nil)
- }
- func testToken(c *bm.Context) {
- params := c.Request.Form
- appID, _ := strconv.ParseInt(params.Get("app_id"), 10, 64)
- if appID < 1 {
- c.JSON(nil, ecode.RequestErr)
- log.Error("app_id is wrong: %s", params.Get("app_id"))
- return
- }
- alertTitle := params.Get("alert_title")
- if alertTitle == "" {
- alertTitle = model.DefaultMessageTitle
- }
- res, err := pushSrv.Filter(c, alertTitle)
- if err == nil && res != alertTitle {
- log.Error("alertTitle(%s) contains invalid content", alertTitle)
- c.JSON(nil, ecode.PushSensitiveWordsErr)
- return
- }
- alertBody := params.Get("alert_body")
- if alertBody == "" {
- c.JSON(nil, ecode.RequestErr)
- log.Error("alert_body is empty")
- return
- }
- res, err = pushSrv.Filter(c, alertBody)
- if err == nil && res != alertBody {
- log.Error("alertBody(%s) contains invalid content", alertBody)
- c.JSON(nil, ecode.PushSensitiveWordsErr)
- return
- }
- token := params.Get("token")
- if token == "" {
- c.JSON(nil, ecode.RequestErr)
- log.Error("token is empty")
- return
- }
- linkType, _ := strconv.Atoi(params.Get("link_type"))
- if linkType < 1 {
- c.JSON(nil, ecode.RequestErr)
- log.Error("link_type is wrong: %s", params.Get("link_type"))
- return
- }
- linkValue := params.Get("link_value")
- expireTime, _ := strconv.ParseInt(params.Get("expire_time"), 10, 64)
- if expireTime == 0 {
- expireTime = time.Now().Add(7 * 24 * time.Hour).Unix()
- }
- sound, vibration := model.SwitchOn, model.SwitchOn
- if params.Get("sound") != "" {
- if sd, _ := strconv.Atoi(params.Get("sound")); sd == model.SwitchOff {
- sound = model.SwitchOff
- }
- }
- if params.Get("vibration") != "" {
- if vr, _ := strconv.Atoi(params.Get("vibration")); vr == model.SwitchOff {
- vibration = model.SwitchOff
- }
- }
- passThrough, _ := strconv.Atoi(params.Get("pass_through"))
- if passThrough != model.SwitchOn {
- passThrough = model.SwitchOff
- }
- info := &model.PushInfo{
- TaskID: model.TempTaskID(),
- Job: model.JobName(time.Now().UnixNano(), alertBody, linkValue, ""),
- APPID: appID,
- Title: alertTitle,
- Summary: alertBody,
- LinkType: int8(linkType),
- LinkValue: linkValue,
- ExpireTime: xtime.Time(expireTime),
- PassThrough: passThrough,
- Sound: sound,
- Vibration: vibration,
- ImageURL: params.Get("image_url"),
- }
- go pushSrv.TestToken(context.Background(), info, token)
- c.JSON(nil, nil)
- }
|