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,119 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"abserver_test.go",
"account_redis_test.go",
"archive_test.go",
"article_redis_test.go",
"bfs_test.go",
"bigdata_test.go",
"bnj_test.go",
"dao_test.go",
"dynamic_redis_test.go",
"elec_test.go",
"feedback_test.go",
"help_test.go",
"icon_redis_test.go",
"newlist_redis_test.go",
"online_redis_test.go",
"online_test.go",
"pay_test.go",
"ranking_redis_test.go",
"reply_test.go",
"search_test.go",
"shop_test.go",
"space_test.go",
"tag_test.go",
"view_redis_test.go",
"wechat_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/web/conf:go_default_library",
"//app/interface/main/web/model:go_default_library",
"//app/interface/openplatform/article/model:go_default_library",
"//app/service/main/archive/api:go_default_library",
"//app/service/main/dynamic/model:go_default_library",
"//app/service/main/resource/model:go_default_library",
"//library/ecode:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"abserver.go",
"account_redis.go",
"archive.go",
"article_redis.go",
"bfs.go",
"bigdata.go",
"bnj.go",
"dao.go",
"dynamic_redis.go",
"elec.go",
"feedback.go",
"help.go",
"icon_redis.go",
"newlist_redis.go",
"online.go",
"online_redis.go",
"pay.go",
"ranking_redis.go",
"reply.go",
"search.go",
"shop.go",
"space.go",
"tag.go",
"view_redis.go",
"wechat.go",
],
importpath = "go-common/app/interface/main/web/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/web/conf:go_default_library",
"//app/interface/main/web/model:go_default_library",
"//app/interface/openplatform/article/model:go_default_library",
"//app/service/main/archive/api:go_default_library",
"//app/service/main/dynamic/model:go_default_library",
"//app/service/main/resource/model: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/net/trace:go_default_library",
"//library/stat/prom:go_default_library",
"//library/time: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"],
)

View File

@@ -0,0 +1,42 @@
package dao
import (
"context"
"net/url"
"strconv"
"go-common/app/interface/main/web/model"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
)
const (
_abServerURI = "/abserver/v1/web/match-exp"
)
// AbServer get ab server info.
func (d *Dao) AbServer(c context.Context, mid int64, platform int, channel, buvid string) (res model.AbServer, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("channel", channel)
params.Set("buvid", buvid)
params.Set("platform", strconv.Itoa(platform))
var data struct {
Code int `json:"errorCode"`
model.AbServer
}
if err = d.httpR.Get(c, d.abServerURL, ip, params, &data); err != nil {
log.Error("AbServer(%s) mid(%d) channel(%s) buvid(%s) error(%v)", d.abServerURL, mid, channel, buvid, err)
return
}
if data.Code != ecode.OK.Code() {
log.Error("AbServer(%s) mid(%d) channel(%s) buvid(%s) code error(%d)", d.abServerURL, mid, channel, buvid, data.Code)
return
}
res = data.AbServer
return
}

View File

@@ -0,0 +1,27 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAbServer(t *testing.T) {
convey.Convey("AbServer", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
platform = int(0)
channel = "test"
buvid = "test"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.AbServer(c, mid, platform, channel, buvid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,75 @@
package dao
import (
"context"
"fmt"
"encoding/json"
"go-common/app/interface/main/web/model"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
_keyCardFmt = "ac_%d"
)
func keyCard(mid int64) string {
return fmt.Sprintf(_keyCardFmt, mid)
}
// SetCardBakCache set card data to cache.
func (d *Dao) SetCardBakCache(c context.Context, mid int64, rs *model.Card) (err error) {
var bs []byte
key := keyCard(mid)
if bs, err = json.Marshal(rs); err != nil {
log.Error("json.Marshal(%v) error(%v)", rs, err)
return
}
err = d.commonSetBakCache(c, key, bs)
return
}
//CardBakCache get card data from cache.
func (d *Dao) CardBakCache(c context.Context, mid int64) (rs *model.Card, err error) {
key := keyCard(mid)
conn := d.redisBak.Get(c)
defer conn.Close()
var values []byte
if values, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
log.Warn("CardBakCache (%s) return nil", key)
} else {
log.Error("conn.Do(GET,%s) error(%v)", key, err)
}
return
}
if err = json.Unmarshal(values, &rs); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", values, err)
}
return
}
func (d *Dao) commonSetBakCache(c context.Context, key string, bs []byte) (err error) {
conn := d.redisBak.Get(c)
defer conn.Close()
if err = conn.Send("SET", key, bs); err != nil {
log.Error("conn.Send(SET,%s,%s) error(%v)", key, string(bs), err)
return
}
if err = conn.Send("EXPIRE", key, d.redisCardBakExpire); err != nil {
log.Error("conn.Send(EXPIRE,%s,%d) error(%v)", key, d.redisCardBakExpire, 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(%d) error(%v)", i, err)
}
}
return
}

View File

@@ -0,0 +1,72 @@
package dao
import (
"context"
"testing"
"go-common/app/interface/main/web/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaokeyCard(t *testing.T) {
convey.Convey("keyCard", t, func(ctx convey.C) {
var (
mid = int64(2222)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyCard(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSetCardBakCache(t *testing.T) {
convey.Convey("SetCardBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2222)
rs = &model.Card{Card: &model.AccountCard{Mid: "2222"}, Space: &model.Space{}}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetCardBakCache(c, mid, rs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoCardBakCache(t *testing.T) {
convey.Convey("CardBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2222)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rs, err := d.CardBakCache(c, mid)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", rs)
})
})
})
}
func TestDaocommonSetBakCache(t *testing.T) {
convey.Convey("commonSetBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
key = ""
bs = []byte("")
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.commonSetBakCache(c, key, bs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,200 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"net/url"
"strconv"
"go-common/library/cache/redis"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
"go-common/library/xstr"
"github.com/pkg/errors"
)
const (
_keyArcAppeal = "arc_appeal_%d_%d"
_testerGroup = "20"
)
// ArcReport add archive report
func (d *Dao) ArcReport(c context.Context, mid, aid, tp int64, reason, pics string) (err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("aid", strconv.FormatInt(aid, 10))
params.Set("type", strconv.FormatInt(tp, 10))
params.Set("reason", reason)
params.Set("pics", pics)
var res struct {
Code int `json:"code"`
}
if err = d.httpW.Post(c, d.arcReportURL, ip, params, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
log.Error("archive report(%s) param(%v) ecode err(%d)", d.arcReportURL, params, res.Code)
err = ecode.Int(res.Code)
}
return
}
// ArcAppeal add archive appeal.
func (d *Dao) ArcAppeal(c context.Context, mid int64, data map[string]string, business int) (err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
for name, value := range data {
params.Set(name, value)
}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("business", strconv.Itoa(business))
if v, ok := data["attach"]; ok && v != "" {
params.Set("attachments", v)
}
var res struct {
Code int `json:"code"`
}
if err = d.httpW.Post(c, d.arcAppealURL, ip, params, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
log.Error("archive report(%s) ecode err(%d)", d.arcAppealURL+"?"+params.Encode(), res.Code)
err = ecode.Int(res.Code)
}
return
}
// AppealTags get appeal tags.
func (d *Dao) AppealTags(c context.Context, business int) (rs json.RawMessage, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params.Set("business", strconv.Itoa(business))
var res struct {
Code int `json:"code"`
Data json.RawMessage `json:"data"`
}
if err = d.httpR.Get(c, d.appealTagsURL, ip, params, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
log.Error("archive report(%s) param(%v) ecode err(%d)", d.arcReportURL, params, res.Code)
err = ecode.Int(res.Code)
}
rs = res.Data
return
}
// RelatedAids get related aids from bigdata
func (d *Dao) RelatedAids(c context.Context, aid int64) (aids []int64, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params.Set("key", strconv.FormatInt(aid, 10))
var res struct {
Code int `json:"code"`
Data []*struct {
Value string `json:"value"`
} `json:"data"`
}
if err = d.httpR.Get(c, d.relatedURL, ip, params, &res); err != nil {
log.Error("realte url(%s) error(%v) ", d.relatedURL+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
log.Error("url(%s) res code(%d) or res.result(%v)", d.relatedURL+"?"+params.Encode(), res.Code, res.Data)
err = ecode.Int(res.Code)
return
}
if res.Data == nil {
err = nil
return
}
if len(res.Data) > 0 {
if aids, err = xstr.SplitInts(res.Data[0].Value); err != nil {
log.Error("realte aids url(%s) error(%v)", d.relatedURL+"?"+params.Encode(), err)
}
}
return
}
func keyArcAppealLimit(mid, aid int64) string {
return fmt.Sprintf(_keyArcAppeal, mid, aid)
}
// SetArcAppealCache set arc appeal cache.
func (d *Dao) SetArcAppealCache(c context.Context, mid, aid int64) (err error) {
key := keyArcAppealLimit(mid, aid)
conn := d.redisBak.Get(c)
defer conn.Close()
if err = conn.Send("SET", key, "1"); err != nil {
log.Error("SetArcAppealCache conn.Send(SET, %s) error(%v)", key, err)
return
}
if err = conn.Send("EXPIRE", key, d.redisAppealLimitExpire); err != nil {
log.Error("SetArcAppealCache conn.Send(Expire, %s, %d) error(%v)", key, d.redisAppealLimitExpire, err)
return
}
if err = conn.Flush(); err != nil {
log.Error("SetArcAppealCache conn.Flush error(%v)", err)
return
}
for i := 0; i < 2; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("SetArcAppealCache conn.Receive() error(%v)", err)
return
}
}
return
}
// ArcAppealCache get arc appeal cache.
func (d *Dao) ArcAppealCache(c context.Context, mid, aid int64) (err error) {
key := keyArcAppealLimit(mid, aid)
conn := d.redisBak.Get(c)
defer conn.Close()
if _, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
return
}
log.Error("ArcAppealCache conn.Do(GET, %s) error(%v)", key, err)
}
err = ecode.ArcAppealLimit
return
}
// Special manager special mid.
func (d *Dao) Special(c context.Context) (midsM map[int64]struct{}, err error) {
params := url.Values{}
params.Set("group_id", _testerGroup)
var res struct {
Code int `json:"code"`
Data []struct {
Mid int64 `json:"mid"`
} `json:"data"`
}
if err = d.httpR.Get(c, d.special, "", params, &res); err != nil {
err = errors.Wrap(err, d.special+"?"+params.Encode())
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.special+"?"+params.Encode())
return
}
midsM = make(map[int64]struct{}, len(res.Data))
for _, l := range res.Data {
midsM[l.Mid] = struct{}{}
}
return
}

View File

@@ -0,0 +1,124 @@
package dao
import (
"context"
"go-common/library/ecode"
"testing"
"github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
func TestDaoArcAppeal(t *testing.T) {
convey.Convey("ArcAppeal", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2222)
business = int(1)
)
data := map[string]string{"tid": "27", "oid": "222", "description": "test111"}
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.ArcAppeal(c, mid, data, business)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAppealTags(t *testing.T) {
convey.Convey("AppealTags", t, func(ctx convey.C) {
var (
c = context.Background()
business = int(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rs, err := d.AppealTags(c, business)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRelatedAids(t *testing.T) {
convey.Convey("RelatedAids", t, func(ctx convey.C) {
var (
c = context.Background()
aid = int64(9912124)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.relatedURL).Reply(200).JSON(`{"data":[{"key":"33817773","value":"14536406,25731794"}]}`)
aids, err := d.RelatedAids(c, aid)
ctx.Convey("Then err should be nil.aids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(aids, convey.ShouldNotBeNil)
ctx.Printf("%+v", aids)
})
})
})
}
func TestDaokeyArcAppealLimit(t *testing.T) {
convey.Convey("keyArcAppealLimit", t, func(ctx convey.C) {
var (
mid = int64(0)
aid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyArcAppealLimit(mid, aid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSetArcAppealCache(t *testing.T) {
convey.Convey("SetArcAppealCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2222)
aid = int64(2222)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetArcAppealCache(c, mid, aid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoArcAppealCache(t *testing.T) {
convey.Convey("ArcAppealCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2222)
aid = int64(2222)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.ArcAppealCache(c, mid, aid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldEqual, ecode.ArcAppealLimit)
})
})
})
}
func TestDaoSpecial(t *testing.T) {
convey.Convey("Special", t, func(convCtx convey.C) {
var (
c = context.Background()
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
midsM, err := d.Special(c)
convCtx.Convey("Then err should be nil.midsM should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(midsM, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,132 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"go-common/app/interface/main/web/model"
artmdl "go-common/app/interface/openplatform/article/model"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
_keyArtList = "art_%d_%d"
_keyArtUp = "art_u"
)
func keyArtList(rid int64, sort int) string {
return fmt.Sprintf(_keyArtList, rid, sort)
}
// ArticleListCache get article list cache
func (d *Dao) ArticleListCache(c context.Context, rid int64, sort int) (res []*artmdl.Meta, err error) {
var (
value []byte
key = keyArtList(rid, sort)
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("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
res = []*artmdl.Meta{}
if err = json.Unmarshal(value, &res); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", value, err)
}
return
}
// SetArticleListCache set article list cache
func (d *Dao) SetArticleListCache(c context.Context, rid int64, sort int, list []*artmdl.Meta) (err error) {
var (
bs []byte
key = keyArtList(rid, sort)
conn = d.redis.Get(c)
)
defer conn.Close()
if bs, err = json.Marshal(list); err != nil {
log.Error("json.Marshal(%v) error (%v)", list, 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
}
if err = conn.Send("EXPIRE", key, d.redisArtBakExpire); err != nil {
log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.redisArtBakExpire, 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
}
// ArticleUpListCache get article up list cache.
func (d *Dao) ArticleUpListCache(c context.Context) (res []*model.Info, err error) {
var (
value []byte
conn = d.redis.Get(c)
)
defer conn.Close()
if value, err = redis.Bytes(conn.Do("GET", _keyArtUp)); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", _keyArtUp, err)
}
return
}
res = []*model.Info{}
if err = json.Unmarshal(value, &res); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", value, err)
}
return
}
// SetArticleUpListCache set article up list cache.
func (d *Dao) SetArticleUpListCache(c context.Context, list []*model.Info) (err error) {
var (
bs []byte
key = _keyArtUp
conn = d.redis.Get(c)
)
defer conn.Close()
if bs, err = json.Marshal(list); err != nil {
log.Error("json.Marshal(%v) error (%v)", list, 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
}
if err = conn.Send("EXPIRE", key, d.redisArtBakExpire); err != nil {
log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.redisArtBakExpire, 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
}

View File

@@ -0,0 +1,90 @@
package dao
import (
"context"
"testing"
"go-common/app/interface/main/web/model"
artmdl "go-common/app/interface/openplatform/article/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaokeyArtList(t *testing.T) {
convey.Convey("keyArtList", t, func(ctx convey.C) {
var (
rid = int64(0)
sort = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyArtList(rid, sort)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoArticleListCache(t *testing.T) {
convey.Convey("ArticleListCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int64(0)
sort = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.ArticleListCache(c, rid, sort)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
convey.Printf("%+v", res)
})
})
})
}
func TestDaoSetArticleListCache(t *testing.T) {
convey.Convey("SetArticleListCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int64(0)
sort = int(0)
list = []*artmdl.Meta{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetArticleListCache(c, rid, sort, list)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoArticleUpListCache(t *testing.T) {
convey.Convey("ArticleUpListCache", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.ArticleUpListCache(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", res)
})
})
})
}
func TestDaoSetArticleUpListCache(t *testing.T) {
convey.Convey("SetArticleUpListCache", t, func(ctx convey.C) {
var (
c = context.Background()
list = []*model.Info{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetArticleUpListCache(c, list)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,93 @@
package dao
import (
"context"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"errors"
"fmt"
"hash"
"net/http"
"strconv"
"strings"
"time"
"go-common/library/log"
"go-common/library/net/trace"
)
const (
_uploadURL = "/%s"
_family = "http_client"
_template = "%s\n%s\n\n%d\n"
_method = "PUT"
_fileType = "txt"
)
var (
errUpload = errors.New("Upload failed")
)
// Upload upload picture or log file to bfs
func (d *Dao) Upload(c context.Context, content string, expire int64) (location string, err error) {
var (
url string
req *http.Request
resp *http.Response
header http.Header
code string
)
bfsConf := d.c.Bfs
url = fmt.Sprintf(bfsConf.Addr+_uploadURL, bfsConf.Bucket)
if req, err = http.NewRequest(_method, url, strings.NewReader(content)); err != nil {
log.Error("http.NewRequest() Upload(%v) error(%v)", url, err)
return
}
authorization := authorize(bfsConf.Key, bfsConf.Secret, _method, bfsConf.Bucket, expire)
req.Header.Set("Host", bfsConf.Addr)
req.Header.Add("Date", fmt.Sprint(expire))
req.Header.Add("Authorization", authorization)
req.Header.Add("Content-Type", _fileType)
if t, ok := trace.FromContext(c); ok {
t = t.Fork(_family, req.URL.Path)
defer t.Finish(&err)
}
c, cancel := context.WithTimeout(c, time.Duration(d.c.Bfs.Timeout))
req = req.WithContext(c)
defer cancel()
resp, err = d.bfsClient.Do(req)
if err != nil {
log.Error("bfsClient.Do(%s) error(%v)", url, err)
return
}
if resp.StatusCode != http.StatusOK {
log.Error("Upload url(%s) http.statuscode:%d", url, resp.StatusCode)
err = errUpload
return
}
header = resp.Header
code = header.Get("Code")
if code != strconv.Itoa(http.StatusOK) {
log.Error("Upload url(%s) code:%s", url, code)
err = errUpload
return
}
location = header.Get("Location")
return
}
// authorize returns authorization for upload file to bfs
func authorize(key, secret, method, bucket string, expire int64) (authorization string) {
var (
content string
mac hash.Hash
signature string
)
content = fmt.Sprintf(_template, method, bucket, expire)
mac = hmac.New(sha1.New, []byte(secret))
mac.Write([]byte(content))
signature = base64.StdEncoding.EncodeToString(mac.Sum(nil))
authorization = fmt.Sprintf("%s:%s:%d", key, signature, expire)
return
}

View File

@@ -0,0 +1,25 @@
package dao
import (
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoauthorize(t *testing.T) {
convey.Convey("authorize", t, func(ctx convey.C) {
var (
key = ""
secret = ""
method = ""
bucket = ""
expire = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
authorization := authorize(key, secret, method, bucket, expire)
ctx.Convey("Then authorization should not be nil.", func(ctx convey.C) {
ctx.So(authorization, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,203 @@
package dao
import (
"context"
"fmt"
"net/url"
"go-common/app/interface/main/web/model"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
)
const (
_rankURI = "%s.json"
_rankAllURI = "all-%d"
_rankAllRidURI = "all_region-%d-%d"
_rankAllRecURI = "recent_all-%d"
_rankAllRecRidURI = "recent_region-%d-%d"
_rankOriAllURI = "all_origin-%d"
_rankOriAllRidURI = "all_region_origin-%d-%d"
_rankOriRecURI = "recent_origin-%d"
_rankOriRecRidURI = "recent_region_origin-%d-%d"
_rankAllNewURI = "all_rookie-%d"
_rankAllNewRidURI = "all_region_rookie-%d-%d"
_rankRegionURI = "recent_region%s-%d-%d.json"
_rankRecURI = "reco_region-%d.json"
_rankTagURI = "/tag/hot/web/%d/%d.json"
_rankIndexURI = "reco-%d.json"
_customURI = "game_custom_2.json"
)
// Ranking get ranking data from new api
func (d *Dao) Ranking(c context.Context, rid int16, rankType, day, arcType int) (res *model.RankNew, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
suffix := rankURI(rid, model.RankType[rankType], day, arcType)
var rs struct {
Code int `json:"code"`
Note string `json:"note"`
List []*model.RankNewArchive `json:"list"`
}
if err = d.httpBigData.RESTfulGet(c, d.rankURL, ip, params, &rs, suffix); err != nil {
log.Error("d.httpBigData.RESTfulGet(%s) error(%v)", suffix, err)
return
}
if rs.Code != ecode.OK.Code() {
log.Error("d.httpBigData.RESTfulGet(%s) error code(%d)", suffix, rs.Code)
err = ecode.Int(rs.Code)
return
}
res = &model.RankNew{Note: rs.Note, List: rs.List}
return
}
// RankingIndex get rank index data from bigdata
func (d *Dao) RankingIndex(c context.Context, day int) (res []*model.NewArchive, err error) {
var (
params = url.Values{}
remoteIP = metadata.String(c, metadata.RemoteIP)
)
var rs struct {
Code int `json:"code"`
Num int `json:"num"`
List []*model.NewArchive `json:"list"`
}
if err = d.httpBigData.RESTfulGet(c, d.rankIndexURL, remoteIP, params, &rs, day); err != nil {
log.Error("d.httpBigData.RESTfulGet(%d) error(%v)", day, err)
return
}
if rs.Code != ecode.OK.Code() {
log.Error("d.httpBigData.RESTfulGet(%d) error(%v)", day, rs.Code)
err = ecode.Int(rs.Code)
return
}
res = rs.List
return
}
// RankingRegion get rank region data from bigdata
func (d *Dao) RankingRegion(c context.Context, rid int16, day, original int) (res []*model.NewArchive, err error) {
var (
params = url.Values{}
remoteIP = metadata.String(c, metadata.RemoteIP)
)
var rs struct {
Code int `json:"code"`
List []*model.NewArchive `json:"list"`
}
if err = d.httpBigData.RESTfulGet(c, d.rankRegionURL, remoteIP, params, &rs, model.OriType[original], rid, day); err != nil {
log.Error("d.httpBigData.RESTfulGet(%d,%d,%d) error(%v)", original, rid, day, err)
return
}
if rs.Code != ecode.OK.Code() {
log.Error("d.httpBigData.RESTfulGet(%d,%d,%d) error code(%d)", original, rid, day, rs.Code)
err = ecode.Int(rs.Code)
return
}
res = rs.List
return
}
// RankingRecommend get rank recommend data from bigdata.
func (d *Dao) RankingRecommend(c context.Context, rid int16) (res []*model.NewArchive, err error) {
var (
params = url.Values{}
remoteIP = metadata.String(c, metadata.RemoteIP)
)
var rs struct {
Code int `json:"code"`
Num int `json:"num"`
List []*model.NewArchive `json:"list"`
}
if err = d.httpBigData.RESTfulGet(c, d.rankRecURL, remoteIP, params, &rs, rid); err != nil {
log.Error("d.httpBigData.RESTfulGet(%d) error(%v)", rid, err)
return
}
if rs.Code != ecode.OK.Code() {
log.Error("d.httpBigData.RESTfulGet(%d) error(%v)", rid, rs.Code)
err = ecode.Int(rs.Code)
return
}
res = rs.List
return
}
// RankingTag get rank tag data from bigdata
func (d *Dao) RankingTag(c context.Context, rid int16, tagID int64) (rs []*model.NewArchive, err error) {
var (
params = url.Values{}
remoteIP = metadata.String(c, metadata.RemoteIP)
)
var res struct {
Code int `json:"code"`
List []*model.NewArchive `json:"list"`
}
if err = d.httpBigData.RESTfulGet(c, d.rankTagURL, remoteIP, params, &res, rid, tagID); err != nil {
log.Error("d.httpBigData.RESTfulGet(%d,%d) error(%v)", rid, tagID, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("d.httpBigData.RESTfulGet(%d,%d) code(%d)", rid, tagID, res.Code)
err = ecode.Int(res.Code)
return
}
rs = res.List
return
}
// RegionCustom get region(game) custom data from big data
func (d *Dao) RegionCustom(c context.Context) (rs []*model.Custom, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
var res struct {
Code int
List []*model.Custom
}
if err = d.httpBigData.Get(c, d.customURL, ip, params, &res); err != nil {
log.Error("d.httpBigData.Get(%s) error(%v)", d.customURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("d.httpBigData.Get(%s) error(%v)", d.customURL, err)
err = ecode.Int(res.Code)
return
}
rs = res.List
return
}
func rankURI(rid int16, rankType string, day, arcType int) string {
if rankType == model.RankType[1] {
if arcType == 1 {
if rid > 0 {
return fmt.Sprintf(_rankAllRecRidURI, rid, day)
}
return fmt.Sprintf(_rankAllRecURI, day)
}
if rid > 0 {
return fmt.Sprintf(_rankAllRidURI, rid, day)
}
return fmt.Sprintf(_rankAllURI, day)
} else if rankType == model.RankType[2] {
if arcType == 1 {
if rid > 0 {
return fmt.Sprintf(_rankOriRecRidURI, rid, day)
}
return fmt.Sprintf(_rankOriRecURI, day)
}
if rid > 0 {
return fmt.Sprintf(_rankOriAllRidURI, rid, day)
}
return fmt.Sprintf(_rankOriAllURI, day)
}
if rid > 0 {
return fmt.Sprintf(_rankAllNewRidURI, rid, day)
}
return fmt.Sprintf(_rankAllNewURI, day)
}

View File

@@ -0,0 +1,139 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
func TestDaoRanking(t *testing.T) {
convey.Convey("Ranking", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(0)
rankType = int(0)
day = int(0)
arcType = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.c.Host.Rank+_rankURL).Reply(200).JSON(`{"code":0,"num":8,"list":[{"aid":33986715,"score":704},{"aid":33913315,"score":571}]}`)
res, err := d.Ranking(c, rid, rankType, day, arcType)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRankingIndex(t *testing.T) {
convey.Convey("RankingIndex", t, func(ctx convey.C) {
var (
c = context.Background()
day = int(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.c.Host.Rank+_rankURL).Reply(200).JSON(`{"code":0,"num":8,"list":[{"aid":33986715,"score":704},{"aid":33913315,"score":571}]}`)
res, err := d.RankingIndex(c, day)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRankingRegion(t *testing.T) {
convey.Convey("RankingRegion", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(0)
day = int(0)
original = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.c.Host.Rank+_rankURL).Reply(200).JSON(`{"code":0,"num":8,"list":[{"aid":33986715,"score":704},{"aid":33913315,"score":571}]}`)
res, err := d.RankingRegion(c, rid, day, original)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRankingRecommend(t *testing.T) {
convey.Convey("RankingRecommend", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.c.Host.Rank+_rankURL).Reply(200).JSON(`{"code":0,"num":8,"list":[{"aid":33986715,"score":704},{"aid":33913315,"score":571}]}`)
res, err := d.RankingRecommend(c, rid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRankingTag(t *testing.T) {
convey.Convey("RankingTag", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(0)
tagID = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.c.Host.Rank).Reply(200).JSON(`{"code":0,"num":8,"list":[{"aid":33986715,"score":704},{"aid":33913315,"score":571}]}`)
rs, err := d.RankingTag(c, rid, tagID)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRegionCustom(t *testing.T) {
convey.Convey("RegionCustom", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.customURL).Reply(200).JSON(`{"code":0,"num":8,"list":[{"aid":33986715},{"aid":33913315}]}`)
rs, err := d.RegionCustom(c)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaorankURI(t *testing.T) {
convey.Convey("rankURI", t, func(ctx convey.C) {
var (
rid = int16(0)
rankType = ""
day = int(0)
arcType = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := rankURI(rid, rankType, day, arcType)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,44 @@
package dao
import (
"context"
"fmt"
"strconv"
"strings"
"go-common/library/ecode"
"github.com/pkg/errors"
)
const _bnjConfURI = "/activity/v0/bainian/config"
// Bnj2019Conf .
func (d *Dao) Bnj2019Conf(c context.Context) (mids []int64, err error) {
var res struct {
Code int `json:"code"`
Data struct {
GreyStatus int `json:"grey_status"`
GreyUids string `json:"grey_uids"`
} `json:"data"`
}
if err = d.httpR.Get(c, d.bnjConfURL, "", nil, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.bnjConfURL)
return
}
if res.Data.GreyStatus == 1 {
midsStr := strings.Split(res.Data.GreyUids, ",")
for _, midStr := range midsStr {
var mid int64
if mid, err = strconv.ParseInt(midStr, 10, 64); err != nil {
err = fmt.Errorf("live grey_uids(%s)", res.Data.GreyUids)
return
}
mids = append(mids, mid)
}
}
return
}

View File

@@ -0,0 +1,21 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDao_Bnj2019Conf(t *testing.T) {
convey.Convey("ElecShow", t, func(ctx convey.C) {
var c = context.Background()
ctx.Convey("When everything gose positive", func(ctx convey.C) {
rs, err := d.Bnj2019Conf(c)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", rs)
})
})
})
}

View File

@@ -0,0 +1,222 @@
package dao
import (
"context"
xhttp "net/http"
"time"
"go-common/app/interface/main/web/conf"
"go-common/library/cache/redis"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/stat/prom"
)
const (
_rankURL = "/data/rank/"
_feedbackURL = "/x/internal/feedback/ugc/add"
_spaceTopPhotoURL = "/api/member/getTopPhoto"
_coinAddURL = "/x/coin/add"
_coinExpURL = "/x/coin/today/exp"
_elecShowURL = "/api/v2/rank/query/av"
_arcReportURL = "/videoup/archive/report"
_arcAppealURL = "/x/internal/workflow/appeal/add"
_appealTagsURL = "/x/internal/workflow/tag/list"
_relatedURL = "/recsys/related"
_onlineURL = "/x/internal/chat/server/ol"
_liveOnlineURL = "/room/v1/Online/get_total_online"
_helpListURL = "/kb/getQuestionTypeListByParentIdBilibili/4"
_helpSearchURL = "/kb/searchInerDocListBilibili/4"
_onlineListURL = "/x/internal/chat/num/top/aid"
_searchURL = "/main/search"
_searchRecURL = "/search/recommend"
_searchDefaultURL = "/query/recommend"
_searchUpRecURL = "/main/recommend"
_searchEggURI = "/x/admin/feed/eggSearchWeb"
_payWalletURL = "/wallet-int/wallet/getUserWalletInfo"
_payWalletOldURL = "/wallet/api/v1/info"
_special = "/x/internal/uper/special"
)
// Dao dao
type Dao struct {
// config
c *conf.Config
// http client
httpR *bm.Client
httpW *bm.Client
httpBigData *bm.Client
httpHelp *bm.Client
httpSearch *bm.Client
httpPay *bm.Client
bfsClient *xhttp.Client
// redis
redis *redis.Pool
redisBak *redis.Pool
redisNlExpire int32
redisNlBakExpire int32
redisRkExpire int32
redisRkBakExpire int32
redisDynamicBakExpire int32
redisArchiveBakExpire int32
redisTagBakExpire int32
redisCardBakExpire int32
redisRcExpire int32
redisRcBakExpire int32
redisArtBakExpire int32
redisIxIconExpire int32
redisIxIconBakExpire int32
redisHelpBakExpire int32
redisOlListBakExpire int32
redisWxHotExpire int32
redisWxHotBakExpire int32
redisAppealLimitExpire int32
// bigdata url
rankURL string
rankIndexURL string
rankRegionURL string
rankRecURL string
rankTagURL string
feedbackURL string
spaceTopPhotoURL string
coinAddURL string
coinExpURL string
customURL string
elecShowURL string
arcReportURL string
appealTagsURL string
arcAppealURL string
relatedURL string
onlineURL string
liveOnlineURL string
helpListURL string
helpSearchURL string
onlineListURL string
shopURL string
replyHotURL string
searchURL string
searchRecURL string
searchDefaultURL string
searchUpRecURL string
searchEggURL string
walletURL string
walletOldURL string
abServerURL string
wxHotURL string
bnjConfURL string
special string
// cache Prom
cacheProm *prom.Prom
}
// New dao new
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// config
c: c,
// http read client
httpR: bm.NewClient(c.HTTPClient.Read),
httpW: bm.NewClient(c.HTTPClient.Write),
httpBigData: bm.NewClient(c.HTTPClient.BigData),
httpHelp: bm.NewClient(c.HTTPClient.Help),
httpSearch: bm.NewClient(c.HTTPClient.Search),
httpPay: bm.NewClient(c.HTTPClient.Pay),
// init bfs http client
bfsClient: xhttp.DefaultClient,
// redis
redis: redis.NewPool(c.Redis.LocalRedis.Config),
redisBak: redis.NewPool(c.Redis.BakRedis.Config),
redisNlExpire: int32(time.Duration(c.Redis.LocalRedis.NewlistExpire) / time.Second),
redisNlBakExpire: int32(time.Duration(c.Redis.BakRedis.NewlistExpire) / time.Second),
redisRkExpire: int32(time.Duration(c.Redis.LocalRedis.RankingExpire) / time.Second),
redisRkBakExpire: int32(time.Duration(c.Redis.BakRedis.RankingExpire) / time.Second),
redisDynamicBakExpire: int32(time.Duration(c.Redis.BakRedis.RegionExpire) / time.Second),
redisArchiveBakExpire: int32(time.Duration(c.Redis.BakRedis.ArchiveExpire) / time.Second),
redisTagBakExpire: int32(time.Duration(c.Redis.BakRedis.TagExpire) / time.Second),
redisCardBakExpire: int32(time.Duration(c.Redis.BakRedis.CardExpire) / time.Second),
redisRcExpire: int32(time.Duration(c.Redis.LocalRedis.RcExpire) / time.Second),
redisRcBakExpire: int32(time.Duration(c.Redis.BakRedis.RcExpire) / time.Second),
redisArtBakExpire: int32(time.Duration(c.Redis.BakRedis.ArtUpExpire) / time.Second),
redisIxIconExpire: int32(time.Duration(c.Redis.LocalRedis.IndexIconExpire) / time.Second),
redisIxIconBakExpire: int32(time.Duration(c.Redis.BakRedis.IndexIconExpire) / time.Second),
redisHelpBakExpire: int32(time.Duration(c.Redis.BakRedis.HelpExpire) / time.Second),
redisOlListBakExpire: int32(time.Duration(c.Redis.BakRedis.OlListExpire) / time.Second),
redisWxHotExpire: int32(time.Duration(c.Redis.LocalRedis.WxHotExpire) / time.Second),
redisWxHotBakExpire: int32(time.Duration(c.Redis.BakRedis.WxHotExpire) / time.Second),
redisAppealLimitExpire: int32(time.Duration(c.Redis.BakRedis.AppealLimitExpire) / time.Second),
// remote source urls
rankURL: c.Host.Rank + _rankURL + _rankURI,
rankIndexURL: c.Host.Rank + _rankURL + _rankIndexURI,
rankRegionURL: c.Host.Rank + _rankURL + _rankRegionURI,
rankRecURL: c.Host.Rank + _rankURL + _rankRecURI,
wxHotURL: c.Host.Rank + _rankURL + _wxHotURI,
rankTagURL: c.Host.Rank + _rankTagURI,
feedbackURL: c.Host.API + _feedbackURL,
spaceTopPhotoURL: c.Host.Space + _spaceTopPhotoURL,
coinAddURL: c.Host.API + _coinAddURL,
coinExpURL: c.Host.API + _coinExpURL,
customURL: c.Host.Rank + _rankURL + _customURI,
elecShowURL: c.Host.Elec + _elecShowURL,
arcReportURL: c.Host.ArcAPI + _arcReportURL,
appealTagsURL: c.Host.API + _appealTagsURL,
arcAppealURL: c.Host.API + _arcAppealURL,
relatedURL: c.Host.Data + _relatedURL,
onlineURL: c.Host.API + _onlineURL,
liveOnlineURL: c.Host.LiveAPI + _liveOnlineURL,
helpListURL: c.Host.HelpAPI + _helpListURL,
helpSearchURL: c.Host.HelpAPI + _helpSearchURL,
onlineListURL: c.Host.API + _onlineListURL,
shopURL: c.Host.Mall + _shopURI,
replyHotURL: c.Host.API + _hotURI,
searchURL: c.Host.Search + _searchURL,
searchRecURL: c.Host.Search + _searchRecURL,
searchDefaultURL: c.Host.Search + _searchDefaultURL,
searchUpRecURL: c.Host.Search + _searchUpRecURL,
searchEggURL: c.Host.Manager + _searchEggURI,
walletURL: c.Host.Pay + _payWalletURL,
walletOldURL: c.Host.Pay + _payWalletOldURL,
abServerURL: c.Host.AbServer + _abServerURI,
bnjConfURL: c.Host.LiveAPI + _bnjConfURI,
special: c.Host.API + _special,
// prom
cacheProm: prom.CacheHit,
}
return d
}
// Ping check connection success.
func (dao *Dao) Ping(c context.Context) (err error) {
if err = dao.pingRedis(c); err != nil {
log.Error("dao.pingRedis error(%v)", err)
return
}
if err = dao.pingRedisBak(c); err != nil {
log.Error("dao.pingRedisBak error(%v)", err)
return
}
return
}
// Close close resource.
func (dao *Dao) Close() {
if dao.redis != nil {
dao.redis.Close()
}
if dao.redisBak != nil {
dao.redisBak.Close()
}
}
func (dao *Dao) pingRedis(c context.Context) (err error) {
conn := dao.redis.Get(c)
_, err = conn.Do("SET", "PING", "PONG")
conn.Close()
return
}
func (dao *Dao) pingRedisBak(c context.Context) (err error) {
conn := dao.redisBak.Get(c)
_, err = conn.Do("SET", "PING", "PONG")
conn.Close()
return
}

View File

@@ -0,0 +1,47 @@
package dao
import (
"flag"
"os"
"strings"
"testing"
"go-common/app/interface/main/web/conf"
"gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "local" {
flag.Set("app_id", "main.web-svr.web-interface")
flag.Set("conf_token", "bc99caee81f27661bd5ff50d1c0d58d6")
flag.Set("tree_id", "2685")
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-interface-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
d.httpR.SetTransport(gock.DefaultTransport)
d.httpBigData.SetTransport(gock.DefaultTransport)
d.httpHelp.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,163 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"go-common/app/service/main/archive/api"
dymdl "go-common/app/service/main/dynamic/model"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
_keyRegion = "dr_"
_keyRegionTag = "drt_"
_keyRegions = "drs_"
_keyRegionFmt = "%d_%d_%d"
_keyRegionTagFmt = "%d_%d_%d_%d"
)
func keyRegion(rid int32, pn, ps int) string {
return _keyRegion + fmt.Sprintf(_keyRegionFmt, rid, pn, ps)
}
func keyRegionTag(tagID int64, rid int32, pn, ps int) string {
return _keyRegionTag + fmt.Sprintf(_keyRegionTagFmt, tagID, rid, pn, ps)
}
// SetRegionBakCache set dynamic region data to cache.
func (d *Dao) SetRegionBakCache(c context.Context, rid int32, pn, ps int, rs *dymdl.DynamicArcs3) (err error) {
key := keyRegion(rid, pn, ps)
err = d.setBakCache(c, key, rs)
return
}
// RegionBakCache get dynamic region from cache.
func (d *Dao) RegionBakCache(c context.Context, rid int32, pn, ps int) (rs *dymdl.DynamicArcs3, err error) {
d.cacheProm.Incr("dynamic_region_remote_cache")
key := keyRegion(rid, pn, ps)
rs, err = d.bakCache(c, key)
if rs == nil {
log.Error("RegionBakCache d.bakCache(%d,%d,%d,%s) is nill", rid, pn, ps, key)
}
return
}
// SetRegionTagBakCache set dynamic region tag data to cache.
func (d *Dao) SetRegionTagBakCache(c context.Context, tagID int64, rid int32, pn, ps int, rs *dymdl.DynamicArcs3) (err error) {
key := keyRegionTag(tagID, rid, pn, ps)
err = d.setBakCache(c, key, rs)
return
}
// RegionTagBakCache get dynamic region tag from cache.
func (d *Dao) RegionTagBakCache(c context.Context, tagID int64, rid int32, pn, ps int) (rs *dymdl.DynamicArcs3, err error) {
d.cacheProm.Incr("dynamic_tag_remote_cache")
key := keyRegionTag(tagID, rid, pn, ps)
rs, err = d.bakCache(c, key)
if rs == nil {
log.Error("RegionTagBakCache d.bakCache(%d,%d,%d,%s) is nill", rid, pn, ps, key)
}
return
}
// SetRegionsBakCache set regions data to cache.
func (d *Dao) SetRegionsBakCache(c context.Context, rs map[int32][]*api.Arc) (err error) {
key := _keyRegions
conn := d.redisBak.Get(c)
defer conn.Close()
var bs []byte
if bs, err = json.Marshal(rs); err != nil {
log.Error("json.Marshal(%v) error(%v)", rs, 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
}
if err = conn.Send("EXPIRE", key, d.redisDynamicBakExpire); err != nil {
log.Error("conn.Send(EXPIRE,%s,%d) error(%v)", key, d.redisDynamicBakExpire, 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.Recevie(%d) error(%v0", i, err)
}
}
return
}
// RegionsBakCache get dynamic region data from cache.
func (d *Dao) RegionsBakCache(c context.Context) (rs map[int32][]*api.Arc, err error) {
d.cacheProm.Incr("dynamic_regions_remote_cache")
conn := d.redisBak.Get(c)
defer conn.Close()
var values []byte
if values, err = redis.Bytes(conn.Do("GET", _keyRegions)); err != nil {
if err == redis.ErrNil {
err = nil
log.Error("RegionsBakCache (%s) return nil ", _keyRegions)
} else {
log.Error("conn.Do(GET,%s) error(%v)", _keyRegions, err)
}
return
}
rs = make(map[int32][]*api.Arc)
if err = json.Unmarshal(values, &rs); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", values, err)
}
return
}
func (d *Dao) setBakCache(c context.Context, key string, rs *dymdl.DynamicArcs3) (err error) {
conn := d.redisBak.Get(c)
defer conn.Close()
var bs []byte
if bs, err = json.Marshal(rs); err != nil {
log.Error("json.Marshal(%v) error(%v)", rs, 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
}
if err = conn.Send("EXPIRE", key, d.redisDynamicBakExpire); err != nil {
log.Error("conn.Send(EXPIRE,%s,%d) error(%v)", key, d.redisDynamicBakExpire, 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(%d) error(%v)", i, err)
}
}
return
}
func (d *Dao) bakCache(c context.Context, key string) (rs *dymdl.DynamicArcs3, err error) {
conn := d.redisBak.Get(c)
defer conn.Close()
var values []byte
if values, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET,%s) error(%v)", key, err)
}
return
}
rs = &dymdl.DynamicArcs3{}
if err = json.Unmarshal(values, rs); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", values, err)
}
return
}

View File

@@ -0,0 +1,179 @@
package dao
import (
"context"
"go-common/app/service/main/archive/api"
dymdl "go-common/app/service/main/dynamic/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaokeyRegion(t *testing.T) {
convey.Convey("keyRegion", t, func(ctx convey.C) {
var (
rid = int32(0)
pn = int(0)
ps = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyRegion(rid, pn, ps)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaokeyRegionTag(t *testing.T) {
convey.Convey("keyRegionTag", t, func(ctx convey.C) {
var (
tagID = int64(0)
rid = int32(0)
pn = int(0)
ps = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyRegionTag(tagID, rid, pn, ps)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSetRegionBakCache(t *testing.T) {
convey.Convey("SetRegionBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int32(0)
pn = int(0)
ps = int(0)
rs = &dymdl.DynamicArcs3{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetRegionBakCache(c, rid, pn, ps, rs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoRegionBakCache(t *testing.T) {
convey.Convey("RegionBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int32(0)
pn = int(0)
ps = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rs, err := d.RegionBakCache(c, rid, pn, ps)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSetRegionTagBakCache(t *testing.T) {
convey.Convey("SetRegionTagBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
tagID = int64(0)
rid = int32(0)
pn = int(0)
ps = int(0)
rs = &dymdl.DynamicArcs3{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetRegionTagBakCache(c, tagID, rid, pn, ps, rs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoRegionTagBakCache(t *testing.T) {
convey.Convey("RegionTagBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
tagID = int64(0)
rid = int32(0)
pn = int(0)
ps = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rs, err := d.RegionTagBakCache(c, tagID, rid, pn, ps)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSetRegionsBakCache(t *testing.T) {
convey.Convey("SetRegionsBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
)
rs := map[int32][]*api.Arc{1111: {{Aid: 1111}}}
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetRegionsBakCache(c, rs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoRegionsBakCache(t *testing.T) {
convey.Convey("RegionsBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rs, err := d.RegionsBakCache(c)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaosetBakCache(t *testing.T) {
convey.Convey("setBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
key = ""
rs = &dymdl.DynamicArcs3{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.setBakCache(c, key, rs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaobakCache(t *testing.T) {
convey.Convey("bakCache", t, func(ctx convey.C) {
var (
c = context.Background()
key = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rs, err := d.bakCache(c, key)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,42 @@
package dao
import (
"context"
"net/url"
"strconv"
"time"
"go-common/app/interface/main/web/model"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
)
// ElecShow elec show.
func (d *Dao) ElecShow(c context.Context, mid, aid, loginID int64) (rs *model.ElecShow, err error) {
var (
params = url.Values{}
remoteIP = metadata.String(c, metadata.RemoteIP)
)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("mid", strconv.FormatInt(mid, 10))
if loginID > 0 {
params.Set("login_mid", strconv.FormatInt(loginID, 10))
}
params.Set("aid", strconv.FormatInt(aid, 10))
params.Set("act", "appkey")
var res struct {
Code int `json:"code"`
Data *model.ElecShow `json:"data"`
}
if err = d.httpR.Get(c, d.elecShowURL, remoteIP, params, &res); err != nil {
log.Error("ElecShow url(%s) error(%v)", d.elecShowURL+"?"+params.Encode(), err)
return
}
if res.Code != ecode.OK.Code() {
err = ecode.Int(res.Code)
return
}
rs = res.Data
return
}

View File

@@ -0,0 +1,29 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
func TestDaoElecShow(t *testing.T) {
convey.Convey("ElecShow", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2089809)
aid = int64(10100652)
loginID = int64(2089809)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.elecShowURL).Reply(200).JSON(`{"code":0,"data":{"show":true,"state":1}}`)
rs, err := d.ElecShow(c, mid, aid, loginID)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,56 @@
package dao
import (
"context"
"encoding/json"
"net/url"
"strconv"
"go-common/app/interface/main/web/model"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
)
const _platform = "web"
// Feedback feedback http request.
func (d *Dao) Feedback(c context.Context, feedParams *model.Feedback) (err error) {
var (
content []byte
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
if feedParams.Aid > 0 {
params.Set("aid", strconv.FormatInt(feedParams.Aid, 10))
}
params.Set("mid", strconv.FormatInt(feedParams.Mid, 10))
params.Set("tag_id", strconv.FormatInt(feedParams.TagID, 10))
if content, err = json.Marshal(feedParams.Content); err != nil {
log.Error("content json.Marshal(%+v) error(%v)", feedParams.Content, err)
} else {
params.Set("content", string(content))
}
params.Set("browser", feedParams.Browser)
params.Set("version", feedParams.Version)
params.Set("buvid", feedParams.Buvid)
params.Set("platform", _platform)
if feedParams.Email != "" {
params.Set("email", feedParams.Email)
}
if feedParams.QQ != "" {
params.Set("qq", feedParams.QQ)
}
var res struct {
Code int `json:"code"`
}
if err = d.httpW.Post(c, d.feedbackURL, ip, params, &res); err != nil {
log.Error("d.client.Post(%s) error(%v)", d.feedbackURL+"?"+params.Encode(), err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("d.client.Post(%s) code(%d)", d.feedbackURL+"?"+params.Encode(), res.Code)
err = ecode.Int(res.Code)
}
return
}

View File

@@ -0,0 +1,25 @@
package dao
import (
"context"
"testing"
"go-common/app/interface/main/web/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoFeedback(t *testing.T) {
convey.Convey("Feedback", t, func(ctx convey.C) {
var (
c = context.Background()
feedParams = &model.Feedback{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.Feedback(c, feedParams)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,294 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"go-common/app/interface/main/web/model"
"go-common/library/cache/redis"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/time"
)
const (
_notRobot = -1
_rsOk = "000000"
_hlKey = "hl_%s"
_hdKey = "hd_%s_%d_%d_%d"
)
// HelpList get help list.
func (d *Dao) HelpList(c context.Context, pTypeID string) (data []*model.HelpList, err error) {
var (
req *http.Request
params = url.Values{}
)
params.Set("parentTypeId", pTypeID)
params.Set("robotFlag", strconv.Itoa(_notRobot))
listURL := d.helpListURL + "?" + params.Encode()
if req, err = http.NewRequest("GET", listURL, nil); err != nil {
log.Error("Help http.NewRequest(%s) error(%v)", listURL, err)
return
}
var res struct {
Code string `json:"retCode"`
Data []*model.HelpList `json:"items"`
}
err = d.httpHelp.Do(c, req, &res)
if err != nil {
log.Error("Help d.httpHelp.Do(%s) error(%v)", listURL, err)
return
}
if res.Code != _rsOk {
log.Error("Help dao.httpHelp.Do(%s) error(%v)", listURL, err)
err = ecode.HelpListError
return
}
data = res.Data
return
}
func keyHl(pTypeID string) string {
return fmt.Sprintf(_hlKey, pTypeID)
}
func keyHd(qTypeID string, keyFlag, pn, ps int) string {
return fmt.Sprintf(_hdKey, qTypeID, keyFlag, pn, ps)
}
// SetHlCache set help list to cache.
func (d *Dao) SetHlCache(c context.Context, pTypeID string, Hl []*model.HelpList) (err error) {
conn := d.redisBak.Get(c)
defer conn.Close()
count := 0
key := keyHl(pTypeID)
if err = conn.Send("DEL", key); err != nil {
log.Error("conn.Send(DEL, %s) error(%v)", key, err)
return
}
count++
var bs []byte
for _, list := range Hl {
if bs, err = json.Marshal(list); err != nil {
log.Error("json.Marshal(%v) error (%v)", list, err)
return
}
if err = conn.Send("ZADD", key, list.SortNo, bs); err != nil {
log.Error("conn.Send(ZADD, %s, %s) error(%v)", key, string(bs), err)
return
}
count++
}
if err = conn.Send("EXPIRE", key, d.redisHelpBakExpire); err != nil {
log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.redisHelpBakExpire, err)
return
}
count++
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
for i := 0; i < count; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
}
return
}
// HlCache get help list from cache.
func (d *Dao) HlCache(c context.Context, pTypeID string) (res []*model.HelpList, err error) {
key := keyHl(pTypeID)
conn := d.redisBak.Get(c)
defer conn.Close()
values, err := redis.Values(conn.Do("ZREVRANGE", key, 0, -1))
if err != nil {
log.Error("conn.Do(ZREVRANGE, %s) error(%v)", key, err)
return
}
if len(values) == 0 {
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush() err(%v)", err)
return
}
for len(values) > 0 {
bs := []byte{}
if values, err = redis.Scan(values, &bs); err != nil {
log.Error("redis.Scan(%v) error(%v)", values, err)
return
}
list := &model.HelpList{}
if err = json.Unmarshal(bs, list); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", bs, err)
return
}
res = append(res, list)
}
return
}
// HelpDetail get help detail.
func (d *Dao) HelpDetail(c context.Context, qTypeID string, keyFlag, pn, ps int, ip string) (data []*model.HelpDeatil, total int, err error) {
var (
req *http.Request
params = url.Values{}
)
params.Set("questionTypeId", qTypeID)
params.Set("keyFlag", strconv.Itoa(keyFlag))
params.Set("keyWords", "")
params.Set("pageNo", strconv.Itoa(pn))
params.Set("pageSize", strconv.Itoa(ps))
params.Set("robotFlag", strconv.Itoa(_notRobot))
searchURL := d.helpSearchURL + "?" + params.Encode()
if req, err = http.NewRequest("GET", searchURL, nil); err != nil {
log.Error("Help http.NewRequest(%s) error(%v)", searchURL, err)
return
}
var res struct {
Code string `json:"retCode"`
Data []*model.HelpDeatil `json:"items"`
Total int `json:"totalCount"`
}
err = d.httpHelp.Do(c, req, &res)
if err != nil {
log.Error("Help d.httpHelp.Do(%s) error(%v)", searchURL, err)
return
}
if res.Code != _rsOk {
log.Error("Help dao.httpHelp.Do(%s) error(%v)", searchURL, err)
err = ecode.HelpDetailError
return
}
total = res.Total
data = res.Data
return
}
// HelpSearch get help search.
func (d *Dao) HelpSearch(c context.Context, pTypeID, keyWords string, keyFlag, pn, ps int) (data []*model.HelpDeatil, total int, err error) {
var (
req *http.Request
params = url.Values{}
)
params.Set("questionTypeId", pTypeID)
params.Set("keyWords", keyWords)
params.Set("keyFlag", strconv.Itoa(keyFlag))
params.Set("pageNo", strconv.Itoa(pn))
params.Set("pageSize", strconv.Itoa(ps))
params.Set("robotFlag", strconv.Itoa(_notRobot))
searchURL := d.helpSearchURL + "?" + params.Encode()
if req, err = http.NewRequest("GET", searchURL, nil); err != nil {
log.Error("Help http.NewRequest(%s) error(%v)", searchURL, err)
return
}
var res struct {
Code string `json:"retCode"`
Data []*model.HelpDeatil `json:"items"`
Total int `json:"totalCount"`
}
err = d.httpHelp.Do(c, req, &res)
if err != nil {
log.Error("Help d.httpHelp.Do(%s) error(%v)", searchURL, err)
return
}
if res.Code != _rsOk {
log.Error("Help dao.httpHelp.Do(%s) error(%v)", searchURL, err)
err = ecode.HelpSearchError
return
}
total = res.Total
data = res.Data
return
}
// SetDetailCache set help detail to cache.
func (d *Dao) SetDetailCache(c context.Context, qTypeID string, keyFlag, pn, ps, total int, data []*model.HelpDeatil) (err error) {
conn := d.redisBak.Get(c)
defer conn.Close()
count := 0
key := keyHd(qTypeID, keyFlag, pn, ps)
if err = conn.Send("DEL", key); err != nil {
log.Error("conn.Send(DEL, %s) error(%v)", key, err)
return
}
count++
var bs []byte
for _, detail := range data {
if bs, err = json.Marshal(detail); err != nil {
log.Error("json.Marshal(%v) error (%v)", detail, err)
return
}
if err = conn.Send("ZADD", key, combineHd(detail.UpdateTime, total), bs); err != nil {
log.Error("conn.Send(ZADD, %s, %s) error(%v)", key, string(bs), err)
return
}
count++
}
if err = conn.Send("EXPIRE", key, d.redisHelpBakExpire); err != nil {
log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.redisHelpBakExpire, err)
return
}
count++
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
for i := 0; i < count; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
}
return
}
// DetailCache get help detail to cache.
func (d *Dao) DetailCache(c context.Context, qTypeID string, keyFlag, pn, ps int) (res []*model.HelpDeatil, count int, err error) {
conn := d.redisBak.Get(c)
defer conn.Close()
key := keyHd(qTypeID, keyFlag, pn, ps)
values, err := redis.Values(conn.Do("ZREVRANGE", key, 0, -1, "WITHSCORES"))
if err != nil {
log.Error("conn.Do(ZREVRANGE, %s) error(%v)", key, err)
return
}
if len(values) == 0 {
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush() err(%v)", err)
return
}
var num int64
for len(values) > 0 {
bs := []byte{}
if values, err = redis.Scan(values, &bs, &num); err != nil {
log.Error("redis.Scan(%v) error(%v)", values, err)
return
}
detail := &model.HelpDeatil{}
if err = json.Unmarshal(bs, detail); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", bs, err)
return
}
res = append(res, detail)
}
count = fromHd(num)
return
}
func fromHd(i int64) int {
return int(i & 0xffff)
}
func combineHd(create time.Time, count int) int64 {
return create.Time().Unix()<<16 | int64(count)
}

View File

@@ -0,0 +1,209 @@
package dao
import (
"context"
"testing"
xtime "time"
"go-common/app/interface/main/web/model"
"go-common/library/time"
"github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
func TestDaoHelpList(t *testing.T) {
convey.Convey("HelpList", t, func(ctx convey.C) {
var (
c = context.Background()
pTypeID = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.helpListURL).Reply(200).JSON(`{"retCode":"000000"}`)
data, err := d.HelpList(c, pTypeID)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", data)
})
})
})
}
func TestDaokeyHl(t *testing.T) {
convey.Convey("keyHl", t, func(ctx convey.C) {
var (
pTypeID = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyHl(pTypeID)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaokeyHd(t *testing.T) {
convey.Convey("keyHd", t, func(ctx convey.C) {
var (
qTypeID = ""
keyFlag = int(0)
pn = int(0)
ps = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyHd(qTypeID, keyFlag, pn, ps)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSetHlCache(t *testing.T) {
convey.Convey("SetHlCache", t, func(ctx convey.C) {
var (
c = context.Background()
pTypeID = ""
Hl = []*model.HelpList{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetHlCache(c, pTypeID, Hl)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoHlCache(t *testing.T) {
convey.Convey("HlCache", t, func(ctx convey.C) {
var (
c = context.Background()
pTypeID = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.HlCache(c, pTypeID)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", res)
})
})
})
}
func TestDaoHelpDetail(t *testing.T) {
convey.Convey("HelpDetail", t, func(ctx convey.C) {
var (
c = context.Background()
qTypeID = ""
keyFlag = int(0)
pn = int(0)
ps = int(0)
ip = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.helpSearchURL).Reply(200).JSON(`{"retCode":"000000", "total":1}`)
data, total, err := d.HelpDetail(c, qTypeID, keyFlag, pn, ps, ip)
ctx.Convey("Then err should be nil.data,total should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldNotBeNil)
ctx.Printf("%+v", data)
})
})
})
}
func TestDaoHelpSearch(t *testing.T) {
convey.Convey("HelpSearch", t, func(ctx convey.C) {
var (
c = context.Background()
pTypeID = ""
keyWords = ""
keyFlag = int(0)
pn = int(0)
ps = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.helpSearchURL).Reply(200).JSON(`{"retCode":"000000", "total":1}`)
data, total, err := d.HelpSearch(c, pTypeID, keyWords, keyFlag, pn, ps)
ctx.Convey("Then err should be nil.data,total should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldNotBeNil)
ctx.Printf("%+v", data)
})
})
})
}
func TestDaoSetDetailCache(t *testing.T) {
convey.Convey("SetDetailCache", t, func(ctx convey.C) {
var (
c = context.Background()
qTypeID = ""
keyFlag = int(0)
pn = int(0)
ps = int(0)
total = int(0)
data = []*model.HelpDeatil{{AllTypeName: "1111"}, {AllTypeName: "2222"}}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetDetailCache(c, qTypeID, keyFlag, pn, ps, total, data)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoDetailCache(t *testing.T) {
convey.Convey("DetailCache", t, func(ctx convey.C) {
var (
c = context.Background()
qTypeID = ""
keyFlag = int(0)
pn = int(0)
ps = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, count, err := d.DetailCache(c, qTypeID, keyFlag, pn, ps)
ctx.Convey("Then err should be nil.res,count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
convey.Printf("%+v", res)
})
})
})
}
func TestDaofromHd(t *testing.T) {
convey.Convey("fromHd", t, func(ctx convey.C) {
var (
i = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := fromHd(i)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaocombineHd(t *testing.T) {
convey.Convey("combineHd", t, func(ctx convey.C) {
var (
create = time.Time(xtime.Now().Unix())
count = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := combineHd(create, count)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,92 @@
package dao
import (
"context"
"encoding/json"
resmdl "go-common/app/service/main/resource/model"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
_indexIconKey = "iik"
_indexIconBakKey = "b_iik"
)
// SetIndexIconCache set index icon cache and bak cache
func (d *Dao) SetIndexIconCache(c context.Context, data []*resmdl.IndexIcon) (err error) {
key := _indexIconKey
conn := d.redis.Get(c)
defer conn.Close()
if err = d.setIndexIconCache(conn, key, d.redisRcExpire, data); err != nil {
return
}
key = _indexIconBakKey
connBak := d.redisBak.Get(c)
err = d.setIndexIconCache(connBak, key, d.redisRcBakExpire, data)
connBak.Close()
return
}
func (d *Dao) setIndexIconCache(conn redis.Conn, key string, expire int32, data []*resmdl.IndexIcon) (err error) {
var bs []byte
if bs, err = json.Marshal(data); err != nil {
log.Error("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
}
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
}
// IndexIconCache get index icon cache
func (d *Dao) IndexIconCache(c context.Context) (res []*resmdl.IndexIcon, err error) {
key := _indexIconKey
conn := d.redis.Get(c)
defer conn.Close()
res, err = d.indexIconCache(conn, key)
return
}
// IndexIconBakCache get index icon bak cache
func (d *Dao) IndexIconBakCache(c context.Context) (res []*resmdl.IndexIcon, err error) {
d.cacheProm.Incr("indexicon_remote_cache")
key := _indexIconBakKey
conn := d.redisBak.Get(c)
defer conn.Close()
res, err = d.indexIconCache(conn, key)
return
}
func (d *Dao) indexIconCache(conn redis.Conn, key string) (res []*resmdl.IndexIcon, err error) {
var value []byte
if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
if err = json.Unmarshal(value, &res); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", value, err)
}
return
}

View File

@@ -0,0 +1,54 @@
package dao
import (
"context"
resmdl "go-common/app/service/main/resource/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoSetIndexIconCache(t *testing.T) {
convey.Convey("SetIndexIconCache", t, func(ctx convey.C) {
var (
c = context.Background()
data = []*resmdl.IndexIcon{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetIndexIconCache(c, data)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoIndexIconCache(t *testing.T) {
convey.Convey("IndexIconCache", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.IndexIconCache(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", res)
})
})
})
}
func TestDaoIndexIconBakCache(t *testing.T) {
convey.Convey("IndexIconBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.IndexIconBakCache(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", res)
})
})
})
}

View File

@@ -0,0 +1,130 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"go-common/app/service/main/archive/api"
"go-common/library/cache/redis"
"go-common/library/log"
"go-common/library/time"
)
const (
// new list
_keyNl = "n_%d_%d"
)
func keyNl(rid int32, tp int8) string {
return fmt.Sprintf(_keyNl, rid, tp)
}
func keyNlBak(rid int32, tp int8) string {
return _keyBakPrefix + keyNl(rid, tp)
}
// NewListCache get region rank list from cache.
func (d *Dao) NewListCache(c context.Context, rid int32, tp int8, start, end int) (arcs []*api.Arc, count int, err error) {
key := keyNl(rid, tp)
conn := d.redis.Get(c)
defer conn.Close()
arcs, count, err = d.nlCache(c, conn, key, start, end)
return
}
// NewListBakCache get region rank list from bak cache.
func (d *Dao) NewListBakCache(c context.Context, rid int32, tp int8, start, end int) (arcs []*api.Arc, count int, err error) {
d.cacheProm.Incr("newlist_remote_cache")
key := keyNlBak(rid, tp)
conn := d.redisBak.Get(c)
defer conn.Close()
arcs, count, err = d.nlCache(c, conn, key, start, end)
if len(arcs) == 0 {
log.Error("NewlistBakCache(%s,%d,%d) is nil", key, start, end)
}
return
}
func (d *Dao) nlCache(c context.Context, conn redis.Conn, key string, start, end int) (arcs []*api.Arc, count int, err error) {
values, err := redis.Values(conn.Do("ZREVRANGE", key, start, end, "WITHSCORES"))
if err != nil {
log.Error("conn.Do(ZREVRANGE, %s) error(%v)", key, err)
return
}
if len(values) == 0 {
return
}
var num int64
for len(values) > 0 {
bs := []byte{}
if values, err = redis.Scan(values, &bs, &num); err != nil {
log.Error("redis.Scan(%v) error(%v)", values, err)
return
}
arc := &api.Arc{}
if err = json.Unmarshal(bs, arc); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", bs, err)
return
}
arcs = append(arcs, arc)
}
count = from(num)
return
}
// SetNewListCache set region cache.
func (d *Dao) SetNewListCache(c context.Context, rid int32, tp int8, arcs []*api.Arc, count int) (err error) {
key := keyNl(rid, tp)
conn := d.redis.Get(c)
defer conn.Close()
if err = d.setNlCache(c, conn, key, d.redisNlExpire, arcs, count); err != nil {
return
}
key = keyNlBak(rid, tp)
connBak := d.redisBak.Get(c)
err = d.setNlCache(c, connBak, key, d.redisNlBakExpire, arcs, count)
connBak.Close()
return
}
func (d *Dao) setNlCache(c context.Context, conn redis.Conn, key string, expire int32, arcs []*api.Arc, num int) (err error) {
count := 0
if err = conn.Send("DEL", key); err != nil {
log.Error("conn.Send(DEL, %s) error(%v)", key, err)
return
}
count++
for _, arc := range arcs {
bs, _ := json.Marshal(arc)
if err = conn.Send("ZADD", key, combine(arc.PubDate, num), bs); err != nil {
log.Error("conn.Send(ZADD, %s, %s) error(%v)", key, string(bs), err)
return
}
count++
}
if err = conn.Send("EXPIRE", key, expire); err != nil {
log.Error("conn.Send(Expire, %s, %d) error(%v)", key, expire, err)
return
}
count++
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
for i := 0; i < count; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
}
return
}
func from(i int64) int {
return int(i & 0xffffff)
}
func combine(pubdate time.Time, count int) int64 {
return pubdate.Time().Unix()<<24 | int64(count)
}

View File

@@ -0,0 +1,129 @@
package dao
import (
"context"
"testing"
xtime "time"
"go-common/app/service/main/archive/api"
"go-common/library/time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaokeyNl(t *testing.T) {
convey.Convey("keyNl", t, func(ctx convey.C) {
var (
rid = int32(0)
tp = int8(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyNl(rid, tp)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaokeyNlBak(t *testing.T) {
convey.Convey("keyNlBak", t, func(ctx convey.C) {
var (
rid = int32(0)
tp = int8(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyNlBak(rid, tp)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoNewListCache(t *testing.T) {
convey.Convey("NewListCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int32(0)
tp = int8(0)
start = int(0)
end = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
arcs, count, err := d.NewListCache(c, rid, tp, start, end)
ctx.Convey("Then err should be nil.arcs,count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
ctx.Printf("%+v", arcs)
})
})
})
}
func TestDaoNewListBakCache(t *testing.T) {
convey.Convey("NewListBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int32(0)
tp = int8(0)
start = int(0)
end = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
arcs, count, err := d.NewListBakCache(c, rid, tp, start, end)
ctx.Convey("Then err should be nil.arcs,count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
ctx.Printf("%+v", arcs)
})
})
})
}
func TestDaoSetNewListCache(t *testing.T) {
convey.Convey("SetNewListCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int32(0)
tp = int8(0)
arcs = []*api.Arc{}
count = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetNewListCache(c, rid, tp, arcs, count)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaofrom(t *testing.T) {
convey.Convey("from", t, func(ctx convey.C) {
var (
i = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := from(i)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaocombine(t *testing.T) {
convey.Convey("combine", t, func(ctx convey.C) {
var (
pubdate = time.Time(xtime.Now().Unix())
count = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := combine(pubdate, count)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,81 @@
package dao
import (
"context"
"net/url"
"strconv"
"go-common/app/interface/main/web/model"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
)
// OnlineCount get online count
func (d *Dao) OnlineCount(c context.Context) (data *model.OnlineCount, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
var res struct {
Code int `json:"code"`
Data *model.OnlineCount `json:"data"`
}
if err = d.httpR.Get(c, d.onlineURL, ip, params, &res); err != nil {
log.Error(" d.httpW.Get.Get(%s) error(%v)", d.onlineURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("d.httpW.Get(%s) code error(%d)", d.onlineURL, res.Code)
err = ecode.Int(res.Code)
return
}
data = res.Data
return
}
// LiveOnlineCount get live online count
func (d *Dao) LiveOnlineCount(c context.Context) (data *model.LiveOnlineCount, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
var res struct {
Code int `json:"code"`
Data *model.LiveOnlineCount `json:"data"`
}
if err = d.httpR.Get(c, d.liveOnlineURL, ip, params, &res); err != nil {
log.Error(" d.httpW.Get.Get(%s) error(%v)", d.liveOnlineURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("d.httpW.Get(%s) code error(%d)", d.liveOnlineURL, res.Code)
err = ecode.Int(res.Code)
return
}
data = res.Data
return
}
// OnlineList get online list
func (d *Dao) OnlineList(c context.Context, num int64) (data []*model.OnlineAid, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params.Set("num", strconv.FormatInt(num, 10))
var res struct {
Code int `json:"code"`
Data []*model.OnlineAid `json:"data"`
}
if err = d.httpR.Get(c, d.onlineListURL, ip, params, &res); err != nil {
log.Error(" d.httpR.Get.Get(%s) error(%v)", d.onlineListURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("d.httpR.Get(%s) code error(%d)", d.onlineListURL, res.Code)
return
}
data = res.Data
return
}

View File

@@ -0,0 +1,61 @@
package dao
import (
"context"
"encoding/json"
"go-common/app/interface/main/web/model"
"go-common/library/cache/redis"
"go-common/library/log"
)
const _onlineListKey = "olk"
// OnlineListBakCache get online list bak cache.
func (d *Dao) OnlineListBakCache(c context.Context) (rs []*model.OnlineArc, err error) {
conn := d.redisBak.Get(c)
defer conn.Close()
var values []byte
if values, err = redis.Bytes(conn.Do("GET", _onlineListKey)); err != nil {
if err == redis.ErrNil {
err = nil
log.Warn("OnlineListBakCache redis (%s) return nil ", _onlineListKey)
} else {
log.Error("OnlineListBakCache conn.Do(GET,%s) error(%v)", _onlineListKey, err)
}
return
}
if err = json.Unmarshal(values, &rs); err != nil {
log.Error("OnlineListBakCache json.Unmarshal(%v) error(%v)", values, err)
}
return
}
// SetOnlineListBakCache set online list bak cache.
func (d *Dao) SetOnlineListBakCache(c context.Context, data []*model.OnlineArc) (err error) {
conn := d.redisBak.Get(c)
defer conn.Close()
var bs []byte
if bs, err = json.Marshal(data); err != nil {
log.Error("SetOnlineListBakCache json.Marshal(%v) error(%v)", data, err)
return
}
if err = conn.Send("SET", _onlineListKey, bs); err != nil {
log.Error("SetOnlineListBakCache conn.Send(SET,%s,%s) error(%v)", _onlineListKey, string(bs), err)
return
}
if err = conn.Send("EXPIRE", _onlineListKey, d.redisOlListBakExpire); err != nil {
log.Error("SetOnlineListBakCache conn.Send(EXPIRE,%s,%d) error(%v)", _onlineListKey, d.redisOlListBakExpire, err)
return
}
if err = conn.Flush(); err != nil {
log.Error("SetOnlineListBakCache conn.Flush error(%v)", err)
return
}
for i := 0; i < 2; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("SetOnlineListBakCache conn.Recevie(%d) error(%v0", i, err)
}
}
return
}

View File

@@ -0,0 +1,40 @@
package dao
import (
"context"
"testing"
"go-common/app/interface/main/web/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoSetOnlineListBakCache(t *testing.T) {
convey.Convey("SetOnlineListBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
data = []*model.OnlineArc{{OnlineCount: 111}, {OnlineCount: 222}}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetOnlineListBakCache(c, data)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoOnlineListBakCache(t *testing.T) {
convey.Convey("OnlineListBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rs, err := d.OnlineListBakCache(c)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,54 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoOnlineCount(t *testing.T) {
convey.Convey("OnlineCount", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.OnlineCount(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)
})
})
})
}
func TestDaoLiveOnlineCount(t *testing.T) {
convey.Convey("LiveOnlineCount", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.LiveOnlineCount(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)
})
})
})
}
func TestDaoOnlineList(t *testing.T) {
convey.Convey("OnlineList", t, func(ctx convey.C) {
var (
c = context.Background()
num = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.OnlineList(c, num)
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)
})
})
})
}

View File

@@ -0,0 +1,113 @@
package dao
import (
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"go-common/app/interface/main/web/model"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
"github.com/pkg/errors"
)
// Wallet get user bcoin doc:http://info.bilibili.co/pages/viewpage.action?pageId=7559096
func (d *Dao) Wallet(c context.Context, mid int64) (wallet *model.Wallet, err error) {
const (
plat = 3
platStr = "3"
customID = "10008"
)
params := url.Values{}
params.Set("customerId", customID)
params.Set("platformType", platStr)
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("traceId", strconv.FormatInt(time.Now().Unix(), 10))
params.Set("timestamp", strconv.FormatInt(time.Now().UnixNano()/1000, 10))
params.Set("signType", "MD5")
params.Set("appkey", d.c.HTTPClient.Pay.Key)
type pJSON struct {
CustomerID string `json:"customerId"`
PlatformType int `json:"platformType"`
Mid int64 `json:"mid"`
TraceID string `json:"traceId"`
Timestamp string `json:"timestamp"`
SignType string `json:"signType"`
Appkey string `json:"appkey"`
Sign string `json:"sign"`
}
tmp := params.Encode() + d.c.HTTPClient.Pay.Secret
if strings.IndexByte(tmp, '+') > -1 {
tmp = strings.Replace(tmp, "+", "%20", -1)
}
mh := md5.Sum([]byte(tmp))
sign := hex.EncodeToString(mh[:])
p := &pJSON{
CustomerID: customID,
PlatformType: plat,
Mid: mid,
TraceID: params.Get("traceId"),
Timestamp: params.Get("timestamp"),
SignType: params.Get("signType"),
Appkey: params.Get("appkey"),
Sign: sign,
}
bs, _ := json.Marshal(p)
req, _ := http.NewRequest("POST", d.walletURL, strings.NewReader(string(bs)))
req.Header.Set("Content-Type", "application/json")
var res struct {
Code int `json:"code"`
Data struct {
// DefaultBp float32 `json:"defaultBp"`
CouponBalance float32 `json:"couponBalance"`
AvailableBp float32 `json:"availableBp"`
} `json:"data"`
}
if err = d.httpPay.Do(c, req, &res); err != nil {
return
}
if res.Code != 0 {
err = errors.Wrap(ecode.Int(res.Code), d.walletURL+"?"+params.Encode())
log.Error("account pay url(%s) error(%v)", d.walletURL+"?"+params.Encode(), res.Code)
return
}
wallet = &model.Wallet{
Mid: mid,
BcoinBalance: res.Data.AvailableBp,
CouponBalance: res.Data.CouponBalance,
}
return
}
// OldWallet get wallet info
func (d *Dao) OldWallet(c context.Context, mid int64) (w *model.Wallet, err error) {
var (
params = url.Values{}
remoteIP = metadata.String(c, metadata.RemoteIP)
)
params.Set("mid", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
Data *model.Wallet `json:"data"`
}
err = d.httpR.Get(c, d.walletOldURL, remoteIP, params, &res)
if err != nil {
log.Error("account pay url(%s) error(%v)", d.walletOldURL+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
log.Error("account pay url(%s) error(%v)", d.walletOldURL+"?"+params.Encode(), res.Code)
err = ecode.Int(res.Code)
return
}
w = res.Data
return
}

View File

@@ -0,0 +1,40 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoWallet(t *testing.T) {
convey.Convey("Wallet", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
wallet, err := d.Wallet(c, mid)
ctx.Convey("Then err should be nil.wallet should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(wallet, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoOldWallet(t *testing.T) {
convey.Convey("OldWallet", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
w, err := d.OldWallet(c, mid)
ctx.Convey("Then err should be nil.w should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(w, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,500 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"go-common/app/interface/main/web/model"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
_keyRkFmt = "r_v2_%d_%d_%d_%d"
_keyRkIndexFmt = "ri_%d"
_keyRkRegionFmt = "rc_%d_%d_%d"
_keyRkRecommendFmt = "rr_%d"
_keyRkTagFmt = "rt_%d_%d"
_keyRegionCustom = "krc"
_keyRegionCustomBak = _keyBakPrefix + _keyRegionCustom
_keyBakPrefix = "b_"
)
func keyRkList(rid int16, rankType, day, arcType int) string {
return fmt.Sprintf(_keyRkFmt, rid, rankType, day, arcType)
}
func keyRkListBak(rid int16, rankType, day, arcType int) string {
return _keyBakPrefix + keyRkList(rid, rankType, day, arcType)
}
func keyRkIndex(day int) string {
return fmt.Sprintf(_keyRkIndexFmt, day)
}
func keyRkIndexBak(day int) string {
return _keyBakPrefix + keyRkIndex(day)
}
func keyRkRegionList(rid int16, day, original int) string {
return fmt.Sprintf(_keyRkRegionFmt, rid, day, original)
}
func keyRkRegionListBak(rid int16, day, original int) string {
return _keyBakPrefix + keyRkRegionList(rid, day, original)
}
func keyRkRecommendList(rid int16) string {
return fmt.Sprintf(_keyRkRecommendFmt, rid)
}
func keyRkRecommendListBak(rid int16) string {
return _keyBakPrefix + fmt.Sprintf(_keyRkRecommendFmt, rid)
}
func keyRkTagList(rid int16, tagID int64) string {
return fmt.Sprintf(_keyRkTagFmt, rid, tagID)
}
func keyRkTagListBak(rid int16, tagID int64) string {
return _keyBakPrefix + keyRkTagList(rid, tagID)
}
// RankingCache get rank list from cache.
func (d *Dao) RankingCache(c context.Context, rid int16, rankType, day, arcType int) (data *model.RankData, err error) {
key := keyRkList(rid, rankType, day, arcType)
conn := d.redis.Get(c)
defer conn.Close()
data, err = d.rankingCache(conn, key)
return
}
// RankingBakCache get rank list from bak cache.
func (d *Dao) RankingBakCache(c context.Context, rid int16, rankType, day, arcType int) (data *model.RankData, err error) {
d.cacheProm.Incr("ranking_remote_cache")
key := keyRkListBak(rid, rankType, day, arcType)
conn := d.redisBak.Get(c)
defer conn.Close()
data, err = d.rankingCache(conn, key)
if data == nil || len(data.List) == 0 {
log.Error("RankingBakCache(%s) is nil", key)
}
return
}
// RankingIndexCache get rank index from cache.
func (d *Dao) RankingIndexCache(c context.Context, day int) (arcs []*model.IndexArchive, err error) {
key := keyRkIndex(day)
conn := d.redis.Get(c)
defer conn.Close()
arcs, err = d.rankingIndexCache(conn, key)
return
}
// RankingIndexBakCache get rank index from bak cache.
func (d *Dao) RankingIndexBakCache(c context.Context, day int) (arcs []*model.IndexArchive, err error) {
d.cacheProm.Incr("ranking_index_remote_cache")
key := keyRkIndexBak(day)
conn := d.redisBak.Get(c)
defer conn.Close()
arcs, err = d.rankingIndexCache(conn, key)
if len(arcs) == 0 {
log.Error("RankingIndexBakCache(%s) is nil", key)
}
return
}
// RankingRegionCache get rank cate list from cache.
func (d *Dao) RankingRegionCache(c context.Context, rid int16, day, original int) (arcs []*model.RegionArchive, err error) {
key := keyRkRegionList(rid, day, original)
conn := d.redis.Get(c)
defer conn.Close()
arcs, err = d.rankingRegionCache(conn, key)
return
}
// RankingRegionBakCache get rank cate list from bak cache.
func (d *Dao) RankingRegionBakCache(c context.Context, rid int16, day, original int) (arcs []*model.RegionArchive, err error) {
d.cacheProm.Incr("ranking_region_remote_cache")
key := keyRkRegionListBak(rid, day, original)
conn := d.redisBak.Get(c)
defer conn.Close()
arcs, err = d.rankingRegionCache(conn, key)
if len(arcs) == 0 {
log.Error("RankingRegionBakCache(%s) is nil", key)
}
return
}
// RankingRecommendCache get rank recommend list from cache.
func (d *Dao) RankingRecommendCache(c context.Context, rid int16) (arcs []*model.IndexArchive, err error) {
key := keyRkRecommendList(rid)
conn := d.redis.Get(c)
defer conn.Close()
arcs, err = d.rankingIndexCache(conn, key)
return
}
// RankingRecommendBakCache get rank recommend list from bak cache.
func (d *Dao) RankingRecommendBakCache(c context.Context, rid int16) (arcs []*model.IndexArchive, err error) {
d.cacheProm.Incr("ranking_rec_remote_cache")
key := keyRkRecommendListBak(rid)
conn := d.redisBak.Get(c)
defer conn.Close()
arcs, err = d.rankingIndexCache(conn, key)
if len(arcs) == 0 {
log.Error("RankingRecommendBakCache(%s) is nil", key)
}
return
}
// RankingTagCache get ranking tag from cache.
func (d *Dao) RankingTagCache(c context.Context, rid int16, tagID int64) (arcs []*model.TagArchive, err error) {
key := keyRkTagList(rid, tagID)
conn := d.redis.Get(c)
defer conn.Close()
arcs, err = d.rankingTagCache(conn, key)
return
}
// RankingTagBakCache get ranking tag from bak cache.
func (d *Dao) RankingTagBakCache(c context.Context, rid int16, tagID int64) (arcs []*model.TagArchive, err error) {
d.cacheProm.Incr("ranking_tag_remote_cache")
key := keyRkTagListBak(rid, tagID)
conn := d.redisBak.Get(c)
defer conn.Close()
arcs, err = d.rankingTagCache(conn, key)
if len(arcs) == 0 {
log.Error("RankingTagBakCache(%s) is nil", key)
}
return
}
// RegionCustomCache get region custom data from cache
func (d *Dao) RegionCustomCache(c context.Context) (res []*model.Custom, err error) {
key := _keyRegionCustom
conn := d.redis.Get(c)
defer conn.Close()
res, err = regionCustomCache(conn, key)
return
}
// RegionCustomBakCache get region custom data from cache
func (d *Dao) RegionCustomBakCache(c context.Context) (res []*model.Custom, err error) {
key := _keyRegionCustomBak
conn := d.redis.Get(c)
defer conn.Close()
res, err = regionCustomCache(conn, key)
return
}
func regionCustomCache(conn redis.Conn, key string) (res []*model.Custom, err error) {
var value []byte
if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
res = []*model.Custom{}
if err = json.Unmarshal(value, &res); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", value, err)
}
return
}
// SetRegionCustomCache set region custom data cache
func (d *Dao) SetRegionCustomCache(c context.Context, data []*model.Custom) (err error) {
key := _keyRegionCustom
conn := d.redis.Get(c)
defer conn.Close()
if err = d.setRegionCustomCache(conn, key, d.redisRcExpire, data); err != nil {
return
}
key = _keyRegionCustomBak
connBak := d.redisBak.Get(c)
err = d.setRegionCustomCache(connBak, key, d.redisRcBakExpire, data)
connBak.Close()
return
}
func (d *Dao) setRegionCustomCache(conn redis.Conn, key string, expire int32, data []*model.Custom) (err error) {
var bs []byte
if bs, err = json.Marshal(data); err != nil {
log.Error("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
}
if err = conn.Send("EXPIRE", key, d.redisRkExpire); err != nil {
log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.redisRkExpire, 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
}
func (d *Dao) rankingCache(conn redis.Conn, key string) (arcs *model.RankData, err error) {
var value []byte
if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
arcs = new(model.RankData)
if err = json.Unmarshal(value, &arcs); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", value, err)
}
return
}
func (d *Dao) rankingIndexCache(conn redis.Conn, key string) (arcs []*model.IndexArchive, err error) {
var value []byte
if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
arcs = []*model.IndexArchive{}
if err = json.Unmarshal(value, &arcs); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", value, err)
}
return
}
func (d *Dao) rankingRegionCache(conn redis.Conn, key string) (arcs []*model.RegionArchive, err error) {
var value []byte
if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
arcs = []*model.RegionArchive{}
if err = json.Unmarshal(value, &arcs); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", value, err)
}
return
}
func (d *Dao) rankingTagCache(conn redis.Conn, key string) (arcs []*model.TagArchive, err error) {
var value []byte
if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
arcs = []*model.TagArchive{}
if err = json.Unmarshal(value, &arcs); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", value, err)
}
return
}
// SetRankingCache set ranking data to cache
func (d *Dao) SetRankingCache(c context.Context, rid int16, rankType, day, arcType int, data *model.RankData) (err error) {
key := keyRkList(rid, rankType, day, arcType)
conn := d.redis.Get(c)
defer conn.Close()
if err = d.setRkCache(c, conn, key, d.redisRkExpire, data); err != nil {
return
}
key = keyRkListBak(rid, rankType, day, arcType)
connBak := d.redisBak.Get(c)
err = d.setRkCache(c, connBak, key, d.redisRkBakExpire, data)
connBak.Close()
return
}
// SetRankingIndexCache set ranking index data to cache
func (d *Dao) SetRankingIndexCache(c context.Context, day int, arcs []*model.IndexArchive) (err error) {
key := keyRkIndex(day)
conn := d.redis.Get(c)
defer conn.Close()
if err = d.setRkIndexCache(c, conn, key, d.redisRkExpire, arcs); err != nil {
return
}
key = keyRkIndexBak(day)
connBak := d.redisBak.Get(c)
err = d.setRkIndexCache(c, connBak, key, d.redisRkBakExpire, arcs)
connBak.Close()
return
}
// SetRankingRegionCache set ranking data to cache
func (d *Dao) SetRankingRegionCache(c context.Context, rid int16, day, original int, arcs []*model.RegionArchive) (err error) {
key := keyRkRegionList(rid, day, original)
conn := d.redis.Get(c)
defer conn.Close()
if err = d.setRkRegionCache(c, conn, key, d.redisRkExpire, arcs); err != nil {
return
}
key = keyRkRegionListBak(rid, day, original)
connBak := d.redisBak.Get(c)
err = d.setRkRegionCache(c, connBak, key, d.redisRkBakExpire, arcs)
connBak.Close()
return
}
// SetRankingRecommendCache set ranking data to bak cache
func (d *Dao) SetRankingRecommendCache(c context.Context, rid int16, arcs []*model.IndexArchive) (err error) {
key := keyRkRecommendList(rid)
conn := d.redis.Get(c)
defer conn.Close()
if err = d.setRkIndexCache(c, conn, key, d.redisRkExpire, arcs); err != nil {
return
}
key = keyRkRecommendListBak(rid)
connBak := d.redisBak.Get(c)
err = d.setRkIndexCache(c, connBak, key, d.redisRkBakExpire, arcs)
connBak.Close()
return
}
// SetRankingTagCache set ranking tag data to cache
func (d *Dao) SetRankingTagCache(c context.Context, rid int16, tagID int64, arcs []*model.TagArchive) (err error) {
key := keyRkTagList(rid, tagID)
conn := d.redis.Get(c)
defer conn.Close()
if err = d.setRkTagCache(c, conn, key, d.redisRkExpire, arcs); err != nil {
return
}
key = keyRkTagListBak(rid, tagID)
connBak := d.redisBak.Get(c)
err = d.setRkTagCache(c, connBak, key, d.redisRkBakExpire, arcs)
connBak.Close()
return
}
func (d *Dao) setRkCache(c context.Context, conn redis.Conn, key string, expire int32, arcs *model.RankData) (err error) {
var bs []byte
if bs, err = json.Marshal(arcs); err != nil {
log.Error("json.Marshal(%v) error (%v)", arcs, 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
}
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
}
func (d *Dao) setRkIndexCache(c context.Context, conn redis.Conn, key string, expire int32, arcs []*model.IndexArchive) (err error) {
var bs []byte
if bs, err = json.Marshal(arcs); err != nil {
log.Error("json.Marshal(%v) error (%v)", arcs, 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
}
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
}
func (d *Dao) setRkRegionCache(c context.Context, conn redis.Conn, key string, expire int32, arcs []*model.RegionArchive) (err error) {
var bs []byte
if bs, err = json.Marshal(arcs); err != nil {
log.Error("json.Marshal(%v) error (%v)", arcs, 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
}
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
}
func (d *Dao) setRkTagCache(c context.Context, conn redis.Conn, key string, expire int32, arcs []*model.TagArchive) (err error) {
var bs []byte
if bs, err = json.Marshal(arcs); err != nil {
log.Error("json.Marshal(%v) error (%v)", arcs, 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
}
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
}

View File

@@ -0,0 +1,465 @@
package dao
import (
"context"
"testing"
"go-common/app/interface/main/web/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaokeyRkList(t *testing.T) {
convey.Convey("keyRkList", t, func(ctx convey.C) {
var (
rid = int16(1)
rankType = int(0)
day = int(1)
arcType = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyRkList(rid, rankType, day, arcType)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaokeyRkListBak(t *testing.T) {
convey.Convey("keyRkListBak", t, func(ctx convey.C) {
var (
rid = int16(0)
rankType = int(0)
day = int(0)
arcType = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyRkListBak(rid, rankType, day, arcType)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaokeyRkIndex(t *testing.T) {
convey.Convey("keyRkIndex", t, func(ctx convey.C) {
var (
day = int(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyRkIndex(day)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaokeyRkIndexBak(t *testing.T) {
convey.Convey("keyRkIndexBak", t, func(ctx convey.C) {
var (
day = int(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyRkIndexBak(day)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaokeyRkRegionList(t *testing.T) {
convey.Convey("keyRkRegionList", t, func(ctx convey.C) {
var (
rid = int16(1)
day = int(3)
original = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyRkRegionList(rid, day, original)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaokeyRkRegionListBak(t *testing.T) {
convey.Convey("keyRkRegionListBak", t, func(ctx convey.C) {
var (
rid = int16(1)
day = int(3)
original = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyRkRegionListBak(rid, day, original)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaokeyRkRecommendList(t *testing.T) {
convey.Convey("keyRkRecommendList", t, func(ctx convey.C) {
var (
rid = int16(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyRkRecommendList(rid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaokeyRkRecommendListBak(t *testing.T) {
convey.Convey("keyRkRecommendListBak", t, func(ctx convey.C) {
var (
rid = int16(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyRkRecommendListBak(rid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaokeyRkTagList(t *testing.T) {
convey.Convey("keyRkTagList", t, func(ctx convey.C) {
var (
rid = int16(1)
tagID = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyRkTagList(rid, tagID)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaokeyRkTagListBak(t *testing.T) {
convey.Convey("keyRkTagListBak", t, func(ctx convey.C) {
var (
rid = int16(1)
tagID = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyRkTagListBak(rid, tagID)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRankingCache(t *testing.T) {
convey.Convey("RankingCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(1)
rankType = int(1)
day = int(3)
arcType = int(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.RankingCache(c, rid, rankType, day, arcType)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", data)
})
})
})
}
func TestDaoRankingBakCache(t *testing.T) {
convey.Convey("RankingBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(1)
rankType = int(1)
day = int(3)
arcType = int(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.RankingBakCache(c, rid, rankType, day, arcType)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", data)
})
})
})
}
func TestDaoRankingIndexCache(t *testing.T) {
convey.Convey("RankingIndexCache", t, func(ctx convey.C) {
var (
c = context.Background()
day = int(3)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
arcs, err := d.RankingIndexCache(c, day)
ctx.Convey("Then err should be nil.arcs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", arcs)
})
})
})
}
func TestDaoRankingIndexBakCache(t *testing.T) {
convey.Convey("RankingIndexBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
day = int(3)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
arcs, err := d.RankingIndexBakCache(c, day)
ctx.Convey("Then err should be nil.arcs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", arcs)
})
})
})
}
func TestDaoRankingRegionCache(t *testing.T) {
convey.Convey("RankingRegionCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(1)
day = int(3)
original = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
arcs, err := d.RankingRegionCache(c, rid, day, original)
ctx.Convey("Then err should be nil.arcs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", arcs)
})
})
})
}
func TestDaoRankingRegionBakCache(t *testing.T) {
convey.Convey("RankingRegionBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(1)
day = int(3)
original = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
arcs, err := d.RankingRegionBakCache(c, rid, day, original)
ctx.Convey("Then err should be nil.arcs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", arcs)
})
})
})
}
func TestDaoRankingRecommendCache(t *testing.T) {
convey.Convey("RankingRecommendCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
arcs, err := d.RankingRecommendCache(c, rid)
ctx.Convey("Then err should be nil.arcs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", arcs)
})
})
})
}
func TestDaoRankingRecommendBakCache(t *testing.T) {
convey.Convey("RankingRecommendBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
arcs, err := d.RankingRecommendBakCache(c, rid)
ctx.Convey("Then err should be nil.arcs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", arcs)
})
})
})
}
func TestDaoRankingTagCache(t *testing.T) {
convey.Convey("RankingTagCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(0)
tagID = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
arcs, err := d.RankingTagCache(c, rid, tagID)
ctx.Convey("Then err should be nil.arcs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", arcs)
})
})
})
}
func TestDaoRankingTagBakCache(t *testing.T) {
convey.Convey("RankingTagBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(0)
tagID = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
arcs, err := d.RankingTagBakCache(c, rid, tagID)
ctx.Convey("Then err should be nil.arcs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", arcs)
})
})
})
}
func TestDaoSetRegionCustomCache(t *testing.T) {
convey.Convey("SetRegionCustomCache", t, func(ctx convey.C) {
var (
c = context.Background()
data = []*model.Custom{{Aid: 1111}, {Aid: 2222}}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetRegionCustomCache(c, data)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoRegionCustomCache(t *testing.T) {
convey.Convey("RegionCustomCache", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.RegionCustomCache(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRegionCustomBakCache(t *testing.T) {
convey.Convey("RegionCustomBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.RegionCustomBakCache(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSetRankingCache(t *testing.T) {
convey.Convey("SetRankingCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(0)
rankType = int(0)
day = int(0)
arcType = int(0)
data = &model.RankData{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetRankingCache(c, rid, rankType, day, arcType, data)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoSetRankingIndexCache(t *testing.T) {
convey.Convey("SetRankingIndexCache", t, func(ctx convey.C) {
var (
c = context.Background()
day = int(0)
arcs = []*model.IndexArchive{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetRankingIndexCache(c, day, arcs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoSetRankingRegionCache(t *testing.T) {
convey.Convey("SetRankingRegionCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(0)
day = int(0)
original = int(0)
arcs = []*model.RegionArchive{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetRankingRegionCache(c, rid, day, original, arcs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoSetRankingRecommendCache(t *testing.T) {
convey.Convey("SetRankingRecommendCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(0)
arcs = []*model.IndexArchive{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetRankingRecommendCache(c, rid, arcs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoSetRankingTagCache(t *testing.T) {
convey.Convey("SetRankingTagCache", t, func(ctx convey.C) {
var (
c = context.Background()
rid = int16(0)
tagID = int64(0)
arcs = []*model.TagArchive{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetRankingTagCache(c, rid, tagID, arcs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,42 @@
package dao
import (
"context"
"net/url"
"strconv"
"go-common/app/interface/main/web/model"
"go-common/library/ecode"
"go-common/library/net/metadata"
"github.com/pkg/errors"
)
const (
_hotURI = "/x/internal/v2/reply/hot"
_archiveType = 1
)
// Hot reply hot.
func (d *Dao) Hot(c context.Context, aid int64) (rs *model.ReplyHot, err error) {
var (
params = url.Values{}
remoteIP = metadata.String(c, metadata.RemoteIP)
)
params.Set("oid", strconv.FormatInt(aid, 10))
params.Set("type", strconv.FormatInt(_archiveType, 10))
var res struct {
Code int `json:"code"`
Data *model.ReplyHot `json:"data"`
}
if err = d.httpR.Get(c, d.replyHotURL, remoteIP, params, &res); err != nil {
err = errors.Wrapf(err, "replyHot url(%s)", d.replyHotURL+"?"+params.Encode())
return
}
if res.Code != ecode.OK.Code() {
err = ecode.Int(res.Code)
return
}
rs = &model.ReplyHot{Replies: res.Data.Replies, Page: res.Data.Page}
return
}

View File

@@ -0,0 +1,24 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoHot(t *testing.T) {
convey.Convey("Hot", t, func(ctx convey.C) {
var (
c = context.Background()
aid = int64(10100652)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rs, err := d.Hot(c, aid)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,568 @@
package dao
import (
"context"
"net/http"
"net/url"
"strconv"
"go-common/app/interface/main/web/model"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
"go-common/library/xstr"
)
const (
_searchVer = "v3"
_searchPlatform = "web"
_searchUpRecType = "up_rec"
)
// SearchAll search all data.
func (d *Dao) SearchAll(c context.Context, mid int64, arg *model.SearchAllArg, buvid, ua, typ string) (res *model.Search, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params = setSearchParam(params, mid, model.SearchTypeAll, arg.Keyword, _searchPlatform, arg.FromSource, buvid, ip)
params.Set("duration", strconv.Itoa(arg.Duration))
params.Set("page", strconv.Itoa(arg.Pn))
params.Set("tids", strconv.Itoa(arg.Rid))
if typ == model.WxSearchType {
params.Set("highlight", strconv.Itoa(arg.Highlight))
for k, v := range model.SearchDefaultArg[model.WxSearchTypeAll] {
params.Set(k, strconv.Itoa(v))
}
} else {
for k, v := range model.SearchDefaultArg[model.SearchTypeAll] {
params.Set(k, strconv.Itoa(v))
}
params.Set("single_column", strconv.Itoa(arg.SingleColumn))
}
var req *http.Request
if req, err = d.httpSearch.NewRequest(http.MethodGet, d.searchURL, ip, params); err != nil {
return
}
req.Header.Set("browser-info", ua)
res = new(model.Search)
if err = d.httpSearch.Do(c, req, &res); err != nil {
log.Error("Search d.httpSearch.Get(%s) error(%v)", d.searchURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("Search d.httpSearch.Get(%s) code(%d) error", d.searchURL, res.Code)
err = ecode.Int(res.Code)
}
return
}
// SearchVideo search season data.
func (d *Dao) SearchVideo(c context.Context, mid int64, arg *model.SearchTypeArg, buvid, ua string) (res *model.SearchTypeRes, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params = setSearchTypeParam(params, mid, model.SearchTypeVideo, buvid, ip, arg)
params.Set("duration", strconv.Itoa(arg.Duration))
params.Set("order", arg.Order)
params.Set("from_source", arg.FromSource)
params.Set("tids", strconv.FormatInt(arg.Rid, 10))
params.Set("page", strconv.Itoa(arg.Pn))
for k, v := range model.SearchDefaultArg[model.SearchTypeVideo] {
params.Set(k, strconv.Itoa(v))
}
var req *http.Request
if req, err = d.httpSearch.NewRequest(http.MethodGet, d.searchURL, ip, params); err != nil {
return
}
req.Header.Set("browser-info", ua)
res = new(model.SearchTypeRes)
if err = d.httpSearch.Do(c, req, &res); err != nil {
log.Error("SearchVideo d.httpSearch.Get(%s) error(%v)", d.searchURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("SearchVideo d.httpSearch.Get(%s) code(%d) error", d.searchURL, res.Code)
err = ecode.Int(res.Code)
}
return
}
// SearchBangumi search bangumi data.
func (d *Dao) SearchBangumi(c context.Context, mid int64, arg *model.SearchTypeArg, buvid, ua string) (res *model.SearchTypeRes, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params = setSearchTypeParam(params, mid, model.SearchTypeBangumi, buvid, ip, arg)
params.Set("duration", strconv.Itoa(arg.Duration))
params.Set("order", arg.Order)
params.Set("page", strconv.Itoa(arg.Pn))
for k, v := range model.SearchDefaultArg[model.SearchTypeBangumi] {
params.Set(k, strconv.Itoa(v))
}
var req *http.Request
if req, err = d.httpSearch.NewRequest(http.MethodGet, d.searchURL, ip, params); err != nil {
return
}
req.Header.Set("browser-info", ua)
res = new(model.SearchTypeRes)
if err = d.httpSearch.Do(c, req, &res); err != nil {
log.Error("SearchBangumi d.httpSearch.Get(%s) error(%v)", d.searchURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("SearchBangumi d.httpSearch.Get(%s) code(%d) error", d.searchURL, res.Code)
err = ecode.Int(res.Code)
}
return
}
// SearchPGC search pgc(movie) data.
func (d *Dao) SearchPGC(c context.Context, mid int64, arg *model.SearchTypeArg, buvid, ua string) (res *model.SearchTypeRes, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params = setSearchTypeParam(params, mid, model.SearchTypePGC, buvid, ip, arg)
params.Set("page", strconv.Itoa(arg.Pn))
for k, v := range model.SearchDefaultArg[model.SearchTypePGC] {
params.Set(k, strconv.Itoa(v))
}
var req *http.Request
if req, err = d.httpSearch.NewRequest(http.MethodGet, d.searchURL, ip, params); err != nil {
return
}
req.Header.Set("browser-info", ua)
res = new(model.SearchTypeRes)
if err = d.httpSearch.Do(c, req, &res); err != nil {
log.Error("SearchPGC d.httpSearch.Get(%s) error(%v)", d.searchURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("SearchPGC d.httpSearch.Get(%s) code(%d) error", d.searchURL, res.Code)
err = ecode.Int(res.Code)
}
return
}
// SearchLive search live data.
func (d *Dao) SearchLive(c context.Context, mid int64, arg *model.SearchTypeArg, buvid, ua string) (res *model.SearchTypeRes, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params = setSearchTypeParam(params, mid, model.SearchTypeLive, buvid, ip, arg)
params.Set("page", strconv.Itoa(arg.Pn))
params.Set("order", arg.Order)
for k, v := range model.SearchDefaultArg[model.SearchTypeLive] {
params.Set(k, strconv.Itoa(v))
}
var req *http.Request
if req, err = d.httpSearch.NewRequest(http.MethodGet, d.searchURL, ip, params); err != nil {
return
}
req.Header.Set("browser-info", ua)
res = new(model.SearchTypeRes)
if err = d.httpSearch.Do(c, req, &res); err != nil {
log.Error("SearchVideo d.httpSearch.Get(%s) error(%v)", d.searchURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("SearchVideo d.httpSearch.Get(%s) code(%d) error", d.searchURL, res.Code)
err = ecode.Int(res.Code)
}
return
}
// SearchLiveRoom search live data.
func (d *Dao) SearchLiveRoom(c context.Context, mid int64, arg *model.SearchTypeArg, buvid, ua string) (res *model.SearchTypeRes, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params = setSearchTypeParam(params, mid, model.SearchTypeLiveRoom, buvid, ip, arg)
params.Set("page", strconv.Itoa(arg.Pn))
params.Set("order", arg.Order)
for k, v := range model.SearchDefaultArg[model.SearchTypeLiveRoom] {
params.Set(k, strconv.Itoa(v))
}
var req *http.Request
if req, err = d.httpSearch.NewRequest(http.MethodGet, d.searchURL, ip, params); err != nil {
return
}
req.Header.Set("browser-info", ua)
res = new(model.SearchTypeRes)
if err = d.httpSearch.Do(c, req, &res); err != nil {
log.Error("SearchLiveRoom d.httpSearch.Get(%s) error(%v)", d.searchURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("SearchLiveRoom d.httpSearch.Get(%s) code(%d) error", d.searchURL, res.Code)
err = ecode.Int(res.Code)
}
return
}
// SearchLiveUser search live user data.
func (d *Dao) SearchLiveUser(c context.Context, mid int64, arg *model.SearchTypeArg, buvid, ua string) (res *model.SearchTypeRes, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params = setSearchTypeParam(params, mid, model.SearchTypeLiveUser, buvid, ip, arg)
params.Set("page", strconv.Itoa(arg.Pn))
params.Set("order", arg.Order)
for k, v := range model.SearchDefaultArg[model.SearchTypeLiveUser] {
params.Set(k, strconv.Itoa(v))
}
var req *http.Request
if req, err = d.httpSearch.NewRequest(http.MethodGet, d.searchURL, ip, params); err != nil {
return
}
req.Header.Set("browser-info", ua)
res = new(model.SearchTypeRes)
if err = d.httpSearch.Do(c, req, &res); err != nil {
log.Error("SearchVideo d.httpSearch.Get(%s) error(%v)", d.searchURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("SearchVideo d.httpSearch.Get(%s) code(%d) error", d.searchURL, res.Code)
err = ecode.Int(res.Code)
}
return
}
// SearchArticle search article.
func (d *Dao) SearchArticle(c context.Context, mid int64, arg *model.SearchTypeArg, buvid, ua string) (res *model.SearchTypeRes, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params = setSearchTypeParam(params, mid, model.SearchTypeArticle, buvid, ip, arg)
params.Set("category_id", strconv.FormatInt(arg.CategoryID, 10))
params.Set("page", strconv.Itoa(arg.Pn))
params.Set("order", arg.Order)
for k, v := range model.SearchDefaultArg[model.SearchTypeArticle] {
params.Set(k, strconv.Itoa(v))
}
var req *http.Request
if req, err = d.httpSearch.NewRequest(http.MethodGet, d.searchURL, ip, params); err != nil {
return
}
req.Header.Set("browser-info", ua)
res = new(model.SearchTypeRes)
if err = d.httpSearch.Do(c, req, &res); err != nil {
log.Error("SearchArticle d.httpSearch.Get(%s) error(%v)", d.searchURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("SearchArticle d.httpSearch.Get(%s) code(%d) error", d.searchURL, res.Code)
err = ecode.Int(res.Code)
}
return
}
// SearchSpecial search special data.
func (d *Dao) SearchSpecial(c context.Context, mid int64, arg *model.SearchTypeArg, buvid, ua string) (res *model.SearchTypeRes, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params = setSearchTypeParam(params, mid, model.SearchTypeSpecial, buvid, ip, arg)
params.Set("page", strconv.Itoa(arg.Pn))
params.Set("vp_num", strconv.Itoa(arg.VpNum))
for k, v := range model.SearchDefaultArg[model.SearchTypeSpecial] {
params.Set(k, strconv.Itoa(v))
}
var req *http.Request
if req, err = d.httpSearch.NewRequest(http.MethodGet, d.searchURL, ip, params); err != nil {
return
}
req.Header.Set("browser-info", ua)
res = new(model.SearchTypeRes)
if err = d.httpSearch.Do(c, req, &res); err != nil {
log.Error("SearchSpecial d.httpSearch.Get(%s) error(%v)", d.searchURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("SearchSpecial d.httpSearch.Get(%s) code(%d) error", d.searchURL, res.Code)
err = ecode.Int(res.Code)
}
return
}
// SearchTopic search topic data.
func (d *Dao) SearchTopic(c context.Context, mid int64, arg *model.SearchTypeArg, buvid, ua string) (res *model.SearchTypeRes, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params = setSearchTypeParam(params, mid, model.SearchTypeTopic, buvid, ip, arg)
params.Set("page", strconv.Itoa(arg.Pn))
for k, v := range model.SearchDefaultArg[model.SearchTypeTopic] {
params.Set(k, strconv.Itoa(v))
}
if arg.Highlight > 0 {
params.Set("highlight", strconv.Itoa(arg.Highlight))
}
var req *http.Request
if req, err = d.httpSearch.NewRequest(http.MethodGet, d.searchURL, ip, params); err != nil {
return
}
req.Header.Set("browser-info", ua)
res = new(model.SearchTypeRes)
if err = d.httpSearch.Do(c, req, &res); err != nil {
log.Error("SearchVideo d.httpSearch.Get(%s) error(%v)", d.searchURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("SearchVideo d.httpSearch.Get(%s) code(%d) error", d.searchURL, res.Code)
err = ecode.Int(res.Code)
}
return
}
// SearchUser search user data.
func (d *Dao) SearchUser(c context.Context, mid int64, arg *model.SearchTypeArg, buvid, ua string) (res *model.SearchTypeRes, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params = setSearchTypeParam(params, mid, model.SearchTypeUser, buvid, ip, arg)
params.Set("page", strconv.Itoa(arg.Pn))
params.Set("user_type", strconv.Itoa(arg.UserType))
params.Set("bili_user_vl", strconv.Itoa(arg.BiliUserVl))
params.Set("order_sort", strconv.Itoa(arg.OrderSort))
params.Set("order", arg.Order)
for k, v := range model.SearchDefaultArg[model.SearchTypeUser] {
params.Set(k, strconv.Itoa(v))
}
var req *http.Request
if req, err = d.httpSearch.NewRequest(http.MethodGet, d.searchURL, ip, params); err != nil {
return
}
req.Header.Set("browser-info", ua)
res = new(model.SearchTypeRes)
if err = d.httpSearch.Do(c, req, &res); err != nil {
log.Error("SearchUser d.httpSearch.Get(%s) error(%v)", d.searchURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("SearchUser d.httpSearch.Get(%s) code(%d) error", d.searchURL, res.Code)
err = ecode.Int(res.Code)
}
return
}
// SearchPhoto search photo data.
func (d *Dao) SearchPhoto(c context.Context, mid int64, arg *model.SearchTypeArg, buvid, ua string) (res *model.SearchTypeRes, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params = setSearchTypeParam(params, mid, model.SearchTypePhoto, buvid, ip, arg)
params.Set("category_id", strconv.FormatInt(arg.CategoryID, 10))
params.Set("page", strconv.Itoa(arg.Pn))
params.Set("order", arg.Order)
for k, v := range model.SearchDefaultArg[model.SearchTypePhoto] {
params.Set(k, strconv.Itoa(v))
}
if arg.Highlight > 0 {
params.Set("highlight", strconv.Itoa(arg.Highlight))
}
var req *http.Request
if req, err = d.httpSearch.NewRequest(http.MethodGet, d.searchURL, ip, params); err != nil {
return
}
req.Header.Set("browser-info", ua)
res = new(model.SearchTypeRes)
if err = d.httpSearch.Do(c, req, &res); err != nil {
log.Error("SearchPhoto d.httpSearch.Get(%s) error(%v)", d.searchURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("SearchPhoto d.httpSearch.Get(%s) code(%d) error", d.searchURL, res.Code)
err = ecode.Int(res.Code)
}
return
}
// SearchRec search recommend data.
func (d *Dao) SearchRec(c context.Context, mid int64, pn, ps int, keyword, fromSource, buvid, ua string) (res *model.SearchRec, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params = setSearchParam(params, mid, "", keyword, _searchPlatform, fromSource, buvid, ip)
params.Set("page", strconv.Itoa(pn))
params.Set("pagesize", strconv.Itoa(ps))
var req *http.Request
if req, err = d.httpSearch.NewRequest(http.MethodGet, d.searchRecURL, ip, params); err != nil {
return
}
req.Header.Set("browser-info", ua)
res = new(model.SearchRec)
if err = d.httpSearch.Do(c, req, &res); err != nil {
log.Error("Search d.httpSearch.Get(%s) error(%v)", d.searchRecURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("Search d.httpSearch.Do(%s) code error(%d)", d.searchRecURL, res.Code)
err = ecode.Int(res.Code)
}
return
}
// SearchDefault get search default word.
func (d *Dao) SearchDefault(c context.Context, mid int64, fromSource, buvid, ua string) (data *model.SearchDefault, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params.Set("main_ver", _searchVer)
params.Set("platform", _searchPlatform)
params.Set("clientip", ip)
params.Set("userid", strconv.FormatInt(mid, 10))
params.Set("search_type", "default")
params.Set("from_source", fromSource)
params.Set("buvid", buvid)
var req *http.Request
if req, err = d.httpSearch.NewRequest(http.MethodGet, d.searchDefaultURL, ip, params); err != nil {
return
}
req.Header.Set("browser-info", ua)
var res struct {
Code int `json:"code"`
SeID string `json:"seid"`
Tips string `json:"recommend_tips"`
Result []struct {
ID int64 `json:"id"`
Name string `json:"name"`
ShowName string `json:"show_name"`
Type string `json:"type"`
} `json:"result"`
}
if err = d.httpSearch.Do(c, req, &res); err != nil {
log.Error("Search d.httpSearch.Get(%s) error(%v)", d.searchDefaultURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("Search d.httpSearch.Do(%s) code error(%d)", d.searchDefaultURL, res.Code)
err = ecode.Int(res.Code)
}
if len(res.Result) == 0 {
err = ecode.NothingFound
return
}
data = &model.SearchDefault{}
for _, v := range res.Result {
data.Trackid = res.SeID
data.ID = v.ID
data.ShowName = v.ShowName
data.Name = v.Name
}
return
}
// UpRecommend .
func (d *Dao) UpRecommend(c context.Context, mid int64, arg *model.SearchUpRecArg, buvid string) (rs []*model.SearchUpRecRes, trackID string, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params.Set("userid", strconv.FormatInt(mid, 10))
params.Set("service_area", arg.ServiceArea)
params.Set("rec_type", _searchUpRecType)
params.Set("platform", arg.Platform)
params.Set("clientip", ip)
params.Set("pagesize", strconv.Itoa(arg.Ps))
params.Set("buvid", buvid)
if arg.MobiApp != "" {
params.Set("mobi_app", arg.MobiApp)
}
if arg.Device != "" {
params.Set("device", arg.Device)
}
if arg.Build != 0 {
params.Set("build", strconv.FormatInt(arg.Build, 10))
}
if arg.ContextID != 0 {
params.Set("context_id", strconv.FormatInt(arg.ContextID, 10))
}
if len(arg.MainTids) > 0 {
params.Set("main_tids", xstr.JoinInts(arg.MainTids))
}
if len(arg.SubTids) > 0 {
params.Set("sub_tids", xstr.JoinInts(arg.SubTids))
}
var res struct {
Code int `json:"code"`
Trackid string `json:"trackid"`
Data []*model.SearchUpRecRes `json:"data"`
}
if err = d.httpSearch.Get(c, d.searchUpRecURL, ip, params, &res); err != nil {
log.Error("UpRecommend d.httpSearch.Get(%s) error(%v)", d.searchUpRecURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("UpRecommend d.httpSearch.Do(%s) code error(%d)", d.searchUpRecURL, res.Code)
err = ecode.Int(res.Code)
return
}
rs = res.Data
trackID = res.Trackid
return
}
// SearchEgg search egg.
func (d *Dao) SearchEgg(c context.Context) (data []*model.SearchEgg, err error) {
var (
ip = metadata.String(c, metadata.RemoteIP)
res struct {
Code int `json:"code"`
Data []*model.SearchEgg `json:"data"`
}
)
if err = d.httpSearch.Get(c, d.searchEggURL, ip, url.Values{}, &res); err != nil {
log.Error("SearchEgg d.httpSearch.Get(%s) error(%v)", d.searchEggURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("SearchEgg d.httpSearch.Do(%s) code error(%d)", d.searchEggURL, res.Code)
err = ecode.Int(res.Code)
return
}
data = res.Data
return
}
func setSearchParam(param url.Values, mid int64, searchType, keyword, platform, fromSource, buvid, ip string) url.Values {
param.Set("main_ver", _searchVer)
if searchType != "" {
param.Set("search_type", searchType)
}
param.Set("platform", platform)
param.Set("keyword", keyword)
param.Set("from_source", fromSource)
param.Set("userid", strconv.FormatInt(mid, 10))
param.Set("buvid", buvid)
param.Set("clientip", ip)
return param
}
func setSearchTypeParam(param url.Values, mid int64, searchType, buvid, ip string, arg *model.SearchTypeArg) url.Values {
param.Set("main_ver", _searchVer)
if searchType != "" {
param.Set("search_type", searchType)
}
param.Set("platform", arg.Platform)
param.Set("keyword", arg.Keyword)
param.Set("from_source", arg.FromSource)
param.Set("userid", strconv.FormatInt(mid, 10))
param.Set("single_column", strconv.Itoa(arg.SingleColumn))
param.Set("buvid", buvid)
param.Set("clientip", ip)
return param
}

View File

@@ -0,0 +1,314 @@
package dao
import (
"context"
"testing"
"go-common/app/interface/main/web/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoSearchAll(t *testing.T) {
convey.Convey("SearchAll", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
arg = &model.SearchAllArg{Pn: 1, Keyword: "test", Rid: 1}
buvid = ""
ua = ""
)
typ := model.WxSearchType
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SearchAll(c, mid, arg, buvid, ua, typ)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSearchVideo(t *testing.T) {
convey.Convey("SearchVideo", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
arg = &model.SearchTypeArg{Pn: 1, SearchType: model.SearchTypeVideo, Keyword: "test"}
buvid = ""
ua = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SearchVideo(c, mid, arg, buvid, ua)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSearchBangumi(t *testing.T) {
convey.Convey("SearchBangumi", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
arg = &model.SearchTypeArg{Pn: 1, SearchType: model.SearchTypeBangumi, Keyword: "test"}
buvid = ""
ua = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SearchBangumi(c, mid, arg, buvid, ua)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSearchPGC(t *testing.T) {
convey.Convey("SearchPGC", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
arg = &model.SearchTypeArg{Pn: 1, SearchType: model.SearchTypePGC, Keyword: "test"}
buvid = ""
ua = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SearchPGC(c, mid, arg, buvid, ua)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSearchLive(t *testing.T) {
convey.Convey("SearchLive", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
arg = &model.SearchTypeArg{Pn: 1, SearchType: model.SearchTypeLive, Keyword: "test"}
buvid = ""
ua = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SearchLive(c, mid, arg, buvid, ua)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSearchLiveRoom(t *testing.T) {
convey.Convey("SearchLiveRoom", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
arg = &model.SearchTypeArg{Pn: 1, SearchType: model.SearchTypeLiveRoom, Keyword: "test"}
buvid = ""
ua = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SearchLiveRoom(c, mid, arg, buvid, ua)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSearchLiveUser(t *testing.T) {
convey.Convey("SearchLiveUser", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
arg = &model.SearchTypeArg{Pn: 1, SearchType: model.SearchTypeLiveUser, Keyword: "test"}
buvid = ""
ua = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SearchLiveUser(c, mid, arg, buvid, ua)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSearchArticle(t *testing.T) {
convey.Convey("SearchArticle", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
arg = &model.SearchTypeArg{Pn: 1, SearchType: model.SearchTypeArticle, Keyword: "test"}
buvid = ""
ua = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SearchArticle(c, mid, arg, buvid, ua)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSearchSpecial(t *testing.T) {
convey.Convey("SearchSpecial", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
arg = &model.SearchTypeArg{Pn: 1, SearchType: model.SearchTypeSpecial, Keyword: "test"}
buvid = ""
ua = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SearchSpecial(c, mid, arg, buvid, ua)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSearchTopic(t *testing.T) {
convey.Convey("SearchTopic", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
arg = &model.SearchTypeArg{Pn: 1, SearchType: model.SearchTypeTopic, Keyword: "test"}
buvid = ""
ua = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SearchTopic(c, mid, arg, buvid, ua)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSearchUser(t *testing.T) {
convey.Convey("SearchUser", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
arg = &model.SearchTypeArg{Pn: 1, SearchType: model.SearchTypeUser, Keyword: "test"}
buvid = ""
ua = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SearchUser(c, mid, arg, buvid, ua)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSearchPhoto(t *testing.T) {
convey.Convey("SearchPhoto", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
arg = &model.SearchTypeArg{Pn: 1, SearchType: model.SearchTypePhoto, Keyword: "test"}
buvid = ""
ua = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SearchPhoto(c, mid, arg, buvid, ua)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSearchRec(t *testing.T) {
convey.Convey("SearchRec", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
pn = int(1)
ps = int(10)
keyword = "test"
fromSource = ""
buvid = ""
ua = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SearchRec(c, mid, pn, ps, keyword, fromSource, buvid, ua)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSearchDefault(t *testing.T) {
convey.Convey("SearchDefault", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
fromSource = ""
buvid = ""
ua = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.SearchDefault(c, mid, fromSource, buvid, ua)
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)
})
})
})
}
func TestDaoUpRecommend(t *testing.T) {
convey.Convey("UpRecommend", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2089809)
arg = &model.SearchUpRecArg{ServiceArea: "reg_ok", Platform: "web"}
buvid = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rs, trackID, err := d.UpRecommend(c, mid, arg, buvid)
ctx.Convey("Then err should be nil.rs,trackID should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(trackID, convey.ShouldNotBeNil)
ctx.Printf("%+v", rs)
})
})
})
}
func TestDaoSearchEgg(t *testing.T) {
convey.Convey("SearchEgg", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.SearchEgg(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)
})
})
})
}

View File

@@ -0,0 +1,42 @@
package dao
import (
"context"
"net/url"
"strconv"
"go-common/app/interface/main/web/model"
"go-common/library/ecode"
"go-common/library/net/metadata"
"github.com/pkg/errors"
)
const (
_shopURI = "/mall-shop/merchant/enter/service/shop/get"
_shopTypePc = "2"
)
// ShopInfo get shop info data.
func (d *Dao) ShopInfo(c context.Context, mid int64) (data *model.ShopInfo, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("type", _shopTypePc)
var res struct {
Code int `json:"code"`
Data *model.ShopInfo `json:"data"`
}
if err = d.httpR.Get(c, d.shopURL, ip, params, &res); err != nil {
err = errors.Wrapf(err, "ShopInfo(%s) mid(%d)", d.shopURL+params.Encode(), mid)
return
}
if res.Code != ecode.OK.Code() {
err = ecode.Int(res.Code)
return
}
data = res.Data
return
}

View File

@@ -0,0 +1,24 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoShopInfo(t *testing.T) {
convey.Convey("ShopInfo", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(27515399)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.ShopInfo(c, mid)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", data)
})
})
})
}

View File

@@ -0,0 +1,36 @@
package dao
import (
"context"
"net/url"
"strconv"
"go-common/app/interface/main/web/model"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
)
// TopPhoto getTopPhoto from space
func (d *Dao) TopPhoto(c context.Context, mid int64) (space *model.Space, err error) {
var (
params = url.Values{}
remoteIP = metadata.String(c, metadata.RemoteIP)
)
params.Set("mid", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
model.Space
}
if err = d.httpR.Get(c, d.spaceTopPhotoURL, remoteIP, params, &res); err != nil {
log.Error("TopPhoto space url(%s) error(%v)", d.spaceTopPhotoURL+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
log.Error("TopPhoto space url(%s) error(%v)", d.spaceTopPhotoURL+"?"+params.Encode(), res.Code)
err = ecode.Int(res.Code)
return
}
space = &res.Space
return
}

View File

@@ -0,0 +1,24 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoTopPhoto(t *testing.T) {
convey.Convey("TopPhoto", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
space, err := d.TopPhoto(c, mid)
ctx.Convey("Then err should be nil.space should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(space, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,97 @@
package dao
import (
"context"
"net/url"
"strconv"
"go-common/app/interface/main/web/model"
"go-common/library/cache/redis"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
"go-common/library/xstr"
)
const (
_redisTagAv = "t_a_"
_tagFeedURL = "/feed/tag/top"
)
func tagAidKey(tid int64) string {
return _redisTagAv + strconv.FormatInt(tid, 10)
}
// TagAids provides aids via tag
func (d *Dao) TagAids(c context.Context, tid int64) (res []int64, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params.Set("tag", strconv.FormatInt(tid, 10))
params.Set("pn", "1")
params.Set("rn", strconv.Itoa(d.c.Tag.PageSize))
params.Set("src", "1") // plat. PC:1, APP:2
rs := &model.TagAids{}
if err = d.httpR.Get(c, d.c.Host.Data+_tagFeedURL, ip, params, rs); err != nil {
log.Error("tag d.httpR.Get(%s, %s, %v) error(%v)", d.c.Host.Data+_tagFeedURL, ip, params, err)
return
}
if rs.Code != ecode.OK.Code() {
err = ecode.Int(rs.Code)
return
}
res = rs.Data
return
}
// TagAidsBakCache gets avids cache
func (d *Dao) TagAidsBakCache(c context.Context, tid int64) (res []int64, err error) {
var (
conn = d.redisBak.Get(c)
key = tagAidKey(tid)
s string
)
defer conn.Close()
if s, err = redis.String(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
if res, err = xstr.SplitInts(s); err != nil {
log.Error("xstr.SplitInts(%s) error(%v)", s, err)
}
return
}
// SetTagAidsBakCache set the avids cache
func (d *Dao) SetTagAidsBakCache(c context.Context, tid int64, aids []int64) (err error) {
var (
conn = d.redisBak.Get(c)
key = tagAidKey(tid)
)
defer conn.Close()
s := xstr.JoinInts(aids)
if err = conn.Send("SET", key, s); err != nil {
log.Error("conn.Do(SET, %s, %s) error(%v)", key, s, err)
return
}
if err = conn.Send("EXPIRE", key, d.redisTagBakExpire); err != nil {
log.Error("conn.Send(EXPIRE, %s, %d) error(%v)", key, d.redisTagBakExpire, 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
}

View File

@@ -0,0 +1,73 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
gock "gopkg.in/h2non/gock.v1"
)
func TestDaotagAidKey(t *testing.T) {
convey.Convey("tagAidKey", t, func(ctx convey.C) {
var (
tid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := tagAidKey(tid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTagAids(t *testing.T) {
convey.Convey("TagAids", t, func(ctx convey.C) {
var (
c = context.Background()
tid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.c.Host.Data+_tagFeedURL).Reply(200).JSON(`{"code":0,"data":[1111,2222],"total":2}`)
res, err := d.TagAids(c, tid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSetTagAidsBakCache(t *testing.T) {
convey.Convey("SetTagAidsBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
tid = int64(2222)
aids = []int64{111, 222}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetTagAidsBakCache(c, tid, aids)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoTagAidsBakCache(t *testing.T) {
convey.Convey("TagAidsBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
tid = int64(2222)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.TagAidsBakCache(c, tid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,70 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"go-common/app/interface/main/web/model"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
_keyArchiveFmt = "va_%d"
)
func keyArchive(aid int64) string {
return fmt.Sprintf(_keyArchiveFmt, aid)
}
// SetViewBakCache set view archive page data to cache.
func (d *Dao) SetViewBakCache(c context.Context, aid int64, a *model.View) (err error) {
key := keyArchive(aid)
conn := d.redisBak.Get(c)
defer conn.Close()
var bs []byte
if bs, err = json.Marshal(a); err != nil {
log.Error("SetViewBakCache json.Marshal(%v) error(%v)", a, err)
return
}
if err = conn.Send("SET", key, bs); err != nil {
log.Error("SetViewBakCache conn.Send(SET,%s,%s) error(%v)", key, string(bs), err)
return
}
if err = conn.Send("EXPIRE", key, d.redisArchiveBakExpire); err != nil {
log.Error("SetViewBakCache conn.Send(EXPIRE,%s,%d) error(%v)", key, d.redisArchiveBakExpire, err)
return
}
if err = conn.Flush(); err != nil {
log.Error("SetViewBakCache conn.Flush error(%v)", err)
return
}
for i := 0; i < 2; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("SetViewBakCache conn.Recevie(%d) error(%v0", i, err)
}
}
return
}
// ViewBakCache get view archive page data from cache.
func (d *Dao) ViewBakCache(c context.Context, aid int64) (rs *model.View, err error) {
key := keyArchive(aid)
conn := d.redisBak.Get(c)
defer conn.Close()
var values []byte
if values, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
log.Warn("ViewBakCache redis (%s) return nil ", key)
} else {
log.Error("ViewBakCache conn.Do(GET,%s) error(%v)", key, err)
}
return
}
if err = json.Unmarshal(values, &rs); err != nil {
log.Error("ViewBakCache json.Unmarshal(%v) error(%v)", values, err)
}
return
}

View File

@@ -0,0 +1,56 @@
package dao
import (
"context"
"testing"
"go-common/app/interface/main/web/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaokeyArchive(t *testing.T) {
convey.Convey("keyArchive", t, func(ctx convey.C) {
var (
aid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := keyArchive(aid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSetViewBakCache(t *testing.T) {
convey.Convey("SetViewBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
aid = int64(0)
a = &model.View{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetViewBakCache(c, aid, a)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoViewBakCache(t *testing.T) {
convey.Convey("ViewBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
aid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rs, err := d.ViewBakCache(c, aid)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,120 @@
package dao
import (
"context"
"encoding/json"
"net/url"
"go-common/app/interface/main/web/model"
"go-common/library/cache/redis"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
)
const (
_wxHotURI = "hot-weixin.json"
_wxCacheKey = "wx_hot"
_wxBkCacheKey = _keyBakPrefix + _wxCacheKey
)
// WxHot get wx hot aids.
func (d *Dao) WxHot(c context.Context) (aids []int64, err error) {
ip := metadata.String(c, metadata.RemoteIP)
var res struct {
Code int `json:"code"`
List []*model.NewArchive `json:"list"`
}
if err = d.httpBigData.Get(c, d.wxHotURL, ip, url.Values{}, &res); err != nil {
log.Error("d.httpBigData.Get(%s) error(%v)", d.wxHotURL, err)
return
}
if res.Code != ecode.OK.Code() {
log.Error("d.httpBigData.Get(%s) error(%v)", d.wxHotURL, err)
err = ecode.Int(res.Code)
return
}
for _, v := range res.List {
if v.Aid > 0 {
aids = append(aids, v.Aid)
}
}
return
}
// WxHotCache get wx hot cache.
func (d *Dao) WxHotCache(c context.Context) (arcs []*model.WxArchive, err error) {
key := _wxCacheKey
conn := d.redis.Get(c)
defer conn.Close()
arcs, err = wxHotCache(conn, key)
return
}
// WxHotBakCache get wx hot bak cache.
func (d *Dao) WxHotBakCache(c context.Context) (arcs []*model.WxArchive, err error) {
key := _wxBkCacheKey
conn := d.redisBak.Get(c)
defer conn.Close()
arcs, err = wxHotCache(conn, key)
return
}
func wxHotCache(conn redis.Conn, key string) (res []*model.WxArchive, err error) {
var value []byte
if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
res = []*model.WxArchive{}
if err = json.Unmarshal(value, &res); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", value, err)
}
return
}
// SetWxHotCache set wx hot to cache.
func (d *Dao) SetWxHotCache(c context.Context, arcs []*model.WxArchive) (err error) {
key := _wxCacheKey
conn := d.redis.Get(c)
defer conn.Close()
if err = d.setWxHotCache(c, conn, key, d.redisWxHotExpire, arcs); err != nil {
return
}
key = _wxBkCacheKey
connBak := d.redisBak.Get(c)
err = d.setWxHotCache(c, connBak, key, d.redisWxHotBakExpire, arcs)
connBak.Close()
return
}
func (d *Dao) setWxHotCache(c context.Context, conn redis.Conn, key string, expire int32, arcs []*model.WxArchive) (err error) {
var bs []byte
if bs, err = json.Marshal(arcs); err != nil {
log.Error("json.Marshal(%v) error (%v)", arcs, 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
}
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
}

View File

@@ -0,0 +1,73 @@
package dao
import (
"context"
"testing"
"go-common/app/interface/main/web/model"
"github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
func TestDaoWxHot(t *testing.T) {
convey.Convey("WxHot", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.wxHotURL).Reply(200).JSON(`{"code":0,"list":[{"aid":111,"score":10},{"aid":2222,"score":20}]}`)
aids, err := d.WxHot(c)
ctx.Convey("Then err should be nil.aids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(aids, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSetWxHotCache(t *testing.T) {
convey.Convey("SetWxHotCache", t, func(ctx convey.C) {
var (
c = context.Background()
arcs = []*model.WxArchive{{Aid: 1111}, {Aid: 2222}}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetWxHotCache(c, arcs)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoWxHotCache(t *testing.T) {
convey.Convey("WxHotCache", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
arcs, err := d.WxHotCache(c)
ctx.Convey("Then err should be nil.arcs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", arcs)
})
})
})
}
func TestDaoWxHotBakCache(t *testing.T) {
convey.Convey("WxHotBakCache", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
arcs, err := d.WxHotBakCache(c)
ctx.Convey("Then err should be nil.arcs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.Printf("%+v", arcs)
})
})
})
}