目录
1. 基于Springboot的个人博客系统的设计与实现
1.1. 第6章 系统详细设计与实现
6.3.4 评论管理
6.3.5 分类/标签管理
6.3.6 文件管理
6.3.7 友链管理
6.3.8 系统设置
- 基于Springboot的个人博客系统的设计与实现
- 第6章 系统详细设计与实现
点击左侧功能菜单可以进入到评论管理界面,管理员可以查看所有评论列表,并可以根据评论内容等信息进行评论审核及删除评论,如图6.8所示。
图6.8 评论管理效果图
该功能主要代码实现如下:
//评论管理列表
@GetMapping(value = "")
public String index(@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "limit", defaultValue = "15") int limit, HttpServletRequest request) {
UserVo users = this.user(request);
CommentVoExample commentVoExample = new CommentVoExample();
commentVoExample.setOrderByClause("coid desc");
commentVoExample.createCriteria().andAuthorIdNotEqualTo(users.getUid());
PageInfo<CommentVo> commentsPaginator = commentsService.getCommentsWithPage(commentVoExample,page, limit);
request.setAttribute("comments", commentsPaginator);
return "admin/comment_list";
}
点击左侧功能菜单可以进入到分类/标签管理界面,管理员查看所有标签和分类信息,可以添加分类或者标签,点击某个分类/标签还可以进行修改和删除操作,如图6.9所示。
图6.9 分类/标签管理效果图
该功能主要代码实现如下:
//分类/标签管理列表
@GetMapping(value = "")
public String index(HttpServletRequest request) {
List<MetaDto>categories= metasService.getMetaList(Types.CATEGORY.getType(),null);
List<MetaDto> tags = metasService.getMetaList(Types.TAG.getType(), null, WebConst.MAX_POSTS);
request.setAttribute("categories", categories);
request.setAttribute("tags", tags);
return "admin/category";
}
//保存分类/标签
@PostMapping(value = "save")
@ResponseBody
@Transactional(rollbackFor = TipException.class)
public RestResponseBo saveCategory(@RequestParam String cname, @RequestParam Integer mid) {
try {
metasService.saveMeta(Types.CATEGORY.getType(),cname,mid);
} catch (Exception e) {
String msg = "分类保存失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
LOGGER.error(msg, e);
}
return RestResponseBo.fail(msg);
}
return RestResponseBo.ok();
}
点击左侧功能菜单可以进入到文件管理界面,在文件管理中可以上传编辑文章或者页面所需要的文件,支持多种文件类型,只需要将需要上传的文件拖拽到上传区域即可,如图6.10所示。
图6.10 文件管理效果图
该功能主要代码实现如下:
//上传文件
@PostMapping(value = "upload")
@ResponseBody
@Transactional(rollbackFor = TipException.class)
public RestResponseBo upload(HttpServletRequest request, @RequestParam("file") MultipartFile[] multipartFiles) throws IOException {
UserVo users = this.user(request);
Integer uid = users.getUid();
List<String> errorFiles = new ArrayList<>();
try {
for (MultipartFile multipartFile : multipartFiles) {
String fname = multipartFile.getOriginalFilename();
if (multipartFile.getSize() <= WebConst.MAX_FILE_SIZE) {
String fkey = TaleUtils.getFileKey(fname);
String ftype = TaleUtils.isImage(multipartFile.getInputStream()) ? Types.IMAGE.getType() : Types.FILE.getType();
File file = new File(CLASSPATH+fkey);
try {
FileCopyUtils.copy(multipartFile.getInputStream(),new FileOutputStream(file));
} catch (IOException e) {
e.printStackTrace();
}
attachService.save(fname, fkey, ftype, uid);
} else {
errorFiles.add(fname);
}
}
} catch (Exception e) {
return RestResponseBo.fail();
}
return RestResponseBo.ok(errorFiles);
}
//删除文件
@RequestMapping(value = "delete")
@ResponseBody
@Transactional(rollbackFor = TipException.class)
public RestResponseBo delete(@RequestParam Integer id, HttpServletRequest request) {
try {
AttachVo attach = attachService.selectById(id);
if (null == attach) return RestResponseBo.fail("不存在该附件");
attachService.deleteById(id);
new File(CLASSPATH+attach.getFkey()).delete();
logService.insertLog(LogActions.DEL_ARTICLE.getAction(), attach.getFkey(), request.getRemoteAddr(), this.getUid(request));
} catch (Exception e) {
String msg = "附件删除失败";
if (e instanceof TipException) msg = e.getMessage();
else LOGGER.error(msg, e);
return RestResponseBo.fail(msg);
}
return RestResponseBo.ok();
}
点击左侧功能菜单可以进入到友链管理界面,在管理管理中可以添加友情链接,包括链接名称、链接地址、链接logo、排序等,并可以编辑或删除等操作如图6.11所示。
图6.11 友链管理效果图
该功能主要代码实现如下:
//友链管理列表
@GetMapping(value = "")
public String index(HttpServletRequest request) {
List<MetaVo> metas = metasService.getMetas(Types.LINK.getType());
request.setAttribute("links", metas);
return "admin/links";
}
//保存友链
@PostMapping(value = "save")
@ResponseBody
@Transactional(rollbackFor = TipException.class)
public RestResponseBo saveLink(@RequestParam String title, @RequestParam String url, @RequestParam String logo, @RequestParam Integer mid,
@RequestParam(value = "sort", defaultValue = "0") int sort) {
try {
MetaVo metas = new MetaVo();
metas.setName(title);
metas.setSlug(url);
metas.setDescription(logo);
metas.setSort(sort);
metas.setType(Types.LINK.getType());
if (null != mid) {
metas.setMid(mid);
metasService.update(metas);
} else {
metasService.saveMeta(metas);
}
} catch (Exception e) {
return RestResponseBo.ok();
}
点击左侧功能菜单可以进入到系统设置界面,在系统设置站点的名称、明书、关键词、主题等,同时也可以系统备份,包括备份文件和导出数据库sql,如图6.12所示。
图6.12 系统设置效果图
该功能主要代码实现如下:
//全局设置
@GetMapping(value = "")
public String setting(HttpServletRequest request) {
List<OptionVo> voList = optionService.getOptions();
Map<String, String> options = new HashMap<>();
voList.forEach((option) -> {
options.put(option.getName(), option.getValue());
});
request.setAttribute("options", options);
return "admin/setting";
}
//保存设置
@PostMapping(value = "")
@ResponseBody
@Transactional(rollbackFor = TipException.class)
public RestResponseBo saveSetting(@RequestParam(required = false) String site_theme, HttpServletRequest request) {
try {
Map<String, String[]> parameterMap = request.getParameterMap();
Map<String, String> querys = new HashMap<>();
parameterMap.forEach((key, value) -> {
querys.put(key, join(value));
});
optionService.saveOptions(querys);
WebConst.initConfig = querys;
if (StringUtils.isNotBlank(site_theme)) {
BaseController.THEME = "themes/" + site_theme;
}
logService.insertLog(LogActions.SYS_SETTING.getAction(), GsonUtils.toJsonString(querys), request.getRemoteAddr(), this.getUid(request));
return RestResponseBo.ok();
} catch (Exception e) {
String msg = "保存设置失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
LOGGER.error(msg, e);
}
return RestResponseBo.fail(msg);
}
}
//系统备份
@PostMapping(value = "backup")
@ResponseBody
@Transactional(rollbackFor = TipException.class)
public RestResponseBo backup(@RequestParam String bk_type, @RequestParam String bk_path,
HttpServletRequest request) {
if (StringUtils.isBlank(bk_type)) {
return RestResponseBo.fail("请确认信息输入完整");
}
try {
BackResponseBo backResponse = siteService.backup(bk_type, bk_path, "yyyyMMddHHmm");
logService.insertLog(LogActions.SYS_BACKUP.getAction(), null, request.getRemoteAddr(), this.getUid(request));
return RestResponseBo.ok(backResponse);
} catch (Exception e) {
String msg = "备份失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
LOGGER.error(msg, e);
}
return RestResponseBo.fail(msg);
}
}