黄梓健 5 years ago
parent
commit
724cc0a6c8
  1. 76
      controllers/client/live.go
  2. 42
      libs/im/im.go
  3. 8
      models/live_config.go
  4. 22
      models/live_viewer.go
  5. 1
      utils/code/code.go

76
controllers/client/live.go

@ -1,24 +1,76 @@
package client package client
import ( import (
"fmt"
"github.com/ouxuanserver/osmanthuswine/src/core" "github.com/ouxuanserver/osmanthuswine/src/core"
"hudongzhuanjia/controllers" "hudongzhuanjia/controllers"
"hudongzhuanjia/libs/filter" "hudongzhuanjia/libs/filter"
"hudongzhuanjia/libs/im"
"hudongzhuanjia/models" "hudongzhuanjia/models"
pay_service "hudongzhuanjia/services/pay" pay_service "hudongzhuanjia/services/pay"
red_envelope_service "hudongzhuanjia/services/red_envelope" red_envelope_service "hudongzhuanjia/services/red_envelope"
"hudongzhuanjia/utils" "hudongzhuanjia/utils"
"hudongzhuanjia/utils/code" "hudongzhuanjia/utils/code"
"hudongzhuanjia/utils/define" "hudongzhuanjia/utils/define"
"sync"
"time" "time"
) )
type NoticeRedPackEvent struct {
OutTradeNo string `json:"out_trade_no"`
Prompt string `json:"prompt"`
RedPackInfoId int64 `json:"red_pack_info_id"`
ActivityId int64 `json:"activity_id"`
Status int `json:"status"`
// 轮询接口
func init() {
go utils.HandleTicker(10*time.Second, QueryOnline)
}
func QueryOnline() error {
lives, err := models.GetLiveConfigByStatus(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(int64, []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 { type LiveCtl struct {
@ -31,13 +83,17 @@ func (t *LiveCtl) Detail() {
areaId := t.MustGetInt64("area_id") areaId := t.MustGetInt64("area_id")
userId := t.MustGetUID() userId := t.MustGetUID()
err := new(models.LiveViewer).Record(userId, activityId)
t.CheckErr(err)
live := new(models.LiveConfig) live := new(models.LiveConfig)
exist, err := live.GetByActivityId(activityId) exist, err := live.GetByActivityId(activityId)
t.CheckErr(err) t.CheckErr(err)
t.Assert(exist, code.MSG_LIVE_CONFIG_NOT_EXIST, "直播活动不存在") t.Assert(exist, code.MSG_LIVE_CONFIG_NOT_EXIST, "直播活动不存在")
if live.WatchNum+1 >= live.Limit {
t.ERROR("直播人数已满", code.MSG_LIVE_LIMIT_ENOUGH)
return
}
err = new(models.LiveViewer).Record(userId, activityId, live.Id)
t.CheckErr(err)
fs := make(map[string]int, 0) fs := make(map[string]int, 0)
fs["is_shake"] = 0 fs["is_shake"] = 0
@ -203,3 +259,5 @@ func (t *LiveCtl) GetLiveRedPack() {
models.Update(record.Id, record, "mch_billno", "is_draw") models.Update(record.Id, record, "mch_billno", "is_draw")
t.JSON(record) t.JSON(record)
} }
// 订单

42
libs/im/im.go

@ -137,3 +137,45 @@ func SendGroupMessage(param *SendGroupMessageParam) error {
} }
return nil return nil
} }
type QueryStateResult struct {
ActionStatus string `json:"ActionStatus"`
ErrorInfo string `json:"ErrorInfo"`
ErrorCode int `json:"ErrorCode"`
QueryResult []*QueryStateResultItem `json:"QueryResult"`
}
type QueryStateResultItem struct {
ToAccount string `json:"ToAccount"`
Status string `json:"Status"`
}
// https://console.tim.qq.com/v4/openim/querystate?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
func QueryState(ids []string) (*QueryStateResult, error) {
sig, err := GenSig("admin")
if err != nil {
return nil, err
}
random := utils.RandomInt(16)
u := fmt.Sprintf("%s/openim/querystate?sdkappid=%d&identifier=admin&usersig=%s&random=%v&contenttype=json",
TxImHostV4, SdkAppid, sig, random)
bs, err := json.Marshal(map[string]interface{}{
"To_Account": ids,
})
if err != nil {
return nil, err
}
resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
if err != nil {
return nil, err
}
var res = QueryStateResult{}
err = resp.Json(&res)
if err != nil {
return nil, err
}
if res.ErrorCode != 0 {
return nil, errors.New(res.ErrorInfo)
}
return &res, nil
}

8
models/live_config.go

@ -13,6 +13,7 @@ type LiveConfig struct {
CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"` CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"`
UpdatedAt time.Time `json:"updated_at" xorm:"not null updated default CURRENT_TIMESTAMP comment('更新时间') TIMESTAMP"` UpdatedAt time.Time `json:"updated_at" xorm:"not null updated default CURRENT_TIMESTAMP comment('更新时间') TIMESTAMP"`
CusId int `json:"cus_id" xorm:"not null default 0 comment('客户id') INT(11)"`
LiveSwitch int `json:"live_switch" xorm:"not null default 0 comment('直播开关0关1开') INT(11)"` LiveSwitch int `json:"live_switch" xorm:"not null default 0 comment('直播开关0关1开') INT(11)"`
ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('互动id') INT(11)"` ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('互动id') INT(11)"`
AreaId int64 `json:"area_id" xorm:"not null default 0 comment('地域') INT(11)"` AreaId int64 `json:"area_id" xorm:"not null default 0 comment('地域') INT(11)"`
@ -39,6 +40,7 @@ type LiveConfig struct {
Announcement string `json:"announcement" xorm:"not null default '' comment('公告内容') VARCHAR(255)"` Announcement string `json:"announcement" xorm:"not null default '' comment('公告内容') VARCHAR(255)"`
WatchNum int `json:"watch_num" xorm:"not null default 0 comment('观看人数') INT(11)"` WatchNum int `json:"watch_num" xorm:"not null default 0 comment('观看人数') INT(11)"`
LikeNum int `json:"like_num" xorm:"not null default 0 comment('点赞数') INT(11)"` LikeNum int `json:"like_num" xorm:"not null default 0 comment('点赞数') INT(11)"`
Limit int `json:"limit" xorm:"not null default 0 comment('限制人数') INT(11)"`
ImGroupId string `json:"im_group_id" xorm:"not null default '' comment('腾讯im聊天群id') VARCHAR(255)"` ImGroupId string `json:"im_group_id" xorm:"not null default '' comment('腾讯im聊天群id') VARCHAR(255)"`
ImGroupName string `json:"im_group_name" xorm:"not null default '' comment('聊天群名称') VARCHAR(255)"` ImGroupName string `json:"im_group_name" xorm:"not null default '' comment('聊天群名称') VARCHAR(255)"`
// live adaptation // live adaptation
@ -61,3 +63,9 @@ func (t *LiveConfig) HaveModule(module string) bool {
exist, _ := core.GetXormAuto().Where("is_delete=0 and name=?", module).In("id", t.AdaptationFunc).Exist(&ModuleService{}) exist, _ := core.GetXormAuto().Where("is_delete=0 and name=?", module).In("id", t.AdaptationFunc).Exist(&ModuleService{})
return exist return exist
} }
func GetLiveConfigByStatus(status interface{}) ([]*LiveConfig, error) {
lives := make([]*LiveConfig, 0)
err := core.GetXormAuto().Where("is_delete=0 and status=?", status).Find(&lives)
return lives, err
}

22
models/live_viewer.go

@ -8,19 +8,20 @@ import (
const LiveViewerTN = TableNamePrefix + "live_viewer" const LiveViewerTN = TableNamePrefix + "live_viewer"
type LiveViewer struct { type LiveViewer struct {
Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"`
IsDelete bool `json:"-" xorm:"not null default 0 comment('是否删除') TINYINT(1)"`
CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"`
UpdatedAt time.Time `json:"updated_at" xorm:"not null updated default CURRENT_TIMESTAMP comment('更新时间') TIMESTAMP"`
UserId int64 `json:"user_id" xorm:"not null default 0 comment('用户id') INT(11)"`
ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('活动id') INT(11)"`
Id int64 `json:"id" xorm:"not null pk autoincr INT(11)"`
IsDelete bool `json:"-" xorm:"not null default 0 comment('是否删除') TINYINT(1)"`
CreatedAt time.Time `json:"created_at" xorm:"not null created comment('创建时间') DATETIME"`
UpdatedAt time.Time `json:"updated_at" xorm:"not null updated default CURRENT_TIMESTAMP comment('更新时间') TIMESTAMP"`
UserId int64 `json:"user_id" xorm:"not null default 0 comment('用户id') INT(11)"`
ActivityId int64 `json:"activity_id" xorm:"not null default 0 comment('活动id') INT(11)"`
LiveConfigId int64 `json:"live_config_id" xorm:"not null default 0 comment('直播id') INT(11)"`
} }
func (t *LiveViewer) TableName() string { func (t *LiveViewer) TableName() string {
return LiveViewerTN return LiveViewerTN
} }
func (t *LiveViewer) Record(uid, aid int64) error {
func (t *LiveViewer) Record(uid, aid, lid int64) error {
session := core.GetXormAuto().NewSession() session := core.GetXormAuto().NewSession()
defer session.Close() defer session.Close()
session.Begin() session.Begin()
@ -35,6 +36,7 @@ func (t *LiveViewer) Record(uid, aid int64) error {
} }
t.UserId = uid t.UserId = uid
t.ActivityId = aid t.ActivityId = aid
t.LiveConfigId = lid
_, err = session.InsertOne(t) _, err = session.InsertOne(t)
if err != nil { if err != nil {
session.Rollback() session.Rollback()
@ -48,3 +50,9 @@ func (t *LiveViewer) Record(uid, aid int64) error {
session.Commit() session.Commit()
return nil return nil
} }
func GetLiveViewerByLiveConfigIds(ids interface{}) ([]*LiveViewer, error) {
viewers := make([]*LiveViewer, 0)
err := core.GetXormAuto().Where("is_delete=0").In("live_config_id", ids).Find(&viewers)
return viewers, err
}

1
utils/code/code.go

@ -12,6 +12,7 @@ const (
MSG_CUSTOMER_NOT_EXIST = 700 MSG_CUSTOMER_NOT_EXIST = 700
MSG_AREASTORE_NOT_EXIST = 800 MSG_AREASTORE_NOT_EXIST = 800
MSG_ENTRYPEOPLE_NOT_EXIST = 900 MSG_ENTRYPEOPLE_NOT_EXIST = 900
MSG_LIVE_LIMIT_ENOUGH = 998 // 直播间人数已满
MSG_LIVE_CONFIG_NOT_EXIST = 999 // 直播不存在 MSG_LIVE_CONFIG_NOT_EXIST = 999 // 直播不存在
MSG_ACTIVITY_NOT_EXIST = 1000 // 互动不存在 MSG_ACTIVITY_NOT_EXIST = 1000 // 互动不存在
MSG_MODULE_NOT_EXIST = 1001 // 模块活动不存在 MSG_MODULE_NOT_EXIST = 1001 // 模块活动不存在

Loading…
Cancel
Save