Compare commits
3 Commits
7380f783ee
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 7db42c6c30 | |||
| 304c458436 | |||
| b7afe9496a |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -65,4 +65,5 @@ Desktop.ini
|
||||
# --------------------------------------------------------------------------
|
||||
application-prod.yml
|
||||
application-dev.yml
|
||||
.env
|
||||
.env
|
||||
/.idea/.cache/.Apifox_Helper/.toolWindow.db
|
||||
|
||||
@@ -38,8 +38,8 @@ COMMENT ON COLUMN t_user.is_deleted IS '逻辑删除:FALSE:未删除 TRUE:
|
||||
CREATE TABLE t_user_role
|
||||
(
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
username VARCHAR(60) NOT NULL,
|
||||
role_name VARCHAR(60) NOT NULL, -- 重命名为 role_name 避免关键字冲突
|
||||
username VARCHAR(60) NOT NULL,
|
||||
role_name VARCHAR(60) NOT NULL, -- 重命名为 role_name 避免关键字冲突
|
||||
create_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
@@ -63,7 +63,7 @@ CREATE TABLE t_category
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
|
||||
-- 分类名称:VARCHAR(60) NOT NULL DEFAULT '',同时是 UNIQUE 约束
|
||||
"name" VARCHAR(60) NOT NULL DEFAULT '',
|
||||
"name" VARCHAR(60) NOT NULL DEFAULT '',
|
||||
|
||||
-- 创建时间
|
||||
create_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
|
||||
@@ -72,7 +72,7 @@ CREATE TABLE t_category
|
||||
update_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
|
||||
|
||||
-- 逻辑删除标志位:tinyint(2) NOT NULL DEFAULT '0',改为 BOOLEAN
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
|
||||
-- UNIQUE KEY uk_name (`name`)
|
||||
CONSTRAINT uk_name UNIQUE ("name")
|
||||
@@ -95,4 +95,94 @@ CREATE TRIGGER set_t_category_update_time
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION set_update_time();
|
||||
-- ====================================================================================================================
|
||||
-- ====================================================================================================================
|
||||
-- ====================================================================================================================
|
||||
-- ====================================================================================================================
|
||||
-- 1. 创建表结构
|
||||
CREATE TABLE t_tag
|
||||
(
|
||||
-- id: 使用 BIG SERIAL,自动创建序列,性能优异
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
|
||||
-- name: 保持 VARCHAR(60),但在 PG 中 TEXT 和 VARCHAR 性能一样,
|
||||
-- 这里为了保留原表 "60字符限制" 的业务逻辑,继续使用 VARCHAR(60)
|
||||
name VARCHAR(60) NOT NULL DEFAULT '',
|
||||
|
||||
-- create_time: 使用带时区的时间戳,更标准严谨
|
||||
create_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
-- update_time: 同上
|
||||
update_time TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
-- is_deleted: 使用原生 BOOLEAN 类型,存储效率高且语义明确
|
||||
is_deleted BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
|
||||
-- 约束:显式命名约束,方便后续维护(如报错时能看到具体约束名)
|
||||
CONSTRAINT uk_tag_name UNIQUE (name)
|
||||
);
|
||||
|
||||
-- 2. 创建普通索引
|
||||
-- 对应 MySQL 的 KEY `idx_create_time`
|
||||
CREATE INDEX idx_tag_create_time ON t_tag (create_time);
|
||||
|
||||
-- 3. 添加注释 (PostgreSQL 标准方式)
|
||||
COMMENT ON TABLE t_tag IS '文章标签表';
|
||||
COMMENT ON COLUMN t_tag.id IS '标签id';
|
||||
COMMENT ON COLUMN t_tag.name IS '标签名称';
|
||||
COMMENT ON COLUMN t_tag.create_time IS '创建时间';
|
||||
COMMENT ON COLUMN t_tag.update_time IS '最后一次更新时间';
|
||||
COMMENT ON COLUMN t_tag.is_deleted IS '逻辑删除标志位:FALSE:未删除 TRUE:已删除';
|
||||
|
||||
-- 4. 应用自动更新时间戳触发器 (体现 PostgreSQL 强大的过程语言优势)
|
||||
-- 前提:您之前已经执行过 CREATE FUNCTION set_update_time() ...
|
||||
CREATE TRIGGER set_t_tag_update_time
|
||||
BEFORE UPDATE
|
||||
ON t_tag
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION set_update_time();
|
||||
-- ====================================================================================================================
|
||||
-- ====================================================================================================================
|
||||
-- ====================================================================================================================
|
||||
-- ====================================================================================================================
|
||||
CREATE TABLE t_blog_settings
|
||||
(
|
||||
-- id: 使用 BIGSERIAL 自动管理序列
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
|
||||
-- logo: 图片路径可能很长,使用 TEXT 替代 VARCHAR(120),无性能损耗
|
||||
logo TEXT NOT NULL DEFAULT '',
|
||||
|
||||
-- name: 博客名称通常较短,保留 VARCHAR 限制也是一种合理的业务约束
|
||||
name VARCHAR(60) NOT NULL DEFAULT '',
|
||||
|
||||
-- author: 作者名同上
|
||||
author VARCHAR(20) NOT NULL DEFAULT '',
|
||||
|
||||
-- introduction: 介绍语可能会变长,使用 TEXT 更灵活
|
||||
introduction TEXT NOT NULL DEFAULT '',
|
||||
|
||||
-- avatar: 头像路径,使用 TEXT
|
||||
avatar TEXT NOT NULL DEFAULT '',
|
||||
|
||||
-- 下面的主页链接:原 MySQL 定义 varchar(60) 风险很高,
|
||||
-- 现在的 URL 很容易超过 60 字符,PG 使用 TEXT 完美解决
|
||||
github_homepage TEXT NOT NULL DEFAULT '',
|
||||
csdn_homepage TEXT NOT NULL DEFAULT '',
|
||||
gitee_homepage TEXT NOT NULL DEFAULT '',
|
||||
zhihu_homepage TEXT NOT NULL DEFAULT ''
|
||||
);
|
||||
|
||||
-- 添加注释
|
||||
COMMENT ON TABLE t_blog_settings IS '博客设置表';
|
||||
COMMENT ON COLUMN t_blog_settings.id IS 'id';
|
||||
COMMENT ON COLUMN t_blog_settings.logo IS '博客Logo';
|
||||
COMMENT ON COLUMN t_blog_settings.name IS '博客名称';
|
||||
COMMENT ON COLUMN t_blog_settings.author IS '作者名';
|
||||
COMMENT ON COLUMN t_blog_settings.introduction IS '介绍语';
|
||||
COMMENT ON COLUMN t_blog_settings.avatar IS '作者头像';
|
||||
COMMENT ON COLUMN t_blog_settings.github_homepage IS 'GitHub 主页访问地址';
|
||||
COMMENT ON COLUMN t_blog_settings.csdn_homepage IS 'CSDN 主页访问地址';
|
||||
COMMENT ON COLUMN t_blog_settings.gitee_homepage IS 'Gitee 主页访问地址';
|
||||
COMMENT ON COLUMN t_blog_settings.zhihu_homepage IS '知乎主页访问地址';
|
||||
-- ====================================================================================================================
|
||||
-- ====================================================================================================================
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.hanserwei.admin.controller;
|
||||
|
||||
import com.hanserwei.admin.model.vo.setting.FindBlogSettingsRspVO;
|
||||
import com.hanserwei.admin.model.vo.setting.UpdateBlogSettingsReqVO;
|
||||
import com.hanserwei.admin.service.AdminBlogSettingsService;
|
||||
import com.hanserwei.common.aspect.ApiOperationLog;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 管理端博客设置控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin/blog/settings")
|
||||
public class AdminBlogSettingsController {
|
||||
|
||||
@Resource
|
||||
private AdminBlogSettingsService blogSettingsService;
|
||||
|
||||
/**
|
||||
* 博客基础信息修改
|
||||
*/
|
||||
@PostMapping("/update")
|
||||
@ApiOperationLog(description = "博客基础信息修改")
|
||||
public Response<?> updateBlogSettings(@RequestBody @Validated UpdateBlogSettingsReqVO updateBlogSettingsReqVO) {
|
||||
return blogSettingsService.updateBlogSettings(updateBlogSettingsReqVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取博客设置详情
|
||||
*/
|
||||
@PostMapping("/detail")
|
||||
@ApiOperationLog(description = "获取博客设置详情")
|
||||
public Response<FindBlogSettingsRspVO> findDetail() {
|
||||
return blogSettingsService.findDetail();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.hanserwei.admin.controller;
|
||||
|
||||
import com.hanserwei.admin.model.vo.AddCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.DeleteCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.FindCategoryPageListReqVO;
|
||||
import com.hanserwei.admin.model.vo.FindCategoryPageListRspVO;
|
||||
import com.hanserwei.admin.model.vo.category.AddCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.DeleteCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListRspVO;
|
||||
import com.hanserwei.admin.service.AdminCategoryService;
|
||||
import com.hanserwei.common.aspect.ApiOperationLog;
|
||||
import com.hanserwei.common.model.vo.SelectRspVO;
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.hanserwei.admin.controller;
|
||||
|
||||
import com.hanserwei.admin.model.vo.file.UploadFileRspVO;
|
||||
import com.hanserwei.admin.service.AdminFileService;
|
||||
import com.hanserwei.common.aspect.ApiOperationLog;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* 管理端文件控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin")
|
||||
public class AdminFileController {
|
||||
|
||||
@Resource
|
||||
private AdminFileService fileService;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*/
|
||||
@PostMapping("/file/upload")
|
||||
@ApiOperationLog(description = "文件上传")
|
||||
public Response<UploadFileRspVO> uploadFile(@RequestParam("file") MultipartFile file) {
|
||||
return fileService.uploadFile(file);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.hanserwei.admin.controller;
|
||||
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListRspVO;
|
||||
import com.hanserwei.admin.model.vo.tag.*;
|
||||
import com.hanserwei.admin.service.AdminTagService;
|
||||
import com.hanserwei.common.aspect.ApiOperationLog;
|
||||
import com.hanserwei.common.model.vo.SelectRspVO;
|
||||
import com.hanserwei.common.utils.PageResponse;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 管理端标签控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin")
|
||||
public class AdminTagController {
|
||||
|
||||
@Resource
|
||||
private AdminTagService adminTagService;
|
||||
|
||||
/**
|
||||
* 添加标签
|
||||
*/
|
||||
@PostMapping("/tag/add")
|
||||
@ApiOperationLog(description = "添加标签")
|
||||
public Response<?> addTag(@RequestBody @Validated AddTagReqVO addTagReqVO) {
|
||||
return adminTagService.addTag(addTagReqVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 标签分页数据获取
|
||||
*/
|
||||
@PostMapping("/tag/list")
|
||||
@ApiOperationLog(description = "标签分页数据获取")
|
||||
public PageResponse<FindTagPageListRspVO> findTagList(@RequestBody @Validated FindTagPageListReqVO findTagPageListReqVO) {
|
||||
return adminTagService.findTagList(findTagPageListReqVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除标签
|
||||
*/
|
||||
@PostMapping("/tag/delete")
|
||||
@ApiOperationLog(description = "删除标签")
|
||||
public Response<?> deleteTag(@RequestBody @Validated DeleteTagReqVO deleteTagReqVO) {
|
||||
return adminTagService.deleteTag(deleteTagReqVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 标签模糊查询
|
||||
*/
|
||||
@PostMapping("/tag/search")
|
||||
@ApiOperationLog(description = "标签模糊查询")
|
||||
public Response<List<SelectRspVO>> searchTag(@RequestBody @Validated SearchTagReqVO searchTagReqVO) {
|
||||
return adminTagService.searchTag(searchTagReqVO);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.hanserwei.admin.controller;
|
||||
|
||||
import com.hanserwei.admin.model.vo.FindUserInfoRspVO;
|
||||
import com.hanserwei.admin.model.vo.UpdateAdminUserPasswordReqVO;
|
||||
import com.hanserwei.admin.model.vo.user.FindUserInfoRspVO;
|
||||
import com.hanserwei.admin.model.vo.user.UpdateAdminUserPasswordReqVO;
|
||||
import com.hanserwei.admin.service.AdminUserService;
|
||||
import com.hanserwei.common.aspect.ApiOperationLog;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.hanserwei.admin.model.vo;
|
||||
package com.hanserwei.admin.model.vo.category;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.hanserwei.admin.model.vo;
|
||||
package com.hanserwei.admin.model.vo.category;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.hanserwei.admin.model.vo;
|
||||
package com.hanserwei.admin.model.vo.category;
|
||||
|
||||
import com.hanserwei.common.model.BasePageQuery;
|
||||
import lombok.*;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.hanserwei.admin.model.vo;
|
||||
package com.hanserwei.admin.model.vo.category;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.hanserwei.admin.model.vo.file;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class UploadFileRspVO {
|
||||
|
||||
/**
|
||||
* 文件的访问链接
|
||||
*/
|
||||
private String url;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.hanserwei.admin.model.vo.setting;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
|
||||
/**
|
||||
* 查询博客设置响应 VO
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class FindBlogSettingsRspVO {
|
||||
|
||||
/**
|
||||
* 博客 Logo
|
||||
*/
|
||||
private String logo;
|
||||
|
||||
/**
|
||||
* 博客名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 作者名称
|
||||
*/
|
||||
private String author;
|
||||
|
||||
/**
|
||||
* 博客介绍
|
||||
*/
|
||||
private String introduction;
|
||||
|
||||
/**
|
||||
* 作者头像
|
||||
*/
|
||||
private String avatar;
|
||||
|
||||
/**
|
||||
* GitHub 主页地址
|
||||
*/
|
||||
private String githubHomepage;
|
||||
|
||||
/**
|
||||
* CSDN 主页地址
|
||||
*/
|
||||
private String csdnHomepage;
|
||||
|
||||
/**
|
||||
* Gitee 主页地址
|
||||
*/
|
||||
private String giteeHomepage;
|
||||
|
||||
/**
|
||||
* 知乎主页地址
|
||||
*/
|
||||
private String zhihuHomepage;
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.hanserwei.admin.model.vo.setting;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
|
||||
/**
|
||||
* 更新博客设置请求 VO
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class UpdateBlogSettingsReqVO {
|
||||
|
||||
/**
|
||||
* 博客 LOGO
|
||||
*/
|
||||
@NotBlank(message = "博客 LOGO 不能为空")
|
||||
private String logo;
|
||||
|
||||
/**
|
||||
* 博客名称
|
||||
*/
|
||||
@NotBlank(message = "博客名称不能为空")
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 博客作者
|
||||
*/
|
||||
@NotBlank(message = "博客作者不能为空")
|
||||
private String author;
|
||||
|
||||
/**
|
||||
* 博客介绍语
|
||||
*/
|
||||
@NotBlank(message = "博客介绍语不能为空")
|
||||
private String introduction;
|
||||
|
||||
/**
|
||||
* 博客头像
|
||||
*/
|
||||
@NotBlank(message = "博客头像不能为空")
|
||||
private String avatar;
|
||||
|
||||
/**
|
||||
* GitHub 主页
|
||||
*/
|
||||
private String githubHomepage;
|
||||
|
||||
/**
|
||||
* CSDN 主页
|
||||
*/
|
||||
private String csdnHomepage;
|
||||
|
||||
/**
|
||||
* Gitee 主页
|
||||
*/
|
||||
private String giteeHomepage;
|
||||
|
||||
/**
|
||||
* 知乎主页
|
||||
*/
|
||||
private String zhihuHomepage;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.hanserwei.admin.model.vo.tag;
|
||||
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class AddTagReqVO {
|
||||
|
||||
/**
|
||||
* 标签集合
|
||||
*/
|
||||
@NotEmpty(message = "标签集合 不能为空")
|
||||
private List<String> tags;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.hanserwei.admin.model.vo.tag;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class DeleteTagReqVO {
|
||||
|
||||
/**
|
||||
* 标签 ID
|
||||
*/
|
||||
@NotNull(message = "标签 ID 不能为空")
|
||||
private Long id;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.hanserwei.admin.model.vo.tag;
|
||||
|
||||
import com.hanserwei.common.model.BasePageQuery;
|
||||
import lombok.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class FindTagPageListReqVO extends BasePageQuery {
|
||||
|
||||
/**
|
||||
* 标签名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 创建的起始日期
|
||||
*/
|
||||
private LocalDate startDate;
|
||||
|
||||
/**
|
||||
* 创建的结束日期
|
||||
*/
|
||||
private LocalDate endDate;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.hanserwei.admin.model.vo.tag;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class FindTagPageListRspVO {
|
||||
/**
|
||||
* 标签 ID
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 标签名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private LocalDateTime createTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.hanserwei.admin.model.vo.tag;
|
||||
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class SearchTagReqVO {
|
||||
|
||||
@NotEmpty(message = "标签查询关键词不能为空!")
|
||||
private String key;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.hanserwei.admin.model.vo;
|
||||
package com.hanserwei.admin.model.vo.user;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.hanserwei.admin.model.vo;
|
||||
package com.hanserwei.admin.model.vo.user;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.hanserwei.admin.service;
|
||||
|
||||
import com.hanserwei.admin.model.vo.setting.FindBlogSettingsRspVO;
|
||||
import com.hanserwei.admin.model.vo.setting.UpdateBlogSettingsReqVO;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
|
||||
public interface AdminBlogSettingsService {
|
||||
/**
|
||||
* 更新博客设置信息
|
||||
*
|
||||
* @param updateBlogSettingsReqVO 博客设置信息
|
||||
* @return 响应
|
||||
*/
|
||||
Response<?> updateBlogSettings(UpdateBlogSettingsReqVO updateBlogSettingsReqVO);
|
||||
|
||||
/**
|
||||
* 获取博客设置详情
|
||||
*
|
||||
* @return 博客设置详情
|
||||
*/
|
||||
Response<FindBlogSettingsRspVO> findDetail();
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.hanserwei.admin.service;
|
||||
|
||||
import com.hanserwei.admin.model.vo.AddCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.DeleteCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.FindCategoryPageListReqVO;
|
||||
import com.hanserwei.admin.model.vo.FindCategoryPageListRspVO;
|
||||
import com.hanserwei.admin.model.vo.category.AddCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.DeleteCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListRspVO;
|
||||
import com.hanserwei.common.model.vo.SelectRspVO;
|
||||
import com.hanserwei.common.utils.PageResponse;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.hanserwei.admin.service;
|
||||
|
||||
import com.hanserwei.admin.model.vo.file.UploadFileRspVO;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public interface AdminFileService {
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 访问地址
|
||||
*/
|
||||
Response<UploadFileRspVO> uploadFile(MultipartFile file);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.hanserwei.admin.service;
|
||||
|
||||
import com.hanserwei.admin.model.vo.tag.*;
|
||||
import com.hanserwei.common.model.vo.SelectRspVO;
|
||||
import com.hanserwei.common.utils.PageResponse;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface AdminTagService {
|
||||
/**
|
||||
* 添加标签
|
||||
*
|
||||
* @param addTagReqVO 添加标签请求参数
|
||||
* @return 响应结果
|
||||
*/
|
||||
Response<?> addTag(AddTagReqVO addTagReqVO);
|
||||
|
||||
/**
|
||||
* 标签分页数据获取
|
||||
*
|
||||
* @param findTagPageListReqVO 标签分页数据获取请求参数
|
||||
* @return 响应结果
|
||||
*/
|
||||
PageResponse<FindTagPageListRspVO> findTagList(FindTagPageListReqVO findTagPageListReqVO);
|
||||
|
||||
/**
|
||||
* 删除标签
|
||||
*
|
||||
* @param deleteTagReqVO 删除标签请求参数
|
||||
* @return 响应结果
|
||||
*/
|
||||
Response<?> deleteTag(DeleteTagReqVO deleteTagReqVO);
|
||||
|
||||
/**
|
||||
* 标签下拉列表数据获取
|
||||
*
|
||||
* @param searchTagReqVO 搜索标签请求参数
|
||||
* @return 响应结果
|
||||
*/
|
||||
Response<List<SelectRspVO>> searchTag(SearchTagReqVO searchTagReqVO);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.hanserwei.admin.service;
|
||||
|
||||
import com.hanserwei.admin.model.vo.FindUserInfoRspVO;
|
||||
import com.hanserwei.admin.model.vo.UpdateAdminUserPasswordReqVO;
|
||||
import com.hanserwei.admin.model.vo.user.FindUserInfoRspVO;
|
||||
import com.hanserwei.admin.model.vo.user.UpdateAdminUserPasswordReqVO;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
|
||||
public interface AdminUserService {
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.hanserwei.admin.service.impl;
|
||||
|
||||
import com.hanserwei.admin.model.vo.setting.FindBlogSettingsRspVO;
|
||||
import com.hanserwei.admin.model.vo.setting.UpdateBlogSettingsReqVO;
|
||||
import com.hanserwei.admin.service.AdminBlogSettingsService;
|
||||
import com.hanserwei.common.domain.dataobject.BlogSettings;
|
||||
import com.hanserwei.common.domain.repository.BlogSettingsRepository;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
@Service
|
||||
public class AdminBlogSettingsServiceImpl implements AdminBlogSettingsService {
|
||||
|
||||
@Resource
|
||||
private BlogSettingsRepository blogSettingsRepository;
|
||||
|
||||
|
||||
@Override
|
||||
public Response<?> updateBlogSettings(UpdateBlogSettingsReqVO updateBlogSettingsReqVO) {
|
||||
// 保存或更新博客设置
|
||||
blogSettingsRepository.findById(1L)
|
||||
.ifPresentOrElse(existingSettings -> {
|
||||
// 如果存在,则更新现有记录
|
||||
BeanUtils.copyProperties(updateBlogSettingsReqVO, existingSettings);
|
||||
blogSettingsRepository.saveAndFlush(existingSettings);
|
||||
}, () -> {
|
||||
// 如果不存在,则创建新记录
|
||||
BlogSettings blogSettings = new BlogSettings();
|
||||
BeanUtils.copyProperties(updateBlogSettingsReqVO, blogSettings);
|
||||
blogSettings.setId(1L);
|
||||
blogSettingsRepository.saveAndFlush(blogSettings);
|
||||
});
|
||||
return Response.success();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Response<FindBlogSettingsRspVO> findDetail() {
|
||||
return blogSettingsRepository.findById(1L)
|
||||
.map(e -> {
|
||||
FindBlogSettingsRspVO findBlogSettingsRspVO = new FindBlogSettingsRspVO();
|
||||
BeanUtils.copyProperties(e, findBlogSettingsRspVO);
|
||||
return Response.success(findBlogSettingsRspVO);
|
||||
})
|
||||
.orElse(Response.success(null));
|
||||
}
|
||||
}
|
||||
@@ -1,34 +1,25 @@
|
||||
package com.hanserwei.admin.service.impl;
|
||||
|
||||
import com.hanserwei.admin.model.vo.AddCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.DeleteCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.FindCategoryPageListReqVO;
|
||||
import com.hanserwei.admin.model.vo.FindCategoryPageListRspVO;
|
||||
import com.hanserwei.admin.model.vo.category.AddCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.DeleteCategoryReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListReqVO;
|
||||
import com.hanserwei.admin.model.vo.category.FindCategoryPageListRspVO;
|
||||
import com.hanserwei.admin.service.AdminCategoryService;
|
||||
import com.hanserwei.common.domain.dataobject.Category;
|
||||
import com.hanserwei.common.domain.repository.CategoryRepository;
|
||||
import com.hanserwei.common.enums.ResponseCodeEnum;
|
||||
import com.hanserwei.common.exception.BizException;
|
||||
import com.hanserwei.common.model.vo.SelectRspVO;
|
||||
import com.hanserwei.common.utils.PageHelper;
|
||||
import com.hanserwei.common.utils.PageResponse;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
import io.jsonwebtoken.lang.Strings;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class AdminCategoryServiceImpl implements AdminCategoryService {
|
||||
@@ -53,53 +44,29 @@ public class AdminCategoryServiceImpl implements AdminCategoryService {
|
||||
|
||||
@Override
|
||||
public PageResponse<FindCategoryPageListRspVO> findCategoryList(FindCategoryPageListReqVO findCategoryPageListReqVO) {
|
||||
Long current = findCategoryPageListReqVO.getCurrent();
|
||||
Long size = findCategoryPageListReqVO.getSize();
|
||||
|
||||
Pageable pageable = PageRequest.of(current.intValue() - 1,
|
||||
size.intValue(),
|
||||
Sort.by(Sort.Direction.DESC, "createTime"));
|
||||
|
||||
// 构建查询条件
|
||||
Specification<Category> specification = (root, query, criteriaBuilder) -> {
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
String name = findCategoryPageListReqVO.getName();
|
||||
if (Strings.hasText(name)) {
|
||||
predicates.add(
|
||||
criteriaBuilder.like(root.get("name"), "%" + name.trim() + "%")
|
||||
);
|
||||
}
|
||||
if (Objects.nonNull(findCategoryPageListReqVO.getStartDate())){
|
||||
predicates.add(
|
||||
criteriaBuilder.greaterThanOrEqualTo(root.get("createTime"), findCategoryPageListReqVO.getStartDate())
|
||||
);
|
||||
}
|
||||
if (Objects.nonNull(findCategoryPageListReqVO.getEndDate())) {
|
||||
predicates.add(
|
||||
criteriaBuilder.lessThan(root.get("createTime"), findCategoryPageListReqVO.getEndDate().plusDays(1).atStartOfDay())
|
||||
);
|
||||
}
|
||||
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
|
||||
};
|
||||
|
||||
Page<Category> categoryDOPage = categoryRepository.findAll(specification, pageable);
|
||||
|
||||
List<FindCategoryPageListRspVO> vos = categoryDOPage.getContent().stream()
|
||||
.map(category -> FindCategoryPageListRspVO.builder()
|
||||
return PageHelper.findPageList(
|
||||
categoryRepository,
|
||||
findCategoryPageListReqVO,
|
||||
findCategoryPageListReqVO.getName(),
|
||||
findCategoryPageListReqVO.getStartDate(),
|
||||
findCategoryPageListReqVO.getEndDate(),
|
||||
category -> FindCategoryPageListRspVO.builder()
|
||||
.id(category.getId())
|
||||
.name(category.getName())
|
||||
.createTime(LocalDateTime.ofInstant(category.getCreateTime(), ZoneId.systemDefault()))
|
||||
.build())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return PageResponse.success(categoryDOPage, vos);
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response<?> deleteCategory(DeleteCategoryReqVO deleteCategoryReqVO) {
|
||||
Long id = deleteCategoryReqVO.getId();
|
||||
categoryRepository.deleteById(id);
|
||||
return Response.success();
|
||||
return categoryRepository.findById(deleteCategoryReqVO.getId())
|
||||
.map(tag -> {
|
||||
tag.setIsDeleted(true);
|
||||
categoryRepository.save(tag);
|
||||
return Response.success();
|
||||
})
|
||||
.orElse(Response.fail(ResponseCodeEnum.CATEGORY_NOT_EXIST));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -107,7 +74,7 @@ public class AdminCategoryServiceImpl implements AdminCategoryService {
|
||||
List<Category> categoryList = categoryRepository.findAll();
|
||||
// DO 转 VO
|
||||
List<SelectRspVO> selectRspVOS = null;
|
||||
if (!CollectionUtils.isEmpty(categoryList)){
|
||||
if (!CollectionUtils.isEmpty(categoryList)) {
|
||||
selectRspVOS = categoryList.stream()
|
||||
.map(category -> SelectRspVO.builder()
|
||||
.label(category.getName())
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.hanserwei.admin.service.impl;
|
||||
|
||||
import com.hanserwei.admin.model.vo.file.UploadFileRspVO;
|
||||
import com.hanserwei.admin.service.AdminFileService;
|
||||
import com.hanserwei.admin.utils.RustfsUtils;
|
||||
import com.hanserwei.common.enums.ResponseCodeEnum;
|
||||
import com.hanserwei.common.exception.BizException;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class AdminFileServiceImpl implements AdminFileService {
|
||||
|
||||
@Resource
|
||||
private RustfsUtils rustfsUtils;
|
||||
|
||||
@Override
|
||||
public Response<UploadFileRspVO> uploadFile(MultipartFile file) {
|
||||
try {
|
||||
// 上传文件
|
||||
String url = rustfsUtils.uploadFile(file);
|
||||
return Response.success(UploadFileRspVO.builder().url(url).build());
|
||||
} catch (Exception e) {
|
||||
log.error("==> 上传文件异常:{} ...", e.getMessage());
|
||||
throw new BizException(ResponseCodeEnum.FILE_UPLOAD_FAILED);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.hanserwei.admin.service.impl;
|
||||
|
||||
import com.hanserwei.admin.model.vo.tag.*;
|
||||
import com.hanserwei.admin.service.AdminTagService;
|
||||
import com.hanserwei.common.domain.dataobject.Tag;
|
||||
import com.hanserwei.common.domain.repository.TagRepository;
|
||||
import com.hanserwei.common.enums.ResponseCodeEnum;
|
||||
import com.hanserwei.common.model.vo.SelectRspVO;
|
||||
import com.hanserwei.common.utils.PageHelper;
|
||||
import com.hanserwei.common.utils.PageResponse;
|
||||
import com.hanserwei.common.utils.Response;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class AdminTagServiceImpl implements AdminTagService {
|
||||
|
||||
@Resource
|
||||
private TagRepository tagRepository;
|
||||
|
||||
|
||||
/**
|
||||
* 添加标签
|
||||
*
|
||||
* @param addTagReqVO 添加标签请求对象
|
||||
* @return 响应结果
|
||||
*/
|
||||
@Override
|
||||
public Response<?> addTag(AddTagReqVO addTagReqVO) {
|
||||
// 获取标签列表
|
||||
List<String> tagList = addTagReqVO.getTags();
|
||||
|
||||
// 对标签进行清洗:去除空格、过滤空字符串、去重
|
||||
List<String> names = tagList.stream()
|
||||
.map(String::trim) // 去除首尾空格
|
||||
.filter(s -> !s.isEmpty()) // 过滤空字符串
|
||||
.distinct() // 去重
|
||||
.toList();
|
||||
|
||||
// 查询数据库中已存在的标签名称
|
||||
List<String> exists = tagRepository.findByNameIn(names).stream()
|
||||
.map(Tag::getName)
|
||||
.toList();
|
||||
|
||||
// 筛选出需要新建的标签,并构建标签对象
|
||||
List<Tag> toCreate = names.stream()
|
||||
.filter(n -> !exists.contains(n)) // 过滤掉已存在的标签
|
||||
.map(n -> Tag.builder().name(n).build()) // 构建标签对象
|
||||
.toList();
|
||||
|
||||
// 批量保存新标签到数据库
|
||||
if (!toCreate.isEmpty()) {
|
||||
tagRepository.saveAllAndFlush(toCreate);
|
||||
}
|
||||
|
||||
return Response.success();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResponse<FindTagPageListRspVO> findTagList(FindTagPageListReqVO findTagPageListReqVO) {
|
||||
return PageHelper.findPageList(
|
||||
tagRepository,
|
||||
findTagPageListReqVO,
|
||||
findTagPageListReqVO.getName(),
|
||||
findTagPageListReqVO.getStartDate(),
|
||||
findTagPageListReqVO.getEndDate(),
|
||||
tag -> FindTagPageListRspVO.builder()
|
||||
.id(tag.getId())
|
||||
.name(tag.getName())
|
||||
.createTime(LocalDateTime.ofInstant(tag.getCreateTime(), ZoneId.systemDefault()))
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response<?> deleteTag(DeleteTagReqVO deleteTagReqVO) {
|
||||
|
||||
return tagRepository.findById(deleteTagReqVO.getId())
|
||||
.map(tag -> {
|
||||
tag.setIsDeleted(true);
|
||||
tagRepository.save(tag);
|
||||
return Response.success();
|
||||
})
|
||||
.orElse(Response.fail(ResponseCodeEnum.TAG_NOT_EXIST));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response<List<SelectRspVO>> searchTag(SearchTagReqVO searchTagReqVO) {
|
||||
// 使用模糊查询获取标签列表
|
||||
List<Tag> tags = tagRepository.findByNameContaining(searchTagReqVO.getKey());
|
||||
|
||||
// 将标签转换为下拉列表格式
|
||||
List<SelectRspVO> vos = tags.stream()
|
||||
.map(tag -> SelectRspVO.builder()
|
||||
.label(tag.getName())
|
||||
.value(tag.getId())
|
||||
.build())
|
||||
.toList();
|
||||
|
||||
return Response.success(vos);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.hanserwei.admin.service.impl;
|
||||
|
||||
import com.hanserwei.admin.model.vo.FindUserInfoRspVO;
|
||||
import com.hanserwei.admin.model.vo.UpdateAdminUserPasswordReqVO;
|
||||
import com.hanserwei.admin.model.vo.user.FindUserInfoRspVO;
|
||||
import com.hanserwei.admin.model.vo.user.UpdateAdminUserPasswordReqVO;
|
||||
import com.hanserwei.admin.service.AdminUserService;
|
||||
import com.hanserwei.common.domain.repository.UserRepository;
|
||||
import com.hanserwei.common.enums.ResponseCodeEnum;
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
|
||||
package com.hanserwei.admin.utils;
|
||||
|
||||
import com.hanserwei.common.config.RustfsProperties;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class RustfsUtils {
|
||||
|
||||
@Resource
|
||||
private RustfsProperties rustfsProperties;
|
||||
|
||||
@Resource
|
||||
private S3Client s3Client;
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 访问地址
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public String uploadFile(MultipartFile file) throws Exception {
|
||||
// 判断文件是否为空
|
||||
if (file == null || file.getSize() == 0) {
|
||||
log.error("==> 上传文件异常:文件大小为空 ...");
|
||||
throw new RuntimeException("文件大小不能为空");
|
||||
}
|
||||
|
||||
// 文件的原始名称
|
||||
String originalFileName = file.getOriginalFilename();
|
||||
// 文件的 Content-Type
|
||||
String contentType = file.getContentType();
|
||||
|
||||
// 生成存储对象的名称(将 UUID 字符串中的 - 替换成空字符串)
|
||||
String key = UUID.randomUUID().toString().replace("-", "");
|
||||
// 获取文件的后缀,如 .jpg
|
||||
String suffix = null;
|
||||
if (originalFileName != null) {
|
||||
suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
|
||||
}
|
||||
|
||||
// 拼接上文件后缀,即为要存储的文件名
|
||||
String objectName = String.format("%s%s", key, suffix);
|
||||
|
||||
log.info("==> 开始上传文件至 Rustfs, ObjectName: {}", objectName);
|
||||
|
||||
// 上传文件至 Rustfs
|
||||
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
|
||||
.key(objectName)
|
||||
.bucket(rustfsProperties.getBucketName())
|
||||
.contentType(contentType)
|
||||
.contentLength(file.getSize())
|
||||
.build();
|
||||
|
||||
s3Client.putObject(putObjectRequest, RequestBody.fromInputStream(file.getInputStream(), file.getSize()));
|
||||
|
||||
// 返回文件的访问链接
|
||||
String url = String.format("%s/%s/%s", rustfsProperties.getEndpoint(), rustfsProperties.getBucketName(), objectName);
|
||||
log.info("==> 上传文件至 Rustfs 成功,访问路径: {}", url);
|
||||
return url;
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ dependencies {
|
||||
api("org.springframework.boot:spring-boot-starter-web")
|
||||
api("org.springframework.boot:spring-boot-starter-security")
|
||||
api("org.springframework.boot:spring-boot-starter-aop")
|
||||
api("software.amazon.awssdk:s3:2.40.1")
|
||||
|
||||
runtimeOnly("org.postgresql:postgresql")
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.hanserwei.common.config;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
@Component
|
||||
public class RustfsClientConfig {
|
||||
|
||||
@Resource
|
||||
private RustfsProperties rustfsProperties;
|
||||
|
||||
@Bean
|
||||
public S3Client s3Client() {
|
||||
return S3Client.builder()
|
||||
.endpointOverride(URI.create(rustfsProperties.getEndpoint()))
|
||||
.region(Region.US_EAST_1)
|
||||
.credentialsProvider(
|
||||
StaticCredentialsProvider.create(
|
||||
AwsBasicCredentials.create(rustfsProperties.getAccessKey(), rustfsProperties.getSecretKey())
|
||||
)
|
||||
)
|
||||
.forcePathStyle(true)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.hanserwei.common.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "rustfs")
|
||||
public class RustfsProperties {
|
||||
private String endpoint;
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
private String bucketName;
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.hanserwei.common.domain.dataobject;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 博客设置表
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
@Entity
|
||||
@Table(name = "t_blog_settings")
|
||||
public class BlogSettings implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* ID
|
||||
*/
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 博客Logo
|
||||
* 数据库类型为 TEXT,Java中映射为 String 即可
|
||||
*/
|
||||
@Column(name = "logo", nullable = false, columnDefinition = "TEXT")
|
||||
@Builder.Default
|
||||
private String logo = "";
|
||||
|
||||
/**
|
||||
* 博客名称
|
||||
*/
|
||||
@Column(name = "name", length = 60, nullable = false)
|
||||
@Builder.Default
|
||||
private String name = "";
|
||||
|
||||
/**
|
||||
* 作者名
|
||||
*/
|
||||
@Column(name = "author", length = 20, nullable = false)
|
||||
@Builder.Default
|
||||
private String author = "";
|
||||
|
||||
/**
|
||||
* 介绍语
|
||||
*/
|
||||
@Column(name = "introduction", nullable = false, columnDefinition = "TEXT")
|
||||
@Builder.Default
|
||||
private String introduction = "";
|
||||
|
||||
/**
|
||||
* 作者头像
|
||||
*/
|
||||
@Column(name = "avatar", nullable = false, columnDefinition = "TEXT")
|
||||
@Builder.Default
|
||||
private String avatar = "";
|
||||
|
||||
/**
|
||||
* GitHub 主页访问地址
|
||||
*/
|
||||
@Column(name = "github_homepage", nullable = false, columnDefinition = "TEXT")
|
||||
@Builder.Default
|
||||
private String githubHomepage = "";
|
||||
|
||||
/**
|
||||
* CSDN 主页访问地址
|
||||
*/
|
||||
@Column(name = "csdn_homepage", nullable = false, columnDefinition = "TEXT")
|
||||
@Builder.Default
|
||||
private String csdnHomepage = "";
|
||||
|
||||
/**
|
||||
* Gitee 主页访问地址
|
||||
*/
|
||||
@Column(name = "gitee_homepage", nullable = false, columnDefinition = "TEXT")
|
||||
@Builder.Default
|
||||
private String giteeHomepage = "";
|
||||
|
||||
/**
|
||||
* 知乎主页访问地址
|
||||
*/
|
||||
@Column(name = "zhihu_homepage", nullable = false, columnDefinition = "TEXT")
|
||||
@Builder.Default
|
||||
private String zhihuHomepage = "";
|
||||
}
|
||||
@@ -27,7 +27,6 @@ import java.time.Instant;
|
||||
@Index(name = "idx_create_time", columnList = "create_time")
|
||||
})
|
||||
@SQLRestriction("is_deleted = false")
|
||||
@SQLDelete(sql = "UPDATE t_category SET is_deleted = true WHERE id = ?")
|
||||
public class Category implements Serializable {
|
||||
|
||||
@Serial
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.hanserwei.common.domain.dataobject;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import org.hibernate.annotations.SQLRestriction;
|
||||
import org.hibernate.annotations.UpdateTimestamp;
|
||||
import java.io.Serializable;
|
||||
import java.time.Instant;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
@Entity
|
||||
@Table(name = "t_tag", indexes = {
|
||||
@Index(name = "idx_tag_create_time", columnList = "create_time")
|
||||
})
|
||||
@SQLRestriction("is_deleted = false")
|
||||
public class Tag implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false, length = 60, unique = true)
|
||||
private String name;
|
||||
|
||||
@CreationTimestamp
|
||||
@Column(name = "create_time", nullable = false, updatable = false)
|
||||
private Instant createTime;
|
||||
|
||||
@UpdateTimestamp
|
||||
@Column(name = "update_time", nullable = false)
|
||||
private Instant updateTime;
|
||||
|
||||
@Column(name = "is_deleted", nullable = false)
|
||||
@Builder.Default
|
||||
private Boolean isDeleted = false;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.hanserwei.common.domain.repository;
|
||||
|
||||
import com.hanserwei.common.domain.dataobject.BlogSettings;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface BlogSettingsRepository extends JpaRepository<BlogSettings, Long> {
|
||||
|
||||
}
|
||||
@@ -5,8 +5,9 @@ import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
public interface CategoryRepository extends JpaRepository<Category, Long> {
|
||||
public interface CategoryRepository extends JpaRepository<Category, Long>, JpaSpecificationExecutor<Category> {
|
||||
boolean existsCategoryByName(String name);
|
||||
|
||||
Page<Category> findAll(Specification<Category> specification, Pageable pageable);
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.hanserwei.common.domain.repository;
|
||||
|
||||
import com.hanserwei.common.domain.dataobject.Tag;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public interface TagRepository extends JpaRepository<Tag, Long>, JpaSpecificationExecutor<Tag> {
|
||||
Collection<Tag> findByNameIn(List<String> names);
|
||||
|
||||
Page<Tag> findAll(Specification<Tag> specification, Pageable pageable);
|
||||
|
||||
/**
|
||||
* 根据标签名称模糊查询
|
||||
* @param name 标签名称关键词
|
||||
* @return 标签列表
|
||||
*/
|
||||
List<Tag> findByNameContaining(String name);
|
||||
|
||||
}
|
||||
@@ -18,8 +18,10 @@ public enum ResponseCodeEnum implements BaseExceptionInterface {
|
||||
UNAUTHORIZED("20002", "无访问权限,请先登录!"),
|
||||
FORBIDDEN("20004", "演示账号仅支持查询操作!"),
|
||||
USER_NOT_EXIST("2005", "有户不存在!"),
|
||||
CATEGORY_NAME_IS_EXISTED("20005", "该分类已存在,请勿重复添加!")
|
||||
;
|
||||
CATEGORY_NAME_IS_EXISTED("20005", "该分类已存在,请勿重复添加!"),
|
||||
TAG_NOT_EXIST("20006", "标签不存在!"),
|
||||
CATEGORY_NOT_EXIST("20007", "分类不存在!"),
|
||||
FILE_UPLOAD_FAILED("20008", "上传文件失败!");
|
||||
|
||||
// 异常码
|
||||
private final String errorCode;
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.hanserwei.common.utils;
|
||||
|
||||
import com.hanserwei.common.model.BasePageQuery;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 分页查询工具类
|
||||
* 用于抽取通用的分页查询逻辑
|
||||
*/
|
||||
public class PageHelper {
|
||||
|
||||
/**
|
||||
* 执行带条件的分页查询
|
||||
*
|
||||
* @param repository JPA Repository
|
||||
* @param pageQuery 分页查询参数
|
||||
* @param name 名称(用于模糊查询)
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
* @param converter DO 到 VO 的转换函数
|
||||
* @param <T> 实体类型
|
||||
* @param <R> 响应VO类型
|
||||
* @return 分页响应
|
||||
*/
|
||||
public static <T, R> PageResponse<R> findPageList(
|
||||
JpaSpecificationExecutor<T> repository,
|
||||
BasePageQuery pageQuery,
|
||||
String name,
|
||||
LocalDate startDate,
|
||||
LocalDate endDate,
|
||||
Function<T, R> converter) {
|
||||
|
||||
// 构建分页参数
|
||||
Pageable pageable = PageRequest.of(
|
||||
pageQuery.getCurrent().intValue() - 1,
|
||||
pageQuery.getSize().intValue(),
|
||||
Sort.by(Sort.Direction.DESC, "createTime")
|
||||
);
|
||||
|
||||
// 构建查询条件
|
||||
Specification<T> specification = (root, query, criteriaBuilder) -> {
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
|
||||
// 名称模糊查询
|
||||
if (StringUtils.hasText(name)) {
|
||||
predicates.add(
|
||||
criteriaBuilder.like(root.get("name"), "%" + name.trim() + "%")
|
||||
);
|
||||
}
|
||||
|
||||
// 开始日期查询
|
||||
if (Objects.nonNull(startDate)) {
|
||||
predicates.add(
|
||||
criteriaBuilder.greaterThanOrEqualTo(root.get("createTime"), startDate)
|
||||
);
|
||||
}
|
||||
|
||||
// 结束日期查询
|
||||
if (Objects.nonNull(endDate)) {
|
||||
predicates.add(
|
||||
criteriaBuilder.lessThan(root.get("createTime"), endDate.plusDays(1).atStartOfDay())
|
||||
);
|
||||
}
|
||||
|
||||
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
|
||||
};
|
||||
|
||||
// 执行查询
|
||||
Page<T> page = repository.findAll(specification, pageable);
|
||||
|
||||
// DO 转 VO
|
||||
List<R> vos = page.getContent().stream()
|
||||
.map(converter)
|
||||
.toList();
|
||||
|
||||
return PageResponse.success(page, vos);
|
||||
}
|
||||
}
|
||||
@@ -44,58 +44,64 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
|
||||
// 1. 从请求头中获取 Authorization
|
||||
String header = request.getHeader("Authorization");
|
||||
|
||||
// 2. 校验头格式 (必须以 Bearer 开头)
|
||||
if (StringUtils.startsWith(header, "Bearer ")) {
|
||||
String token = StringUtils.substring(header, 7);
|
||||
log.info("JWT Token: {}", token);
|
||||
if (StringUtils.isNotBlank(token)) {
|
||||
try {
|
||||
// 3. 解析 Token (核心步骤)
|
||||
// 注意:JwtTokenHelper.parseToken 方法内部已经处理了 JWT 格式校验和过期校验,
|
||||
// 并将 JJWT 异常转换为了 Spring Security 的 AuthenticationException 抛出。
|
||||
Jws<Claims> claimsJws = jwtTokenHelper.parseToken(token);
|
||||
String requestURI = request.getRequestURI();
|
||||
log.info("Request URI: {}", requestURI);
|
||||
if (requestURI.startsWith("/admin")) {
|
||||
// 2. 校验头格式 (必须以 Bearer 开头)
|
||||
if (header != null && header.startsWith("Bearer ")) {
|
||||
String token = StringUtils.substring(header, 7);
|
||||
log.info("JWT Token: {}", token);
|
||||
if (StringUtils.isNotBlank(token)) {
|
||||
try {
|
||||
// 3. 解析 Token (核心步骤)
|
||||
// 注意:JwtTokenHelper.parseToken 方法内部已经处理了 JWT 格式校验和过期校验,
|
||||
// 并将 JJWT 异常转换为了 Spring Security 的 AuthenticationException 抛出。
|
||||
Jws<Claims> claimsJws = jwtTokenHelper.parseToken(token);
|
||||
|
||||
// 4. 获取用户名
|
||||
// JJWT 0.12+ 建议使用 getPayload() 替代 getBody()
|
||||
// 在 Helper 中生成 Token 时使用的是 .subject(username),所以这里取 Subject
|
||||
String username = claimsJws.getPayload().getSubject();
|
||||
// 4. 获取用户名
|
||||
// JJWT 0.12+ 建议使用 getPayload() 替代 getBody()
|
||||
// 在 Helper 中生成 Token 时使用的是 .subject(username),所以这里取 Subject
|
||||
String username = claimsJws.getPayload().getSubject();
|
||||
|
||||
// 5. 组装认证信息 (如果当前上下文没有认证信息)
|
||||
if (StringUtils.isNotBlank(username) && Objects.isNull(SecurityContextHolder.getContext().getAuthentication())) {
|
||||
// 5. 组装认证信息 (如果当前上下文没有认证信息)
|
||||
if (StringUtils.isNotBlank(username) && Objects.isNull(SecurityContextHolder.getContext().getAuthentication())) {
|
||||
|
||||
// 查询数据库获取用户完整信息 (包含权限)
|
||||
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
|
||||
// 查询数据库获取用户完整信息 (包含权限)
|
||||
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
|
||||
|
||||
// 构建 Spring Security 的认证 Token
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
|
||||
userDetails,
|
||||
null,
|
||||
userDetails.getAuthorities()
|
||||
);
|
||||
// 构建 Spring Security 的认证 Token
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
|
||||
userDetails,
|
||||
null,
|
||||
userDetails.getAuthorities()
|
||||
);
|
||||
|
||||
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
||||
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
||||
|
||||
// 6. 将认证信息存入 SecurityContext
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
// 6. 将认证信息存入 SecurityContext
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
}
|
||||
} catch (AuthenticationException e) {
|
||||
// 7. 异常处理
|
||||
// 捕获 JwtTokenHelper 抛出的 BadCredentialsException 或 CredentialsExpiredException
|
||||
// 如果 Token 存在但是无效/过期,直接交给 EntryPoint 处理响应 (通常返回 401)
|
||||
// 并 return 结束当前过滤器,不再往下执行
|
||||
authenticationEntryPoint.commence(request, response, e);
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
// 处理其他未预料到的异常
|
||||
log.error("JWT处理过程中发生未知错误", e);
|
||||
authenticationEntryPoint.commence(request, response, new AuthenticationException("系统内部认证错误") {
|
||||
});
|
||||
return;
|
||||
}
|
||||
} catch (AuthenticationException e) {
|
||||
// 7. 异常处理
|
||||
// 捕获 JwtTokenHelper 抛出的 BadCredentialsException 或 CredentialsExpiredException
|
||||
// 如果 Token 存在但是无效/过期,直接交给 EntryPoint 处理响应 (通常返回 401)
|
||||
// 并 return 结束当前过滤器,不再往下执行
|
||||
authenticationEntryPoint.commence(request, response, e);
|
||||
return;
|
||||
} catch (Exception e) {
|
||||
// 处理其他未预料到的异常
|
||||
log.error("JWT处理过程中发生未知错误", e);
|
||||
authenticationEntryPoint.commence(request, response, new AuthenticationException("系统内部认证错误") {
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 8. 放行请求 (如果没有 Token 或者 Token 校验通过,继续执行下一个过滤器)
|
||||
filterChain.doFilter(request, response);
|
||||
// 8. 放行请求 (如果没有 Token 或者 Token 校验通过,继续执行下一个过滤器)
|
||||
filterChain.doFilter(request, response);
|
||||
} else {
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,7 +80,7 @@ public class JwtTokenHelper implements InitializingBean {
|
||||
*/
|
||||
public String generateToken(String username) {
|
||||
Instant now = Instant.now();
|
||||
Instant expireTime = now.plus(1, ChronoUnit.HOURS);
|
||||
Instant expireTime = now.plus(30, ChronoUnit.DAYS);
|
||||
|
||||
return Jwts.builder()
|
||||
.header().add("type", "JWT").and() // 推荐添加 header
|
||||
|
||||
Reference in New Issue
Block a user