互动
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.

145 lines
3.7 KiB

package client
import (
"hudongzhuanjia/controllers"
"hudongzhuanjia/libs/filter"
"hudongzhuanjia/models"
pay_service "hudongzhuanjia/services/pay"
red_envelope_service "hudongzhuanjia/services/red_envelope"
"hudongzhuanjia/utils/code"
"strings"
"time"
)
type LiveCtl struct {
controllers.AuthorCtl
//controllers.BaseCtl
}
// 详情
func (t *LiveCtl) Detail() {
activityId := t.MustGetInt64("activity_id")
areaId := t.MustGetInt64("area_id")
//var userId int64 = 0
userId := t.MustGetUID()
err := new(models.LiveViewer).Record(userId, activityId)
t.CheckErr(err)
live := new(models.LiveConfig)
exist, err := live.GetByActivityId(activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
config := new(models.LiveConfigArea)
exist, err = config.GetByActivityIdAndAreaId(activityId, areaId)
t.CheckErr(err)
if exist {
live.SharePosterImg = config.MergeSharePoster
}
live.AdminLiveUrl = ""
t.JSON(live)
}
func (t *LiveCtl) Like() {
activityId := t.MustGetInt64("activity_id")
//userId := t.MustGetUID()
_, err := new(models.LiveConfig).Like(activityId)
t.CheckErr(err)
live := new(models.LiveConfig)
exist, err := live.GetByActivityId(activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
t.JSON(map[string]interface{}{
"like": live.LikeNum,
"watch": live.WatchNum,
})
}
func (t *LiveCtl) LoopQuery() {
activityId := t.MustGetInt64("activity_id")
live := new(models.LiveConfig)
exist, err := live.GetByActivityId(activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "直播活动不存在")
t.JSON(map[string]interface{}{
"like": live.LikeNum,
"watch": live.WatchNum,
})
}
// 发送红包
func (t *LiveCtl) SendLiveRedPack() {
userId := t.MustGetUID() // 用户 uid
activityId := t.MustGetInt64("activity_id") // activity_id
num := t.MustGetInt("num") // 红包数量
amount := t.MustGetDouble("amount") // 金额
prompt := t.MustGet("prompt") // 提示
user := models.User{}
exist, err := models.GetById(&user, userId)
t.CheckErr(err)
t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
ip := strings.Split(t.Request.OriginRequest.RemoteAddr, ":")
res, err := pay_service.Order("欧轩互动-直播红包", ip[0], user.Openid, int(amount*100), 3, userId, activityId)
t.CheckErr(err)
info := models.LiveRedPackInfo{}
info.UserOrderId = res["user_order_id"].(int64)
info.Amount = amount
info.UserId = userId
info.ActivityId = activityId
info.Prompt = prompt
info.IsDelete = false
info.UpdatedAt = time.Now()
info.CreatedAt = time.Now()
_, err = info.Add()
t.CheckErr(err)
redPacks := red_envelope_service.GenRedPack(int(amount*100), num)
for _, v := range redPacks {
redPack := new(models.LiveRedPack)
redPack.LiveRedPackInfoId = info.Id
redPack.ActivityId = activityId
redPack.Receiver = 0
redPack.Amount = float64(v) / 100
redPack.CreatedAt = time.Now()
redPack.UpdatedAt = time.Now()
_, err = redPack.Add()
t.CheckErr(err)
}
info.Prompt = filter.Replace(info.Prompt)
t.JSON(info)
}
func (t *LiveCtl) QueryRedPack() {
}
// 领取红包
func (t *LiveCtl) GetRedPack() {
liveRedPackInfoId := t.MustGetInt64("live_red_pack_info_id")
userId := t.MustGetUID()
redPack := new(models.LiveRedPack)
exist, err := redPack.GetByInfoId(liveRedPackInfoId)
t.CheckErr(err)
if !exist {
// 通知其他的人
t.ERROR("红包被领完了", code.MSG_LIVE_RED_PACK_NOT_EXIST)
return
}
// 乐观锁 ==> 防止并发
row, err := redPack.Receive(userId)
t.CheckErr(err)
if row != 1 {
t.ERROR("红包被领完了", code.MSG_LIVE_RED_PACK_NOT_EXIST)
return
}
t.JSON(redPack)
}