From 724cc0a6c8aa29a72623269f61240ea930d41edd Mon Sep 17 00:00:00 2001 From: tommy <3405129587@qq.com> Date: Tue, 9 Jun 2020 17:57:47 +0800 Subject: [PATCH] fix --- controllers/client/live.go | 76 ++++++++++++++++++++++++++++++++++++++++------ libs/im/im.go | 42 +++++++++++++++++++++++++ models/live_config.go | 8 +++++ models/live_viewer.go | 22 +++++++++----- utils/code/code.go | 1 + 5 files changed, 133 insertions(+), 16 deletions(-) diff --git a/controllers/client/live.go b/controllers/client/live.go index e5d620b..05b2984 100644 --- a/controllers/client/live.go +++ b/controllers/client/live.go @@ -1,24 +1,76 @@ package client import ( + "fmt" "github.com/ouxuanserver/osmanthuswine/src/core" "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" ) -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 { @@ -31,13 +83,17 @@ func (t *LiveCtl) Detail() { areaId := t.MustGetInt64("area_id") 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_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["is_shake"] = 0 @@ -203,3 +259,5 @@ func (t *LiveCtl) GetLiveRedPack() { models.Update(record.Id, record, "mch_billno", "is_draw") t.JSON(record) } + +// 订单 diff --git a/libs/im/im.go b/libs/im/im.go index 7a9383a..d7dca2d 100644 --- a/libs/im/im.go +++ b/libs/im/im.go @@ -137,3 +137,45 @@ func SendGroupMessage(param *SendGroupMessageParam) error { } 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 +} diff --git a/models/live_config.go b/models/live_config.go index e26ce99..01e5fb8 100644 --- a/models/live_config.go +++ b/models/live_config.go @@ -13,6 +13,7 @@ type LiveConfig struct { 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"` + 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)"` 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)"` @@ -39,6 +40,7 @@ type LiveConfig struct { Announcement string `json:"announcement" xorm:"not null default '' comment('公告内容') VARCHAR(255)"` 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)"` + 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)"` ImGroupName string `json:"im_group_name" xorm:"not null default '' comment('聊天群名称') VARCHAR(255)"` // 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{}) 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 +} diff --git a/models/live_viewer.go b/models/live_viewer.go index 2d3a79c..2ee3f6a 100644 --- a/models/live_viewer.go +++ b/models/live_viewer.go @@ -8,19 +8,20 @@ import ( const LiveViewerTN = TableNamePrefix + "live_viewer" 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 { return LiveViewerTN } -func (t *LiveViewer) Record(uid, aid int64) error { +func (t *LiveViewer) Record(uid, aid, lid int64) error { session := core.GetXormAuto().NewSession() defer session.Close() session.Begin() @@ -35,6 +36,7 @@ func (t *LiveViewer) Record(uid, aid int64) error { } t.UserId = uid t.ActivityId = aid + t.LiveConfigId = lid _, err = session.InsertOne(t) if err != nil { session.Rollback() @@ -48,3 +50,9 @@ func (t *LiveViewer) Record(uid, aid int64) error { session.Commit() 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 +} diff --git a/utils/code/code.go b/utils/code/code.go index 1b84e3a..e417a5d 100644 --- a/utils/code/code.go +++ b/utils/code/code.go @@ -12,6 +12,7 @@ const ( MSG_CUSTOMER_NOT_EXIST = 700 MSG_AREASTORE_NOT_EXIST = 800 MSG_ENTRYPEOPLE_NOT_EXIST = 900 + MSG_LIVE_LIMIT_ENOUGH = 998 // 直播间人数已满 MSG_LIVE_CONFIG_NOT_EXIST = 999 // 直播不存在 MSG_ACTIVITY_NOT_EXIST = 1000 // 互动不存在 MSG_MODULE_NOT_EXIST = 1001 // 模块活动不存在