Skip to content

Commit 2dd2c21

Browse files
authored
Merge pull request #2 from HackyleShawe/feat-202601
important features merge to master
2 parents 052efaa + 4ebeb2d commit 2dd2c21

29 files changed

Lines changed: 499 additions & 72 deletions

File tree

blog-admin-web/src/views/article/category/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<el-table-column label="Name" prop="name" width="100"></el-table-column>
1313
<el-table-column label="Code" prop="code" width="80"></el-table-column>
1414
<el-table-column label="Description" prop="description"></el-table-column>
15+
<el-table-column label="Articles" prop="articleCount" width="75"></el-table-column>
1516
<el-table-column label="UpdateTime" prop="updateTime"></el-table-column>
1617

1718
<el-table-column label="Operate">
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.hackyle.blog.admin.infrastructure.redis;
2+
3+
/**
4+
* 定义本模块下所有的缓存key前缀
5+
*/
6+
public class CacheKey {
7+
//统一前缀:blog admin
8+
private static final String UNI_PREFIX = "ba:";
9+
10+
11+
/**
12+
* 登录认证
13+
*/
14+
public static class Auth {
15+
private static final String PREFIX = UNI_PREFIX + "auth:";
16+
17+
//每次生成验证码的缓存前缀
18+
public static final String CAPTCHA = PREFIX + "cha:";
19+
//登录者用户id
20+
public static final String LOGIN_UID = PREFIX + "uid:";
21+
22+
}
23+
24+
/**
25+
* 系统管理
26+
*/
27+
public static class Sys {
28+
private static final String PREFIX = UNI_PREFIX + "sys:";
29+
30+
//系统配置
31+
public static final String CONFIG_KEY = PREFIX + "config:";
32+
//字典
33+
public static final String DICT_KEY = PREFIX + "dict:";
34+
}
35+
36+
37+
38+
}

blog-admin/src/main/java/com/hackyle/blog/admin/infrastructure/threadpool/LogTaskThreadPool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class LogTaskThreadPool {
3333
public LogTaskThreadPool() {
3434
ioTaskThreadPool = new ThreadPoolExecutor(
3535
CPU_COUNT *2,
36-
CPU_COUNT *2,
36+
CPU_COUNT *4,
3737
60L,
3838
TimeUnit.SECONDS,
3939
new LinkedBlockingQueue<>(1000), //todo 设计合理的阻塞队列

blog-admin/src/main/java/com/hackyle/blog/admin/module/article/mapper/ArticleAuthorMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public interface ArticleAuthorMapper extends BaseMapper<ArticleAuthorRelationEnt
1515

1616
int deleteByArticleIds(@Param("articleIds") Set<Long> idSet);
1717

18-
List<ArticleAuthorRelationDto> getArticleAuthor(@Param("articleIds")Set<Long> articleId);
18+
List<ArticleAuthorRelationDto> getAuthorByArticleId(@Param("articleIds")Set<Long> articleId);
1919

2020
}

blog-admin/src/main/java/com/hackyle/blog/admin/module/article/mapper/ArticleCategoryMapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ public interface ArticleCategoryMapper extends BaseMapper<ArticleCategoryRelatio
1515

1616
int deleteByArticleIds(@Param("articleIds")Set<Long> idSet);
1717

18-
List<ArticleCategoryRelationDto> getArticleCategory(@Param("articleIds")Set<Long> articleId);
18+
List<ArticleCategoryRelationDto> getCategoryByArticleId(@Param("articleIds")Set<Long> articleId);
19+
20+
List<ArticleCategoryRelationDto> getArticleByCategoryId(@Param("categoryIds")Set<Long> categoryIds);
1921
}

blog-admin/src/main/java/com/hackyle/blog/admin/module/article/model/vo/CategoryVo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ public class CategoryVo {
4444
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
4545
private LocalDateTime createTime;
4646

47+
/**
48+
* 分类下的文章数量
49+
*/
50+
private Integer articleCount;
51+
4752
}

blog-admin/src/main/java/com/hackyle/blog/admin/module/article/service/impl/ArticleServiceImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,9 @@ public PageInfo<ArticleListVo> list(ArticleQueryDto queryDto) {
429429

430430
Set<Long> articleIds = articleListVoList.stream().map(ArticleListVo::getId).collect(Collectors.toSet());
431431
//查文章的作者
432-
Map<Long, String> articleAuthorMap = getArticleAuthor(articleIds);
432+
Map<Long, String> articleAuthorMap = getAuthorByArticleId(articleIds);
433433
//查文章的分类
434-
Map<Long, String> articleCategoryMap = getArticleCategory(articleIds);
434+
Map<Long, String> articleCategoryMap = getCategoryByArticleId(articleIds);
435435

436436
for (ArticleListVo articleVo : articleListVoList) {
437437
//没有进行更新操作时,updateTime值为null,设置为createTime
@@ -448,11 +448,11 @@ public PageInfo<ArticleListVo> list(ArticleQueryDto queryDto) {
448448
return pageInfo;
449449
}
450450

451-
private Map<Long, String> getArticleAuthor(Set<Long> articleIds) {
451+
private Map<Long, String> getAuthorByArticleId(Set<Long> articleIds) {
452452
if(CollectionUtil.isEmpty(articleIds)) {
453453
return Collections.emptyMap();
454454
}
455-
List<ArticleAuthorRelationDto> authors = articleAuthorMapper.getArticleAuthor(articleIds);
455+
List<ArticleAuthorRelationDto> authors = articleAuthorMapper.getAuthorByArticleId(articleIds);
456456
if(CollectionUtil.isEmpty(authors)) {
457457
return Collections.emptyMap();
458458
}
@@ -466,11 +466,11 @@ private Map<Long, String> getArticleAuthor(Set<Long> articleIds) {
466466
return res;
467467
}
468468

469-
private Map<Long, String> getArticleCategory(Set<Long> articleIds) {
469+
private Map<Long, String> getCategoryByArticleId(Set<Long> articleIds) {
470470
if(CollectionUtil.isEmpty(articleIds)) {
471471
return Collections.emptyMap();
472472
}
473-
List<ArticleCategoryRelationDto> categories = articleCategoryMapper.getArticleCategory(articleIds);
473+
List<ArticleCategoryRelationDto> categories = articleCategoryMapper.getCategoryByArticleId(articleIds);
474474
if(CollectionUtil.isEmpty(categories)) {
475475
return Collections.emptyMap();
476476
}

blog-admin/src/main/java/com/hackyle/blog/admin/module/article/service/impl/CategoryServiceImpl.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package com.hackyle.blog.admin.module.article.service.impl;
22

3+
import cn.hutool.core.collection.CollectionUtil;
34
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
45
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
56
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
67
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
78
import com.github.pagehelper.PageHelper;
89
import com.github.pagehelper.PageInfo;
10+
import com.hackyle.blog.admin.module.article.mapper.ArticleCategoryMapper;
911
import com.hackyle.blog.admin.module.article.mapper.CategoryMapper;
12+
import com.hackyle.blog.admin.module.article.model.dto.ArticleCategoryRelationDto;
1013
import com.hackyle.blog.admin.module.article.model.dto.CategoryAddDto;
1114
import com.hackyle.blog.admin.module.article.model.dto.CategoryQueryDto;
1215
import com.hackyle.blog.admin.module.article.model.dto.CategoryUpdateDto;
16+
import com.hackyle.blog.admin.module.article.model.entity.ArticleCategoryRelationEntity;
1317
import com.hackyle.blog.admin.module.article.model.entity.CategoryEntity;
1418
import com.hackyle.blog.admin.module.article.model.vo.CategoryVo;
1519
import com.hackyle.blog.admin.module.article.service.CategoryService;
@@ -23,14 +27,19 @@
2327
import org.springframework.stereotype.Service;
2428

2529
import java.util.List;
30+
import java.util.Map;
31+
import java.util.Objects;
2632
import java.util.Set;
33+
import java.util.stream.Collectors;
2734

2835
@Slf4j
2936
@Service
3037
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity>
3138
implements CategoryService {
3239
@Autowired
3340
private CategoryMapper categoryMapper;
41+
@Autowired
42+
private ArticleCategoryMapper articleCategoryMapper;
3443

3544
@Override
3645
public boolean add(CategoryAddDto categoryAddDto) {
@@ -128,9 +137,18 @@ public PageInfo<CategoryVo> list(CategoryQueryDto queryDto) {
128137

129138
PageInfo<CategoryVo> pageInfo = PageHelperUtils.getPageInfo(categoryEntityList, CategoryVo.class);
130139

131-
for (CategoryVo categoryVo : pageInfo.getList()) {
132-
if(categoryVo.getUpdateTime() == null) {
133-
categoryVo.setUpdateTime(categoryVo.getCreateTime());
140+
if(CollectionUtil.isNotEmpty(categoryEntityList)) {
141+
Set<Long> categoryIds = categoryEntityList.stream().map(CategoryEntity::getId).filter(Objects::nonNull).collect(Collectors.toSet());
142+
//收集每个分类下的文章数
143+
List<ArticleCategoryRelationDto> articleCategory = articleCategoryMapper.getArticleByCategoryId(categoryIds);
144+
145+
Map<Long, List<ArticleCategoryRelationDto>> categoryMap = articleCategory.stream().collect(Collectors.groupingBy(ArticleCategoryRelationDto::getCategoryId));
146+
for (CategoryVo categoryVo : pageInfo.getList()) {
147+
List<ArticleCategoryRelationDto> articles = categoryMap.get(categoryVo.getId());
148+
categoryVo.setArticleCount(CollectionUtil.isEmpty(articles) ? 0 : articles.size());
149+
if(categoryVo.getUpdateTime() == null) {
150+
categoryVo.setUpdateTime(categoryVo.getCreateTime());
151+
}
134152
}
135153
}
136154

blog-admin/src/main/java/com/hackyle/blog/admin/module/article/service/impl/IdConfusionServiceImpl.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.hackyle.blog.admin.module.article.service.impl;
22

33
import com.hackyle.blog.admin.module.article.service.IdConfusionService;
4+
import com.hackyle.blog.common.exception.BizException;
5+
import com.hackyle.blog.common.util.CrcUtils;
46
import org.springframework.beans.factory.annotation.Value;
57
import org.springframework.stereotype.Service;
68

9+
import java.util.Objects;
10+
711
/**
812
* ID混淆
913
*/
@@ -19,6 +23,7 @@ public class IdConfusionServiceImpl implements IdConfusionService {
1923
* 先模62,从Base62字符数组中取得一个字符
2024
* 再除62,直到num小于0时停止
2125
* 得到一个唯一串
26+
* 计算CRC8校验码
2227
*/
2328
public String encode(long id) {
2429
if(id < 0) {
@@ -35,20 +40,35 @@ public String encode(long id) {
3540
//while (sb.length() < CODE_LENGTH) {
3641
// sb.append('0'); // 也可填充随机字符
3742
//}
38-
return sb.reverse().toString(); // 高位在前
43+
44+
//计算CRC8校验码
45+
String code = sb.reverse().toString();
46+
char crc8CheckSum = CrcUtils.crc8CheckSum(code);
47+
if(Character.isWhitespace(crc8CheckSum)) {
48+
throw new BizException("CRC8 Check Sum 计算失败");
49+
}
50+
51+
return code + crc8CheckSum; // 高位在前
3952
}
4053

4154
/**
55+
* 检查校验码
4256
* 依次遍历唯一串的每个字符c
4357
* 从字符数组中找到该个字符c所在的下标i
4458
* id = id*62+i
4559
* 最终解析出的值即为原始id
4660
*/
4761
public long decode(String code) {
62+
String body = code.substring(0, code.length() - 1);
63+
char crc8CheckSum = code.charAt(code.length() - 1);
64+
if(!Objects.equals(crc8CheckSum, CrcUtils.crc8CheckSum(body))) {
65+
throw new BizException("CRC8 Check Sum 校验失败");
66+
}
67+
4868
long id = 0;
4969
int baseSize = idConfusionBaseChar.length();
5070

51-
for (char cc : code.toCharArray()) {
71+
for (char cc : body.toCharArray()) {
5272
Integer mod = null;
5373
for (int j = 0; j < baseSize; j++) {
5474
if(cc == idConfusionBaseChar.charAt(j)) {

blog-admin/src/main/java/com/hackyle/blog/admin/module/auth/service/impl/AuthServiceImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cn.hutool.core.util.StrUtil;
44
import com.google.code.kaptcha.impl.DefaultKaptcha;
55
import com.hackyle.blog.admin.infrastructure.holder.AuthedContextHolder;
6+
import com.hackyle.blog.admin.infrastructure.redis.CacheKey;
67
import com.hackyle.blog.admin.module.auth.model.dto.LoginDto;
78
import com.hackyle.blog.admin.module.auth.model.dto.UserDetailsDto;
89
import com.hackyle.blog.admin.module.auth.model.vo.CaptchaVo;
@@ -50,8 +51,9 @@ public class AuthServiceImpl implements AuthService {
5051

5152
@Override
5253
public LoginVo login(LoginDto loginDto) {
53-
String cacheCode = (String) redisTemplate.opsForValue().get(loginDto.getUuid());
54-
redisTemplate.delete(loginDto.getUuid()); // 清除验证码
54+
String key = CacheKey.Auth.CAPTCHA+loginDto.getUuid();
55+
String cacheCode = (String) redisTemplate.opsForValue().get(key);
56+
redisTemplate.delete(key); // 清除验证码
5557
if (StringUtils.isBlank(cacheCode)) {
5658
throw new IllegalArgumentException("验证码不存在或已过期");
5759
}
@@ -109,7 +111,7 @@ public CaptchaVo captcha() {
109111
String uuid = UUID.randomUUID().toString();
110112
captchaVo.setUuid(uuid);
111113
captchaVo.setCode("data:image/png;base64," + base64Code);
112-
redisTemplate.opsForValue().set(uuid, kaptchaText, 60L, TimeUnit.SECONDS);
114+
redisTemplate.opsForValue().set(CacheKey.Auth.CAPTCHA+uuid, kaptchaText, 60L, TimeUnit.SECONDS);
113115

114116
return captchaVo;
115117
}

0 commit comments

Comments
 (0)