-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathHomeController.java
More file actions
282 lines (241 loc) · 10.4 KB
/
HomeController.java
File metadata and controls
282 lines (241 loc) · 10.4 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package com.wip.controller;
import com.github.pagehelper.PageInfo;
import com.vdurmont.emoji.EmojiParser;
import com.wip.constant.ErrorConstant;
import com.wip.constant.Types;
import com.wip.constant.WebConst;
import com.wip.dto.MetaDto;
import com.wip.dto.StatisticsDto;
import com.wip.dto.cond.ContentCond;
import com.wip.dto.cond.MetaCond;
import com.wip.exception.BusinessException;
import com.wip.model.CommentDomain;
import com.wip.model.ContentDomain;
import com.wip.model.MetaDomain;
import com.wip.service.article.ContentService;
import com.wip.service.comment.CommentService;
import com.wip.service.meta.MetaService;
import com.wip.service.site.SiteService;
import com.wip.utils.APIResponse;
import com.wip.utils.IPKit;
import com.wip.utils.TaleUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.util.List;
@Api("博客前台页面")
@Controller
public class HomeController extends BaseController {
@Autowired
private ContentService contentService;
@Autowired
private CommentService commentService;
@Autowired
private MetaService metaService;
@GetMapping(value = "/")
public String index(
HttpServletRequest request,
@ApiParam(name = "page", value = "页数", required = false)
@RequestParam(name = "page", required = false, defaultValue = "1")
int page,
@ApiParam(name = "limit", value = "每页数量", required = false)
@RequestParam(name = "limit", required = false, defaultValue = "5")
int limit
) {
PageInfo<ContentDomain> articles = contentService.getArticlesByCond(new ContentCond(), page, limit);
request.setAttribute("articles",articles);
return "blog/home";
}
@ApiOperation("归档内容页")
@GetMapping(value = "/archives")
public String archives(
HttpServletRequest request,
@ApiParam(name = "page", value = "页数", required = false)
@RequestParam(name = "page", required = false, defaultValue = "1")
int page,
@ApiParam(name = "limit", value = "每页数量", required = false)
@RequestParam(name = "limit", required = false, defaultValue = "10")
int limit
) {
PageInfo<ContentDomain> articles = contentService.getArticlesByCond(new ContentCond(), page, limit);
request.setAttribute("articles", articles);
return "blog/archives";
}
@ApiOperation("分类内容页")
@GetMapping(value = "/categories")
public String categories(HttpServletRequest request) {
// 获取分类
List<MetaDto> categories = metaService.getMetaList(Types.CATEGORY.getType(),null,WebConst.MAX_POSTS);
// 分类总数
Long categoryCount = metaService.getMetasCountByType(Types.CATEGORY.getType());
request.setAttribute("categories", categories);
request.setAttribute("categoryCount", categoryCount);
return "blog/category";
}
@ApiOperation("分类详情页")
@GetMapping(value = "/categories/{name}")
public String categoriesDetail(
HttpServletRequest request,
@ApiParam(name = "name", value = "分类名称", required = true)
@PathVariable("name")
String name
) {
MetaDomain category = metaService.getMetaByName(Types.CATEGORY.getType(),name);
if (null == category.getName())
throw BusinessException.withErrorCode(ErrorConstant.Common.PARAM_IS_EMPTY);
List<ContentDomain> articles = contentService.getArticleByCategory(category.getName());
request.setAttribute("category", category.getName());
request.setAttribute("articles", articles);
return "blog/category_detail";
}
@ApiOperation("标签内容页")
@GetMapping(value = "/tags")
public String tags(HttpServletRequest request) {
// 获取标签
List<MetaDto> tags = metaService.getMetaList(Types.TAG.getType(), null, WebConst.MAX_POSTS);
// 标签总数
Long tagCount = metaService.getMetasCountByType(Types.TAG.getType());
request.setAttribute("tags", tags);
request.setAttribute("tagCount", tagCount);
return "blog/tags";
}
@ApiOperation("标签详情页")
@GetMapping(value = "/tags/{name}")
public String tagsDetail(
HttpServletRequest request,
@ApiParam(name = "name", value = "标签名", required = true)
@PathVariable("name")
String name
) {
MetaDomain tags = metaService.getMetaByName(Types.TAG.getType(),name);
List<ContentDomain> articles = contentService.getArticleByTags(tags);
request.setAttribute("articles",articles);
request.setAttribute("tags",tags.getName());
return "blog/tags_detail";
}
@GetMapping(value = "/about")
public String about() {
return "blog/about";
}
@ApiOperation("文章内容页")
@GetMapping(value = "/detail/{cid}")
public String detail(
@ApiParam(name = "cid", value = "文章主键", required = true)
@PathVariable("cid")
Integer cid,
HttpServletRequest request
) {
ContentDomain article = contentService.getArticleById(cid);
request.setAttribute("article", article);
// 更新文章的点击量
this.updateArticleHits(article.getCid(),article.getHits());
// 获取评论
List<CommentDomain> comments = commentService.getCommentsByCId(cid);
request.setAttribute("comments", comments);
return "blog/detail";
}
/**
* 更新文章的点击率
* @param cid
* @param chits
*/
private void updateArticleHits(Integer cid, Integer chits) {
Integer hits = cache.hget("article", "hits");
if (chits == null) {
chits = 0;
}
hits = null == hits ? 1 : hits + 1;
if (hits >= WebConst.HIT_EXEED) {
ContentDomain temp = new ContentDomain();
temp.setCid(cid);
temp.setHits(chits + hits);
contentService.updateContentByCid(temp);
cache.hset("article", "hits", 1);
} else {
cache.hset("article", "hits", hits);
}
}
@PostMapping(value = "/comment")
@ResponseBody
public APIResponse comment(HttpServletRequest request, HttpServletResponse response,
@RequestParam(name = "cid", required = true) Integer cid,
@RequestParam(name = "coid", required = false) Integer coid,
@RequestParam(name = "author", required = false) String author,
@RequestParam(name = "email", required = false) String email,
@RequestParam(name = "url", required = false) String url,
@RequestParam(name = "content", required = true) String content,
@RequestParam(name = "csrf_token", required = true) String csrf_token
) {
String ref = request.getHeader("Referer");
if (StringUtils.isBlank(ref) || StringUtils.isBlank(csrf_token)){
return APIResponse.fail("访问失败");
}
String token = cache.hget(Types.CSRF_TOKEN.getType(), csrf_token);
if (StringUtils.isBlank(token)) {
return APIResponse.fail("访问失败");
}
if (null == cid || StringUtils.isBlank(content)) {
return APIResponse.fail("请输入完整后评论");
}
if (StringUtils.isNotBlank(author) && author.length() > 50) {
return APIResponse.fail("姓名过长");
}
if (StringUtils.isNotBlank(email) && !TaleUtils.isEmail(email)) {
return APIResponse.fail("请输入正确的邮箱格式");
}
if (StringUtils.isNotBlank(url) && !TaleUtils.isURL(url)) {
return APIResponse.fail("请输入正确的网址格式");
}
if (content.length() < 5) {
return APIResponse.fail("请输入5个字符及以上的评价");
}
if (content.length() > 200) {
return APIResponse.fail("请输入200个字符以内的评价");
}
String val = IPKit.getIpAddressByRequest1(request) + ":" + cid;
Integer count = cache.hget(Types.COMMENTS_FREQUENCY.getType(), val);
if (null != count && count > 0) {
return APIResponse.fail("您发表的评论太快了,请过会再试");
}
author = TaleUtils.cleanXSS(author);
content = TaleUtils.cleanXSS(content);
author = EmojiParser.parseToAliases(author);
content = EmojiParser.parseToAliases(content);
CommentDomain comments = new CommentDomain();
comments.setAuthor(author);
comments.setCid(cid);
comments.setIp(request.getRemoteAddr());
comments.setUrl(url);
comments.setContent(content);
comments.setEmail(email);
comments.setParent(coid);
try {
commentService.addComment(comments);
cookie("tale_remember_author", URLEncoder.encode(author,"UTF-8"), 7 * 24 * 60 * 60, response);
cookie("tale_remember_mail", URLEncoder.encode(email,"UTF-8"), 7 * 24 * 60 * 60, response);
if (StringUtils.isNotBlank(url)) {
cookie("tale_remember_url",URLEncoder.encode(url,"UTF-8"),7 * 24 * 60 * 60, response);
}
// 设置对每个文章1分钟可以评论一次
cache.hset(Types.COMMENTS_FREQUENCY.getType(),val,1,60);
return APIResponse.success();
} catch (Exception e) {
throw BusinessException.withErrorCode(ErrorConstant.Comment.ADD_NEW_COMMENT_FAIL);
}
}
private void cookie(String name, String value, int maxAge, HttpServletResponse response) {
Cookie cookie = new Cookie(name,value);
cookie.setMaxAge(maxAge);
cookie.setSecure(false);
response.addCookie(cookie);
}
}