You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
449 lines
12 KiB
449 lines
12 KiB
package live
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/shopspring/decimal"
|
|
"live/app/common"
|
|
"live/app/lib"
|
|
"live/app/lib/back"
|
|
"live/app/lib/config"
|
|
"live/app/lib/recook"
|
|
"live/app/lib/tencent"
|
|
"live/app/lib/tools"
|
|
"live/app/logic/live"
|
|
live2 "live/app/model/live"
|
|
"live/app/model/user"
|
|
)
|
|
|
|
type Live struct {
|
|
}
|
|
|
|
type replyLists struct {
|
|
Title string `json:"title"`
|
|
Id uint `json:"id"`
|
|
Cover string `json:"cover"`
|
|
Nickname string `json:"nickname"`
|
|
HeadImgUrl string `json:"headImgUrl"`
|
|
Look int `json:"look"`
|
|
GoodsName string `json:"goodsName"`
|
|
MainPhotoURL string `json:"mainPhotoUrl"`
|
|
OriginalPrice string `json:"originalPrice"`
|
|
DiscountPrice string `json:"discountPrice"`
|
|
Praise int `json:"praise"`
|
|
IsLive int `json:"isLive"`
|
|
}
|
|
|
|
func (l *Live) List(c *gin.Context) {
|
|
args := lib.Page{}
|
|
if err := tools.ParseParams(&args, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
result := []replyLists{}
|
|
lists := live.LiveLogic.Lists(args)
|
|
if len(lists) > 0 {
|
|
userIds := []uint{}
|
|
liveItemIds := []uint{}
|
|
mainGoodsIds := []uint{}
|
|
for _, list := range lists {
|
|
userIds = append(userIds, list.UserId)
|
|
liveItemIds = append(liveItemIds, list.Id)
|
|
mainGoodsIds = append(mainGoodsIds, list.MainGoodsId)
|
|
}
|
|
users, _ := recook.User.GetUsers(userIds)
|
|
userMap := map[uint]recook.ReplyUserItem{}
|
|
for _, item := range *users {
|
|
userMap[item.UserId] = item
|
|
}
|
|
liveData := &live2.LiveData{}
|
|
liveDatas := liveData.GetByLiveItemIds(liveItemIds)
|
|
liveDataMap := map[uint]live2.LiveData{}
|
|
for _, item := range liveDatas {
|
|
liveDataMap[item.LiveItemId] = item
|
|
}
|
|
|
|
goodsInfos, _ := recook.Goods.GetListByIds(mainGoodsIds)
|
|
goodsMap := map[uint]recook.GoodsInfo{}
|
|
for _, info := range *goodsInfos {
|
|
goodsMap[info.ID] = info
|
|
}
|
|
|
|
for _, list := range lists {
|
|
result = append(result, replyLists{
|
|
Id: list.Id,
|
|
Title: list.Title,
|
|
Cover: list.Cover,
|
|
IsLive: list.Status,
|
|
Nickname: userMap[list.UserId].Nickname,
|
|
HeadImgUrl: userMap[list.UserId].HeadImgUrl,
|
|
Look: liveDataMap[list.Id].Look,
|
|
Praise: liveDataMap[list.Id].Praise,
|
|
GoodsName: goodsMap[list.MainGoodsId].GoodsName,
|
|
MainPhotoURL: goodsMap[list.MainGoodsId].MainPhotoURL,
|
|
OriginalPrice: goodsMap[list.MainGoodsId].OriginalPrice,
|
|
DiscountPrice: goodsMap[list.MainGoodsId].DiscountPrice,
|
|
})
|
|
}
|
|
}
|
|
back.Suc(c, "操作成功", result)
|
|
}
|
|
|
|
type userFolowLive struct {
|
|
Id uint `json:"id"`
|
|
UserId uint `json:"userId"`
|
|
}
|
|
|
|
type replyFollowList struct {
|
|
Id uint `json:"id"`
|
|
Nickname string `json:"nickname"`
|
|
HeadImgUrl string `json:"headImgUrl"`
|
|
IsLive int `json:"isLive"`
|
|
}
|
|
|
|
// @Title 关注直播列表
|
|
func (l *Live) FollowList(c *gin.Context) {
|
|
args := lib.Page{}
|
|
if err := tools.ParseParams(&args, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
userId := common.GetUserId(c)
|
|
db := (&user.UserFollow{}).GetDb()
|
|
prefix := config.Config.Section("mysql").Key("prefix").String()
|
|
lists := []userFolowLive{}
|
|
|
|
db.Table(fmt.Sprintf("%vuser_follow AS user_follow", prefix)).Joins(fmt.Sprintf("left join %vlive_item as live_item on live_item.user_id=user_follow.follow_user_id", prefix)).
|
|
Where("live_item.status = ? and user_follow.user_id = ?", live2.LIVE_STATUS_ing, userId).
|
|
Select("live_item.id,live_item.user_id").Offset(args.GetStart()).Limit(args.Limit).Order("live_item.id").Find(&lists)
|
|
|
|
userIds := []uint{}
|
|
for _, list := range lists {
|
|
userIds = append(userIds, list.UserId)
|
|
}
|
|
|
|
users, _ := recook.User.GetUsers(userIds)
|
|
userMap := map[uint]recook.ReplyUserItem{}
|
|
for _, item := range *users {
|
|
userMap[item.UserId] = item
|
|
}
|
|
replay := []replyFollowList{}
|
|
for _, list := range lists {
|
|
replay = append(replay, replyFollowList{
|
|
Id: list.Id,
|
|
Nickname: userMap[list.UserId].Nickname,
|
|
HeadImgUrl: userMap[list.UserId].HeadImgUrl,
|
|
IsLive: 1,
|
|
})
|
|
}
|
|
|
|
back.Suc(c, "操作成功", replay)
|
|
}
|
|
|
|
type argsStart struct {
|
|
Title string `json:"title" form:"title"`
|
|
Cover string `json:"cover" form:"cover"`
|
|
Topic uint `json:"" form:"topic"`
|
|
GoodsIds []uint `json:"goodsIds" form:"goodsIds"`
|
|
}
|
|
|
|
// @Title 开播
|
|
func (l *Live) Start(c *gin.Context) {
|
|
args := argsStart{}
|
|
if err := tools.ParseParams(&args, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if args.Title == "" || args.Cover == "" || len(args.GoodsIds) == 0 {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
userId := common.GetUserId(c)
|
|
start, err := live.LiveLogic.Start(userId, args.Title, args.Cover, args.Topic, args.GoodsIds)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
back.Suc(c, "操作成功", start)
|
|
}
|
|
|
|
type argsLiveId struct {
|
|
Id uint `json:"id" form:"id"`
|
|
}
|
|
|
|
type replyLiveInfo struct {
|
|
Nickname string `json:"nickname"`
|
|
HeadImgUrl string `json:"headImgUrl"`
|
|
PlayUrl string `json:"playUrl"`
|
|
UserId uint `json:"userId"`
|
|
Id uint `json:"id"`
|
|
IsFollow int `json:"isFollow"`
|
|
IsPraise int `json:"isPraise"`
|
|
Praise int `json:"praise"`
|
|
GoodsLists []GoodsItem `json:"goodsLists"`
|
|
GroupId string `json:"groupId"`
|
|
}
|
|
|
|
type GoodsItem struct {
|
|
recook.GoodsInfo
|
|
IsExplain uint `json:"isExplain"`
|
|
}
|
|
|
|
// @Title 直播信息
|
|
func (l *Live) LiveInfo(c *gin.Context) {
|
|
args := argsLiveId{}
|
|
if err := tools.ParseParams(&args, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if args.Id <= 0 {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
liveItemModel := &live2.LiveItem{}
|
|
liveItem := liveItemModel.FindById(args.Id)
|
|
if liveItem.Id <= 0 {
|
|
back.Fail(c, "直播间错误")
|
|
return
|
|
}
|
|
if liveItem.Status == live2.LIVE_STATUS_finish {
|
|
back.Fail(c, "直播已结束")
|
|
return
|
|
}
|
|
users, err := recook.User.GetUsers([]uint{liveItem.UserId})
|
|
if err != nil || len(*users) == 0 {
|
|
back.Fail(c, "网络异常")
|
|
return
|
|
}
|
|
isFollow := 0
|
|
isPraise := 0
|
|
userId := common.GetUserId(c)
|
|
if userId > 0 {
|
|
userFollow := (&user.UserFollow{}).GetByUserIdAndFollowId(userId, liveItem.UserId)
|
|
if userFollow.Id > 0 && userFollow.IsDel == 0 {
|
|
isFollow = 1
|
|
}
|
|
|
|
userLivePraise := (&user.UserLivePraise{}).GetPraiseByUserIdAndLiveItemId(userId, liveItem.Id)
|
|
if userLivePraise.Id > 0 && userLivePraise.IsDel == 0 {
|
|
isPraise = 1
|
|
}
|
|
}
|
|
|
|
liveData := (&live2.LiveData{}).GetByLiveItemId(liveItem.Id)
|
|
liveRoom := (&live2.LiveRoom{}).GetLiveByUserId(liveItem.UserId)
|
|
|
|
itemGoods := (&live2.LiveGoods{}).GetGoodsByLiveItemId(liveItem.Id)
|
|
goodsIds := []uint{}
|
|
for _, item := range itemGoods {
|
|
goodsIds = append(goodsIds, item.GoodsId)
|
|
}
|
|
goodsInfos, err := recook.Goods.GetListByIds(goodsIds)
|
|
if err != nil {
|
|
back.Fail(c, "网络异常")
|
|
return
|
|
}
|
|
goodsInfoMap := map[uint]recook.GoodsInfo{}
|
|
for _, info := range *goodsInfos {
|
|
goodsInfoMap[info.ID] = info
|
|
}
|
|
goodsList := []GoodsItem{}
|
|
for _, item := range itemGoods {
|
|
goodsList = append(goodsList, GoodsItem{
|
|
GoodsInfo: goodsInfoMap[item.GoodsId],
|
|
IsExplain: item.IsExplain,
|
|
})
|
|
}
|
|
|
|
// 观看直播,进行统计
|
|
live.DataLogic.Look(userId, liveItem.Id)
|
|
|
|
reply := replyLiveInfo{
|
|
Nickname: (*users)[0].Nickname,
|
|
HeadImgUrl: (*users)[0].HeadImgUrl,
|
|
PlayUrl: tencent.Live.GetPlayUrl(liveRoom.StreamName),
|
|
GroupId: liveRoom.GroupId,
|
|
UserId: liveItem.UserId,
|
|
Id: liveItem.Id,
|
|
IsFollow: isFollow,
|
|
IsPraise: isPraise,
|
|
Praise: liveData.Praise,
|
|
GoodsLists: goodsList,
|
|
}
|
|
back.Suc(c, "操作成功", reply)
|
|
}
|
|
|
|
// @Title 点播
|
|
func (l *Live) VideoInfo(c *gin.Context) {
|
|
args := argsLiveId{}
|
|
if err := tools.ParseParams(&args, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if args.Id <= 0 {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
liveItemModel := &live2.LiveItem{}
|
|
liveItem := liveItemModel.FindById(args.Id)
|
|
if liveItem.Id <= 0 {
|
|
back.Fail(c, "直播间错误")
|
|
return
|
|
}
|
|
if liveItem.Status != live2.LIVE_STATUS_finish {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
users, err := recook.User.GetUsers([]uint{liveItem.UserId})
|
|
if err != nil || len(*users) == 0 {
|
|
back.Fail(c, "网络异常")
|
|
return
|
|
}
|
|
isFollow := 0
|
|
isPraise := 0
|
|
userId := common.GetUserId(c)
|
|
if userId > 0 {
|
|
userFollow := (&user.UserFollow{}).GetByUserIdAndFollowId(userId, liveItem.UserId)
|
|
if userFollow.Id > 0 && userFollow.IsDel == 0 {
|
|
isFollow = 1
|
|
}
|
|
|
|
userLivePraise := (&user.UserLivePraise{}).GetPraiseByUserIdAndLiveItemId(userId, liveItem.Id)
|
|
if userLivePraise.Id > 0 && userLivePraise.IsDel == 0 {
|
|
isPraise = 1
|
|
}
|
|
}
|
|
|
|
liveData := (&live2.LiveData{}).GetByLiveItemId(liveItem.Id)
|
|
|
|
itemGoods := (&live2.LiveGoods{}).GetGoodsByLiveItemId(liveItem.Id)
|
|
goodsIds := []uint{}
|
|
for _, item := range itemGoods {
|
|
goodsIds = append(goodsIds, item.GoodsId)
|
|
}
|
|
goodsInfos, err := recook.Goods.GetListByIds(goodsIds)
|
|
if err != nil {
|
|
back.Fail(c, "网络异常")
|
|
return
|
|
}
|
|
goodsInfoMap := map[uint]recook.GoodsInfo{}
|
|
for _, info := range *goodsInfos {
|
|
goodsInfoMap[info.ID] = info
|
|
}
|
|
goodsList := []GoodsItem{}
|
|
for _, item := range itemGoods {
|
|
goodsList = append(goodsList, GoodsItem{
|
|
GoodsInfo: goodsInfoMap[item.GoodsId],
|
|
})
|
|
}
|
|
|
|
reply := replyLiveInfo{
|
|
Nickname: (*users)[0].Nickname,
|
|
HeadImgUrl: (*users)[0].HeadImgUrl,
|
|
PlayUrl: liveItem.VideoUrl,
|
|
UserId: liveItem.UserId,
|
|
Id: liveItem.Id,
|
|
IsFollow: isFollow,
|
|
IsPraise: isPraise,
|
|
Praise: liveData.Praise,
|
|
GoodsLists: goodsList,
|
|
}
|
|
back.Suc(c, "操作成功", reply)
|
|
}
|
|
|
|
type argsExplain struct {
|
|
LiveItemId uint `json:"liveItemId" form:"liveItemId"`
|
|
GoodsId uint `json:"goodsId" form:"goodsId"`
|
|
}
|
|
|
|
// @Title 讲解
|
|
func (l *Live) Explain(c *gin.Context) {
|
|
args := argsExplain{}
|
|
if err := tools.ParseParams(&args, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if args.LiveItemId <= 0 || args.GoodsId <= 0 {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
userId := common.GetUserId(c)
|
|
liveItem := (&live2.LiveItem{}).FindById(args.LiveItemId)
|
|
if liveItem.Id <= 0 || liveItem.UserId != userId {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
liveGoods := &live2.LiveGoods{}
|
|
liveGoods.UnExplain(args.LiveItemId)
|
|
liveGoods.Explain(args.LiveItemId, args.GoodsId)
|
|
|
|
live.ImLogic.SendLiveGroupMessage(liveItem.RoomId, live.LiveGroupMsgTypeExplain, gin.H{
|
|
"goodsId": args.GoodsId,
|
|
})
|
|
|
|
back.Suc(c, "操作成功", nil)
|
|
}
|
|
|
|
// @Title 取消讲解
|
|
func (l *Live) UnExplain(c *gin.Context) {
|
|
args := argsExplain{}
|
|
if err := tools.ParseParams(&args, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if args.LiveItemId <= 0 || args.GoodsId <= 0 {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
userId := common.GetUserId(c)
|
|
liveItem := (&live2.LiveItem{}).FindById(args.LiveItemId)
|
|
if liveItem.Id <= 0 || liveItem.UserId != userId {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
liveGoods := &live2.LiveGoods{}
|
|
liveGoods.UnExplain(args.LiveItemId)
|
|
|
|
live.ImLogic.SendLiveGroupMessage(liveItem.RoomId, live.LiveGroupMsgTypeUnExplain, gin.H{
|
|
"goodsId": args.GoodsId,
|
|
})
|
|
back.Suc(c, "操作成功", nil)
|
|
}
|
|
|
|
type argsStop struct {
|
|
LiveItemId uint `json:"liveItemId" form:"liveItemId"`
|
|
}
|
|
|
|
type replyStop struct {
|
|
Nickname string `json:"nickname"`
|
|
HeadImgUrl string `json:"headImgUrl"`
|
|
Duration int `json:"duration"`
|
|
SalesVolume decimal.Decimal `json:"salesVolume"`
|
|
MonthDuration int `json:"monthDuration"`
|
|
AnticipatedRevenue decimal.Decimal `json:"anticipatedRevenue"`
|
|
Buy int `json:"buy"`
|
|
Look int `json:"look"`
|
|
Praise int `json:"praise"`
|
|
Fans int `json:"'fans'"`
|
|
}
|
|
|
|
// @Title 结束直播
|
|
func (l *Live) Stop(c *gin.Context) {
|
|
args := argsStop{}
|
|
if err := tools.ParseParams(&args, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if args.LiveItemId <= 0 {
|
|
back.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
userId := common.GetUserId(c)
|
|
if err := live.LiveLogic.Stop(userId, args.LiveItemId); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
result := live.LiveLogic.GetLiveInfo(userId, args.LiveItemId)
|
|
back.Suc(c, "操作成功", result)
|
|
}
|