inleft
2022-08-10 d80267c86dae99a64327cda6c7b48a7ce0f64f25
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());
d80267 151         param.put("fileType", queryDto.getFileType());
0613f2 152
cfde48 153         String blogSourcePrefix = ConstantContextHolder.getBlogSourcePrefix();
ba780f 154
I 155         Date now = DateUtil.date();
156         //空类型查询条件,排除笔记系列,按更新时间倒序
157         //含有类型查询,按发布时间倒序
a9c4c9 158         List<BlogArticleVo> resList = blogArticleService.searchList(param).stream().map(e -> {
504927 159             if (StrUtil.isNotEmpty(e.getCoverFileURL())) {
I 160                 if (!e.getCoverFileURL().startsWith("http")) {
161                     //补上访问参数
cfde48 162                     e.setCoverFileURL(blogSourcePrefix + e.getCoverFileURL());
504927 163                 }
0613f2 164             }
ba780f 165
I 166             //前端根据条件标注小红点(发布时间和更新时间在7天内)
167             if (e.getPublishDate().before(e.getUpdateDate())
168                     && DateUtil.between(e.getUpdateDate(), now, DateUnit.DAY) <= 7) {
169                 e.setIsAnyUpdate(MyConstant.Yes);
170             } else {
171                 e.setIsAnyUpdate(MyConstant.No);
172             }
173
0613f2 174             return e;
I 175         }).collect(Collectors.toList());
176
a9c4c9 177         long count = blogArticleService.searchListCount(param);
0613f2 178
I 179         Page<BlogArticleVo> queryPage = new Page<>(queryDto.getPageNo(), queryDto.getPageSize());
180         queryPage.setRecords(resList);
181         queryPage.setTotal(count);
182
183         return new SuccessResponseData(queryPage);
184     }
185
bd3bc1 186     @GetMapping("/blogArticle/queryBlogArticleDetail")
I 187     @BusinessLog(title = "外部blog系统_blog文章详情_查询", opType = LogAnnotionOpTypeEnum.QUERY)
188     public ResponseData queryBlogArticleDetail(BlogArticleQueryDto queryDto) {
189         BlogArticle find = blogArticleService.lambdaQuery()
190                 .eq(BlogArticle::getIsEnable, MyConstant.Yes)
191                 .eq(BlogArticle::getEditorStatus, MyConstant.Yes)
192                 .eq(BlogArticle::getId, queryDto.getId())
193                 .one();
194
195         if (find == null) {
196             throw new BlogException(BlogExceptionEnum.article_not_found);
197         }
198
199         //加密文章
200         if (find.getAuthStatus().equals(MyConstant.AuthStatus.authCode)) {
201             //授权码缺失
202             if (StrUtil.isEmpty(queryDto.getAuthWord())) {
203                 throw new BlogException(BlogExceptionEnum.article_auth_error);
204             }
205             //授权码比对
88f419 206             if (!find.getAuthPassword().equals(queryDto.getAuthWord())) {
bd3bc1 207                 throw new BlogException(BlogExceptionEnum.article_auth_pass_error);
I 208             }
cfde48 209         } else if (find.getAuthStatus().equals(MyConstant.AuthStatus.privateCode)) {
bd3bc1 210             throw new BlogException(BlogExceptionEnum.article_auth_private_error);
I 211         }
212
213         BlogArticleVo vo = new BlogArticleVo();
214
215         BeanUtil.copyProperties(find, vo);
216
31e475 217
e99de8 218         String tempURL = this.getBlogSourceURL(find.getArticleFileId());
cfde48 219
e99de8 220         if (StrUtil.isEmpty(tempURL))
I 221             throw new BlogException(BlogExceptionEnum.article_file_lose);
222
223         vo.setArticleFileURL(tempURL);
bd3bc1 224
I 225         return new SuccessResponseData(vo);
226     }
227
cfde48 228     private String getBlogSourceURL(Long fileId) {
I 229         SysFileInfo sysFileInfo = fileInfoService.getById(fileId);
230         if (sysFileInfo == null) {
231             return null;
232         } else {
233             String blogSourcePrefix = ConstantContextHolder.getBlogSourcePrefix();
e99de8 234             return blogSourcePrefix + sysFileInfo.getFileBucket() + "/" + sysFileInfo.getFileObjectName();
cfde48 235         }
I 236     }
0613f2 237
I 238 }