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,40 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["dao.go"],
importpath = "go-common/app/interface/live/web-ucenter/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/live/web-ucenter/conf:go_default_library",
"//app/service/live/rankdb/api/liverpc:go_default_library",
"//app/service/live/rc/api/liverpc:go_default_library",
"//app/service/live/room/api/liverpc:go_default_library",
"//library/net/rpc/liverpc:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/interface/live/web-ucenter/dao/capsule:all-srcs",
"//app/interface/live/web-ucenter/dao/history:all-srcs",
"//app/interface/live/web-ucenter/dao/user:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,32 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["dao.go"],
importpath = "go-common/app/interface/live/web-ucenter/dao/capsule",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/live/web-ucenter/conf:go_default_library",
"//app/service/live/xlottery/api/grpc/v1: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,44 @@
package capsule
import (
"context"
"go-common/app/interface/live/web-ucenter/conf"
lotteryApi "go-common/app/service/live/xlottery/api/grpc/v1"
)
// Dao dao
type Dao struct {
client *lotteryApi.Client
}
// New init
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{}
client, err := lotteryApi.NewClient(conf.Conf.Warden)
if err != nil {
panic(err)
}
dao.client = client
return
}
// GetDetail grpc
func (d *Dao) GetDetail(ctx context.Context, uid int64, from string) (*lotteryApi.CapsuleGetDetailResp, error) {
return d.client.CapsuleClient.GetDetail(ctx, &lotteryApi.CapsuleGetDetailReq{Uid: uid, From: from})
}
// OpenCapsule grpc
func (d *Dao) OpenCapsule(ctx context.Context, uid int64, otype string, count int64, platform string) (*lotteryApi.CapsuleOpenCapsuleResp, error) {
return d.client.CapsuleClient.OpenCapsule(ctx, &lotteryApi.CapsuleOpenCapsuleReq{Uid: uid, Type: otype, Count: count, Platform: platform})
}
// GetCapsuleInfo grpc
func (d *Dao) GetCapsuleInfo(ctx context.Context, uid int64, otype, from string) (*lotteryApi.CapsuleGetCapsuleInfoResp, error) {
return d.client.CapsuleClient.GetCapsuleInfo(ctx, &lotteryApi.CapsuleGetCapsuleInfoReq{Uid: uid, Type: otype, From: from})
}
// OpenCapsuleByType grpc
func (d *Dao) OpenCapsuleByType(ctx context.Context, uid int64, otype string, count int64, platform string) (*lotteryApi.CapsuleOpenCapsuleByTypeResp, error) {
return d.client.CapsuleClient.OpenCapsuleByType(ctx, &lotteryApi.CapsuleOpenCapsuleByTypeReq{Uid: uid, Type: otype, Count: count, Platform: platform})
}

View File

@@ -0,0 +1,53 @@
package dao
import (
"crypto/md5"
"encoding/hex"
"go-common/app/interface/live/web-ucenter/conf"
rank_api "go-common/app/service/live/rankdb/api/liverpc"
rc_api "go-common/app/service/live/rc/api/liverpc"
room_api "go-common/app/service/live/room/api/liverpc"
"go-common/library/net/rpc/liverpc"
"net/url"
"strconv"
"time"
)
// RoomAPI liverpc room-service api
var RoomAPI *room_api.Client
// RcApi liverpc rc api
var RcApi *rc_api.Client
// RankdbApi liverpc rankdb api
var RankdbApi *rank_api.Client
// InitAPI init all service APIs
func InitAPI() {
RoomAPI = room_api.New(getConf("room"))
RcApi = rc_api.New(getConf("rc"))
RankdbApi = rank_api.New(getConf("rank"))
}
func getConf(appName string) *liverpc.ClientConfig {
c := conf.Conf.LiveRpc
if c != nil {
return c[appName]
}
return nil
}
// EncodeHttpParams end http params and return encoded string
func EncodeHttpParams(params map[string]string, appKey, appSecret string) string {
v := url.Values{}
for key, value := range params {
v.Set(key, value)
}
v.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
v.Set("appkey", appKey)
tmp := v.Encode() + appSecret
mh := md5.Sum([]byte(tmp))
sign := hex.EncodeToString(mh[:])
v.Set("sign", sign)
return v.Encode()
}

View File

@@ -0,0 +1,37 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["dao.go"],
importpath = "go-common/app/interface/live/web-ucenter/dao/history",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/live/web-ucenter/conf:go_default_library",
"//app/interface/live/web-ucenter/model: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",
"//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,96 @@
package history
import (
"context"
"fmt"
"net/url"
"strconv"
"time"
"go-common/library/ecode"
"github.com/pkg/errors"
"go-common/app/interface/live/web-ucenter/conf"
"go-common/app/interface/live/web-ucenter/model"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/metadata"
)
// Dao dao
type Dao struct {
c *conf.Config
client *bm.Client
}
const (
_historyResourceURI = "/x/internal/v2/history/resource"
_historyDeleteURI = "/x/internal/v2/history/clear"
_historyPageSize = 24
)
// New init
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
client: bm.NewClient(c.HTTPClient),
}
return
}
// GetMainHistory 获取直播历史记录
func (d *Dao) GetMainHistory(c context.Context, mid int32) (data []*model.HistoryData, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params.Set("mid", fmt.Sprint(mid))
params.Set("business", "live")
params.Set("pn", fmt.Sprint(1))
params.Set("ps", fmt.Sprint(_historyPageSize))
params.Set("appkey", conf.APPKey)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
var res struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data []*model.HistoryData `json:"data"`
}
if d.client.Get(c, conf.MainInnerHostHTTP+_historyResourceURI, ip, params, &res); err != nil {
err = errors.WithMessage(err, "调用主站获取观看历史错误")
log.Error("call_history_resource_error:httpCode=%d params=%s", res.Code, params)
return
}
if int(res.Code) != ecode.OK.Code() {
log.Error("call_history_resource_error:%d", res.Code)
}
log.Info("call_history_resource_info:param:%v,%v", mid, res.Data)
data = res.Data
return
}
// DelHistory 删除直播历史记录
func (d *Dao) DelHistory(c context.Context, mid int64) (data int32, err error) {
var (
params = url.Values{}
ip = metadata.String(c, metadata.RemoteIP)
)
params.Set("appkey", conf.APPKey)
params.Set("mid", fmt.Sprint(mid))
params.Set("business", "live")
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
var res struct {
Code int32 `json:"code"`
}
if d.client.Post(c, conf.MainInnerHostHTTP+_historyDeleteURI, ip, params, &res); err != nil {
err = errors.WithMessage(err, "调用主站删除观看历史错误")
log.Error("call_history_delete_error:%s", err)
return
}
if int(res.Code) != ecode.OK.Code() {
log.Error("call_history_delete_code_error:%d", res.Code)
}
data = res.Code
return
}

View File

@@ -0,0 +1,54 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = ["dao.go"],
importpath = "go-common/app/interface/live/web-ucenter/dao/user",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/live/web-ucenter/conf:go_default_library",
"//app/interface/live/web-ucenter/dao:go_default_library",
"//app/service/live/rankdb/api/liverpc/v1:go_default_library",
"//app/service/live/rc/api/liverpc/v1:go_default_library",
"//app/service/live/xuser/api/grpc/v1:go_default_library",
"//app/service/main/account/model:go_default_library",
"//app/service/main/account/rpc/client:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["dao_test.go"],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/live/web-ucenter/conf:go_default_library",
"//app/interface/live/web-ucenter/dao:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,151 @@
package user
import (
"context"
"github.com/pkg/errors"
"go-common/app/interface/live/web-ucenter/conf"
"go-common/app/interface/live/web-ucenter/dao"
rankdbv1 "go-common/app/service/live/rankdb/api/liverpc/v1"
rcv1 "go-common/app/service/live/rc/api/liverpc/v1"
xuserv1 "go-common/app/service/live/xuser/api/grpc/v1"
accModel "go-common/app/service/main/account/model"
account "go-common/app/service/main/account/rpc/client"
"go-common/library/ecode"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"net/http"
"strconv"
)
var (
_walletApiUrl = "/x/internal/livewallet/wallet/getAll"
)
// Dao user dao, wrap clients
type Dao struct {
c *conf.Config
bmClient *bm.Client
vipClient xuserv1.VipClient
expClient xuserv1.UserExpClient
walletUrl string
accountClient *account.Service3
rankdbClient rankdbv1.UserRank
rcClient rcv1.AchvRPCClient
}
// New new user dao
func New(c *conf.Config) *Dao {
conn, err := xuserv1.NewClient(c.Warden)
if err != nil {
panic(err)
}
d := &Dao{
c: c,
bmClient: bm.NewClient(c.HTTPClient),
walletUrl: c.Host.LiveRpc + _walletApiUrl,
accountClient: account.New3(c.AccountRPC),
rankdbClient: dao.RankdbApi.V1UserRank,
rcClient: dao.RcApi.V1Achv,
}
d.vipClient = conn.VipClient
d.expClient = conn.UserExpClient
return d
}
// GetAccountProfile get account profile
func (d *Dao) GetAccountProfile(ctx context.Context, uid int64) (profile *accModel.ProfileStat, err error) {
arg := &accModel.ArgMid{Mid: uid}
if profile, err = d.accountClient.ProfileWithStat3(ctx, arg); err != nil || profile == nil {
log.Error("[dao.user|GetAccountProfile] get account profile3 error(%v), uid(%d), profile(%v)", err, uid, profile)
return
}
return
}
// GetWallet get silver/gold from go-wallet by http request
func (d *Dao) GetWallet(ctx context.Context, uid int64, platform string) (silver, gold int64, err error) {
m := make(map[string]string)
m["uid"] = strconv.FormatInt(uid, 10)
paramString := dao.EncodeHttpParams(m, d.c.HTTPClient.Key, d.c.HTTPClient.Secret)
req, _ := http.NewRequest("GET", d.walletUrl+"?"+paramString, nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("platform", platform)
var wr struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
Gold string `json:"gold"`
Silver string `json:"silver"`
} `json:"data"`
}
if err = d.bmClient.Do(ctx, req, &wr); err != nil {
log.Error("[dao.user|GetWallet] connect error(%v), uid(%d), platform(%s)", err, uid, platform)
return
}
if wr.Code != 0 {
err = errors.Wrap(ecode.Int(wr.Code), d.walletUrl+"?"+paramString)
log.Error("[dao.user|GetWallet] request error(%v), uid(%d), platform(%s)", err, uid, platform)
return
}
gold, _ = strconv.ParseInt(wr.Data.Gold, 10, 64)
silver, _ = strconv.ParseInt(wr.Data.Silver, 10, 64)
return
}
// GetLiveVip get live vip/svip from xuser.vip.Info
func (d *Dao) GetLiveVip(ctx context.Context, uid int64) (vipInfo *xuserv1.InfoReply, err error) {
uidReq := &xuserv1.UidReq{
Uid: uid,
}
if vipInfo, err = d.vipClient.Info(ctx, uidReq); err != nil || vipInfo == nil {
log.Error("[dao.user|GetLiveVip] get vip error(%v), uid(%d)", err, uid)
return
}
return
}
// GetLiveExp get live exp from xuser.exp.GetUserExp
func (d *Dao) GetLiveExp(ctx context.Context, uid int64) (expInfo *xuserv1.LevelInfo, err error) {
req := &xuserv1.GetUserExpReq{
Uids: []int64{uid},
}
resp, err := d.expClient.GetUserExp(ctx, req)
if err != nil {
log.Error("[dao.user|GetLiveExp] get exp error(%v), uid(%d)", err, uid)
return
}
var ok bool
if expInfo, ok = resp.Data[uid]; !ok {
log.Error("[dao.user|GetLiveExp] get exp empty, uid(%d)", uid)
return
}
return
}
// GetLiveAchieve get rc achieve by liverpc
func (d *Dao) GetLiveAchieve(ctx context.Context, uid int64) (achieve int64, err error) {
resp, err := d.rcClient.Userstatus(ctx, &rcv1.AchvUserstatusReq{})
if err != nil || resp == nil || resp.Data == nil {
log.Error("[dao.user|GetLiveAchieve] get rc achieve error(%v), uid(%d), resp(%v)", err, uid, resp)
return
}
achieve = resp.Data.Point
return
}
// GetLiveRank get user rank by liverpc
func (d *Dao) GetLiveRank(ctx context.Context, uid int64) (rank string, err error) {
rank = "1000000"
req := &rankdbv1.UserRankGetUserRankReq{
Uid: uid,
Type: "user_level",
}
resp, err := d.rankdbClient.GetUserRank(ctx, req)
if err != nil || resp == nil || resp.Data == nil {
log.Error("[dao.user|GetLiveRank] get rankdb user rank error(%v), uid(%d)", err, uid)
return
}
rank = strconv.FormatInt(resp.Data.Rank, 10)
return
}

View File

@@ -0,0 +1,73 @@
package user
import (
"context"
"flag"
. "github.com/smartystreets/goconvey/convey"
"go-common/app/interface/live/web-ucenter/conf"
"go-common/app/interface/live/web-ucenter/dao"
"testing"
)
var d *Dao
func init() {
flag.Set("conf", "../../cmd/test.toml")
flag.Set("env", "uat")
var err error
if err = conf.Init(); err != nil {
panic(err)
}
dao.InitAPI()
d = New(conf.Conf)
}
var (
uid = int64(110000681)
ctx = context.Background()
)
func TestDao_GetAccountProfile(t *testing.T) {
Convey("test get account profile", t, func() {
profile, err := d.GetAccountProfile(ctx, uid)
So(profile, ShouldNotBeNil)
So(err, ShouldBeNil)
})
}
func TestDao_GetWallet(t *testing.T) {
Convey("test get wallet", t, func() {
_, _, err := d.GetWallet(ctx, uid, "pc")
So(err, ShouldBeNil)
})
}
func TestDao_GetLiveAchieve(t *testing.T) {
Convey("test get rc achieve", t, func() {
_, err := d.GetLiveAchieve(ctx, uid)
So(err, ShouldBeNil)
})
}
func TestDao_GetLiveExp(t *testing.T) {
Convey("test get exp", t, func() {
expInfo, err := d.GetLiveExp(ctx, uid)
So(expInfo, ShouldNotBeNil)
So(err, ShouldBeNil)
})
}
func TestDao_GetLiveVip(t *testing.T) {
Convey("test get vip", t, func() {
vipInfo, err := d.GetLiveVip(ctx, uid)
So(vipInfo, ShouldNotBeNil)
So(err, ShouldBeNil)
})
}
func TestDao_GetLiveRank(t *testing.T) {
Convey("test get rankdb", t, func() {
_, err := d.GetLiveRank(ctx, uid)
So(err, ShouldBeNil)
})
}