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.
256 lines
7.1 KiB
256 lines
7.1 KiB
package client
|
|
|
|
import (
|
|
"fmt"
|
|
"hudongzhuanjia/controllers"
|
|
"hudongzhuanjia/libs/filter"
|
|
"hudongzhuanjia/libs/im"
|
|
"hudongzhuanjia/models"
|
|
pay_service "hudongzhuanjia/services/pay"
|
|
red_envelope_service "hudongzhuanjia/services/red_envelope"
|
|
"hudongzhuanjia/utils"
|
|
"hudongzhuanjia/utils/code"
|
|
"hudongzhuanjia/utils/define"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/ouxuanserver/osmanthuswine/src/core"
|
|
)
|
|
|
|
// 轮询接口
|
|
func init() {
|
|
go utils.HandleTicker(10*time.Second, QueryOnline)
|
|
}
|
|
|
|
func QueryOnline() error {
|
|
lives, err := models.GetLiveConfigByStatus(2, 2) // 进行中的直播, 统计峰值
|
|
if err != nil {
|
|
return err
|
|
}
|
|
liveIds := make([]int64, 0)
|
|
for _, live := range lives { // 查询在线人数
|
|
liveIds = append(liveIds, live.Id)
|
|
}
|
|
viewers, err := models.GetLiveViewerByLiveConfigIds(liveIds)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var idm = make(map[int64][]string, 0)
|
|
for _, live := range lives {
|
|
for _, viewer := range viewers {
|
|
if live.Id == viewer.LiveConfigId {
|
|
if _, ok := idm[live.Id]; !ok {
|
|
idm[live.Id] = make([]string, 0)
|
|
}
|
|
idm[live.Id] = append(idm[live.Id], fmt.Sprint(viewer.UserId))
|
|
}
|
|
}
|
|
}
|
|
var wg sync.WaitGroup
|
|
wg.Add(len(idm))
|
|
var errs []error
|
|
for lid, ids := range idm {
|
|
go func(lid int64, ids []string) {
|
|
defer func() { recover() }()
|
|
defer wg.Done()
|
|
res, err := im.QueryState(ids)
|
|
if err != nil {
|
|
errs = append(errs, err)
|
|
}
|
|
var count int
|
|
for _, item := range res.QueryResult {
|
|
if item.Status != "Offline" {
|
|
count++
|
|
}
|
|
}
|
|
_, err = models.Update(lid, &models.LiveConfig{WatchNum: count}, "watch_num")
|
|
if err != nil {
|
|
errs = append(errs, err)
|
|
}
|
|
}(lid, ids)
|
|
}
|
|
wg.Wait()
|
|
return nil
|
|
}
|
|
|
|
type LiveCtl struct {
|
|
controllers.AuthorCtl
|
|
}
|
|
|
|
// 详情
|
|
func (t *LiveCtl) Detail() {
|
|
activityId := t.MustGetInt64("activity_id")
|
|
areaId := t.MustGetInt64("area_id")
|
|
|
|
live := new(models.LiveConfig)
|
|
exist, err := live.GetByActivityId(activityId)
|
|
t.CheckErr(err)
|
|
t.Assert(exist, code.MSG_LIVE_CONFIG_NOT_EXIST, "直播活动不存在")
|
|
|
|
fs := make(map[string]int, 0)
|
|
fs["is_shake"] = 0
|
|
fs["is_reward"] = 0
|
|
fs["is_order"] = 0
|
|
fs["is_lottery"] = 0
|
|
if live.AdaptationFunc != nil && len(live.AdaptationFunc) != 0 {
|
|
modules := make([]*models.ModuleServiceHistory, 0)
|
|
err = core.GetXormAuto().Where("is_delete=0").In("id", live.AdaptationFunc).Find(&modules)
|
|
t.CheckErr(err)
|
|
for _, module := range modules {
|
|
if module.Name == define.MODULE_ORDER {
|
|
fs["is_order"] = 1
|
|
} else if module.Name == define.MODULE_SHAKRB {
|
|
fs["is_shake"] = 1
|
|
} else if module.Name == define.MODULE_REWARD {
|
|
fs["is_reward"] = 1
|
|
} else if module.Name == define.MODULE_LOTTERY {
|
|
fs["is_lottery"] = 1
|
|
}
|
|
}
|
|
}
|
|
|
|
config := new(models.LiveConfigArea)
|
|
exist, err = config.GetByActivityIdAndAreaId(activityId, areaId)
|
|
t.CheckErr(err)
|
|
if exist {
|
|
live.SharePosterImg = config.MergeSharePoster
|
|
}
|
|
|
|
live.AdminLiveUrl = ""
|
|
live.AreaId = areaId
|
|
live.Adaptation = fs
|
|
t.JSON(live)
|
|
}
|
|
|
|
func (t *LiveCtl) Like() {
|
|
activityId := t.MustGetInt64("activity_id")
|
|
_, 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") // 红包数量
|
|
prompt := t.MustGet("prompt") // 提示
|
|
amount := utils.Float64CusDecimal(t.MustGetDouble("amount"), 2) // 金额
|
|
areaId := t.MustGetInt64("area_id")
|
|
|
|
if amount/float64(num) < 1 && amount/float64(num) > 200 { // 平均每个红包不得小于0.3
|
|
t.ERROR("每个红包的平均金额必须在1.00元到200.00元之间", code.MSG_ERR)
|
|
return
|
|
}
|
|
|
|
activity := new(models.Activity)
|
|
exist, err := models.Get(activity, activityId)
|
|
t.CheckErr(err)
|
|
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
|
|
|
|
user := models.User{}
|
|
exist, err = models.Get(&user, userId)
|
|
t.CheckErr(err)
|
|
t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
|
|
|
|
res, err := pay_service.UnifiedOrder("欧轩互动-直播红包", user.Openid, int64(amount*100),
|
|
3, userId, activityId, time.Now().Add(1*time.Hour).Unix())
|
|
t.CheckErr(err)
|
|
|
|
rule := models.LiveRedEnvelopeRule{
|
|
UserId: 0,
|
|
ActivityId: activityId,
|
|
AreaId: areaId,
|
|
OutTradeNo: res["out_trade_no"].(string),
|
|
RehearsalId: activity.RehearsalId,
|
|
Prompt: filter.Replace(prompt),
|
|
Amount: amount,
|
|
Num: num,
|
|
Status: 0,
|
|
}
|
|
_, err = models.Add(&rule)
|
|
t.CheckErr(err)
|
|
|
|
records := red_envelope_service.GenRedPack(int(amount*100), num)
|
|
for _, v := range records {
|
|
record := models.ShakeRedEnvelopeRecord{
|
|
ActivityId: activityId,
|
|
RehearsalId: activity.RehearsalId,
|
|
ShakeRedEnvelopeType: 1,
|
|
ShakeRedEnvelopeRuleId: rule.Id,
|
|
AreaId: areaId,
|
|
Name: user.Nickname + "发红包",
|
|
UserId: user.Id,
|
|
Amount: utils.Float64CusDecimal(float64(v)/float64(100), 2),
|
|
IsDraw: -1,
|
|
}
|
|
_, err = models.Add(&record)
|
|
t.CheckErr(err)
|
|
}
|
|
|
|
res["rehearsal_id"] = activity.RehearsalId
|
|
res["live_red_envelope_rule_id"] = rule.Id
|
|
t.JSON(res)
|
|
}
|
|
|
|
// 领取红包
|
|
func (t *LiveCtl) GetLiveRedPack() {
|
|
ruleId := t.MustGetInt64("live_red_envelope_rule_id")
|
|
userId := t.MustGetUID()
|
|
|
|
rule := new(models.LiveRedEnvelopeRule)
|
|
exist, err := models.Get(rule, ruleId)
|
|
t.CheckErr(err)
|
|
t.Assert(exist, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包规则不存在")
|
|
t.Assert(rule.Status == 1, code.MSG_SHAKERB_RULE_NOT_EXIST, "红包规则尚未生效")
|
|
|
|
user := models.User{}
|
|
exist, err = models.Get(&user, userId)
|
|
t.CheckErr(err)
|
|
t.Assert(exist, code.MSG_USER_NOT_EXIST, "不存在用户")
|
|
|
|
record := new(models.ShakeRedEnvelopeRecord)
|
|
exist, err = record.GetByRuleId(ruleId, rule.RehearsalId, 1)
|
|
t.CheckErr(err)
|
|
t.Assert(exist, code.MSG_SHAKERB_RECORD_NOT_HIT, "红包领完了")
|
|
|
|
// 乐观锁 ==> 防止并发
|
|
record.UserId = user.Id
|
|
record.IsDraw = 0
|
|
row, err := models.Update(record.Id, record, "user_id", "is_draw")
|
|
t.CheckErr(err)
|
|
t.Assert(row == 1, code.MSG_SHAKERB_RECORD_NOT_HIT, "红包被领完了")
|
|
|
|
result, err := pay_service.SendRedPack("欧轩互动", user.Openid, rule.Prompt, "直播抢红包活动",
|
|
"抢的多,赚得多", int(record.Amount*100), 1, 2)
|
|
t.CheckErr(err)
|
|
record.MchBillno = result.MchBillno
|
|
record.IsDraw = 1
|
|
models.Update(record.Id, record, "mch_billno", "is_draw")
|
|
t.JSON(record)
|
|
}
|
|
|
|
// 订单
|