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,72 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"anchorTask.go",
"capsule.go",
"history.go",
"user.go",
],
importpath = "go-common/app/interface/live/web-ucenter/service/v1",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/live/web-ucenter/api/http:go_default_library",
"//app/interface/live/web-ucenter/api/http/v1:go_default_library",
"//app/interface/live/web-ucenter/conf:go_default_library",
"//app/interface/live/web-ucenter/dao:go_default_library",
"//app/interface/live/web-ucenter/dao/capsule:go_default_library",
"//app/interface/live/web-ucenter/dao/history:go_default_library",
"//app/interface/live/web-ucenter/dao/user:go_default_library",
"//app/interface/live/web-ucenter/model:go_default_library",
"//app/service/live/room/api/liverpc/v2:go_default_library",
"//app/service/live/xrewardcenter/api/grpc/v1:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/log/infoc:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata:go_default_library",
"//library/sync/errgroup:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"@org_golang_google_grpc//status: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 = [
"anchorTask_test.go",
"history_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/live/web-ucenter/api/http/v1:go_default_library",
"//app/interface/live/web-ucenter/conf:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,261 @@
package v1
import (
"context"
v1pb "go-common/app/interface/live/web-ucenter/api/http/v1"
"go-common/app/interface/live/web-ucenter/conf"
"go-common/app/service/live/xrewardcenter/api/grpc/v1"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
)
// AnchorTaskService struct
type AnchorTaskService struct {
conf *conf.Config
// optionally add other properties here, such as dao
// dao *dao.Dao
conn v1.AnchorRewardClient
}
//NewAnchorTaskService init
func NewAnchorTaskService(c *conf.Config) (s *AnchorTaskService) {
s = &AnchorTaskService{
conf: c,
}
conn, err := v1.NewClient(conf.Conf.Warden)
if err != nil {
panic(err)
}
s.conn = conn
return s
}
// MyReward implementation
// * (主播侧)-我的主播奖励(登录态)
func (s *AnchorTaskService) MyReward(ctx context.Context, req *v1pb.AnchorTaskMyRewardReq) (resp *v1pb.AnchorTaskMyRewardResp, err error) {
resp = &v1pb.AnchorTaskMyRewardResp{}
mid, _ := metadata.Value(ctx, "mid").(int64)
if mid <= 0 {
err = ecode.NoLogin
return
}
page := req.GetPage()
if page <= 0 {
page = 1
}
ret, err := s.conn.MyReward(ctx, &v1.AnchorTaskMyRewardReq{
Page: page,
Uid: mid,
})
if err != nil {
return
}
resp.Page = &v1pb.AnchorTaskMyRewardResp_Page{
Page: ret.GetPage().GetPage(),
PageSize: ret.GetPage().GetPageSize(),
TotalPage: ret.GetPage().GetTotalPage(),
TotalCount: ret.GetPage().GetTotalCount(),
}
resp.ExpireCount = ret.GetExpireCount()
for _, v := range ret.Data {
resp.Data = append(resp.Data, &v1pb.AnchorTaskMyRewardResp_RewardObj{
Id: v.GetId(),
RewardType: v.GetRewardType(),
Status: v.GetStatus(),
RewardId: v.GetRewardId(),
Name: v.GetName(),
Icon: v.GetIcon(),
AchieveTime: v.GetAchieveTime(),
ExpireTime: v.GetExpireTime(),
Source: v.GetSource(),
RewardIntro: v.GetRewardIntro(),
})
}
return
}
// UseRecord implementation
// (主播侧)-奖励使用记录(登录态)
// `midware:"auth"`
func (s *AnchorTaskService) UseRecord(ctx context.Context, req *v1pb.AnchorTaskUseRecordReq) (resp *v1pb.AnchorTaskUseRecordResp, err error) {
resp = &v1pb.AnchorTaskUseRecordResp{}
mid, _ := metadata.Value(ctx, "mid").(int64)
if mid <= 0 {
err = ecode.NoLogin
return
}
page := req.GetPage()
if page <= 0 {
page = 1
}
ret, err := s.conn.UseRecord(ctx, &v1.AnchorTaskUseRecordReq{
Page: page,
Uid: mid,
})
if err != nil {
return
}
resp.Page = &v1pb.AnchorTaskUseRecordResp_Page{
Page: ret.GetPage().GetPage(),
PageSize: ret.GetPage().GetPageSize(),
TotalPage: ret.GetPage().GetTotalPage(),
TotalCount: ret.GetPage().GetTotalCount(),
}
for _, v := range ret.Data {
resp.Data = append(resp.Data, &v1pb.AnchorTaskUseRecordResp_RewardObj{
Id: v.GetId(),
RewardId: v.GetRewardId(),
Status: v.GetStatus(),
Name: v.GetName(),
Icon: v.GetIcon(),
AchieveTime: v.GetAchieveTime(),
ExpireTime: v.GetExpireTime(),
Source: v.GetSource(),
RewardIntro: v.GetRewardIntro(),
UseTime: v.GetUseTime(),
})
}
return
}
// UseReward implementation
// (主播侧)-使用奖励(登录态)
// `method:"POST" midware:"auth"`
func (s *AnchorTaskService) UseReward(ctx context.Context, req *v1pb.AnchorTaskUseRewardReq) (resp *v1pb.AnchorTaskUseRewardResp, err error) {
resp = &v1pb.AnchorTaskUseRewardResp{}
mid, _ := metadata.Value(ctx, "mid").(int64)
if mid <= 0 {
err = ecode.NoLogin
return
}
id := req.GetId()
if id <= 0 {
err = ecode.ParamInvalid
return
}
platform := req.GetPlatform()
if "" == platform {
platform = "web"
}
request := &v1.AnchorTaskUseRewardReq{
Id: id,
Uid: mid,
UsePlat: platform,
}
ret, err := s.conn.UseReward(ctx, request)
log.Info("useReward req(%v) ret(%v), err(%v)", request, ret, err)
if err != nil {
statusCode := ecode.Cause(err)
log.Info("useReward error statusCode(%v) ret(%v), err(%+v)", statusCode, statusCode.Code(), err)
busCode := statusCode.Code()
msg := ""
switch busCode {
case 1:
msg = "参数错误"
case 2:
msg = "这个奖励已经过期了呢"
case 3:
msg = "这个奖励已经被你使用啦~"
case 4:
msg = "为了更好的使用体验,请在开播状态下使用【任意门】哦"
case 5:
msg = "奖励不存在"
default:
msg = "内部错误"
}
err = ecode.Error(ecode.Code(busCode), msg)
return
}
resp.Result = ret.GetResult()
return
}
// IsViewed implementation
// (主播侧)-奖励和任务红点(登录态)
// `midware:"auth"`
func (s *AnchorTaskService) IsViewed(ctx context.Context, req *v1pb.AnchorTaskIsViewedReq) (resp *v1pb.AnchorTaskIsViewedResp, err error) {
resp = &v1pb.AnchorTaskIsViewedResp{}
mid, _ := metadata.Value(ctx, "mid").(int64)
if mid <= 0 {
err = ecode.NoLogin
return
}
ret, err := s.conn.IsViewed(ctx, &v1.AnchorTaskIsViewedReq{
Uid: mid,
})
log.Info("IsViewed req(%v) ret(%v), err(%v)", mid, ret, err)
if err != nil {
return
}
resp = &v1pb.AnchorTaskIsViewedResp{
TaskShouldNotice: ret.GetTaskShouldNotice(),
ShowRewardEntry: ret.GetShowRewardEntry(),
RewardShouldNotice: ret.GetRewardShouldNotice(),
TaskStatus: ret.GetTaskStatus(),
IsBlacked: ret.GetIsBlacked(),
Url: ret.GetUrl(),
}
return
}
// AddReward implementation
// (主播侧)-添加主播奖励(内部接口)
// `method:"POST" internal:"true"`
func (s *AnchorTaskService) AddReward(ctx context.Context, req *v1pb.AnchorTaskAddRewardReq) (resp *v1pb.AnchorTaskAddRewardResp, err error) {
resp = &v1pb.AnchorTaskAddRewardResp{}
ret, err := s.conn.AddReward(ctx, &v1.AnchorTaskAddRewardReq{
RewardId: req.GetRewardId(),
Roomid: req.GetRoomid(),
Source: req.GetSource(),
Uid: req.GetUid(),
OrderId: req.GetOrderId(),
})
if err != nil {
return
}
resp = &v1pb.AnchorTaskAddRewardResp{
Result: ret.GetResult(),
}
resp.Result = ret.GetResult()
return
}

View File

@@ -0,0 +1,41 @@
package v1
import (
"flag"
. "github.com/smartystreets/goconvey/convey"
api "go-common/app/interface/live/web-ucenter/api/http/v1"
"go-common/app/interface/live/web-ucenter/conf"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/metadata"
"testing"
)
var (
AnchorTask *AnchorTaskService
)
func init() {
flag.Set("conf", "../../cmd/test.toml")
var err error
if err = conf.Init(); err != nil {
panic(err)
}
AnchorTask = NewAnchorTaskService(conf.Conf)
}
// go test -test.v -test.run TestServiceAllowanceList
func TestMyReward(t *testing.T) {
Convey("TestMyReward", t, func() {
ctx := metadata.NewContext(bm.Context{}, metadata.MD{
"mid": 10000,
})
res, err := AnchorTask.MyReward(ctx, &api.AnchorTaskMyRewardReq{
Page: 1,
})
t.Logf("%+v", res)
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,321 @@
package v1
import (
"context"
"encoding/json"
"fmt"
"strconv"
"time"
"google.golang.org/grpc/status"
"go-common/library/log"
"github.com/pkg/errors"
v1pb "go-common/app/interface/live/web-ucenter/api/http/v1"
"go-common/app/interface/live/web-ucenter/conf"
capsuledao "go-common/app/interface/live/web-ucenter/dao/capsule"
"go-common/library/log/infoc"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/metadata"
)
// CapsuleService struct
type CapsuleService struct {
conf *conf.Config
// optionally add other properties here, such as dao
dao *capsuledao.Dao
infoc *infoc.Infoc
}
//NewCapsuleService init
func NewCapsuleService(c *conf.Config) (s *CapsuleService) {
s = &CapsuleService{
conf: c,
dao: capsuledao.New(c),
}
if c.Infoc != nil && c.Infoc.CapsuleInfoc != nil {
s.infoc = infoc.New(c.Infoc.CapsuleInfoc)
}
return s
}
// GetDetail implementation
func (s *CapsuleService) GetDetail(ctx context.Context, req *v1pb.CapsuleGetDetailReq) (resp *v1pb.CapsuleGetDetailResp, err error) {
resp = &v1pb.CapsuleGetDetailResp{}
uid, ok := metadata.Value(ctx, metadata.Mid).(int64)
if !ok {
err = errors.Wrap(err, "未取到uid")
return
}
data, err := s.dao.GetDetail(ctx, uid, req.From)
if err != nil {
return
}
if data.Normal != nil {
normal := &v1pb.CapsuleGetDetailResp_CapsuleInfo{}
normal.Status = data.Normal.Status
normal.Coin = data.Normal.Coin
normal.Change = data.Normal.Change
normal.Rule = data.Normal.Rule
if data.Normal.Progress != nil {
normal.Progress = &v1pb.Progress{}
normal.Progress.Now = data.Normal.Progress.Now
normal.Progress.Max = data.Normal.Progress.Max
}
glen := len(data.Normal.Gift)
normal.Gift = make([]*v1pb.CapsuleGetDetailResp_Gift, glen)
for i := 0; i < glen; i++ {
gift := &v1pb.CapsuleGetDetailResp_Gift{}
gift.Name = data.Normal.Gift[i].Name
gift.WebImage = data.Normal.Gift[i].WebImage
gift.MobileImage = data.Normal.Gift[i].MobileImage
gift.Image = data.Normal.Gift[i].Image
if data.Normal.Gift[i].Usage != nil {
gift.Usage = &v1pb.Usage{}
gift.Usage.Text = data.Normal.Gift[i].Usage.Text
gift.Usage.Url = data.Normal.Gift[i].Usage.Url
}
normal.Gift[i] = gift
}
glen = len(data.Normal.List)
normal.List = make([]*v1pb.CapsuleGetDetailResp_List, glen)
for i := 0; i < glen; i++ {
info := &v1pb.CapsuleGetDetailResp_List{}
info.Name = data.Normal.List[i].Name
info.Num = data.Normal.List[i].Num
info.Date = data.Normal.List[i].Date
info.Gift = data.Normal.List[i].Gift
normal.List[i] = info
}
resp.Normal = normal
}
if data.Colorful != nil {
colorful := &v1pb.CapsuleGetDetailResp_CapsuleInfo{}
colorful.Status = data.Colorful.Status
colorful.Coin = data.Colorful.Coin
colorful.Change = data.Colorful.Change
colorful.Rule = data.Colorful.Rule
if data.Colorful.Progress != nil {
colorful.Progress = &v1pb.Progress{}
colorful.Progress.Now = data.Colorful.Progress.Now
colorful.Progress.Max = data.Colorful.Progress.Max
}
glen := len(data.Colorful.Gift)
colorful.Gift = make([]*v1pb.CapsuleGetDetailResp_Gift, glen)
for i := 0; i < glen; i++ {
gift := &v1pb.CapsuleGetDetailResp_Gift{}
gift.Name = data.Colorful.Gift[i].Name
gift.WebImage = data.Colorful.Gift[i].WebImage
gift.MobileImage = data.Colorful.Gift[i].MobileImage
gift.Image = data.Colorful.Gift[i].Image
if data.Colorful.Gift[i].Usage != nil {
gift.Usage = &v1pb.Usage{}
gift.Usage.Text = data.Colorful.Gift[i].Usage.Text
gift.Usage.Url = data.Colorful.Gift[i].Usage.Url
}
colorful.Gift[i] = gift
}
glen = len(data.Colorful.List)
colorful.List = make([]*v1pb.CapsuleGetDetailResp_List, glen)
for i := 0; i < glen; i++ {
info := &v1pb.CapsuleGetDetailResp_List{}
info.Name = data.Colorful.List[i].Name
info.Num = data.Colorful.List[i].Num
info.Date = data.Colorful.List[i].Date
info.Gift = data.Colorful.List[i].Gift
colorful.List[i] = info
}
resp.Colorful = colorful
}
return
}
// OpenCapsule implementation
func (s *CapsuleService) OpenCapsule(ctx context.Context, req *v1pb.CapsuleOpenCapsuleReq) (resp *v1pb.CapsuleOpenCapsuleResp, err error) {
resp = &v1pb.CapsuleOpenCapsuleResp{}
uid, ok := metadata.Value(ctx, metadata.Mid).(int64)
if !ok {
err = errors.Wrap(err, "未取到uid")
return
}
data, err := s.dao.OpenCapsule(ctx, uid, req.Type, req.Count, req.Platform)
if err != nil {
return
}
if data.Info != nil {
resp.Info = &v1pb.CapsuleOpenCapsuleResp_Info{}
if data.Info.Colorful != nil {
resp.Info.Colorful = &v1pb.CapsuleOpenCapsuleResp_CapsuleInfo{}
resp.Info.Colorful.Change = data.Info.Colorful.Change
resp.Info.Colorful.Coin = data.Info.Colorful.Coin
if data.Info.Colorful.Progress != nil {
resp.Info.Colorful.Progress = &v1pb.Progress{}
resp.Info.Colorful.Progress.Max = data.Info.Colorful.Progress.Max
resp.Info.Colorful.Progress.Now = data.Info.Colorful.Progress.Now
}
}
if data.Info.Normal != nil {
resp.Info.Normal = &v1pb.CapsuleOpenCapsuleResp_CapsuleInfo{}
resp.Info.Normal.Change = data.Info.Normal.Change
resp.Info.Normal.Coin = data.Info.Normal.Coin
if data.Info.Normal.Progress != nil {
resp.Info.Normal.Progress = &v1pb.Progress{}
resp.Info.Normal.Progress.Max = data.Info.Normal.Progress.Max
resp.Info.Normal.Progress.Now = data.Info.Normal.Progress.Now
}
}
}
resp.Status = data.Status
resp.IsEntity = data.IsEntity
resp.ShowTitle = data.ShowTitle
resp.Text = data.Text
l := len(data.Awards)
resp.Awards = make([]*v1pb.CapsuleOpenCapsuleResp_Award, l)
for i := 0; i < l; i++ {
resp.Awards[i] = &v1pb.CapsuleOpenCapsuleResp_Award{}
resp.Awards[i].Num = data.Awards[i].Num
resp.Awards[i].Name = data.Awards[i].Name
resp.Awards[i].Text = data.Awards[i].Text
resp.Awards[i].MobileImage = data.Awards[i].MobileImage
resp.Awards[i].WebImage = data.Awards[i].WebImage
resp.Awards[i].Img = data.Awards[i].Img
if data.Awards[i].Usage != nil {
resp.Awards[i].Usage = &v1pb.Usage{}
resp.Awards[i].Usage.Text = data.Awards[i].Usage.Text
resp.Awards[i].Usage.Url = data.Awards[i].Usage.Url
}
}
if s.infoc != nil {
awards, err1 := json.Marshal(resp.Awards)
if err1 != nil {
log.Error("OpenCapsule OpenCapsule err")
return
}
bmc, ok := ctx.(bm.Context)
if !ok {
return
}
header := bmc.Request.Header
userip := header.Get("x-backend-bili-real-ip")
if userip == "" {
userip = bmc.Request.RemoteAddr
}
s.infoc.Infov(context.Background(), uid, userip, strconv.FormatInt(time.Now().Unix(), 10), awards, header.Get("platform"), header.Get("version"), header.Get("buvid"), bmc.Request.UserAgent(), bmc.Request.Referer())
}
return
}
// FormatErr format error msg
func (s *CapsuleService) FormatErr(statusCode *status.Status) (code int32, msg string) {
gCode := statusCode.Code()
fmt.Printf("FormatErr %d %s", gCode, statusCode.Message())
code = 1
if gCode == 2 {
code, _ := strconv.Atoi(statusCode.Message())
switch code {
case -400:
msg = "参数错误"
case -401:
msg = "扭蛋币不足"
case -500:
msg = "系统繁忙,请稍后再试"
case -501:
msg = "系统繁忙,请稍后再试"
default:
msg = "系统繁忙,请稍后再试"
}
} else {
msg = "系统繁忙,请稍后再试"
}
return
}
// GetCapsuleInfo implementation
// `midware:"guest"`
func (s *CapsuleService) GetCapsuleInfo(ctx context.Context, req *v1pb.CapsuleGetCapsuleInfoReq) (resp *v1pb.CapsuleGetCapsuleInfoResp, err error) {
resp = &v1pb.CapsuleGetCapsuleInfoResp{}
uid, _ := metadata.Value(ctx, metadata.Mid).(int64)
data, err := s.dao.GetCapsuleInfo(ctx, uid, req.Type, req.From)
if err != nil || data == nil {
return
}
resp.Coin = data.Coin
resp.Rule = data.Rule
if len(data.GiftFilter) > 0 {
resp.GiftFilter = make([]*v1pb.CapsuleGetCapsuleInfoResp_GiftFilter, len(data.GiftFilter))
for ix, award := range data.GiftFilter {
resp.GiftFilter[ix] = &v1pb.CapsuleGetCapsuleInfoResp_GiftFilter{}
resp.GiftFilter[ix].Id = award.Id
resp.GiftFilter[ix].Name = award.Name
resp.GiftFilter[ix].WebUrl = award.WebUrl
resp.GiftFilter[ix].MobileUrl = award.MobileUrl
if award.Usage != nil {
resp.GiftFilter[ix].Usage = &v1pb.Usage{}
resp.GiftFilter[ix].Usage.Text = award.Usage.Text
resp.GiftFilter[ix].Usage.Url = award.Usage.Url
}
}
}
if len(data.GiftList) > 0 {
resp.GiftList = make([]*v1pb.CapsuleGetCapsuleInfoResp_GiftList, len(data.GiftList))
for ix, award := range data.GiftList {
resp.GiftList[ix] = &v1pb.CapsuleGetCapsuleInfoResp_GiftList{}
resp.GiftList[ix].Id = award.Id
resp.GiftList[ix].Name = award.Name
resp.GiftList[ix].Num = award.Num
resp.GiftList[ix].WebUrl = award.WebUrl
resp.GiftList[ix].MobileUrl = award.MobileUrl
resp.GiftList[ix].Type = award.Type
resp.GiftList[ix].Expire = award.Expire
if award.Usage != nil {
resp.GiftList[ix].Usage = &v1pb.Usage{}
resp.GiftList[ix].Usage.Text = award.Usage.Text
resp.GiftList[ix].Usage.Url = award.Usage.Url
}
}
}
return
}
// OpenCapsuleByType implementation
// `method:"POST" midware:"auth"`
func (s *CapsuleService) OpenCapsuleByType(ctx context.Context, req *v1pb.CapsuleOpenCapsuleByTypeReq) (resp *v1pb.CapsuleOpenCapsuleByTypeResp, err error) {
resp = &v1pb.CapsuleOpenCapsuleByTypeResp{}
uid, ok := metadata.Value(ctx, metadata.Mid).(int64)
if !ok {
err = errors.Wrap(err, "未取到uid")
return
}
data, err := s.dao.OpenCapsuleByType(ctx, uid, req.Type, req.Count, req.Platform)
if err != nil || data == nil {
return
}
resp.IsEntity = data.IsEntity
resp.Status = data.Status
resp.Text = data.Text
resp.Info = &v1pb.CapsuleOpenCapsuleByTypeResp_CapsuleInfo{Coin: 0}
if data.Info != nil {
resp.Info.Coin = data.Info.Coin
}
resp.Awards = make([]*v1pb.CapsuleOpenCapsuleByTypeResp_Award, len(data.Awards))
for ix, award := range data.Awards {
resp.Awards[ix] = &v1pb.CapsuleOpenCapsuleByTypeResp_Award{}
resp.Awards[ix].Id = award.Id
resp.Awards[ix].Name = award.Name
resp.Awards[ix].Num = award.Num
resp.Awards[ix].Text = award.Text
resp.Awards[ix].WebUrl = award.WebUrl
resp.Awards[ix].MobileUrl = award.MobileUrl
resp.Awards[ix].Type = award.Type
resp.Awards[ix].Expire = award.Expire
if award.Usage != nil {
resp.Awards[ix].Usage = &v1pb.Usage{}
resp.Awards[ix].Usage.Text = award.Usage.Text
resp.Awards[ix].Usage.Url = award.Usage.Url
}
}
return
}

View File

@@ -0,0 +1,106 @@
package v1
import (
"context"
"github.com/pkg/errors"
historypb "go-common/app/interface/live/web-ucenter/api/http/v1"
"go-common/app/interface/live/web-ucenter/conf"
"go-common/app/interface/live/web-ucenter/dao"
historydao "go-common/app/interface/live/web-ucenter/dao/history"
"go-common/app/service/live/room/api/liverpc/v2"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
)
// Service struct
type Service struct {
c *conf.Config
dao *historydao.Dao
}
// New init
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
dao: historydao.New(c),
}
return s
}
// GetHistoryByUid 获取直播历史记录
func (s *Service) GetHistoryByUid(ctx context.Context, req *historypb.GetHistoryReq) (resp *historypb.GetHistoryResp, err error) {
uid, ok := metadata.Value(ctx, metadata.Mid).(int64)
if !ok {
err = errors.Wrap(err, "未取到uid")
return
}
mainHistoryInfo, err := s.dao.GetMainHistory(ctx, int32(uid))
if err != nil {
err = errors.Wrap(err, "Call GetMainHistory err")
return
}
if mainHistoryInfo == nil {
return
}
RoomIds := make([]int64, 0)
for _, v := range mainHistoryInfo {
RoomIds = append(RoomIds, v.RoomId)
}
reply, err := dao.RoomAPI.V2Room.GetByIds(ctx, &v2.RoomGetByIdsReq{Ids: RoomIds})
if err != nil {
err = errors.Wrap(err, "Call GetByIds err")
return
}
if reply.GetCode() != 0 {
err = ecode.Int(int(reply.GetCode()))
return
}
roomInfos := reply.Data
resp = &historypb.GetHistoryResp{}
for _, RoomId := range RoomIds {
list := &historypb.GetHistoryResp_List{}
room, ok := roomInfos[RoomId]
if !ok {
log.Warn("[GetHistoryByUid] req(%v), uid(%d), failed to get room(%d) info from (%v)", req, uid, RoomId, roomInfos)
continue
}
list.Roomid = RoomId
list.Uid = int32(room.Uid)
list.Uname = room.Uname
list.Title = room.Title
list.Face = room.Face
list.LiveStatus = int32(room.LiveStatus)
list.FansNum = int32(room.Attentions)
list.AreaV2Id = int32(room.AreaV2Id)
list.AreaV2Name = room.AreaV2Name
list.LiveStatus = int32(room.LiveStatus)
list.UserCover = room.UserCover
list.AreaV2ParentId = int32(room.AreaV2ParentId)
list.AreaV2ParentName = room.AreaV2ParentName
list.Tags = room.Tags
resp.List = append(resp.List, list)
}
resp.Title = "哔哩哔哩直播 - 观看历史"
resp.Count = int32(len(roomInfos))
return
}
// DelHistory 删除直播历史记录
func (s *Service) DelHistory(ctx context.Context, req *historypb.DelHistoryReq) (resp *historypb.DelHistoryResp, err error) {
uid, ok := metadata.Value(ctx, metadata.Mid).(int64)
if !ok {
err = errors.Wrap(err, "未取到uid")
return
}
reply, err := s.dao.DelHistory(ctx, uid)
resp = &historypb.DelHistoryResp{}
if err != nil || reply != 0 {
err = ecode.Int(int(reply))
return
}
return
}

View File

@@ -0,0 +1,43 @@
package v1
import (
"context"
"flag"
"testing"
. "github.com/smartystreets/goconvey/convey"
api "go-common/app/interface/live/web-ucenter/api/http/v1"
"go-common/app/interface/live/web-ucenter/conf"
bm "go-common/library/net/http/blademaster"
)
var (
s *Service
)
func init() {
flag.Set("conf", "../../cmd/account-interface-example.toml")
var err error
if err = conf.Init(); err != nil {
panic(err)
}
s = New(conf.Conf)
}
// go test -test.v -test.run TestServiceAllowanceList
func TestGetHistoryByUid(t *testing.T) {
Convey("TestGetHistoryByUid", t, func() {
res, err := s.GetHistoryByUid(&bm.Context{Context: context.TODO()}, &api.GetHistoryReq{})
t.Logf("%v", res)
So(err, ShouldBeNil)
})
}
func TestDelHistory(t *testing.T) {
Convey("TestDelHistory", t, func() {
res, err := s.DelHistory(&bm.Context{Context: context.TODO()}, &api.DelHistoryReq{})
t.Logf("%v", res)
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,141 @@
package v1
import (
"context"
"github.com/pkg/errors"
pb "go-common/app/interface/live/web-ucenter/api/http"
"go-common/app/interface/live/web-ucenter/conf"
"go-common/app/interface/live/web-ucenter/dao/user"
"go-common/app/interface/live/web-ucenter/model"
"go-common/library/log"
"go-common/library/net/metadata"
"go-common/library/sync/errgroup"
"strings"
)
// UserService user service
type UserService struct {
c *conf.Config
dao *user.Dao
}
// NewUserService new user service
func NewUserService(c *conf.Config) (s *UserService) {
s = &UserService{
c: c,
dao: user.New(c),
}
return s
}
// GetUserInfo implementation
// 根据uid查询用户信息
// `midware:"auth"`,需要登录态
func (s *UserService) GetUserInfo(ctx context.Context, req *pb.GetInfoReq) (resp *pb.GetInfoResp, err error) {
var (
group, errCtx = errgroup.WithContext(ctx)
userExp int64
userRank string
)
// check login
uid := metadata.Int64(ctx, metadata.Mid)
if uid == 0 {
err = errors.Wrap(err, "请先登录")
return
}
platform := checkPlatform(req.Platform)
resp = &pb.GetInfoResp{
Uid: uid,
UserCharged: 0,
}
// 并行获取account / xuser.vip / xuser.exp / wallet / rc / rankdb
func() {
// account.ProfileWithStat3
group.Go(func() (err error) {
profile, err := s.dao.GetAccountProfile(errCtx, uid)
if err != nil {
log.Error("[service.v1.user|GetUserInfo] GetAccountProfile error(%v), uid(%d)", err, uid)
return nil
}
resp.Uname = profile.Name
resp.Face = strings.Replace(profile.Face, "http://", "https://", 1)
resp.Coin = profile.Coins
return
})
// wallet
group.Go(func() (err error) {
silver, gold, err := s.dao.GetWallet(errCtx, uid, platform)
if err != nil {
log.Error("[service.v1.user|GetUserInfo] GetWallet error(%v), uid(%d)", err, uid)
return nil
}
resp.Silver = silver
resp.Gold = gold
return
})
// xuser.vip
group.Go(func() (err error) {
vipInfo, err := s.dao.GetLiveVip(errCtx, uid)
if err != nil || vipInfo == nil || vipInfo.Info == nil {
log.Error("[service.v1.user|GetUserInfo] GetLiveVip error(%v), uid(%d)", err, uid)
return nil
}
resp.Vip = vipInfo.Info.Vip
resp.Svip = vipInfo.Info.Svip
return
})
// xuser.exp
group.Go(func() (err error) {
expInfo, err := s.dao.GetLiveExp(errCtx, uid)
if err != nil || expInfo == nil || expInfo.UserLevel == nil {
log.Error("[service.v1.user|GetUserInfo] GetLiveExp error(%v), uid(%d)", err, uid)
return nil
}
userExp = expInfo.UserLevel.UserExp
resp.UserLevel = expInfo.UserLevel.Level
resp.UserNextLevel = expInfo.UserLevel.NextLevel
resp.UserIntimacy = expInfo.UserLevel.UserExp - expInfo.UserLevel.UserExpLeft
resp.UserNextIntimacy = expInfo.UserLevel.UserExpNextLevel
resp.IsLevelTop = expInfo.UserLevel.IsLevelTop
return
})
// rc
group.Go(func() (err error) {
achieve, err := s.dao.GetLiveAchieve(errCtx, uid)
if err != nil {
log.Error("[service.v1.user|GetUserInfo] GetLiveAchieve error(%v), uid(%d)", err, uid)
return nil
}
resp.Achieve = achieve
return
})
// rankdb
group.Go(func() (err error) {
if userRank, err = s.dao.GetLiveRank(errCtx, uid); err != nil {
log.Error("[service.v1.user|GetUserInfo] GetLiveRank error(%v), uid(%d)", err, uid)
return nil
}
return
})
}()
group.Wait()
// 根据exp & rankdb 判断组装返回的user_level_rank字段
if userExp < 120000000 {
resp.UserLevelRank = ">50000"
} else {
resp.UserLevelRank = userRank
}
log.Info("GetUserInfo.resp(%v)", resp)
return
}
func checkPlatform(p string) string {
if p == "" || (p != model.PlatformIos && p != model.PlatformAndroid) {
return model.PlatformPc
}
return p
}