Create & Init Project...
This commit is contained in:
62
app/interface/main/web-goblin/dao/wechat/BUILD
Normal file
62
app/interface/main/web-goblin/dao/wechat/BUILD
Normal file
@ -0,0 +1,62 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_test",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = [
|
||||
"dao_test.go",
|
||||
"qrcode_test.go",
|
||||
],
|
||||
embed = [":go_default_library"],
|
||||
rundir = ".",
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//app/interface/main/web-goblin/conf:go_default_library",
|
||||
"//app/interface/main/web-goblin/model/wechat:go_default_library",
|
||||
"//library/ecode:go_default_library",
|
||||
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = [
|
||||
"cache.go",
|
||||
"dao.cache.go",
|
||||
"dao.go",
|
||||
"qrcode.go",
|
||||
],
|
||||
importpath = "go-common/app/interface/main/web-goblin/dao/wechat",
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//app/interface/main/web-goblin/conf:go_default_library",
|
||||
"//app/interface/main/web-goblin/model/wechat:go_default_library",
|
||||
"//library/cache:go_default_library",
|
||||
"//library/cache/redis:go_default_library",
|
||||
"//library/ecode:go_default_library",
|
||||
"//library/log:go_default_library",
|
||||
"//library/net/http/blademaster:go_default_library",
|
||||
"//library/net/metadata:go_default_library",
|
||||
"//library/stat/prom: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"],
|
||||
)
|
13
app/interface/main/web-goblin/dao/wechat/cache.go
Normal file
13
app/interface/main/web-goblin/dao/wechat/cache.go
Normal file
@ -0,0 +1,13 @@
|
||||
package wechat
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go-common/app/interface/main/web-goblin/model/wechat"
|
||||
)
|
||||
|
||||
//go:generate $GOPATH/src/go-common/app/tool/cache/gen
|
||||
type _cache interface {
|
||||
// cache
|
||||
AccessToken(c context.Context) (*wechat.AccessToken, error)
|
||||
}
|
49
app/interface/main/web-goblin/dao/wechat/dao.cache.go
Normal file
49
app/interface/main/web-goblin/dao/wechat/dao.cache.go
Normal file
@ -0,0 +1,49 @@
|
||||
// Code generated by $GOPATH/src/go-common/app/tool/cache/gen. DO NOT EDIT.
|
||||
|
||||
/*
|
||||
Package wechat is a generated cache proxy package.
|
||||
It is generated from:
|
||||
type _cache interface {
|
||||
// cache
|
||||
AccessToken(c context.Context) (*wechat.AccessToken, error)
|
||||
}
|
||||
*/
|
||||
|
||||
package wechat
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go-common/app/interface/main/web-goblin/model/wechat"
|
||||
"go-common/library/net/metadata"
|
||||
"go-common/library/stat/prom"
|
||||
)
|
||||
|
||||
var _ _cache
|
||||
|
||||
// AccessToken get data from cache if miss will call source method, then add to cache.
|
||||
func (d *Dao) AccessToken(c context.Context) (res *wechat.AccessToken, err error) {
|
||||
addCache := true
|
||||
res, err = d.CacheAccessToken(c)
|
||||
if err != nil {
|
||||
addCache = false
|
||||
err = nil
|
||||
}
|
||||
if res != nil {
|
||||
prom.CacheHit.Incr("AccessToken")
|
||||
return
|
||||
}
|
||||
prom.CacheMiss.Incr("AccessToken")
|
||||
res, err = d.RawAccessToken(c)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var miss = res
|
||||
if !addCache {
|
||||
return
|
||||
}
|
||||
d.cache.Save(func() {
|
||||
d.AddCacheAccessToken(metadata.WithContext(c), miss)
|
||||
})
|
||||
return
|
||||
}
|
36
app/interface/main/web-goblin/dao/wechat/dao.go
Normal file
36
app/interface/main/web-goblin/dao/wechat/dao.go
Normal file
@ -0,0 +1,36 @@
|
||||
package wechat
|
||||
|
||||
import (
|
||||
"go-common/app/interface/main/web-goblin/conf"
|
||||
"go-common/library/cache"
|
||||
"go-common/library/cache/redis"
|
||||
bm "go-common/library/net/http/blademaster"
|
||||
)
|
||||
|
||||
// Dao dao struct.
|
||||
type Dao struct {
|
||||
// config
|
||||
c *conf.Config
|
||||
// redis
|
||||
redis *redis.Pool
|
||||
// httpClient
|
||||
httpClient *bm.Client
|
||||
// url
|
||||
wxAccessTokenURL string
|
||||
wxQrcodeURL string
|
||||
cache *cache.Cache
|
||||
}
|
||||
|
||||
// New new dao.
|
||||
func New(c *conf.Config) (d *Dao) {
|
||||
d = &Dao{
|
||||
// config
|
||||
c: c,
|
||||
redis: redis.NewPool(c.Redis.Config),
|
||||
httpClient: bm.NewClient(c.HTTPClient),
|
||||
cache: cache.New(1, 1024),
|
||||
}
|
||||
d.wxAccessTokenURL = d.c.Host.Wechat + _accessTokenURI
|
||||
d.wxQrcodeURL = d.c.Host.Wechat + _qrcodeURI
|
||||
return
|
||||
}
|
35
app/interface/main/web-goblin/dao/wechat/dao_test.go
Normal file
35
app/interface/main/web-goblin/dao/wechat/dao_test.go
Normal file
@ -0,0 +1,35 @@
|
||||
package wechat
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"go-common/app/interface/main/web-goblin/conf"
|
||||
)
|
||||
|
||||
var (
|
||||
d *Dao
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if os.Getenv("DEPLOY_ENV") != "" {
|
||||
flag.Set("app_id", "main.web-svr.web-goblin")
|
||||
flag.Set("conf_token", "b2850ed0834343a9e435809857f5670d")
|
||||
flag.Set("tree_id", "41730")
|
||||
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/web-goblin-test.toml")
|
||||
}
|
||||
flag.Parse()
|
||||
if err := conf.Init(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
d = New(conf.Conf)
|
||||
os.Exit(m.Run())
|
||||
}
|
137
app/interface/main/web-goblin/dao/wechat/qrcode.go
Normal file
137
app/interface/main/web-goblin/dao/wechat/qrcode.go
Normal file
@ -0,0 +1,137 @@
|
||||
package wechat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"go-common/app/interface/main/web-goblin/model/wechat"
|
||||
"go-common/library/cache/redis"
|
||||
"go-common/library/ecode"
|
||||
"go-common/library/log"
|
||||
)
|
||||
|
||||
const (
|
||||
_accessTokenKey = "a_t_k"
|
||||
_accessTokenURI = "/cgi-bin/token"
|
||||
_qrcodeURI = "/wxa/getwxacodeunlimit"
|
||||
)
|
||||
|
||||
// RawAccessToken get wechat access token.
|
||||
func (d *Dao) RawAccessToken(c context.Context) (data *wechat.AccessToken, err error) {
|
||||
params := url.Values{}
|
||||
params.Set("grant_type", "client_credential")
|
||||
params.Set("appid", d.c.Wechat.AppID)
|
||||
params.Set("secret", d.c.Wechat.Secret)
|
||||
var req *http.Request
|
||||
if req, err = http.NewRequest(http.MethodGet, d.wxAccessTokenURL+"?"+params.Encode(), nil); err != nil {
|
||||
log.Error("AccessToken http.NewRequest error(%v)", err)
|
||||
return
|
||||
}
|
||||
var res struct {
|
||||
Errcode int `json:"errcode"`
|
||||
Errmsg string `json:"errmsg"`
|
||||
AccessToken string `json:"access_token"`
|
||||
ExpiresIN int64 `json:"expires_in"`
|
||||
}
|
||||
if err = d.httpClient.Do(c, req, &res); err != nil {
|
||||
log.Error("AccessToken d.httpClient.Do error(%v)", err)
|
||||
return
|
||||
}
|
||||
if res.Errcode != ecode.OK.Code() {
|
||||
log.Error("AccessToken errcode error(%d) msg(%s)", res.Errcode, res.Errmsg)
|
||||
err = ecode.RequestErr
|
||||
return
|
||||
}
|
||||
data = &wechat.AccessToken{AccessToken: res.AccessToken, ExpiresIn: res.ExpiresIN}
|
||||
return
|
||||
}
|
||||
|
||||
// Qrcode get qrcode.
|
||||
func (d *Dao) Qrcode(c context.Context, accessToken, arg string) (qrcode []byte, err error) {
|
||||
var (
|
||||
req *http.Request
|
||||
bs []byte
|
||||
jsonErr error
|
||||
)
|
||||
params := url.Values{}
|
||||
params.Set("access_token", accessToken)
|
||||
if req, err = http.NewRequest(http.MethodPost, d.wxQrcodeURL+"?"+params.Encode(), strings.NewReader(arg)); err != nil {
|
||||
log.Error("Qrcode http.NewRequest error(%v)", err)
|
||||
return
|
||||
}
|
||||
if bs, err = d.httpClient.Raw(c, req); err != nil {
|
||||
log.Error("Qrcode d.httpClient.Do error(%v)", err)
|
||||
return
|
||||
}
|
||||
var res struct {
|
||||
Errcode int `json:"errcode"`
|
||||
Errmsg string `json:"errmsg"`
|
||||
}
|
||||
if jsonErr = json.Unmarshal(bs, &res); jsonErr == nil && res.Errcode != ecode.OK.Code() {
|
||||
log.Error("Qrcode errcode error(%d) msg(%s)", res.Errcode, res.Errmsg)
|
||||
err = ecode.RequestErr
|
||||
return
|
||||
}
|
||||
qrcode = bs
|
||||
return
|
||||
}
|
||||
|
||||
// CacheAccessToken cache access token
|
||||
func (d *Dao) CacheAccessToken(c context.Context) (data *wechat.AccessToken, err error) {
|
||||
var (
|
||||
value []byte
|
||||
key = _accessTokenKey
|
||||
conn = d.redis.Get(c)
|
||||
)
|
||||
defer conn.Close()
|
||||
if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
|
||||
if err == redis.ErrNil {
|
||||
err = nil
|
||||
} else {
|
||||
log.Error("CacheAccessToken conn.Do(GET, %s) error(%v)", key, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
data = new(wechat.AccessToken)
|
||||
if err = json.Unmarshal(value, &data); err != nil {
|
||||
log.Error("CacheAccessToken json.Unmarshal(%v) error(%v)", value, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// AddCacheAccessToken add access token cache
|
||||
func (d *Dao) AddCacheAccessToken(c context.Context, data *wechat.AccessToken) (err error) {
|
||||
var (
|
||||
bs []byte
|
||||
key = _accessTokenKey
|
||||
conn = d.redis.Get(c)
|
||||
)
|
||||
defer conn.Close()
|
||||
if bs, err = json.Marshal(data); err != nil {
|
||||
log.Error("AddCacheAccessToken json.Marshal(%v) error (%v)", data, err)
|
||||
return
|
||||
}
|
||||
if err = conn.Send("SET", key, bs); err != nil {
|
||||
log.Error("conn.Send(SET, %s, %s) error(%v)", key, string(bs), err)
|
||||
return
|
||||
}
|
||||
expire := data.ExpiresIn - 60
|
||||
if err = conn.Send("EXPIRE", key, expire); err != nil {
|
||||
log.Error("conn.Send(Expire, %s, %d) error(%v)", key, expire, err)
|
||||
return
|
||||
}
|
||||
if err = conn.Flush(); err != nil {
|
||||
log.Error("conn.Flush error(%v)", err)
|
||||
return
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
if _, err = conn.Receive(); err != nil {
|
||||
log.Error("conn.Receive() error(%v)", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
58
app/interface/main/web-goblin/dao/wechat/qrcode_test.go
Normal file
58
app/interface/main/web-goblin/dao/wechat/qrcode_test.go
Normal file
@ -0,0 +1,58 @@
|
||||
package wechat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go-common/app/interface/main/web-goblin/model/wechat"
|
||||
"go-common/library/ecode"
|
||||
|
||||
"github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestWechatQrcode(t *testing.T) {
|
||||
convey.Convey("Qrcode", t, func(ctx convey.C) {
|
||||
var (
|
||||
c = context.Background()
|
||||
accessToken = "14_LZVbKTtstzal_T-AfG-EgkUI2WlCdRvKUqhiYKMhNyxsGjzc1K_a1GGWuMPCbX"
|
||||
arg = `{"page":"","scene":"?avid=34188644"}`
|
||||
)
|
||||
ctx.Convey("When everything goes positive", func(ctx convey.C) {
|
||||
qrcode, err := d.Qrcode(c, accessToken, arg)
|
||||
ctx.Convey("Then err should be nil.qrcode should not be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldEqual, ecode.RequestErr)
|
||||
ctx.Println(qrcode)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestWechatAddCacheAccessToken(t *testing.T) {
|
||||
convey.Convey("AddCacheAccessToken", t, func(ctx convey.C) {
|
||||
var (
|
||||
c = context.Background()
|
||||
data = &wechat.AccessToken{AccessToken: "string", ExpiresIn: 1111}
|
||||
)
|
||||
ctx.Convey("When everything goes positive", func(ctx convey.C) {
|
||||
err := d.AddCacheAccessToken(c, data)
|
||||
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestWechatCacheAccessToken(t *testing.T) {
|
||||
convey.Convey("CacheAccessToken", t, func(ctx convey.C) {
|
||||
var (
|
||||
c = context.Background()
|
||||
)
|
||||
ctx.Convey("When everything goes positive", func(ctx convey.C) {
|
||||
data, err := d.CacheAccessToken(c)
|
||||
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
|
||||
ctx.So(err, convey.ShouldBeNil)
|
||||
ctx.So(data, convey.ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user