Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["server_test.go"],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/service/main/push/api/gorpc:go_default_library",
"//app/service/main/push/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["server.go"],
importpath = "go-common/app/service/main/push/server/gorpc",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/push/conf:go_default_library",
"//app/service/main/push/model:go_default_library",
"//app/service/main/push/service:go_default_library",
"//library/net/rpc:go_default_library",
"//library/net/rpc/context:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,158 @@
package gorpc
import (
"go-common/app/service/main/push/conf"
"go-common/app/service/main/push/model"
"go-common/app/service/main/push/service"
"go-common/library/net/rpc"
"go-common/library/net/rpc/context"
)
// RPC rpc.
type RPC struct {
s *service.Service
}
// New .
func New(c *conf.Config, s *service.Service) (svc *rpc.Server) {
r := &RPC{s: s}
svc = rpc.NewServer(c.RPCServer)
if err := svc.Register(r); err != nil {
panic(err)
}
return
}
// Ping checks connection success.
func (r *RPC) Ping(c context.Context, arg *struct{}, res *struct{}) (err error) {
return
}
// Auth check connection success.
func (r *RPC) Auth(c context.Context, arg *rpc.Auth, res *struct{}) (err error) {
return
}
// AddReport adds report by mid.
func (r *RPC) AddReport(c context.Context, arg *model.ArgReport, res *struct{}) (err error) {
report := &model.Report{
APPID: arg.APPID,
PlatformID: arg.PlatformID,
Mid: arg.Mid,
Buvid: arg.Buvid,
DeviceToken: arg.DeviceToken,
Build: arg.Build,
TimeZone: arg.TimeZone,
NotifySwitch: arg.NotifySwitch,
DeviceBrand: arg.DeviceBrand,
DeviceModel: arg.DeviceModel,
OSVersion: arg.OSVersion,
Extra: arg.Extra,
}
err = r.s.AddReport(c, report)
return
}
// DelInvalidReports deletes invalid reports.
func (r *RPC) DelInvalidReports(c context.Context, arg *model.ArgDelInvalidReport, res *struct{}) (err error) {
err = r.s.DelInvalidReports(c, arg.Type)
return
}
// DelReport deletes report.
func (r *RPC) DelReport(c context.Context, arg *model.ArgReport, res *struct{}) (err error) {
err = r.s.DelReport(c, arg.APPID, arg.Mid, arg.DeviceToken)
return
}
// AddCallback adds callback data.
func (r *RPC) AddCallback(c context.Context, arg *model.ArgCallback, res *struct{}) (err error) {
cb := &model.Callback{
Task: arg.Task,
APP: arg.APP,
Platform: arg.Platform,
Mid: arg.Mid,
Pid: arg.Pid,
Token: arg.Token,
Buvid: arg.Buvid,
Click: arg.Click,
Extra: arg.Extra,
}
err = r.s.AddCallback(c, cb)
return
}
// AddReportCache adds report cache.
func (r *RPC) AddReportCache(c context.Context, arg *model.ArgReport, res *struct{}) (err error) {
report := &model.Report{
ID: arg.ID,
APPID: arg.APPID,
PlatformID: arg.PlatformID,
Mid: arg.Mid,
Buvid: arg.Buvid,
DeviceToken: arg.DeviceToken,
Build: arg.Build,
TimeZone: arg.TimeZone,
NotifySwitch: arg.NotifySwitch,
DeviceBrand: arg.DeviceBrand,
DeviceModel: arg.DeviceModel,
OSVersion: arg.OSVersion,
Extra: arg.Extra,
}
err = r.s.AddReportCache(c, report)
return
}
// AddUserReportCache adds user report cache.
func (r *RPC) AddUserReportCache(c context.Context, arg *model.ArgUserReports, res *struct{}) (err error) {
err = r.s.AddUserReportCache(c, arg.Mid, arg.Reports)
return
}
// Setting gets user push switch setting.
func (r *RPC) Setting(c context.Context, arg *model.ArgMid, res *map[int]int) (err error) {
*res, err = r.s.Setting(c, arg.Mid)
return
}
// SetSetting sets user push switch setting.
func (r *RPC) SetSetting(c context.Context, arg *model.ArgSetting, res *struct{}) (err error) {
err = r.s.SetSetting(c, arg.Mid, arg.Type, arg.Value)
return
}
// AddMidProgress add mid count number to task progress field
func (r *RPC) AddMidProgress(c context.Context, arg *model.ArgMidProgress, res *struct{}) (err error) {
err = r.s.AddMidProgress(c, arg.Task, arg.MidTotal, arg.MidValid)
return
}
// AddTokenCache add token cache
func (r *RPC) AddTokenCache(ctx context.Context, arg *model.ArgReport, res *struct{}) (err error) {
report := &model.Report{
APPID: arg.APPID,
PlatformID: arg.PlatformID,
Mid: arg.Mid,
Buvid: arg.Buvid,
DeviceToken: arg.DeviceToken,
Build: arg.Build,
TimeZone: arg.TimeZone,
NotifySwitch: arg.NotifySwitch,
DeviceBrand: arg.DeviceBrand,
DeviceModel: arg.DeviceModel,
OSVersion: arg.OSVersion,
Extra: arg.Extra,
}
err = r.s.AddTokenCache(ctx, report)
return
}
// AddTokensCache add token cache
func (r *RPC) AddTokensCache(ctx context.Context, arg *model.ArgReports, res *struct{}) (err error) {
rs := make(map[string]*model.Report, len(arg.Reports))
for _, v := range arg.Reports {
rs[v.DeviceToken] = v
}
err = r.s.AddTokensCache(ctx, rs)
return
}

View File

@@ -0,0 +1,92 @@
package gorpc
import (
"context"
"testing"
pushsrv "go-common/app/service/main/push/api/gorpc"
"go-common/app/service/main/push/model"
. "github.com/smartystreets/goconvey/convey"
)
var (
// _noArg = &struct{}{}
// _noRes = &struct{}{}
ctx = context.TODO()
)
func WithRPC(f func(client *pushsrv.Service)) func() {
return func() {
client := pushsrv.New(nil)
f(client)
}
}
func Test_AddReport(t *testing.T) {
Convey("AddReport", t, WithRPC(func(client *pushsrv.Service) {
arg := &model.ArgReport{
APPID: 1,
PlatformID: 1,
Mid: 1,
Buvid: "b",
DeviceToken: "d",
Build: 8080,
TimeZone: 8,
NotifySwitch: 1,
}
err := client.AddReport(ctx, arg)
So(err, ShouldBeNil)
}))
}
func Test_Setting(t *testing.T) {
Convey("get setting", t, WithRPC(func(client *pushsrv.Service) {
arg := &model.ArgMid{Mid: 88888888}
res, err := client.Setting(ctx, arg)
So(err, ShouldBeNil)
t.Logf("setting(%v)", res)
}))
Convey("set setting", t, WithRPC(func(client *pushsrv.Service) {
arg := &model.ArgSetting{Mid: 999999999, Type: model.UserSettingArchive, Value: model.SwitchOff}
err := client.SetSetting(ctx, arg)
So(err, ShouldBeNil)
argMid := &model.ArgMid{Mid: 999999999}
res, err := client.Setting(ctx, argMid)
So(err, ShouldBeNil)
t.Logf("setting(%v)", res)
}))
}
func TestAddUserReportCache(t *testing.T) {
Convey("AddUserReportCache", t, WithRPC(func(client *pushsrv.Service) {
arg := &model.ArgUserReports{Mid: 123456, Reports: []*model.Report{{
APPID: 1,
PlatformID: 1,
Mid: 123456,
DeviceToken: "dtrpc",
}}}
err := client.AddUserReportCache(context.Background(), arg)
So(err, ShouldBeNil)
}))
}
func TestAddTokensCache(t *testing.T) {
Convey("AddTokensCache", t, WithRPC(func(client *pushsrv.Service) {
arg := &model.ArgReports{Reports: []*model.Report{{
APPID: 1,
PlatformID: 1,
Mid: 123456,
DeviceToken: "dtrpc",
}, {
APPID: 1,
PlatformID: 1,
Mid: 123456,
DeviceToken: "dtrpc2",
}}}
err := client.AddTokensCache(context.Background(), arg)
So(err, ShouldBeNil)
}))
}

View File

@@ -0,0 +1,34 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["server.go"],
importpath = "go-common/app/service/main/push/server/grpc",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/push/api/grpc/v1:go_default_library",
"//app/service/main/push/model:go_default_library",
"//app/service/main/push/service:go_default_library",
"//library/net/rpc/warden:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,213 @@
package grpc
import (
"context"
pb "go-common/app/service/main/push/api/grpc/v1"
"go-common/app/service/main/push/model"
"go-common/app/service/main/push/service"
"go-common/library/net/rpc/warden"
)
// New warden rpc server
func New(c *warden.ServerConfig, svc *service.Service) *warden.Server {
svr := warden.NewServer(c)
pb.RegisterPushServer(svr.Server(), &server{svc: svc})
ws, err := svr.Start()
if err != nil {
panic(err)
}
return ws
}
type server struct {
svc *service.Service
}
// AddReport 上报
func (s *server) AddReport(ctx context.Context, req *pb.AddReportRequest) (reply *pb.AddReportReply, err error) {
reply = new(pb.AddReportReply)
if req.Report == nil {
return
}
r := req.Report
err = s.svc.AddReport(ctx, &model.Report{
APPID: int64(r.APPID),
PlatformID: int(r.PlatformID),
Mid: r.Mid,
Buvid: r.Buvid,
DeviceToken: r.DeviceToken,
Build: int(r.Build),
TimeZone: int(r.TimeZone),
NotifySwitch: int(r.NotifySwitch),
DeviceBrand: r.DeviceBrand,
DeviceModel: r.DeviceModel,
OSVersion: r.OSVersion,
Extra: r.Extra,
})
return
}
// DelReport 删除上报
func (s *server) DelReport(ctx context.Context, req *pb.DelReportRequest) (reply *pb.DelReportReply, err error) {
reply = new(pb.DelReportReply)
err = s.svc.DelReport(ctx, int64(req.APPID), req.Mid, req.DeviceToken)
return
}
// DelInvalidReports 删除无效token
func (s *server) DelInvalidReports(ctx context.Context, req *pb.DelInvalidReportsRequest) (reply *pb.DelInvalidReportsReply, err error) {
reply = new(pb.DelInvalidReportsReply)
err = s.svc.DelInvalidReports(ctx, int(req.Type))
return
}
// AddReportCache 上报缓存
func (s *server) AddReportCache(ctx context.Context, req *pb.AddReportCacheRequest) (reply *pb.AddReportCacheReply, err error) {
reply = new(pb.AddReportCacheReply)
if req.Report == nil {
return
}
r := req.Report
err = s.svc.AddReportCache(ctx, &model.Report{
APPID: int64(r.APPID),
PlatformID: int(r.PlatformID),
Mid: r.Mid,
Buvid: r.Buvid,
DeviceToken: r.DeviceToken,
Build: int(r.Build),
TimeZone: int(r.TimeZone),
NotifySwitch: int(r.NotifySwitch),
DeviceBrand: r.DeviceBrand,
DeviceModel: r.DeviceModel,
OSVersion: r.OSVersion,
Extra: r.Extra,
})
return
}
// AddUserReportCache 用户的上报缓存
func (s *server) AddUserReportCache(ctx context.Context, req *pb.AddUserReportCacheRequest) (reply *pb.AddUserReportCacheReply, err error) {
reply = new(pb.AddUserReportCacheReply)
var reports []*model.Report
for _, r := range req.Reports {
reports = append(reports, &model.Report{
APPID: int64(r.APPID),
PlatformID: int(r.PlatformID),
Mid: r.Mid,
Buvid: r.Buvid,
DeviceToken: r.DeviceToken,
Build: int(r.Build),
TimeZone: int(r.TimeZone),
NotifySwitch: int(r.NotifySwitch),
DeviceBrand: r.DeviceBrand,
DeviceModel: r.DeviceModel,
OSVersion: r.OSVersion,
Extra: r.Extra,
})
}
err = s.svc.AddUserReportCache(ctx, req.Mid, reports)
return
}
// AddTokenCache token缓存
func (s *server) AddTokenCache(ctx context.Context, req *pb.AddTokenCacheRequest) (reply *pb.AddTokenCacheReply, err error) {
reply = new(pb.AddTokenCacheReply)
if req.Report == nil {
return
}
r := req.Report
err = s.svc.AddTokenCache(ctx, &model.Report{
APPID: int64(r.APPID),
PlatformID: int(r.PlatformID),
Mid: r.Mid,
Buvid: r.Buvid,
DeviceToken: r.DeviceToken,
Build: int(r.Build),
TimeZone: int(r.TimeZone),
NotifySwitch: int(r.NotifySwitch),
DeviceBrand: r.DeviceBrand,
DeviceModel: r.DeviceModel,
OSVersion: r.OSVersion,
Extra: r.Extra,
})
return
}
// AddTokensCache 批量token缓存
func (s *server) AddTokensCache(ctx context.Context, req *pb.AddTokensCacheRequest) (reply *pb.AddTokensCacheReply, err error) {
reply = new(pb.AddTokensCacheReply)
if len(req.Reports) == 0 {
return
}
reports := make(map[string]*model.Report, len(req.Reports))
for _, v := range req.Reports {
reports[v.DeviceToken] = &model.Report{
APPID: int64(v.APPID),
PlatformID: int(v.PlatformID),
Mid: v.Mid,
Buvid: v.Buvid,
DeviceToken: v.DeviceToken,
Build: int(v.Build),
TimeZone: int(v.TimeZone),
NotifySwitch: int(v.NotifySwitch),
DeviceBrand: v.DeviceBrand,
DeviceModel: v.DeviceModel,
OSVersion: v.OSVersion,
Extra: v.Extra,
}
}
err = s.svc.AddTokensCache(ctx, reports)
return
}
// AddCallback 回执
func (s *server) AddCallback(ctx context.Context, req *pb.AddCallbackRequest) (reply *pb.AddCallbackReply, err error) {
reply = new(pb.AddCallbackReply)
cb := &model.Callback{
Task: req.Task,
APP: req.APP,
Platform: int(req.Platform),
Mid: int64(req.Mid),
Pid: int(req.Pid),
Token: req.Token,
Buvid: req.Buvid,
Click: uint8(req.Click),
}
if req.Extra != nil {
cb.Extra = &model.CallbackExtra{
Status: int(req.Extra.Status),
Channel: int(req.Extra.Channel),
}
}
err = s.svc.AddCallback(ctx, cb)
return
}
// AddMidProgress 记录任务中mid数
func (s *server) AddMidProgress(ctx context.Context, req *pb.AddMidProgressRequest) (reply *pb.AddMidProgressReply, err error) {
reply = new(pb.AddMidProgressReply)
err = s.svc.AddMidProgress(ctx, req.Task, req.MidTotal, req.MidValid)
return
}
// Setting 获取用户业务开关配置
func (s *server) Setting(ctx context.Context, req *pb.SettingRequest) (reply *pb.SettingReply, err error) {
reply = new(pb.SettingReply)
res, err := s.svc.Setting(ctx, req.Mid)
if err != nil || res == nil {
return
}
reply.Settings = make(map[int32]int32, len(res))
for k, v := range res {
reply.Settings[int32(k)] = int32(v)
}
return
}
// SetSetting 设置用户业务开关
func (s *server) SetSetting(ctx context.Context, req *pb.SetSettingRequest) (reply *pb.SetSettingReply, err error) {
reply = new(pb.SetSettingReply)
err = s.svc.SetSetting(ctx, req.Mid, int(req.Type), int(req.Value))
return
}

View File

@@ -0,0 +1,44 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"http.go",
"push.go",
"setting.go",
"upload.go",
],
importpath = "go-common/app/service/main/push/server/http",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/push/conf:go_default_library",
"//app/service/main/push/model:go_default_library",
"//app/service/main/push/service:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/http/blademaster/middleware/verify:go_default_library",
"//library/time:go_default_library",
"//library/xstr:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,51 @@
package http
import (
"net/http"
"go-common/app/service/main/push/conf"
"go-common/app/service/main/push/service"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/http/blademaster/middleware/verify"
)
var (
pushSrv *service.Service
idfSrv *verify.Verify
)
// Init init http.
func Init(c *conf.Config, srv *service.Service) {
idfSrv = verify.New(c.Verify)
pushSrv = srv
engine := bm.DefaultServer(c.HTTPServer)
route(engine)
if err := engine.Start(); err != nil {
log.Error("engine.Start() error(%v)", err)
panic(err)
}
}
func route(e *bm.Engine) {
e.Ping(ping)
g := e.Group("/x/internal/push-service", bm.CORS())
{
g.POST("/single", idfSrv.Verify, singlePush)
// for 直播
g.POST("/setting/set", idfSrv.Verify, setSettingInternal)
// for 管理后台测试推送
g.POST("/push", idfSrv.Verify, push)
// for test
g.POST("/test/token", idfSrv.Verify, testToken)
// upload image
g.POST("/upimg", upimg)
}
}
func ping(c *bm.Context) {
if err := pushSrv.Ping(c); err != nil {
log.Error("push-service ping error(%v)", err)
c.AbortWithStatus(http.StatusServiceUnavailable)
}
}

View File

@@ -0,0 +1,283 @@
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)
}

View File

@@ -0,0 +1,38 @@
package http
import (
"strconv"
pushmdl "go-common/app/service/main/push/model"
"go-common/library/ecode"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
)
func setSettingInternal(c *bm.Context) {
var (
params = c.Request.Form
midStr = params.Get("mid")
typeStr = params.Get("type")
valStr = params.Get("value")
)
mid, _ := strconv.ParseInt(midStr, 10, 64)
if mid <= 0 {
log.Warn("mid(%s) is wrong", midStr)
c.JSON(nil, ecode.RequestErr)
return
}
typ, _ := strconv.Atoi(typeStr)
if _, ok := pushmdl.Settings[typ]; !ok {
log.Warn("type(%s) is wrong", typeStr)
c.JSON(nil, ecode.RequestErr)
return
}
val, _ := strconv.Atoi(valStr)
if val != pushmdl.SwitchOn && val != pushmdl.SwitchOff {
log.Warn("value(%s) is wrong", valStr)
c.JSON(nil, ecode.RequestErr)
return
}
c.JSON(nil, pushSrv.SetSetting(c, mid, typ, val))
}

View File

@@ -0,0 +1,42 @@
package http
import (
"path/filepath"
"go-common/app/service/main/push/conf"
"go-common/library/ecode"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
)
var imgExts = map[string]bool{
".jpg": true,
".jpeg": true,
".png": true,
}
func upimg(ctx *bm.Context) {
f, h, err := ctx.Request.FormFile("file")
if err != nil {
log.Error("upimg error(%v)", err)
ctx.JSON(nil, err)
return
}
defer f.Close()
if h.Size > conf.Conf.Push.UpimgMaxSize {
log.Error("filesize error name(%s) size(%d)", h.Filename, h.Size)
ctx.JSON(nil, ecode.PushServiceFileSizeErr)
return
}
if ok := imgExts[filepath.Ext(h.Filename)]; !ok {
log.Error("file ext error name(%s)", h.Filename)
ctx.JSON(nil, ecode.PushServiceFileExtErr)
return
}
url, err := pushSrv.Upimg(ctx, f)
if err != nil {
ctx.JSON(nil, err)
return
}
ctx.JSON(map[string]string{"url": url}, nil)
}