-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.go
188 lines (176 loc) · 5.26 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package controllers
import (
"blog/common"
"blog/models"
"github.com/astaxie/beego"
"html/template"
"strconv"
)
type IndexController struct {
beego.Controller
}
/**
* 公共部分
*/
func public(this *IndexController, categoryId int){
//获取博客标题,声明,寄语
config := models.GetConfig("blog_title", "blog_avatar", "favicon_icon", "notice", "word", "keywords", "description", "website_title")
if value, ok := config.(map[string]string); ok {
this.Data["title"] = value["blog_title"]
this.Data["avatar"] = common.Asset(value["blog_avatar"])
this.Data["favicon_icon"] = common.Asset(value["favicon_icon"])
this.Data["notice"] = value["notice"]
this.Data["word"] = template.HTML(value["word"])
this.Data["keywords"] = value["keywords"]
this.Data["description"] = value["description"]
this.Data["website_title"] = value["website_title"]
}
this.Data["categorys"] = models.CategoryList(categoryId)
this.Data["top"] = models.ArticleTop()
this.Data["recommend"] = models.ArticleSadeBar("recommend")
this.Data["hot"] = models.ArticleSadeBar("hot")
}
/**
* 首页
*/
func (this *IndexController) Index () {
public(this,0)
//获取搜索关键字
wd := this.GetString("wd")
//获取轮播列表
this.Data["banners"] = models.BannerList()
//分页
page,_ := this.GetInt("page")
if page == 0{
page = 1
}
total := models.ArticleTotal(0,wd)
pagesize := 10
this.Data["paginator"] = common.Paginator(page,pagesize,total)
//获取文章列表
this.Data["list"] = models.ArticleListByTime(page, pagesize, wd)
this.Layout = "base/layout.tpl"
this.TplName = "index.tpl"
this.LayoutSections = make(map[string]string)
this.LayoutSections["SideBar"] = "base/sidebar.tpl"
this.LayoutSections["Script"] = "script/index_js.tpl"
}
/**
* 列表页
*/
func (this *IndexController) List() {
cid := this.Ctx.Input.Param(":id")//获取分类id
categoryId, _ := strconv.Atoi(cid)//转为int
category,err := models.CategoryOne(categoryId)//获取分类信息
//如果分类不存在->404
if err != nil {
this.Abort("404")
}
//加载公共数据
public(this, categoryId)
this.Data["category_id"] = categoryId
this.Data["category"] = category
this.Data["website_title"] = common.MergeString(category.Name," - ",this.Data["title"].(string))
//分页
page,_ := this.GetInt("page")
if page == 0{
page = 1
}
total := models.ArticleTotal(categoryId,"")
pagesize := 10
this.Data["paginator"] = common.Paginator(page,pagesize,total)
//分类下文章列表数据
this.Data["list"] = models.ArticleListByCategory(categoryId, page, pagesize)
//模板渲染
this.Layout = "base/layout.tpl"
this.TplName = "list.tpl"
this.LayoutSections = make(map[string]string)
this.LayoutSections["SideBar"] = "base/sidebar.tpl"
}
/**
* 文章详情
*/
func (this *IndexController) Article() {
articleId := this.Ctx.Input.Param(":id")//获取文章id
id, _ := strconv.Atoi(articleId)//转为int
article, err := models.ArticleOne(id)//获取文章数据
//阅读数+1
models.ArticleIncrViews(id)
//如果文章不存在跳到404页
if err != nil{
this.Abort("404")
}
//加载公共数据
public(this, 0)
//将标签添加到keywords
keywords := ""
for k,v := range article.TagSlice{
keywords = common.MergeString(keywords,v["name"])
if k != len(article.TagSlice) - 1{
keywords = common.MergeString(keywords,",")
}
}
this.Data["keywords"] = keywords
this.Data["description"] = article.Title
this.Data["website_title"] = article.Title
preArticle, err := models.ArticleOne(id - 1)
nextArticle, err := models.ArticleOne(id + 1)
this.Data["article"] = article//文章详情
this.Data["about"] = models.ArticleAbout(article.TagSlice)//相关文章
this.Data["pre_article"] = preArticle//上一篇
this.Data["next_article"] = nextArticle//下一篇
//渲染模板
this.Layout = "base/layout.tpl"
this.TplName = "article.tpl"
this.LayoutSections = make(map[string]string)
this.LayoutSections["SideBar"] = "base/sidebar_2.tpl"
this.LayoutSections["Script"] = "script/article_js.tpl"
}
func (this *IndexController) Tag() {
public(this,0)
//获取参数
name := this.GetString("name")
color := this.GetString("color")
this.Data["color"] = color
this.Data["name"] = name
//分页
page,_ := this.GetInt("page")
if page == 0{
page = 1
}
total := models.ArticleTotal(0,name)
pagesize := 10
this.Data["paginator"] = common.Paginator(page, pagesize, total)
//获取文章列表
this.Data["list"] = models.ArticleListByTime(page, pagesize, name)
//渲染页面
this.Layout = "base/layout.tpl"
this.TplName = "tag.tpl"
this.LayoutSections = make(map[string]string)
this.LayoutSections["SideBar"] = "base/sidebar.tpl"
}
/**
* 爬虫页
*/
func (this *IndexController) News() {
//加载公共数据
public(this, 0)
this.Data["category_name"] = "外部资讯"
this.Data["category_id"] = 999
//分页
page,_ := this.GetInt("page")
if page == 0{
page = 1
}
total := models.UrlTotal()
pagesize := 20
this.Data["paginator"] = common.Paginator(page,pagesize,total)
//分类下文章列表数据
this.Data["list"] = models.UrlList(page, pagesize)
//模板渲染
this.Layout = "base/layout.tpl"
this.TplName = "url.tpl"
this.LayoutSections = make(map[string]string)
this.LayoutSections["SideBar"] = "base/sidebar.tpl"
this.LayoutSections["Header"] = "header/url_header.tpl"
}