inleft
2022-07-29 ba780fb8c047c665f8d85c013b02e47f8269451d
commit | author | age
0613f2 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25 package vip.xiaonuo.modular.blogarticle.controller;
26
bd3bc1 27 import cn.hutool.core.bean.BeanUtil;
ba780f 28 import cn.hutool.core.date.DateUnit;
31e475 29 import cn.hutool.core.date.DateUtil;
0613f2 30 import cn.hutool.core.util.PageUtil;
I 31 import cn.hutool.core.util.StrUtil;
bd3bc1 32 import cn.hutool.crypto.SecureUtil;
0613f2 33 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
552004 34 import org.springframework.validation.annotation.Validated;
I 35 import org.springframework.web.bind.annotation.*;
0613f2 36 import vip.xiaonuo.core.annotion.BusinessLog;
bd3bc1 37 import vip.xiaonuo.core.consts.MyConstant;
cfde48 38 import vip.xiaonuo.core.context.constant.ConstantContextHolder;
0613f2 39 import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
bd3bc1 40 import vip.xiaonuo.core.exception.BlogException;
I 41 import vip.xiaonuo.core.exception.enums.BlogExceptionEnum;
0613f2 42 import vip.xiaonuo.core.pojo.response.ResponseData;
I 43 import vip.xiaonuo.core.pojo.response.SuccessResponseData;
bd3bc1 44 import vip.xiaonuo.modular.blogarticle.entity.BlogArticle;
ba780f 45 import vip.xiaonuo.modular.blogarticle.entity.BlogArticleVo;
552004 46 import vip.xiaonuo.modular.blogarticle.param.BlogArticleAddDto;
0613f2 47 import vip.xiaonuo.modular.blogarticle.param.BlogArticleQueryDto;
I 48 import vip.xiaonuo.modular.blogarticle.service.BlogArticleService;
552004 49 import vip.xiaonuo.modular.blogarticletype.entity.BlogArticleType;
I 50 import vip.xiaonuo.modular.blogarticletype.service.BlogArticleTypeService;
cfde48 51 import vip.xiaonuo.sys.modular.file.entity.SysFileInfo;
I 52 import vip.xiaonuo.sys.modular.file.service.SysFileInfoService;
0613f2 53
I 54 import javax.annotation.Resource;
552004 55 import java.util.Date;
0613f2 56 import java.util.HashMap;
I 57 import java.util.List;
58 import java.util.Map;
59 import java.util.stream.Collectors;
60
61 /**
62  * blog文章控制器 (提供给外部blog系统查询)
63  *
64  * @author inleft
65  * @date 2022-02-09 18:20:22
66  */
67 @RestController
68 @RequestMapping("/outside")
69 public class BlogArticleOutsideController {
70
71     @Resource
72     private BlogArticleService blogArticleService;
73
cfde48 74
I 75     @Resource
552004 76     private BlogArticleTypeService blogArticleTypeService;
I 77
78     @Resource
cfde48 79     private SysFileInfoService fileInfoService;
552004 80
I 81     @PostMapping("/blogArticle/add")
82     @BusinessLog(title = "外部blog系统_blog文章_增加", opType = LogAnnotionOpTypeEnum.ADD)
83     public ResponseData add(@RequestBody @Validated(BlogArticleAddDto.add.class) BlogArticleAddDto addDto) {
31e475 84         if (!SecureUtil.md5(addDto.getSecret()).equals("b49e4cc48616cfb8d5ed3e5b983165c8")) {
552004 85             throw new BlogException("口令错误..");
I 86         }
87
88         if (MyConstant.Yes.equals(addDto.getIsTop()) && addDto.getTopValue() == null) {
89             throw new BlogException("选择了置顶需要指定置顶值..");
90         }
91
92         if (addDto.getIsLock().equals(MyConstant.Yes)) {
93             if (addDto.getAuthStatus().equals(MyConstant.AuthStatus.authCode) && StrUtil.isEmpty(addDto.getAuthPassword())) {
94                 throw new BlogException("选择了密码授权,密码不能为空");
95             }
96         } else {
97             addDto.setAuthPassword(null);
98         }
99
100         if (MyConstant.Yes.equals(addDto.getIsOnline())) {
101             if (addDto.getArticleFileId() == null || fileInfoService.getById(addDto.getArticleFileId()) == null) {
102                 throw new BlogException("已选择在线模式..文件id非法");
103             }
104         } else {
105             if (StrUtil.isEmpty(addDto.getContent())) {
106                 throw new BlogException("已选择在线模式..文件内容非法");
107             }
108         }
109
110         if (addDto.getCoverFileId() != null && fileInfoService.getById(addDto.getCoverFileId()) == null) {
111             throw new BlogException("封面文件id非法");
112         }
113
114         if (blogArticleTypeService.lambdaQuery()
115                 .eq(BlogArticleType::getIsEnable, MyConstant.Yes)
116                 .eq(BlogArticleType::getId, addDto.getArticleTypeId())
117                 .one() == null) {
118             throw new BlogException("分类id非法");
119         }
120
121         if (addDto.getPublishDate() == null) {
122             addDto.setPublishDate(new Date());
123         }
124
125         BlogArticle article = new BlogArticle();
126         BeanUtil.copyProperties(addDto, article);
31e475 127         article.setSeparateYear(DateUtil.year(article.getPublishDate()));
ba780f 128         article.setSeparateMonth(DateUtil.month(article.getPublishDate()) + 1);
31e475 129         article.setSeparateDay(DateUtil.dayOfMonth(article.getPublishDate()));
I 130
131         article.setIsEnable(MyConstant.Yes);
132         blogArticleService.save(article);
552004 133         return new SuccessResponseData();
I 134     }
cfde48 135
0613f2 136     /**
I 137      * 查询blog文章
138      *
139      * @author inleft
140      * @date 2022-02-09 18:20:22
141      */
142     @GetMapping("/blogArticle/queryBlogArticleList")
143     @BusinessLog(title = "外部blog系统_blog文章_查询", opType = LogAnnotionOpTypeEnum.QUERY)
144     public ResponseData queryBlogArticleList(BlogArticleQueryDto queryDto) {
145
146
147         Map<String, Object> param = new HashMap<>(16);
148         param.put("pageNo", PageUtil.getStart(queryDto.getPageNo() - 1, queryDto.getPageSize()));
149         param.put("pageSize", queryDto.getPageSize());
a9c4c9 150         param.put("typeId", queryDto.getTypeId());
0613f2 151
cfde48 152         String blogSourcePrefix = ConstantContextHolder.getBlogSourcePrefix();
ba780f 153
I 154         Date now = DateUtil.date();
155         //空类型查询条件,排除笔记系列,按更新时间倒序
156         //含有类型查询,按发布时间倒序
a9c4c9 157         List<BlogArticleVo> resList = blogArticleService.searchList(param).stream().map(e -> {
504927 158             if (StrUtil.isNotEmpty(e.getCoverFileURL())) {
I 159                 if (!e.getCoverFileURL().startsWith("http")) {
160                     //补上访问参数
cfde48 161                     e.setCoverFileURL(blogSourcePrefix + e.getCoverFileURL());
504927 162                 }
0613f2 163             }
ba780f 164
I 165             //前端根据条件标注小红点(发布时间和更新时间在7天内)
166             if (e.getPublishDate().before(e.getUpdateDate())
167                     && DateUtil.between(e.getUpdateDate(), now, DateUnit.DAY) <= 7) {
168                 e.setIsAnyUpdate(MyConstant.Yes);
169             } else {
170                 e.setIsAnyUpdate(MyConstant.No);
171             }
172
0613f2 173             return e;
I 174         }).collect(Collectors.toList());
175
a9c4c9 176         long count = blogArticleService.searchListCount(param);
0613f2 177
I 178         Page<BlogArticleVo> queryPage = new Page<>(queryDto.getPageNo(), queryDto.getPageSize());
179         queryPage.setRecords(resList);
180         queryPage.setTotal(count);
181
182         return new SuccessResponseData(queryPage);
183     }
184
bd3bc1 185     @GetMapping("/blogArticle/queryBlogArticleDetail")
I 186     @BusinessLog(title = "外部blog系统_blog文章详情_查询", opType = LogAnnotionOpTypeEnum.QUERY)
187     public ResponseData queryBlogArticleDetail(BlogArticleQueryDto queryDto) {
188         BlogArticle find = blogArticleService.lambdaQuery()
189                 .eq(BlogArticle::getIsEnable, MyConstant.Yes)
190                 .eq(BlogArticle::getEditorStatus, MyConstant.Yes)
191                 .eq(BlogArticle::getId, queryDto.getId())
192                 .one();
193
194         if (find == null) {
195             throw new BlogException(BlogExceptionEnum.article_not_found);
196         }
197
198         //加密文章
199         if (find.getAuthStatus().equals(MyConstant.AuthStatus.authCode)) {
200             //授权码缺失
201             if (StrUtil.isEmpty(queryDto.getAuthWord())) {
202                 throw new BlogException(BlogExceptionEnum.article_auth_error);
203             }
204             //授权码比对
88f419 205             if (!find.getAuthPassword().equals(queryDto.getAuthWord())) {
bd3bc1 206                 throw new BlogException(BlogExceptionEnum.article_auth_pass_error);
I 207             }
cfde48 208         } else if (find.getAuthStatus().equals(MyConstant.AuthStatus.privateCode)) {
bd3bc1 209             throw new BlogException(BlogExceptionEnum.article_auth_private_error);
I 210         }
211
212         BlogArticleVo vo = new BlogArticleVo();
213
214         BeanUtil.copyProperties(find, vo);
215
31e475 216
e99de8 217         String tempURL = this.getBlogSourceURL(find.getArticleFileId());
cfde48 218
e99de8 219         if (StrUtil.isEmpty(tempURL))
I 220             throw new BlogException(BlogExceptionEnum.article_file_lose);
221
222         vo.setArticleFileURL(tempURL);
bd3bc1 223
I 224         return new SuccessResponseData(vo);
225     }
226
cfde48 227     private String getBlogSourceURL(Long fileId) {
I 228         SysFileInfo sysFileInfo = fileInfoService.getById(fileId);
229         if (sysFileInfo == null) {
230             return null;
231         } else {
232             String blogSourcePrefix = ConstantContextHolder.getBlogSourcePrefix();
e99de8 233             return blogSourcePrefix + sysFileInfo.getFileBucket() + "/" + sysFileInfo.getFileObjectName();
cfde48 234         }
I 235     }
0613f2 236
I 237 }