소스 검색

live broad add recommend

abiao 5 년 전
부모
커밋
dc31337505

+ 44 - 3
go/gopath/src/fohow.com/apps/controllers/live_controller/live_controller.go

@@ -5,7 +5,9 @@ import (
 	// "strings"
 
 	"fohow.com/apps/models/live_model"
+	"github.com/astaxie/beego"
 	"github.com/astaxie/beego/context"
+	"time"
 
 	// "api.com/apps/models/zt_model"
 
@@ -14,8 +16,8 @@ import (
 
 var (
 	//以下Action无需登录校验,exceptCheckUserLoginAction = []string{"*"} *代表全部不需要
-	exceptCheckUserLoginAction   = []string{"GetCurrentLiveShow"}
-	exceptCheckWxUserLoginAction = []string{"GetCurrentLiveShow"}
+	exceptCheckUserLoginAction   = []string{"*"}
+	exceptCheckWxUserLoginAction = []string{"*"}
 )
 
 type LiveController struct {
@@ -28,9 +30,48 @@ func (self *LiveController) Init(ctx *context.Context, controllerName, actionNam
 	self.ExceptCheckWxUserLoginAction = exceptCheckWxUserLoginAction
 }
 
-//根据广告位id,获取广告图
+//获取直播列表
 func (self *LiveController) GetCurrentLiveShow() {
 	live := live_model.GetCurrentShowLive(true)
 	self.Data["json"] = live
 	self.ServeJSON()
 }
+
+//获取推介直播列表
+func (self *LiveController) Latest() {
+	recommend, _ := self.GetInt64("rd", 0)
+	page, _ := self.GetInt64("page")
+	perPage, _ := self.GetInt64("per_page")
+	cache, _ := self.GetBool("cache", false)
+	ptype := self.GetString("ptype")
+
+	if page <= 0 {
+		page = 1
+	}
+	if perPage <= 0 || perPage > 100 {
+		perPage = 20
+	}
+
+	type Ret struct {
+		List      []*live_model.LiveBroad `json:"list"`
+		ListCount int64                   `json:"list_count"`
+	}
+
+	lives := live_model.GetLatest(page, perPage, recommend, cache)
+	count := live_model.GetLatestCount(recommend, ptype, cache)
+	timeNowUnix := time.Now().Unix() - int64(8*60*60)
+	for _, live := range lives {
+		beego.BeeLogger.Warn("live_timeNowUnix: %d", timeNowUnix)
+		beego.BeeLogger.Warn("live-BeginDate.Unix: %d", live.BeginDate.Unix())
+		if live.BeginDate.Unix() < timeNowUnix {
+			live.State = "尚未开播"
+		} else if live.BeginDate.Unix() >= timeNowUnix && live.EndDate.Unix() > timeNowUnix {
+			live.State = "正在直播"
+		} else {
+			live.State = "直播结束"
+		}
+	}
+
+	self.Data["json"] = &Ret{List: lives, ListCount: count}
+	self.ServeJSON()
+}

+ 17 - 1
go/gopath/src/fohow.com/apps/models/live_model/init.go

@@ -1,9 +1,25 @@
 package live_model
 
-import "github.com/astaxie/beego/orm"
+import (
+	"fmt"
+	"github.com/astaxie/beego"
+	"github.com/astaxie/beego/orm"
+	"strings"
+)
 
 func init() {
 	orm.RegisterModel(
 		new(LiveBroad),
 	)
 }
+
+func GetCdnFullImgUrl(img string) string {
+	if img == "" {
+		return ""
+	}
+	if strings.HasPrefix(img, "http://") || strings.HasPrefix(img, "https://") {
+		return img
+	} else {
+		return fmt.Sprintf("%s/%s", beego.AppConfig.String("AliCDNImgHost"), img)
+	}
+}

+ 37 - 1
go/gopath/src/fohow.com/apps/models/live_model/live_broad.go

@@ -21,7 +21,8 @@ type LiveBroad struct {
 	BeginDate time.Time `orm:"column(begin_time);null;type(datetime)"                json:"begin_time"` // datetime
 	EndDate   time.Time `orm:"column(end_time);null;type(datetime)"                  json:"end_time"`   // datetime
 	Show      bool      `orm:"column(show);null"                         json:"show"`                   // tinyint(1)
-	Remark    string    `orm:"column(remark)"                                         json:"cover"`
+	Remark    string    `orm:"column(remark)"                                         json:"remark"`
+	State     string    `orm:"-"                                       json:"state"`                             // varchar(255)
 	CreatedAt time.Time `orm:"column(created_at);null;auto_now_add;type(datetime)"  json:"created_at,omitempty"` // datetime
 	UpdatedAt time.Time `orm:"column(updated_at);null;auto_now;type(datetime)"      json:"updated_at,omitempty"` // datetime
 }
@@ -61,3 +62,38 @@ func GetCurrentShowLive(useCache bool) *LiveBroad {
 	}
 	return item
 }
+
+//获取最直播列表
+func GetLatest(page, perPage, recommend int64, useCache bool) (LiveBroads []*LiveBroad) {
+	k := fmt.Sprintf("LiveBroad_model.GetLatest.page(%d).perPage(%d).recommend(%d)", page, perPage, recommend)
+	if useCache {
+		if ret, ok := cache.Cache.Get(k).([]*LiveBroad); ok {
+			return ret
+		}
+	}
+	o := orm.NewOrm()
+	_, err := o.QueryTable(new(LiveBroad)).Filter("recommend__gt", recommend).OrderBy("-recommend", "-created_at").Limit(perPage, (page-1)*perPage).All(&LiveBroads)
+	if err != nil {
+		beego.BeeLogger.Debug("GetLatest err=%s", err)
+	}
+	for _, pd := range LiveBroads {
+		pd.Cover = GetCdnFullImgUrl(pd.Cover)
+	}
+	cache.Cache.Put(k, LiveBroads, 10*time.Minute)
+	return LiveBroads
+}
+
+func GetLatestCount(recommend int64, ptype string, useCache bool) int64 {
+	k := fmt.Sprintf("LiveBroad_model.GetLatestCount.recommend(%d)", recommend)
+	if useCache {
+		if ret, ok := cache.Cache.Get(k).(int64); ok {
+			return ret
+		}
+	}
+	item := new(LiveBroad)
+	o := orm.NewOrm()
+	count, _ := o.QueryTable(item).Filter("recommend__gt", recommend).Filter("show_flag", true).Filter("ptype", ptype).Filter("status", 1).Count()
+
+	cache.Cache.Put(k, count, 10*time.Minute)
+	return count
+}

+ 2 - 1
go/gopath/src/fohow.com/routers/routes.go

@@ -308,5 +308,6 @@ func init() {
 	//----------------直播管理-------------------
 	//获取当前直播内容
 	beego.Router("/v1/live_broad/current", &live_controller.LiveController{}, "get:GetCurrentLiveShow")
-
+	//获取直播列表
+	beego.Router("/v1/live_broad/list", &live_controller.LiveController{}, "get:Latest")
 }