feat(note): 增加私密笔记功能
- 在 MarkdownFile 表中添加 is_private 字段,用于标记笔记是否私密- 修改 MarkdownController 中的 getMarkdownContent 方法,增加对私密笔记的处理逻辑 - 更新 MarkdownFileService 接口和 MarkdownFileServiceImpl 实现类,支持根据认证状态获取笔记内容 - 优化未认证用户访问私密笔记时的处理,只显示标题不显示内容
This commit is contained in:
@@ -10,6 +10,9 @@ import io.swagger.v3.oas.annotations.Parameter;
|
|||||||
import io.swagger.v3.oas.annotations.Parameters;
|
import io.swagger.v3.oas.annotations.Parameters;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@@ -36,8 +39,15 @@ public class MarkdownController {
|
|||||||
})
|
})
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public R<String> getMarkdownContent(@PathVariable Long id) {
|
public R<String> getMarkdownContent(@PathVariable Long id) {
|
||||||
MarkdownFile file = markdownFileService.getMarkdownById(id);
|
// 获取当前认证状态
|
||||||
|
boolean isAuthenticated = isUserAuthenticated();
|
||||||
|
|
||||||
|
MarkdownFile file = markdownFileService.getMarkdownById(id, isAuthenticated);
|
||||||
if (ObjectUtil.isNotNull(file)) {
|
if (ObjectUtil.isNotNull(file)) {
|
||||||
|
// 如果是私密笔记且用户未认证,只返回标题
|
||||||
|
if (file.getIsPrivate() != null && file.getIsPrivate() == 1 && !isAuthenticated) {
|
||||||
|
return R.success(""); // 返回空内容,只显示标题
|
||||||
|
}
|
||||||
return R.success(file.getContent());
|
return R.success(file.getContent());
|
||||||
}
|
}
|
||||||
return R.fail();
|
return R.fail();
|
||||||
@@ -102,4 +112,20 @@ public class MarkdownController {
|
|||||||
List<MarkdownFileVO> files = markdownFileService.getRecentFiles(12);
|
List<MarkdownFileVO> files = markdownFileService.getRecentFiles(12);
|
||||||
return R.success(files);
|
return R.success(files);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查用户是否已认证
|
||||||
|
* @return 是否已认证
|
||||||
|
*/
|
||||||
|
private boolean isUserAuthenticated() {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
if (authentication == null || !authentication.isAuthenticated()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Object principal = authentication.getPrincipal();
|
||||||
|
if (principal instanceof UserDetails) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,4 +45,7 @@ public class MarkdownFile implements Serializable {
|
|||||||
|
|
||||||
@Schema(description = "删除人ID", implementation = Long.class)
|
@Schema(description = "删除人ID", implementation = Long.class)
|
||||||
private Long deletedBy;
|
private Long deletedBy;
|
||||||
|
|
||||||
|
@Schema(description = "是否私密 0-公开 1-私密", implementation = Integer.class)
|
||||||
|
private Integer isPrivate;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,9 +17,10 @@ public interface MarkdownFileService extends IService<MarkdownFile> {
|
|||||||
/**
|
/**
|
||||||
* 根据ID获取Markdown文件
|
* 根据ID获取Markdown文件
|
||||||
* @param id 文件ID
|
* @param id 文件ID
|
||||||
|
* @param isAuthenticated 是否已认证
|
||||||
* @return Markdown文件对象
|
* @return Markdown文件对象
|
||||||
*/
|
*/
|
||||||
MarkdownFile getMarkdownById(Long id);
|
MarkdownFile getMarkdownById(Long id, boolean isAuthenticated);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -59,8 +59,14 @@ public class MarkdownFileServiceImpl
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MarkdownFile getMarkdownById(Long id) {
|
public MarkdownFile getMarkdownById(Long id, boolean isAuthenticated) {
|
||||||
return this.getById(id);
|
MarkdownFile file = this.getById(id);
|
||||||
|
if (file != null && !isAuthenticated && file.getIsPrivate() != null && file.getIsPrivate() == 1) {
|
||||||
|
// 对于未认证用户,私密笔记只返回标题,内容置空
|
||||||
|
file.setContent("");
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -22,9 +22,10 @@ CREATE TABLE IF NOT EXISTS markdown_file (
|
|||||||
content TEXT NOT NULL,
|
content TEXT NOT NULL,
|
||||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
is_deleted INTEGER DEFAULT 0,
|
is_deleted INTEGER DEFAULT 0,
|
||||||
deleted_at DATETIME,
|
deleted_at DATETIME,
|
||||||
deleted_by INTEGER
|
deleted_by INTEGER,
|
||||||
|
is_private INTEGER DEFAULT 0 -- 新增字段:0=公开,1=私密
|
||||||
);
|
);
|
||||||
|
|
||||||
-- 图片表
|
-- 图片表
|
||||||
|
|||||||
Reference in New Issue
Block a user