inleft
2022-02-21 552004d0902687de16cd50e3378d4d6671d7f015
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;
0613f2 28 import cn.hutool.core.util.PageUtil;
I 29 import cn.hutool.core.util.StrUtil;
bd3bc1 30 import cn.hutool.crypto.SecureUtil;
0613f2 31 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
552004 32 import org.springframework.validation.annotation.Validated;
I 33 import org.springframework.web.bind.annotation.*;
0613f2 34 import vip.xiaonuo.core.annotion.BusinessLog;
bd3bc1 35 import vip.xiaonuo.core.consts.MyConstant;
cfde48 36 import vip.xiaonuo.core.context.constant.ConstantContextHolder;
0613f2 37 import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
bd3bc1 38 import vip.xiaonuo.core.exception.BlogException;
I 39 import vip.xiaonuo.core.exception.enums.BlogExceptionEnum;
0613f2 40 import vip.xiaonuo.core.pojo.response.ResponseData;
I 41 import vip.xiaonuo.core.pojo.response.SuccessResponseData;
bd3bc1 42 import vip.xiaonuo.modular.blogarticle.entity.BlogArticle;
552004 43 import vip.xiaonuo.modular.blogarticle.param.BlogArticleAddDto;
0613f2 44 import vip.xiaonuo.modular.blogarticle.param.BlogArticleQueryDto;
I 45 import vip.xiaonuo.modular.blogarticle.param.BlogArticleVo;
46 import vip.xiaonuo.modular.blogarticle.service.BlogArticleService;
552004 47 import vip.xiaonuo.modular.blogarticletype.entity.BlogArticleType;
I 48 import vip.xiaonuo.modular.blogarticletype.service.BlogArticleTypeService;
cfde48 49 import vip.xiaonuo.sys.modular.file.entity.SysFileInfo;
I 50 import vip.xiaonuo.sys.modular.file.service.SysFileInfoService;
0613f2 51
I 52 import javax.annotation.Resource;
552004 53 import java.util.Date;
0613f2 54 import java.util.HashMap;
I 55 import java.util.List;
56 import java.util.Map;
57 import java.util.stream.Collectors;
58
59 /**
60  * blog文章控制器 (提供给外部blog系统查询)
61  *
62  * @author inleft
63  * @date 2022-02-09 18:20:22
64  */
65 @RestController
66 @RequestMapping("/outside")
67 public class BlogArticleOutsideController {
68
69     @Resource
70     private BlogArticleService blogArticleService;
71
cfde48 72
I 73     @Resource
552004 74     private BlogArticleTypeService blogArticleTypeService;
I 75
76     @Resource
cfde48 77     private SysFileInfoService fileInfoService;
552004 78
I 79     @PostMapping("/blogArticle/add")
80     @BusinessLog(title = "外部blog系统_blog文章_增加", opType = LogAnnotionOpTypeEnum.ADD)
81     public ResponseData add(@RequestBody @Validated(BlogArticleAddDto.add.class) BlogArticleAddDto addDto) {
82         if (!SecureUtil.md5(addDto.getSecret()).equals("8616CFB8D5ED3E5B")) {
83             throw new BlogException("口令错误..");
84         }
85
86         if (MyConstant.Yes.equals(addDto.getIsTop()) && addDto.getTopValue() == null) {
87             throw new BlogException("选择了置顶需要指定置顶值..");
88         }
89
90         if (addDto.getIsLock().equals(MyConstant.Yes)) {
91             if (addDto.getAuthStatus().equals(MyConstant.AuthStatus.authCode) && StrUtil.isEmpty(addDto.getAuthPassword())) {
92                 throw new BlogException("选择了密码授权,密码不能为空");
93             }
94         } else {
95             addDto.setAuthPassword(null);
96         }
97
98         if (MyConstant.Yes.equals(addDto.getIsOnline())) {
99             if (addDto.getArticleFileId() == null || fileInfoService.getById(addDto.getArticleFileId()) == null) {
100                 throw new BlogException("已选择在线模式..文件id非法");
101             }
102         } else {
103             if (StrUtil.isEmpty(addDto.getContent())) {
104                 throw new BlogException("已选择在线模式..文件内容非法");
105             }
106         }
107
108         if (addDto.getCoverFileId() != null && fileInfoService.getById(addDto.getCoverFileId()) == null) {
109             throw new BlogException("封面文件id非法");
110         }
111
112         if (blogArticleTypeService.lambdaQuery()
113                 .eq(BlogArticleType::getIsEnable, MyConstant.Yes)
114                 .eq(BlogArticleType::getId, addDto.getArticleTypeId())
115                 .one() == null) {
116             throw new BlogException("分类id非法");
117         }
118
119         if (addDto.getPublishDate() == null) {
120             addDto.setPublishDate(new Date());
121         }
122
123         BlogArticle article = new BlogArticle();
124         BeanUtil.copyProperties(addDto, article);
125         System.out.println(article.toString());
126
127 //        blogArticleService.add(blogArticleParam);
128         return new SuccessResponseData();
129     }
cfde48 130
0613f2 131     /**
I 132      * 查询blog文章
133      *
134      * @author inleft
135      * @date 2022-02-09 18:20:22
136      */
137     @GetMapping("/blogArticle/queryBlogArticleList")
138     @BusinessLog(title = "外部blog系统_blog文章_查询", opType = LogAnnotionOpTypeEnum.QUERY)
139     public ResponseData queryBlogArticleList(BlogArticleQueryDto queryDto) {
140
141
142         Map<String, Object> param = new HashMap<>(16);
143         param.put("pageNo", PageUtil.getStart(queryDto.getPageNo() - 1, queryDto.getPageSize()));
144         param.put("pageSize", queryDto.getPageSize());
a9c4c9 145         param.put("typeId", queryDto.getTypeId());
0613f2 146
cfde48 147         String blogSourcePrefix = ConstantContextHolder.getBlogSourcePrefix();
a9c4c9 148         List<BlogArticleVo> resList = blogArticleService.searchList(param).stream().map(e -> {
504927 149             if (StrUtil.isNotEmpty(e.getCoverFileURL())) {
I 150                 if (!e.getCoverFileURL().startsWith("http")) {
151                     //补上访问参数
cfde48 152                     e.setCoverFileURL(blogSourcePrefix + e.getCoverFileURL());
504927 153                 }
0613f2 154             }
I 155             return e;
156         }).collect(Collectors.toList());
157
a9c4c9 158         long count = blogArticleService.searchListCount(param);
0613f2 159
I 160         Page<BlogArticleVo> queryPage = new Page<>(queryDto.getPageNo(), queryDto.getPageSize());
161         queryPage.setRecords(resList);
162         queryPage.setTotal(count);
163
164         return new SuccessResponseData(queryPage);
165     }
166
bd3bc1 167     @GetMapping("/blogArticle/queryBlogArticleDetail")
I 168     @BusinessLog(title = "外部blog系统_blog文章详情_查询", opType = LogAnnotionOpTypeEnum.QUERY)
169     public ResponseData queryBlogArticleDetail(BlogArticleQueryDto queryDto) {
170         BlogArticle find = blogArticleService.lambdaQuery()
171                 .eq(BlogArticle::getIsEnable, MyConstant.Yes)
172                 .eq(BlogArticle::getEditorStatus, MyConstant.Yes)
173                 .eq(BlogArticle::getId, queryDto.getId())
174                 .one();
175
176         if (find == null) {
177             throw new BlogException(BlogExceptionEnum.article_not_found);
178         }
179
180         //加密文章
181         if (find.getAuthStatus().equals(MyConstant.AuthStatus.authCode)) {
182             //授权码缺失
183             if (StrUtil.isEmpty(queryDto.getAuthWord())) {
184                 throw new BlogException(BlogExceptionEnum.article_auth_error);
185             }
186             //授权码比对
187             if (!SecureUtil.md5(find.getAuthPassword()).equals(queryDto.getAuthWord())) {
188                 throw new BlogException(BlogExceptionEnum.article_auth_pass_error);
189             }
cfde48 190         } else if (find.getAuthStatus().equals(MyConstant.AuthStatus.privateCode)) {
bd3bc1 191             throw new BlogException(BlogExceptionEnum.article_auth_private_error);
I 192         }
193
194         BlogArticleVo vo = new BlogArticleVo();
195
196         BeanUtil.copyProperties(find, vo);
197
198         if (queryDto.getId() % 2 == 0) {
cfde48 199             String tempURL = this.getBlogSourceURL(find.getArticleFileId());
552004 200 //            if (StrUtil.isEmpty(tempURL))
I 201 //                throw new BlogException(BlogExceptionEnum.article_file_lose);
cfde48 202
I 203             vo.setArticleFileURL(tempURL);
552004 204             vo.setArticleFileURL("http://t.inleft.com/share/book/blog/a岛神灯.md");
bd3bc1 205         } else {
I 206             vo.setArticleFileURL("http://t.inleft.com/share/book/blog/es-search.md");
207         }
208
209         return new SuccessResponseData(vo);
210     }
211
cfde48 212     private String getBlogSourceURL(Long fileId) {
I 213         SysFileInfo sysFileInfo = fileInfoService.getById(fileId);
214         if (sysFileInfo == null) {
215             return null;
216         } else {
217             String blogSourcePrefix = ConstantContextHolder.getBlogSourcePrefix();
218             return blogSourcePrefix + sysFileInfo.getFileBucket() + sysFileInfo.getFileObjectName();
219         }
220     }
0613f2 221
I 222 }