阳光沙滩博客系统-管理中心网站信息模块
# 获取网站title
网站的标题,就是tab标签栏上显示的标题
这个标题一般只显示在首页,如果是文章页面,则会显示:文章的名称+网站名称
前端获取接口
@PreAuthorize("@permission.admin()")
@GetMapping("/title")
public ResponseResult getWebSizeTitle() {
return iWebSizeInfoService.getWebSizeTitle();
}
1
2
3
4
5
2
3
4
5
实现接口
@Override
public ResponseResult getWebSizeTitle() {
Setting title = settingDao.findOneByKey(Constants.Settings.WEB_SIZE_TITLE);
return ResponseResult.SUCCESS("获取网站title成功.").setData(title);
}
1
2
3
4
5
2
3
4
5
这里我们没有判空,如果没有设置过网站相关的信息,则会为空,如果前端判断到空,可以跳转到设置的页面。
也就是接下来的修改网站标题页面。
# 修改网站标题
接口:
@PreAuthorize("@permission.admin()")
@PutMapping("/title")
public ResponseResult upWebSizeTitle(@RequestParam("title") String title) {
return iWebSizeInfoService.putWebSizeTitle(title);
}
1
2
3
4
5
2
3
4
5
实现:
没有什么难点,直接更新即可
@Override
public ResponseResult putWebSizeTitle(String title) {
if (TextUtils.isEmpty(title)) {
return ResponseResult.FAILED("网站标题不可以为空.");
}
Setting titleFromDb = settingDao.findOneByKey(Constants.Settings.WEB_SIZE_TITLE);
if (titleFromDb == null) {
titleFromDb = new Setting();
titleFromDb.setId(idWorker.nextId() + "");
titleFromDb.setUpdateTime(new Date());
titleFromDb.setCreateTime(new Date());
titleFromDb.setKey(Constants.Settings.WEB_SIZE_TITLE);
}
titleFromDb.setValue(title);
settingDao.save(titleFromDb);
return ResponseResult.SUCCESS("网站Title更新成功.");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 获取网站seo信息
网站SEO信息,其实包括:描述、关键字和标题
标题我们单独获取了,这里的话,我们就获取关键字和描述信息,主要是首页。
如果是文章页面的话,就使用文章的摘要作为描述,用标签作为关键字即可。
@PreAuthorize("@permission.admin()")
@GetMapping("/seo")
public ResponseResult getSeoInfo() {
return iWebSizeInfoService.getSeoInfo();
}
1
2
3
4
5
2
3
4
5
接口实现:
@Override
public ResponseResult getSeoInfo() {
Setting description = settingDao.findOneByKey(Constants.Settings.WEB_SIZE_DESCRIPTION);
Setting keyWords = settingDao.findOneByKey(Constants.Settings.WEB_SIZE_KEYWORDS);
Map<String, String> result = new HashMap<>();
if (description != null && keyWords != null) {
result.put(description.getKey(), description.getValue());
result.put(keyWords.getKey(), keyWords.getValue());
}
return ResponseResult.SUCCESS("获取SEO信息成功.").setData(result);
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 修改网站seo信息
更新网站的SEO信息,不建议经常更新哦,一般定了就别改了。
接口
@PreAuthorize("@permission.admin()")
@PutMapping("/seo")
public ResponseResult putSeoInfo(@RequestParam("keywords") String keywords,
@RequestParam("description") String description) {
return iWebSizeInfoService.putSeoInfo(keywords, description);
}
1
2
3
4
5
6
2
3
4
5
6
实现
@Override
public ResponseResult putSeoInfo(String keywords, String description) {
//判断
if (TextUtils.isEmpty(keywords)) {
return ResponseResult.FAILED("关键字不可以为空.");
}
if (TextUtils.isEmpty(description)) {
return ResponseResult.FAILED("网站描述不可以为空.");
}
Setting descriptionFromDb = settingDao.findOneByKey(Constants.Settings.WEB_SIZE_DESCRIPTION);
if (descriptionFromDb == null) {
descriptionFromDb = new Setting();
descriptionFromDb.setId(idWorker.nextId() + "");
descriptionFromDb.setCreateTime(new Date());
descriptionFromDb.setUpdateTime(new Date());
descriptionFromDb.setKey(Constants.Settings.WEB_SIZE_DESCRIPTION);
}
descriptionFromDb.setValue(description);
settingDao.save(descriptionFromDb);
Setting keyWordsFromDb = settingDao.findOneByKey(Constants.Settings.WEB_SIZE_KEYWORDS);
if (keyWordsFromDb == null) {
keyWordsFromDb = new Setting();
keyWordsFromDb.setId(idWorker.nextId() + "");
keyWordsFromDb.setCreateTime(new Date());
keyWordsFromDb.setUpdateTime(new Date());
keyWordsFromDb.setKey(Constants.Settings.WEB_SIZE_KEYWORDS);
}
keyWordsFromDb.setValue(keywords);
settingDao.save(keyWordsFromDb);
return ResponseResult.SUCCESS("更新SEO信息成功.");
}
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
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
# 获取网站的浏览量
网站的浏览量,
更新时机:用户页面加载完以后,向统计接口提交一下。
获取时机:管理中心获取访问量/前端如果有需要的话也可以获取
接口:
@PreAuthorize("@permission.admin()")
@GetMapping("/view_count")
public ResponseResult getWebSizeViewCount() {
return iWebSizeInfoService.getSizeViewCount();
}
1
2
3
4
5
2
3
4
5
实现
/**
* 这个是全网站的访问量,要做得细一点,还得分来源
* 这里只统计浏览量,只统计文章的浏览量,提供一个浏览量的统计接口(页面级的)
*
* @return 浏览量
*/
@Override
public ResponseResult getSizeViewCount() {
//先从redis里拿出来
String viewCountStr = (String) redisUtils.get(Constants.Settings.WEB_SIZE_VIEW_COUNT);
Setting viewCount = settingDao.findOneByKey(Constants.Settings.WEB_SIZE_VIEW_COUNT);
if (viewCount == null) {
viewCount = this.initViewItem();
settingDao.save(viewCount);
}
if (TextUtils.isEmpty(viewCountStr)) {
viewCountStr = viewCount.getValue();
redisUtils.set(Constants.Settings.WEB_SIZE_VIEW_COUNT, viewCountStr);
} else {
//把redis里的更新到数据里
viewCount.setValue(viewCountStr);
settingDao.save(viewCount);
}
Map<String, Integer> result = new HashMap<>();
result.put(viewCount.getKey(), Integer.valueOf(viewCount.getValue()));
return ResponseResult.SUCCESS("获取网站浏览量成功.").setData(result);
}
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
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
编辑 (opens new window)