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,56 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"msg.go",
],
importpath = "go-common/app/interface/main/mcn/dao/msg",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/mcn/conf:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/xstr:go_default_library",
"//vendor/github.com/pkg/errors: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"],
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"msg_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/mcn/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)

View File

@@ -0,0 +1,28 @@
package msg
import (
"go-common/app/interface/main/mcn/conf"
bm "go-common/library/net/http/blademaster"
)
const (
msgurl = "/api/notify/send.user.notify.do"
)
// Dao .
type Dao struct {
c *conf.Config
client *bm.Client
msgURL string
}
// New new a Dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
// http client
client: bm.NewClient(c.HTTPClient),
msgURL: c.Host.Msg + msgurl,
}
return
}

View File

@@ -0,0 +1,48 @@
package msg
import (
"flag"
"os"
"strings"
"testing"
"go-common/app/interface/main/mcn/conf"
"gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.mcn-interface")
flag.Set("conf_token", "49e4671bafbf93059aeb602685052ca0")
flag.Set("tree_id", "58909")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../../cmd/mcn-interface.toml")
}
if os.Getenv("UT_LOCAL_TEST") != "" {
flag.Set("conf", "../../cmd/mcn-interface.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
d.client.SetTransport(gock.DefaultTransport)
os.Exit(m.Run())
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
return r
}

View File

@@ -0,0 +1,61 @@
package msg
import (
"context"
"net/url"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/xstr"
"github.com/pkg/errors"
)
// MutliSendSysMsg Mutli send sys msg.
func (d *Dao) MutliSendSysMsg(c context.Context, allUids []int64, mc, title string, context string, ip string) (err error) {
var times int
ulen := len(allUids)
if ulen%100 == 0 {
times = ulen / 100
} else {
times = ulen/100 + 1
}
var uids []int64
for i := 0; i < times; i++ {
if i == times-1 {
uids = allUids[i*100:]
} else {
uids = allUids[i*100 : (i+1)*100]
}
if err = d.SendSysMsg(c, uids, mc, title, context, ip); err != nil {
continue
}
}
return
}
// SendSysMsg send sys msg.
func (d *Dao) SendSysMsg(c context.Context, uids []int64, mc, title string, context string, ip string) (err error) {
params := url.Values{}
params.Set("mc", mc)
params.Set("title", title)
params.Set("data_type", "4")
params.Set("context", context)
params.Set("mid_list", xstr.JoinInts(uids))
var res struct {
Code int `json:"code"`
Data *struct {
Status int8 `json:"status"`
Remark string `json:"remark"`
} `json:"data"`
}
if err = d.client.Post(c, d.msgURL, ip, params, &res); err != nil {
return
}
if res.Code != 0 {
err = errors.Wrapf(ecode.Int(res.Code), "SendSysMsg d.client.Post(%s,%d)", d.msgURL+"?"+params.Encode(), res.Code)
} else {
log.Info("send msg ok, resdata=%+v", res.Data)
}
return
}

View File

@@ -0,0 +1,51 @@
package msg
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
func TestMsgMutliSendSysMsg(t *testing.T) {
convey.Convey("MutliSendSysMsg", t, func(ctx convey.C) {
var (
c = context.Background()
allUids = []int64{}
mc = ""
title = ""
context = ""
ip = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.MutliSendSysMsg(c, allUids, mc, title, context, ip)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMsgSendSysMsg(t *testing.T) {
convey.Convey("SendSysMsg", t, func(ctx convey.C) {
var (
c = context.Background()
uids = []int64{}
mc = ""
title = ""
context = ""
ip = ""
)
defer gock.OffAll()
httpMock("POST", "http://message.bilibili.co/api/notify/send.user.notify.do").Reply(200).JSON(`{"code": 0}`)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SendSysMsg(c, uids, mc, title, context, ip)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}