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,49 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["callback_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-wall/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["callback.go"],
importpath = "go-common/app/interface/main/app-wall/dao/callback",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-wall/conf:go_default_library",
"//app/interface/main/app-wall/model:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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"],
)

View File

@@ -0,0 +1,131 @@
package callback
import (
"context"
"crypto/md5"
"encoding/base64"
"encoding/hex"
"fmt"
"net/url"
"strings"
"time"
"go-common/app/interface/main/app-wall/conf"
"go-common/app/interface/main/app-wall/model"
"go-common/library/ecode"
"go-common/library/log"
httpx "go-common/library/net/http/blademaster"
"github.com/pkg/errors"
)
const (
_iis = "/iis?clkid=%s"
)
type Dao struct {
client *httpx.Client
iisURL string
}
func New(c *conf.Config) (d *Dao) {
d = &Dao{
client: httpx.NewClient(conf.Conf.HTTPActive),
iisURL: c.Host.Dotin + _iis,
}
return
}
func (d *Dao) GdtCallback(c context.Context, appID, appType, aderID string, idfa, cb string, now time.Time) (err error) {
key, ok := model.ChannelGdt[aderID]
if !ok {
return
}
encrypt := []byte(key.Encrypt)
signKey := key.Sign
uri := fmt.Sprintf("http://t.gdt.qq.com/conv/app/%s/conv?", appID)
// sign v
queryS := fmt.Sprintf("muid=%s&conv_time=%d&click_id=%s", idfa, now.Unix(), cb)
page := signKey + "&GET&" + url.QueryEscape(uri+queryS)
bs := md5.Sum([]byte(page))
sign := hex.EncodeToString(bs[:])
queryS = queryS + "&sign=" + sign
queryBs := []byte(queryS)
i := 0
bss := []byte{}
for _, b := range queryBs {
bss = append(bss, b^encrypt[i])
i = i + 1
i = i % len(encrypt)
}
baseS := base64.StdEncoding.EncodeToString(bss)
baseS = strings.Replace(baseS, "\n", "", -1)
// finish uri
furi := uri + "v=" + url.QueryEscape(baseS) + fmt.Sprintf("&conv_type=MOBILEAPP_ACTIVITE&app_type=%s&advertiser_id=%s", appType, aderID)
var res struct {
Ret int `json:"ret"`
Msg string `json:"msg"`
}
for i := 0; i < 5; i++ {
if err = d.client.Get(c, furi, "", nil, &res); err == nil {
break
}
}
if err != nil {
return
}
if !ecode.Int(res.Ret).Equal(ecode.OK) {
err = errors.Wrapf(ecode.Int(res.Ret), furi)
if res.Ret == -1 {
log.Error("%+v", err)
err = nil
}
return
}
log.Info("callback gdt furi(%s) idfa(%s) cb(%s) success ret(%d) msg(%s)", furi, idfa, cb, res.Ret, res.Msg)
return
}
func (d *Dao) ShikeCallback(c context.Context, idfa, cb string, now time.Time) (err error) {
var res struct {
Success string `json:"success"`
Message string `json:"message"`
}
if err = d.client.Get(c, cb, "", nil, &res); err != nil {
return
}
log.Info("callback shike idfa(%s) cb(%s) success ret(%s) msg(%s)", idfa, cb, res.Success, res.Message)
return
}
func (d *Dao) DontinCallback(c context.Context, idfa, clickid string) (err error) {
urlStr := fmt.Sprintf(d.iisURL, clickid)
if err = d.client.Get(c, urlStr, "", nil, nil); err != nil {
return
}
log.Info("callback dontin idfa(%s) clickid(%s) success", idfa, clickid)
return
}
func (d *Dao) ToutiaoCallback(c context.Context, cb string, eventType string) (err error) {
if cb == "" {
return
}
cbURL := strings.TrimSpace(cb + "&event_type=" + eventType)
var res struct {
Ret int `json:"ret"`
}
if err = d.client.Get(c, cbURL, "", nil, &res); err != nil {
return
}
if !ecode.Int(res.Ret).Equal(ecode.OK) {
err = errors.Wrap(ecode.Int(res.Ret), cbURL)
if res.Ret == -1 {
log.Error("%+v", err)
err = nil
}
return
}
log.Info("callback toutiao cb(%s) eventType(%s) success", cb, eventType)
return
}

View File

@@ -0,0 +1,133 @@
package callback
import (
"context"
"flag"
"path/filepath"
"testing"
"time"
"go-common/app/interface/main/app-wall/conf"
. "github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func init() {
dir, _ := filepath.Abs("../../cmd/app-wall-test.toml")
flag.Set("conf", dir)
conf.Init()
d = New(conf.Conf)
time.Sleep(time.Second)
}
func TestNew(t *testing.T) {
type args struct {
c *conf.Config
}
tests := []struct {
name string
args args
wantD *Dao
}{
// TODO: Add test cases.
}
for _, tt := range tests {
Convey(tt.name, t, func() {
gotD := New(tt.args.c)
So(gotD, ShouldResemble, tt.wantD)
})
}
}
func TestDao_GdtCallback(t *testing.T) {
type args struct {
c context.Context
appID string
appType string
aderID string
idfa string
cb string
now time.Time
}
tests := []struct {
name string
args args
wantErr error
}{
// TODO: Add test cases.
}
for _, tt := range tests {
Convey(tt.name, t, func() {
err := d.GdtCallback(tt.args.c, tt.args.appID, tt.args.appType, tt.args.aderID, tt.args.idfa, tt.args.cb, tt.args.now)
So(err, ShouldEqual, tt.wantErr)
})
}
}
func TestDao_ShikeCallback(t *testing.T) {
type args struct {
c context.Context
idfa string
cb string
now time.Time
}
tests := []struct {
name string
args args
wantErr error
}{
// TODO: Add test cases.
}
for _, tt := range tests {
Convey(tt.name, t, func() {
err := d.ShikeCallback(tt.args.c, tt.args.idfa, tt.args.cb, tt.args.now)
So(err, ShouldEqual, tt.wantErr)
})
}
}
func TestDao_DontinCallback(t *testing.T) {
type args struct {
c context.Context
idfa string
clickid string
}
tests := []struct {
name string
args args
wantErr error
}{
// TODO: Add test cases.
}
for _, tt := range tests {
Convey(tt.name, t, func() {
err := d.DontinCallback(tt.args.c, tt.args.idfa, tt.args.clickid)
So(err, ShouldEqual, tt.wantErr)
})
}
}
func TestDao_ToutiaoCallback(t *testing.T) {
type args struct {
c context.Context
cb string
eventType string
}
tests := []struct {
name string
args args
wantErr error
}{
// TODO: Add test cases.
}
for _, tt := range tests {
Convey(tt.name, t, func() {
err := d.ToutiaoCallback(tt.args.c, tt.args.cb, tt.args.eventType)
So(err, ShouldEqual, tt.wantErr)
})
}
}