package com.example.blog.web.controller.admin;
import cn.hutool.http.HtmlUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.blog.api.exception.MyBusinessException;
import com.example.blog.api.vo.SearchVo;
import com.example.blog.web.controller.common.BaseController;
import com.example.blog.api.dto.JsonResult;
import com.example.blog.api.dto.QueryCondition;
import com.example.blog.api.entity.*;
import com.example.blog.api.enums.*;
import com.example.blog.api.service.*;
import com.example.blog.api.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Objects;
/**
* <pre>
* 后台文章管理控制器
* </pre>
*
* @author : author
* @date : 2017/12/10
*/
@Slf4j
@Controller
@RequestMapping(value = "/admin/post")
public class PostController extends BaseController {
@Reference(version = "1.0.0",
application = "${dubbo.application.id}",
interfaceName = "com.example.blog.api.service.PostService",
check = false,
timeout = 3000,
retries = 0
)
private PostService postService;
@Reference(version = "1.0.0",
application = "${dubbo.application.id}",
interfaceName = "com.example.blog.api.service.CategoryService",
check = false,
timeout = 3000,
retries = 0
)
private CategoryService categoryService;
@Reference(version = "1.0.0",
application = "${dubbo.application.id}",
interfaceName = "com.example.blog.api.service.UserService",
check = false,
timeout = 3000,
retries = 0
)
private UserService userService;
@Reference(version = "1.0.0",
application = "${dubbo.application.id}",
interfaceName = "com.example.blog.api.service.TagService",
check = false,
timeout = 3000,
retries = 0
)
private TagService tagService;
public static final String TITLE = "title";
public static final String CONTENT = "content";
/**
* 处理后台获取文章列表的请求
*
* @param model model
* @return 模板路径admin/admin_post
*/
@GetMapping
public String posts(Model model,
@RequestParam(value = "status", defaultValue = "0") Integer status,
@RequestParam(value = "keywords", defaultValue = "") String keywords,
@RequestParam(value = "searchType", defaultValue = "") String searchType,
@RequestParam(value = "postSource", defaultValue = "-1") Integer postSource,
@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
@RequestParam(value = "size", defaultValue = "10") Integer pageSize,
@RequestParam(value = "sort", defaultValue = "createTime") String sort,
@RequestParam(value = "order", defaultValue = "desc") String order,
@ModelAttribute SearchVo searchVo) {
Long loginUserId = getLoginUserId();
Post condition = new Post();
if (!StringUtils.isBlank(keywords)) {
if (TITLE.equals(searchType)) {
condition.setPostTitle(keywords);
} else {
condition.setPostContent(keywords);
}
}
condition.setPostType(PostTypeEnum.POST_TYPE_POST.getValue());
condition.setPostStatus(status);
// 管理员可以查看所有用户的,非管理员只能看到自己的文章
if (!loginUserIsAdmin()) {
condition.setUserId(loginUserId);
}
Page page = PageUtil.initMpPage(pageNumber, pageSize, sort, order);
Page<Post> posts = postService.findAll(
page,
new QueryCondition<>(condition, searchVo));
//封装分类和标签
model.addAttribute("posts", posts.getRecords());
model.addAttribute("pageInfo", PageUtil.convertPageVo(page));
model.addAttribute("status", status);
model.addAttribute("keywords", keywords);
model.addAttribute("searchType", searchType);
model.addAttribute("postSource", postSource);
model.addAttribute("order", order);
model.addAttribute("sort", sort);
return "admin/admin_post";
}
/**
* 处理跳转到新建文章页面
*
* @return 模板路径admin/admin_editor
*/
@GetMapping(value = "/new")
public String newPost(Model model) {
//所有分类
List<Category> allCategories = categoryService.findAll();
model.addAttribute("categories", allCategories);
return "admin/admin_post_new";
}
/**
* 添加/更新文章
*
* @param post Post实体
* @param cateId 分类ID
* @param tags 标签列表
*/
@PostMapping(value = "/save")
@ResponseBody
public JsonResult pushPost(@ModelAttribute Post post,
@RequestParam("cateId") Long cateId,
@RequestParam("tags") String tags) {
// 1,检查分类和标签
checkTags(tags);
// 2.获得登录用户
User user = getLoginUser();
Boolean isAdmin = loginUserIsAdmin();
post.setUserId(getLoginUserId());
//3、非管理员只能修改自己的文章,管理员都可以修改
Post originPost = null;
if (post.getId() != null) {
originPost = postService.get(post.getId());
if (!Objects.equals(originPost.getUserId(), user.getId()) && !isAdmin) {
return JsonResult.error("没有权限");
}
//以下属性不能修改
post.setUserId(originPost.getUserId());
post.setPostViews(originPost.getPostViews());
post.setCommentSize(originPost.getCommentSize());
post.setPostLikes(originPost.getPostLikes());
post.setCommentSize(originPost.getCommentSize());
post.setDelFlag(originPost.getDelFlag());
}
// 4、提取摘要
int postSummaryLength = 100;
//文章摘要
String summaryText = HtmlUtil.cleanHtmlTag(post.getPostContent());
if (summaryText.length() > postSummaryLength) {
summaryText = summaryText.substring(0, postSummaryLength);
}
post.setPostSummary(summaryText);
// 5、分类标签
Category category = new Category();
category.setId(cateId);
post.setCategory(category);
if (StringUtils.isNotEmpty(tags)) {
List<Tag> tagList = tagService.strListToTagList(StringUtils.deleteWhitespace(tags));
post.setTagList(tagList);
}
// 6.类型
post.setPostType(PostTypeEnum.POST_TYPE_POST.getValue());
// 7.添加/更新入库
postService.insertOrUpdate(post);
return JsonResult.success("发布成功");
}
/**
* 限制一篇文章最多5个标签
*
* @param tagList
*/
private void checkTags(String tagList) {
String[] tags = tagList.split(",");
if (tags.length > 5) {
throw new MyBusinessException("每篇文章最多5个标签");
}
for (String tag : tags) {
if (tag.length() > 20) {
throw new MyBusinessException("每个标签长度最多为20个字符");
}
}
}
/**
* 处理移至回收站的请求
*
* @param postId 文章编号
* @return 重定向到/admin/post
*/
@PostMapping(value = "/throw")
@ResponseBody
public JsonResult mov
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
dubbo+springboot 博客系统 (465个子文件)
style.css 205KB
style.min.css 158KB
bootstrap.css 143KB
bootstrap.min.css 118KB
AdminLTE.css 109KB
AdminLTE.min.css 89KB
AdminLTE-without-plugins.css 88KB
AdminLTE-without-plugins.min.css 72KB
froala_editor.pkgd.css 71KB
froala_editor.pkgd.min.css 60KB
loaders.css 55KB
animate.min.css 52KB
_all-skins.css 47KB
dashicons.min.css 45KB
_all-skins.min.css 41KB
royal.css 40KB
dark.css 40KB
gray.css 40KB
font-awesome.css 37KB
red.css 36KB
royal.min.css 35KB
gray.min.css 35KB
dark.min.css 35KB
froala_editor.css 34KB
red.min.css 32KB
font-awesome.min.css 30KB
froala_editor.min.css 29KB
bootstrap-theme.css 26KB
login.min.css 25KB
bootstrap-theme.min.css 23KB
pretty-checkbox.css 22KB
pretty-checkbox.min.css 19KB
AdminLTE-bootstrap-social.css 15KB
layer.css 14KB
AdminLTE-bootstrap-social.min.css 12KB
jsmodern-1.1.1.min.css 11KB
froala_style.css 9KB
image_manager.css 8KB
froala_style.min.css 8KB
image_manager.min.css 7KB
buttons.min.css 6KB
bootstrapStyle.css 6KB
comment.css 6KB
highlight.css 5KB
layer.css 5KB
video.css 5KB
image.css 5KB
jquery.toast.min.css 4KB
video.min.css 4KB
table.css 4KB
image.min.css 4KB
colors.css 4KB
style.css 3KB
table.min.css 3KB
colors.min.css 3KB
loader.css 3KB
AdminLTE-select2.css 3KB
file.css 3KB
code_view.css 3KB
AdminLTE-select2.min.css 3KB
file.min.css 2KB
code_view.min.css 2KB
pace.css 2KB
pace.min.css 2KB
spell_checker.css 2KB
quick_insert.css 2KB
spell_checker.min.css 1KB
help.css 1KB
quick_insert.min.css 1KB
char_counter.css 1KB
embedly.css 1KB
special_characters.css 1KB
font_awesome.css 1KB
help.min.css 1KB
special_characters.min.css 1KB
char_counter.min.css 1KB
embedly.min.css 1KB
font_awesome.min.css 1KB
emoticons.css 897B
draggable.css 868B
emoticons.min.css 758B
image_tui.css 748B
bootstrapValidator.css 700B
bootstrapValidator.min.css 694B
line_breaker.css 681B
draggable.min.css 663B
main.css 656B
image_tui.min.css 612B
line_breaker.min.css 574B
fullscreen.css 566B
fullscreen.min.css 489B
fontawesome-webfont.eot 162KB
glyphicons-halflings-regular.eot 20KB
loading-0.gif 6KB
loading-2.gif 2KB
loading-1.gif 701B
default-50x50.gif 184B
.gitattributes 86B
.gitignore 439B
favicon.ico 17KB
共 465 条
- 1
- 2
- 3
- 4
- 5
资源评论
季风泯灭的季节
- 粉丝: 601
- 资源: 2920
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功