Exemple #1
0
/* 分类显示页面 */
func (this *ArticleController) Category() {
	//是否用手机访问
	isMobile := IsMobile(this.Ctx.Input.UserAgent())
	//验证
	valid := validation.Validation{}
	valid.Alpha(this.Ctx.Input.Param(":category"), "category")
	if valid.HasErrors() { //没有通过验证则返回首页
		this.Ctx.Redirect(302, this.UrlFor("IndexController.Index"))
		this.StopRun()
	}

	//获得页面数
	page, _ := this.GetInt("page")

	if page < 1 {
		page = 1
	}

	category := &models.Category{}
	ok, parent, childs, articles, hots, allPage := category.FindByName(this.Ctx.Input.Param(":category"), (page-1)*15, page*15, 15)
	if !ok { //不存在此分类
		this.Ctx.Redirect(302, this.UrlFor("IndexController.Index"))
		this.StopRun()
	}
	//保存分类信息
	mainCategory := *category
	results := make([]ArticleReturn, len(articles))
	//查询每个文章的分类信息
	for k, _ := range articles {
		category.FindOne(articles[k].Category)
		results[k].Art = articles[k]
		//查找第一个图片
		results[k].Image = FindImages(results[k].Art.Content, 1)[0]
		//移除markdown标识
		results[k].Art.Content = RemoveMarkdown(results[k].Art.Content)
		//截取长度200
		results[k].Art.Content = beego.Substr(results[k].Art.Content, 0, 200)
		results[k].CategoryName = category.Title
		results[k].CategoryPath = category.Name
		results[k].Time = results[k].Art.Time.Format("2006-01-02 15:04:05")
	}
	//查询每个文章作者信息
	member := &models.Member{}
	for k, _ := range articles {
		member.FindOne(articles[k].Uid.Hex())
		results[k].AuthorName = member.Nickname
	}
	//赋值模版变量
	this.Data["category"] = mainCategory
	this.Data["parent"] = parent
	this.Data["childs"] = childs
	this.Data["articles"] = results
	this.Data["hots"] = hots
	this.Data["allpage"] = allPage
	this.Data["page"] = page //当前页数
	if isMobile {            //如果是手机访问
		this.TplNames = "mobile/article/category.html"
	} else {
		this.TplNames = "article/category.html"
	}
	this.Render()
}