├── .gitignore ├── .mvn └── wrapper │ ├── MavenWrapperDownloader.java │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── LICENSE ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml └── src ├── main ├── docker │ └── Dockerfile ├── java │ └── com │ │ └── hiyj │ │ └── blog │ │ ├── LBlogApplication.java │ │ ├── annotation │ │ ├── PassToken.java │ │ └── Permission.java │ │ ├── caches │ │ └── redis │ │ │ ├── JDKSerializer.java │ │ │ ├── RedisCache.java │ │ │ ├── RedisCallback.java │ │ │ ├── RedisConfig.java │ │ │ ├── RedisConfigurationBuilder.java │ │ │ └── Serializer.java │ │ ├── config │ │ └── WebConfig.java │ │ ├── controller │ │ ├── ArticleController.java │ │ ├── ArticleLabelController.java │ │ ├── CommentController.java │ │ ├── FileController.java │ │ ├── FriendLinkController.java │ │ ├── OtherController.java │ │ ├── SysConfigController.java │ │ └── UserController.java │ │ ├── doc │ │ └── SwaggerConfig.java │ │ ├── exception │ │ └── GlobalExceptionHandler.java │ │ ├── interceptor │ │ ├── CodeInterceptor.java │ │ ├── CrosInterceptor.java │ │ ├── MappingInterceptor.java │ │ └── PermissionsInterceptor.java │ │ ├── mapper │ │ ├── ArticleLabelMapper.java │ │ ├── ArticleMapper.java │ │ ├── CommentMapper.java │ │ ├── FileMapper.java │ │ ├── LinkMapper.java │ │ ├── PermissionMapper.java │ │ ├── RoleMapper.java │ │ ├── SysConfigMapper.java │ │ └── UserMapper.java │ │ ├── model │ │ ├── request │ │ │ ├── ArticlePageModel.java │ │ │ ├── CodeUrlModel.java │ │ │ ├── ContentModel.java │ │ │ ├── IdModel.java │ │ │ ├── IdTokenModel.java │ │ │ ├── IdTokenTypeModel.java │ │ │ ├── IdTypeModel.java │ │ │ ├── PageBaseModel.java │ │ │ ├── PageModel.java │ │ │ ├── ReqCommentModel.java │ │ │ ├── ReqFriendLinkModel.java │ │ │ ├── ReqFriendLinkStatusModel.java │ │ │ ├── TokenModel.java │ │ │ └── TokenTypeModel.java │ │ ├── response │ │ │ ├── ClientIdModel.java │ │ │ ├── RspCommentsModel.java │ │ │ ├── RspFriendLinkModel.java │ │ │ └── RspListType.java │ │ └── share │ │ │ └── ClientModel.java │ │ ├── object │ │ ├── Article.java │ │ ├── ArticleLabel.java │ │ ├── ArticleType.java │ │ ├── CakeChartData.java │ │ ├── Comment.java │ │ ├── FriendLink.java │ │ ├── Msg.java │ │ ├── OtherUser.java │ │ ├── PanelData.java │ │ ├── Permission.java │ │ ├── Role.java │ │ ├── SysUiConfig.java │ │ ├── User.java │ │ └── base │ │ │ ├── CommentBase.java │ │ │ ├── LabelBase.java │ │ │ ├── Link.java │ │ │ └── PermissionRole.java │ │ ├── oss │ │ └── OssUtils.java │ │ ├── services │ │ ├── ArticleJsonService.java │ │ ├── ArticleLabelJsonService.java │ │ ├── CommentJsonService.java │ │ ├── FileJsonService.java │ │ ├── FriendLinkJsonService.java │ │ ├── OtherJsonService.java │ │ ├── SysConfigJsonService.java │ │ ├── UserJsonService.java │ │ ├── base │ │ │ ├── ArticleLabelService.java │ │ │ ├── ArticleService.java │ │ │ ├── CommentService.java │ │ │ ├── FileService.java │ │ │ ├── FriendLinkService.java │ │ │ ├── OtherService.java │ │ │ ├── PermissionService.java │ │ │ ├── RoleService.java │ │ │ ├── SysConfigService.java │ │ │ └── UserService.java │ │ └── otherlogin │ │ │ └── GiteeService.java │ │ └── utils │ │ ├── CodeUtils.java │ │ └── JwtUtils.java └── resources │ ├── application-dev.yml │ ├── application-prod.yml │ ├── application.yml │ ├── db │ └── migration │ │ └── V1_0_1__init.sql │ └── mybatis │ └── mapper │ ├── ArticleLabelMapper.xml │ ├── ArticleMapper.xml │ ├── CommentMapper.xml │ ├── LinkMapper.xml │ ├── PermissionMapper.xml │ ├── RoleMapper.xml │ ├── SysConfigMapper.xml │ └── UserMapper.xml └── test └── java └── com └── hiyj └── blog ├── LBlogApplicationTests.java ├── caches └── redis │ └── RedisConfigTest.java ├── mapper ├── ArticleLabelMapperTest.java ├── ArticleMapperTest.java ├── CommentMapperTest.java ├── LinkMapperTest.java ├── PermissionsServiceTest.java ├── RoleMapperTest.java ├── SysConfigMapperTest.java └── UserMapperTest.java ├── oss └── OssUtilsTest.java ├── services ├── ArticleLabelServiceTest.java ├── ArticleServiceTest.java ├── CommentServiceTest.java ├── FileServiceTest.java ├── GiteeServiceTest.java ├── OtherServiceTest.java ├── SysConfigServiceTest.java └── UserServiceTest.java ├── test ├── HttpRequestTest.java └── TestEnum.java └── utils └── JwtUtilsTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | 35 | ### log ### 36 | log.* 37 | /log/ 38 | *.log -------------------------------------------------------------------------------- /.mvn/wrapper/MavenWrapperDownloader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2007-present the original author or authors. 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 | * https://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 | 17 | import java.net.*; 18 | import java.io.*; 19 | import java.nio.channels.*; 20 | import java.util.Properties; 21 | 22 | public class MavenWrapperDownloader { 23 | 24 | private static final String WRAPPER_VERSION = "0.5.6"; 25 | /** 26 | * Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided. 27 | */ 28 | private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/" 29 | + WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar"; 30 | 31 | /** 32 | * Path to the maven-wrapper.properties file, which might contain a downloadUrl property to 33 | * use instead of the default one. 34 | */ 35 | private static final String MAVEN_WRAPPER_PROPERTIES_PATH = 36 | ".mvn/wrapper/maven-wrapper.properties"; 37 | 38 | /** 39 | * Path where the maven-wrapper.jar will be saved to. 40 | */ 41 | private static final String MAVEN_WRAPPER_JAR_PATH = 42 | ".mvn/wrapper/maven-wrapper.jar"; 43 | 44 | /** 45 | * Name of the property which should be used to override the default download url for the wrapper. 46 | */ 47 | private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl"; 48 | 49 | public static void main(String args[]) { 50 | System.out.println("- Downloader started"); 51 | File baseDirectory = new File(args[0]); 52 | System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath()); 53 | 54 | // If the maven-wrapper.properties exists, read it and check if it contains a custom 55 | // wrapperUrl parameter. 56 | File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH); 57 | String url = DEFAULT_DOWNLOAD_URL; 58 | if (mavenWrapperPropertyFile.exists()) { 59 | FileInputStream mavenWrapperPropertyFileInputStream = null; 60 | try { 61 | mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile); 62 | Properties mavenWrapperProperties = new Properties(); 63 | mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream); 64 | url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url); 65 | } catch (IOException e) { 66 | System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'"); 67 | } finally { 68 | try { 69 | if (mavenWrapperPropertyFileInputStream != null) { 70 | mavenWrapperPropertyFileInputStream.close(); 71 | } 72 | } catch (IOException e) { 73 | // Ignore ... 74 | } 75 | } 76 | } 77 | System.out.println("- Downloading from: " + url); 78 | 79 | File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH); 80 | if (!outputFile.getParentFile().exists()) { 81 | if (!outputFile.getParentFile().mkdirs()) { 82 | System.out.println( 83 | "- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'"); 84 | } 85 | } 86 | System.out.println("- Downloading to: " + outputFile.getAbsolutePath()); 87 | try { 88 | downloadFileFromURL(url, outputFile); 89 | System.out.println("Done"); 90 | System.exit(0); 91 | } catch (Throwable e) { 92 | System.out.println("- Error downloading"); 93 | e.printStackTrace(); 94 | System.exit(1); 95 | } 96 | } 97 | 98 | private static void downloadFileFromURL(String urlString, File destination) throws Exception { 99 | if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) { 100 | String username = System.getenv("MVNW_USERNAME"); 101 | char[] password = System.getenv("MVNW_PASSWORD").toCharArray(); 102 | Authenticator.setDefault(new Authenticator() { 103 | @Override 104 | protected PasswordAuthentication getPasswordAuthentication() { 105 | return new PasswordAuthentication(username, password); 106 | } 107 | }); 108 | } 109 | URL website = new URL(urlString); 110 | ReadableByteChannel rbc; 111 | rbc = Channels.newChannel(website.openStream()); 112 | FileOutputStream fos = new FileOutputStream(destination); 113 | fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 114 | fos.close(); 115 | rbc.close(); 116 | } 117 | 118 | } 119 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WindSnowLi/w-blog-api/5636845ead19a03a37e29062b3a8a44eda6fa78b/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.1/apache-maven-3.8.1-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 WindSnowLi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # w-blog 2 | 3 | ## 描述 4 | 5 | 一个后端使用Spring Boot 2.x、前台使用nuxtJs、后台使用vue的个人博客 6 | 7 | ## 简介 8 | 9 | 1. 链接 10 | * Gitee链接: 11 | * [api](https://gitee.com/windsnowli/w-blog-api): `https://gitee.com/windsnowli/w-blog-api` 12 | 13 | * [前台](https://gitee.com/windsnowli/vue-ssr-blog): `https://gitee.com/windsnowli/vue-ssr-blog` 14 | 15 | * [后台](https://gitee.com/windsnowli/vue-admin-blog): `https://gitee.com/windsnowli/vue-admin-blog` 16 | 17 | * GitHub链接: 18 | * [api](https://github.com/WindSnowLi/w-blog-api): `https://github.com/WindSnowLi/w-blog-api` 19 | 20 | * [前台](https://github.com/WindSnowLi/vue-ssr-blog): `https://github.com/WindSnowLi/vue-ssr-blog` 21 | 22 | * [后台](https://github.com/WindSnowLi/vue-admin-blog): `https://github.com/WindSnowLi/vue-admin-blog` 23 | 24 | 2. 一个简单的的个人博客项目,共分为了 `前台`、 `后台`、 `api`三个部分。 25 | 26 | * api: 后端基于 `SpringBoot` 。主要依赖 `Mybatis` 、 `Mybatis-Redis` 、 `Redis` 、 `fastjson` 、 `DruidDataSource` 、 `Lombok` 、 `java-jwt` 、 `aliyun-sdk-oss` 、 `knife4j` 等,数据库使用的是 `MySQL8.0+` 27 | 28 | * 前台: 前台的主要样式是来源于网络上了一个 `BizBlog` 模板,最初来源于哪我不得而知,在原本的基础上改写成了 `nuxtJs` 项目。 29 | * 后台: 后台UI套用的[vue-element-admin](https://github.com/PanJiaChen/vue-element-admin),基本是直接拿来用了,想自己定制着实实力不允许。 30 | 31 | 3. 示例:[绿色食品——菜狗](https://www.blog.hiyj.cn/) 32 | 33 | ## 本地启动 34 | 35 | ### api:前台后台请求的api使用的是同一个项目 36 | 37 | 1. `git clone https://gitee.com/WindSnowLi/w-blog-api.git`或`git clone https://github.com/WindSnowLi/w-blog-api.git` 克隆项目到本地 38 | 2. `mvn clean install dependency:tree` 安装依赖 39 | 3. 创建数据库,并设置为`UTF8`编码(`utf8mb4`) 40 | 4. 修改环境设置 41 | * 修改开发环境 `application-dev.yml` 和生产环境 `application-prod.yml` 中的数据库配置信息; 42 | * 修改 `redis.properties` 中的 `Redis` 相关信息; 43 | * 注: `knife4j` 只在开发环境中激活。 44 | 45 | 5. `mvn clean package -Dmaven.test.skip=true` 跳过测试并生成 `jar` 包 46 | 6. `java -jar 生成的包名.jar` 运行开发配置环境,初次运行会自动初始化数据库(生产环境可指定加载的配置文件`--spring.profiles.active="prod"`) 47 | 7. 访问 `http://127.0.0.1:9000/doc.html` 查看 `api` 文档 48 | 8. *推荐使用IDEA打开项目文件夹自动处理依赖、方便运行* 49 | 50 | ### 前台 51 | 52 | 1. `git clone https://gitee.com/WindSnowLi/vue-ssr-blog.git`或`git clone https://github.com/WindSnowLi/vue-ssr-blog.git` 克隆项目到本地 53 | 2. `npm install` 安装依赖 54 | 3. 可修改 `config/sitemap.xml` 文件中的 `host` 地址,用于生成访问地图 55 | 4. 可修改 `nuxt.config.js` 中的端口号 56 | 5. 可修改 `package.json` 文件中的 `script` 中的 `BASE_URL` 来指定后端 `api` 地址 57 | 6. `npm run build` 编译 58 | 7. `npm start` 本地运行 59 | 60 | ### 后台 61 | 62 | 1. `git clone https://gitee.com/WindSnowLi/vue-admin-blog.git`或`git clone https://github.com/WindSnowLi/vue-admin-blog.git` 克隆项目到本地 63 | 2. `npm install` 安装依赖 64 | 3. `npm run dev` 使用模拟数据预览界面 65 | 4. 修改 `.env.production` 文件中的 `VUE_APP_BASE_API` 地址为后端 `api` 的地址 66 | 5. `npm run build:prod` 编译 67 | 6. `dist` 文件夹下的为编译好的文件,可放到 `http` 服务器下(可以使用 `npm` 安装 `http-server` )进行访问 68 | 69 | ## 界面展示 70 | 71 | ### 前台 72 | 73 | ![首页](https://gitee.com/windsnowli/w-blog/raw/main/images/homepage.png) 74 | 75 |
76 | 77 | ![文章详情](https://gitee.com/windsnowli/w-blog/raw/main/images/article-detail.png) 78 | 79 |
80 | 81 | ![友链](https://gitee.com/windsnowli/w-blog/raw/main/images/links.png) 82 | 83 | ### 后台 84 | 85 | ![首页](https://gitee.com/windsnowli/w-blog/raw/main/images/dashboard.png) 86 | 87 |
88 | 89 | ![创建文章](https://gitee.com/windsnowli/w-blog/raw/main/images/create-art.png) 90 | 91 |
92 | 93 | ![管理文章](https://gitee.com/windsnowli/w-blog/raw/main/images/man-art-list.png) 94 | 95 |
96 | 97 | ![文章列表](https://gitee.com/windsnowli/w-blog/raw/main/images/art-list.png) 98 | 99 |
100 | 101 | ![友链管理](https://gitee.com/windsnowli/w-blog/raw/main/images/man-links.png) 102 | 103 | ## License 104 | 105 | [MIT](https://github.com/WindSnowLi/w-blog-api/blob/master/LICENSE) 106 | 107 | Copyright (c) 2021 WindSnowLi 108 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.5.0 9 | 10 | 11 | com.hiyj 12 | w-blog 13 | 1.0.1 14 | w-blog 15 | WindSnowLi blog 16 | 17 | 11 18 | w-blog 19 | 20 | 21 | 22 | org.mybatis.spring.boot 23 | mybatis-spring-boot-starter 24 | 2.1.4 25 | 26 | 27 | com.alibaba 28 | fastjson 29 | 1.2.62 30 | 31 | 32 | com.alibaba.druid 33 | druid-wrapper 34 | 0.2.9 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-web-services 39 | 40 | 41 | 42 | mysql 43 | mysql-connector-java 44 | runtime 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-test 49 | test 50 | 51 | 52 | org.projectlombok 53 | lombok 54 | 55 | 56 | com.auth0 57 | java-jwt 58 | 3.4.0 59 | 60 | 61 | 62 | com.aliyun.oss 63 | aliyun-sdk-oss 64 | 3.10.2 65 | 66 | 67 | javax.activation 68 | activation 69 | 1.1.1 70 | 71 | 72 | 73 | org.glassfish.jaxb 74 | jaxb-runtime 75 | 2.3.3 76 | 77 | 78 | 79 | com.fasterxml.uuid 80 | java-uuid-generator 81 | 3.3.0 82 | 83 | 84 | 85 | com.github.xiaoymin 86 | knife4j-spring-boot-starter 87 | 3.0.3 88 | 89 | 90 | 91 | 92 | org.flywaydb 93 | flyway-core 94 | 5.2.4 95 | 96 | 97 | 98 | 99 | redis.clients 100 | jedis 101 | 3.7.0 102 | 103 | 104 | 105 | 106 | 107 | 108 | org.springframework.boot 109 | spring-boot-maven-plugin 110 | 2.5.3 111 | 112 | 113 | 114 | repackage 115 | 116 | 117 | 118 | 119 | 120 | 121 | com.spotify 122 | docker-maven-plugin 123 | 1.0.0 124 | 125 | 126 | javax.activation 127 | activation 128 | 1.1.1 129 | 130 | 131 | 132 | ${docker.image.prefix}/${project.artifactId} 133 | src/main/docker 134 | 135 | 136 | / 137 | ${project.build.directory} 138 | ${project.build.finalName}.jar 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | -------------------------------------------------------------------------------- /src/main/docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM adoptopenjdk/openjdk11:jre-11.0.6_10-alpine 2 | 3 | LABEL "author"="WindSnowLi" 4 | LABEL "version"="1.0.1" 5 | LABEL "email"="windsnowli@163.com" 6 | 7 | ADD w-blog-1.0.1.jar w-blog.jar 8 | 9 | # 配置环境变量支持中文 10 | ENV LANG="en_US.UTF-8" 11 | 12 | # 环境配置文件 13 | ENV active="dev" 14 | 15 | EXPOSE 9000 16 | 17 | CMD ["sh","-c","java -jar w-blog.jar --spring.profiles.active=$active"] -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/LBlogApplication.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog; 2 | 3 | import com.alibaba.druid.pool.DruidDataSource; 4 | import com.hiyj.blog.caches.redis.RedisConfigurationBuilder; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.SpringApplication; 7 | import org.springframework.boot.autoconfigure.SpringBootApplication; 8 | import org.springframework.boot.context.properties.ConfigurationProperties; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.context.annotation.Import; 11 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 12 | 13 | @EnableSwagger2 14 | @SpringBootApplication 15 | @Import(RedisConfigurationBuilder.class) 16 | public class LBlogApplication { 17 | 18 | @Autowired 19 | public void setRedisConfigurationBuilder(RedisConfigurationBuilder redisConfigurationBuilder) { 20 | // 为了优先加载 21 | } 22 | 23 | public static void main(String[] args) { 24 | SpringApplication.run(LBlogApplication.class, args); 25 | } 26 | 27 | @Bean(name = "DruidDataSource", destroyMethod = "close", initMethod = "init") 28 | @ConfigurationProperties(prefix = "spring.datasource") 29 | public DruidDataSource druidDataSource() { 30 | return new DruidDataSource(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/annotation/PassToken.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.annotation; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | * @author windSnowLi 10 | */ 11 | @Target({ElementType.METHOD, ElementType.TYPE}) 12 | @Retention(RetentionPolicy.RUNTIME) 13 | public @interface PassToken { 14 | boolean required() default true; 15 | } -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/annotation/Permission.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Permission.java, 2021-08-26 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.annotation; 9 | 10 | import java.lang.annotation.ElementType; 11 | import java.lang.annotation.Retention; 12 | import java.lang.annotation.RetentionPolicy; 13 | import java.lang.annotation.Target; 14 | 15 | @Target({ElementType.METHOD, ElementType.TYPE}) 16 | @Retention(RetentionPolicy.RUNTIME) 17 | public @interface Permission { 18 | // 需要的权限 19 | String[] value() default {}; 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/caches/redis/JDKSerializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2021 the original author or authors. 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 | package com.hiyj.blog.caches.redis; 17 | 18 | import java.io.ByteArrayInputStream; 19 | import java.io.ByteArrayOutputStream; 20 | import java.io.ObjectInputStream; 21 | import java.io.ObjectOutputStream; 22 | 23 | import org.apache.ibatis.cache.CacheException; 24 | 25 | public enum JDKSerializer implements Serializer { 26 | // Enum singleton, which is preferred approach since Java 1.5 27 | INSTANCE; 28 | 29 | JDKSerializer() { 30 | // prevent instantiation 31 | } 32 | 33 | public byte[] serialize(Object object) { 34 | try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); 35 | ObjectOutputStream oos = new ObjectOutputStream(baos)) { 36 | oos.writeObject(object); 37 | return baos.toByteArray(); 38 | } catch (Exception e) { 39 | throw new CacheException(e); 40 | } 41 | } 42 | 43 | public Object unserialize(byte[] bytes) { 44 | if (bytes == null) { 45 | return null; 46 | } 47 | try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 48 | ObjectInputStream ois = new ObjectInputStream(bais)) { 49 | return ois.readObject(); 50 | } catch (Exception e) { 51 | throw new CacheException(e); 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/caches/redis/RedisCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2021 the original author or authors. 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 | package com.hiyj.blog.caches.redis; 17 | 18 | import org.apache.ibatis.cache.Cache; 19 | import redis.clients.jedis.Jedis; 20 | import redis.clients.jedis.JedisPool; 21 | 22 | import java.util.Map; 23 | 24 | /** 25 | * Cache adapter for Redis. 26 | * 27 | * @author Eduardo Macarron 28 | */ 29 | public class RedisCache implements Cache { 30 | 31 | private final String id; 32 | 33 | private static JedisPool pool; 34 | 35 | private final RedisConfig redisConfig; 36 | 37 | public RedisCache(final String id) { 38 | if (id == null) { 39 | throw new IllegalArgumentException("Cache instances require an ID"); 40 | } 41 | this.id = id; 42 | redisConfig = RedisConfigurationBuilder.getRedisConfig(); 43 | pool = new JedisPool(redisConfig, redisConfig.getHost(), redisConfig.getPort(), redisConfig.getConnectionTimeout(), 44 | redisConfig.getSoTimeout(), redisConfig.getPassword(), redisConfig.getDatabase(), redisConfig.getClientName(), 45 | redisConfig.isSsl(), redisConfig.getSslSocketFactory(), redisConfig.getSslParameters(), 46 | redisConfig.getHostnameVerifier()); 47 | } 48 | 49 | // TODO Review this is UNUSED 50 | private Object execute(RedisCallback callback) { 51 | try (Jedis jedis = pool.getResource()) { 52 | return callback.doWithRedis(jedis); 53 | } 54 | } 55 | 56 | @Override 57 | public String getId() { 58 | return this.id; 59 | } 60 | 61 | @Override 62 | public int getSize() { 63 | return (Integer) execute(jedis -> { 64 | Map result = jedis.hgetAll(id.getBytes()); 65 | return result.size(); 66 | }); 67 | } 68 | 69 | @Override 70 | public void putObject(final Object key, final Object value) { 71 | execute(jedis -> { 72 | final byte[] idBytes = id.getBytes(); 73 | jedis.hset(idBytes, key.toString().getBytes(), redisConfig.getSerializer().serialize(value)); 74 | if (redisConfig.getTimeout() != null && jedis.ttl(idBytes) == -1) { 75 | jedis.expire(idBytes, redisConfig.getTimeout()); 76 | } 77 | return null; 78 | }); 79 | } 80 | 81 | @Override 82 | public Object getObject(final Object key) { 83 | return execute(jedis -> redisConfig.getSerializer().unserialize(jedis.hget(id.getBytes(), key.toString().getBytes()))); 84 | } 85 | 86 | @Override 87 | public Object removeObject(final Object key) { 88 | return execute(jedis -> jedis.hdel(id, key.toString())); 89 | } 90 | 91 | @Override 92 | public void clear() { 93 | execute(jedis -> { 94 | jedis.del(id); 95 | return null; 96 | }); 97 | 98 | } 99 | 100 | @Override 101 | public String toString() { 102 | return "Redis {" + id + "}"; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/caches/redis/RedisCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2021 the original author or authors. 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 | package com.hiyj.blog.caches.redis; 17 | 18 | import redis.clients.jedis.Jedis; 19 | 20 | public interface RedisCallback { 21 | 22 | Object doWithRedis(Jedis jedis); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/caches/redis/RedisConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2021 the original author or authors. 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 | package com.hiyj.blog.caches.redis; 17 | 18 | import lombok.Getter; 19 | import lombok.Setter; 20 | import lombok.ToString; 21 | import org.springframework.boot.context.properties.ConfigurationProperties; 22 | import org.springframework.stereotype.Component; 23 | import redis.clients.jedis.JedisPoolConfig; 24 | import redis.clients.jedis.Protocol; 25 | 26 | import javax.net.ssl.HostnameVerifier; 27 | import javax.net.ssl.SSLParameters; 28 | import javax.net.ssl.SSLSocketFactory; 29 | 30 | @Component 31 | @ConfigurationProperties(prefix = "redis") 32 | @ToString 33 | @Getter 34 | @Setter 35 | public class RedisConfig extends JedisPoolConfig { 36 | private String host = Protocol.DEFAULT_HOST; 37 | private int port = Protocol.DEFAULT_PORT; 38 | private int connectionTimeout = Protocol.DEFAULT_TIMEOUT; 39 | private int soTimeout = Protocol.DEFAULT_TIMEOUT; 40 | private String password; 41 | private int database = Protocol.DEFAULT_DATABASE; 42 | private String clientName; 43 | private boolean ssl; 44 | private SSLSocketFactory sslSocketFactory; 45 | private SSLParameters sslParameters; 46 | private HostnameVerifier hostnameVerifier; 47 | private Serializer serializer = JDKSerializer.INSTANCE; 48 | private Long timeout = 60 * 60 * 24L; 49 | 50 | public boolean isSsl() { 51 | return ssl; 52 | } 53 | 54 | public void setHost(String host) { 55 | if (host == null || "".equals(host)) { 56 | host = Protocol.DEFAULT_HOST; 57 | } 58 | this.host = host; 59 | } 60 | 61 | public void setPassword(String password) { 62 | if ("".equals(password)) { 63 | password = null; 64 | } 65 | this.password = password; 66 | } 67 | 68 | public void setClientName(String clientName) { 69 | if ("".equals(clientName)) { 70 | clientName = null; 71 | } 72 | this.clientName = clientName; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/caches/redis/RedisConfigurationBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2021 the original author or authors. 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 | package com.hiyj.blog.caches.redis; 17 | 18 | import lombok.Getter; 19 | import lombok.NonNull; 20 | import lombok.Setter; 21 | import org.springframework.beans.BeansException; 22 | import org.springframework.context.ApplicationContext; 23 | import org.springframework.context.ApplicationContextAware; 24 | import org.springframework.stereotype.Component; 25 | 26 | /** 27 | * Converter from the Config to a proper {@link RedisConfig}. 28 | * 29 | * @author Eduardo Macarron 30 | */ 31 | @Getter 32 | @Component 33 | public final class RedisConfigurationBuilder implements ApplicationContextAware { 34 | 35 | private static ApplicationContext applicationContext; 36 | 37 | /** 38 | * Hidden constructor, this class can't be instantiated. 39 | */ 40 | private RedisConfigurationBuilder() { 41 | } 42 | 43 | public static RedisConfig getRedisConfig() { 44 | return applicationContext.getBean(RedisConfig.class); 45 | } 46 | 47 | @Override 48 | public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException { 49 | RedisConfigurationBuilder.applicationContext = applicationContext; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/caches/redis/Serializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2021 the original author or authors. 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 | package com.hiyj.blog.caches.redis; 17 | 18 | public interface Serializer { 19 | 20 | /** 21 | * Serialize method 22 | * 23 | * @param object 24 | * @return serialized bytes 25 | */ 26 | byte[] serialize(Object object); 27 | 28 | /** 29 | * Unserialize method 30 | * 31 | * @param bytes 32 | * @return unserialized object 33 | */ 34 | Object unserialize(byte[] bytes); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/config/WebConfig.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.config; 2 | 3 | import com.hiyj.blog.interceptor.CodeInterceptor; 4 | import com.hiyj.blog.interceptor.CrosInterceptor; 5 | import com.hiyj.blog.interceptor.MappingInterceptor; 6 | import com.hiyj.blog.interceptor.PermissionsInterceptor; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 10 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; 11 | 12 | /** 13 | * 配置拦截器 14 | * 15 | * @author windSnowLi 16 | */ 17 | @Configuration 18 | public class WebConfig implements WebMvcConfigurer { 19 | 20 | private CrosInterceptor cros; 21 | 22 | @Autowired 23 | public void setCros(CrosInterceptor cros) { 24 | this.cros = cros; 25 | } 26 | 27 | private MappingInterceptor mappingInterceptor; 28 | 29 | @Autowired 30 | public void setMappingInterceptor(MappingInterceptor mappingInterceptor) { 31 | this.mappingInterceptor = mappingInterceptor; 32 | } 33 | 34 | private PermissionsInterceptor permissionsInterceptor; 35 | 36 | @Autowired 37 | public void setPassTokenInterceptor(PermissionsInterceptor permissionsInterceptor) { 38 | this.permissionsInterceptor = permissionsInterceptor; 39 | } 40 | 41 | 42 | private CodeInterceptor codeInterceptor; 43 | 44 | @Autowired 45 | public void setCodeInterceptor(CodeInterceptor codeInterceptor) { 46 | this.codeInterceptor = codeInterceptor; 47 | } 48 | 49 | @Override 50 | public void addInterceptors(InterceptorRegistry registry) { 51 | registry.addInterceptor(codeInterceptor) 52 | .addPathPatterns("/**") 53 | .excludePathPatterns("/swagger**/**", "/webjars/**", "/v3/**", "/doc.html"); 54 | registry.addInterceptor(cros) 55 | .addPathPatterns("/**") 56 | .excludePathPatterns("/swagger**/**", "/webjars/**", "/v3/**", "/doc.html"); 57 | registry.addInterceptor(mappingInterceptor) 58 | .addPathPatterns("/**") 59 | .excludePathPatterns("/swagger**/**", "/webjars/**", "/v3/**", "/doc.html"); 60 | registry.addInterceptor(permissionsInterceptor) 61 | // 拦截所有请求,通过判断是否有 @LoginRequired 注解 决定是否需要登录 62 | .addPathPatterns("/**") 63 | .excludePathPatterns("/swagger**/**", "/webjars/**", "/v3/**", "/doc.html"); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/controller/ArticleLabelController.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.controller; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.hiyj.blog.annotation.Permission; 5 | import com.hiyj.blog.model.request.PageBaseModel; 6 | import com.hiyj.blog.object.ArticleLabel; 7 | import io.swagger.annotations.Api; 8 | import io.swagger.annotations.ApiOperation; 9 | import io.swagger.annotations.ApiResponse; 10 | import io.swagger.annotations.ApiResponses; 11 | import lombok.extern.slf4j.Slf4j; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.web.bind.annotation.PostMapping; 14 | import org.springframework.web.bind.annotation.RequestBody; 15 | import org.springframework.web.bind.annotation.RequestMapping; 16 | import org.springframework.web.bind.annotation.RestController; 17 | import com.hiyj.blog.annotation.PassToken; 18 | import com.hiyj.blog.model.request.IdModel; 19 | import com.hiyj.blog.object.Msg; 20 | import com.hiyj.blog.services.ArticleLabelJsonService; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | @Slf4j 26 | @RestController 27 | @Api(tags = "文章标签相关", value = "文章标签相关") 28 | @RequestMapping(value = "/api/articleLabel", produces = "application/json;charset=UTF-8") 29 | public class ArticleLabelController { 30 | private ArticleLabelJsonService articleLabelJsonService; 31 | 32 | @Autowired 33 | public void setArticleLabelJsonService(ArticleLabelJsonService articleLabelJsonService) { 34 | this.articleLabelJsonService = articleLabelJsonService; 35 | } 36 | 37 | /** 38 | * 通过类型ID获取类型 39 | * 40 | * @param idModel 类型ID {id:int} 41 | * @return 类型信息串 42 | */ 43 | @ApiOperation(value = "通过标签ID获取标签") 44 | @ApiResponses({ 45 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS, response = ArrayList.class), 46 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 47 | }) 48 | @PostMapping(value = "getTypeById") 49 | @PassToken 50 | public String getTypeById(@RequestBody IdModel idModel) { 51 | return articleLabelJsonService.getTypeByIdJson(idModel.getId()); 52 | } 53 | 54 | /** 55 | * 获取所有标签 56 | * 57 | * @return Msg 58 | */ 59 | @ApiOperation(value = "获取所有标签") 60 | @ApiResponses({ 61 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 62 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 63 | }) 64 | @PostMapping(value = "getAllLabel") 65 | @PassToken 66 | public String getAllLabel() { 67 | return articleLabelJsonService.getAllLabelJson(); 68 | } 69 | 70 | 71 | /** 72 | * 通过标签ID获取标签 73 | * 74 | * @param idModel 标签ID {id:int} 75 | * @return Msg 76 | */ 77 | @ApiOperation(value = "通过标签ID获取标签") 78 | @ApiResponses({ 79 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS, response = ArticleLabel.class), 80 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 81 | }) 82 | @PostMapping(value = "getLabelById") 83 | @PassToken 84 | public String getLabelById(@RequestBody IdModel idModel) { 85 | return articleLabelJsonService.getLabelByIdJson(idModel.getId()); 86 | } 87 | 88 | /** 89 | * 文章可选标签 90 | * 91 | * @return { 92 | * code: 20000, 93 | * message: "请求成功", 94 | * data: [{value:"",label:""}] 95 | * } 96 | */ 97 | @ApiOperation(value = "文章可选标签") 98 | @ApiResponses({ 99 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 100 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 101 | }) 102 | @PostMapping(value = "labels") 103 | @PassToken 104 | public String labels() { 105 | final List allLabels = articleLabelJsonService.getAllLabel(); 106 | final ArrayList labelList = new ArrayList<>(); 107 | for (ArticleLabel label : allLabels) { 108 | JSONObject temp = new JSONObject(); 109 | temp.put("value", label.getName()); 110 | temp.put("label", label.getName()); 111 | labelList.add(temp); 112 | } 113 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, labelList); 114 | } 115 | 116 | /** 117 | * 获取所有分类 118 | * 119 | * @return 分类表 120 | */ 121 | @ApiOperation(value = "获取所有分类") 122 | @ApiResponses({ 123 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 124 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 125 | }) 126 | @PostMapping(value = "getAllType") 127 | @PassToken 128 | public String getAllType() { 129 | return articleLabelJsonService.getTypes(); 130 | } 131 | 132 | /** 133 | * 分页获取标签 134 | * 135 | * @param pageBaseModel 分页信息 136 | * @return 分类表 137 | */ 138 | @ApiOperation(value = "分页获取标签") 139 | @ApiResponses({ 140 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 141 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 142 | }) 143 | @PostMapping(value = "getLabelByPage") 144 | @PassToken 145 | public String getLabelByPage(@RequestBody PageBaseModel pageBaseModel) { 146 | return articleLabelJsonService.getLabelByPageJson(pageBaseModel.getLimit(), 147 | pageBaseModel.getPage()); 148 | } 149 | 150 | /** 151 | * 设置标签内容 152 | * 153 | * @return Msg 154 | */ 155 | @ApiOperation(value = "设置标签内容") 156 | @ApiResponses({ 157 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 158 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 159 | }) 160 | @PostMapping(value = "setLabel") 161 | @Permission(value = {"UPDATE-ARTICLE-LABEL"}) 162 | public String setLabel(@RequestBody ArticleLabel articleLabel) { 163 | articleLabelJsonService.setLabel(articleLabel); 164 | return Msg.getSuccessMsg(); 165 | } 166 | } 167 | 168 | 169 | 170 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/controller/CommentController.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.controller; 2 | 3 | import com.hiyj.blog.annotation.PassToken; 4 | import com.hiyj.blog.annotation.Permission; 5 | import com.hiyj.blog.model.request.*; 6 | import com.hiyj.blog.model.response.RspCommentsModel; 7 | import com.hiyj.blog.object.Comment; 8 | import com.hiyj.blog.object.Msg; 9 | import com.hiyj.blog.object.base.CommentBase; 10 | import com.hiyj.blog.services.CommentJsonService; 11 | import com.hiyj.blog.utils.JwtUtils; 12 | import io.swagger.annotations.Api; 13 | import io.swagger.annotations.ApiOperation; 14 | import io.swagger.annotations.ApiResponse; 15 | import io.swagger.annotations.ApiResponses; 16 | import lombok.extern.slf4j.Slf4j; 17 | import org.springframework.beans.factory.annotation.Autowired; 18 | import org.springframework.web.bind.annotation.PostMapping; 19 | import org.springframework.web.bind.annotation.RequestBody; 20 | import org.springframework.web.bind.annotation.RequestMapping; 21 | import org.springframework.web.bind.annotation.RestController; 22 | 23 | @Slf4j 24 | @RestController 25 | @Api(tags = "评论相关", value = "评论相关") 26 | @RequestMapping(value = "/api/comment", produces = "application/json;charset=UTF-8") 27 | public class CommentController { 28 | private CommentJsonService commentJsonService; 29 | 30 | @Autowired 31 | public void setCommentService(CommentJsonService commentJsonService) { 32 | this.commentJsonService = commentJsonService; 33 | } 34 | 35 | /** 36 | * 添加评论 37 | * 38 | * @param tokenTypeModel 评论对象和token 39 | * @return Msg 40 | */ 41 | @ApiOperation(value = "通过token添加评论") 42 | @ApiResponses({ 43 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 44 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 45 | }) 46 | @PostMapping(value = "addComment") 47 | @Permission(value = {"COMMENT"}) 48 | public String addComment(@RequestBody TokenTypeModel tokenTypeModel) { 49 | int userId = JwtUtils.getTokenUserId(tokenTypeModel.getToken()); 50 | tokenTypeModel.getContent().setFromUser(userId); 51 | if (tokenTypeModel.getContent().getContent() == null || tokenTypeModel.getContent().getContent().length() == 0) { 52 | Msg.makeJsonMsg(Msg.CODE_FAIL, "内容不可为空", null); 53 | } 54 | commentJsonService.addComment(tokenTypeModel.getContent()); 55 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, "评论成功,审核通过后可见。", null); 56 | } 57 | 58 | /** 59 | * 根据评论目标ID、会话类型、评论状态获取评论 60 | * 61 | * @param reqCommentModel 目标ID、会话类型、评论状态 62 | * @return Msg 63 | */ 64 | @ApiOperation(value = "获取评论") 65 | @ApiResponses({ 66 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS, response = RspCommentsModel.class), 67 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 68 | }) 69 | @PostMapping(value = "getTargetComments") 70 | @PassToken 71 | public String getTargetComments(@RequestBody ReqCommentModel reqCommentModel) { 72 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, 73 | commentJsonService.getTargetCommentsJson(reqCommentModel.getTargetId(), 74 | reqCommentModel.getSessionType(), 75 | reqCommentModel.getStatus())); 76 | } 77 | 78 | /** 79 | * 修改评论状态 80 | * 81 | * @param idTypeModel { id: "评论ID", content:"Status" } 82 | * @return Msg 83 | */ 84 | @ApiOperation(value = "管理员修改评论状态") 85 | @ApiResponses({ 86 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 87 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 88 | }) 89 | @PostMapping(value = "setCommentStatus") 90 | @Permission(value = {"VERIFY-COMMENT"}) 91 | public String setCommentStatus(@RequestBody IdTypeModel idTypeModel) { 92 | commentJsonService.setCommentStatus(idTypeModel.getId(), idTypeModel.getContent()); 93 | return Msg.getSuccessMsg(); 94 | } 95 | 96 | /** 97 | * 分页获取评论列表 98 | * 99 | * @param pageModel 分页查询对象 100 | * @return Msg 101 | */ 102 | @ApiOperation(value = "分页获取评论列表") 103 | @ApiResponses({ 104 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 105 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 106 | }) 107 | @PostMapping(value = "getCommentList") 108 | @Permission(value = {"VERIFY-COMMENT"}) 109 | public String getCommentListJson(@RequestBody PageModel pageModel) { 110 | return Msg.getSuccessMsg(commentJsonService.getCommentListJson( 111 | pageModel.getLimit(), 112 | (pageModel.getPage() - 1) * pageModel.getLimit(), 113 | pageModel.getSort(), 114 | pageModel.getStatus())); 115 | } 116 | 117 | /** 118 | * 获取所有文章最新的评论 119 | * 120 | * @param limit 条数限制 {"limit": "int"} 121 | * @return Comment json list 122 | */ 123 | @ApiOperation(value = "获取所有文章最新的评论") 124 | @ApiResponses({ 125 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 126 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 127 | }) 128 | @PostMapping(value = "getRecentComment") 129 | @PassToken 130 | public String getRecentComment(@RequestBody ContentModel limit) { 131 | return Msg.getSuccessMsg(commentJsonService.getRecentComment(limit.getContent())); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/controller/FileController.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.controller; 2 | 3 | import io.swagger.annotations.Api; 4 | import io.swagger.annotations.ApiOperation; 5 | import io.swagger.annotations.ApiResponse; 6 | import io.swagger.annotations.ApiResponses; 7 | import lombok.extern.slf4j.Slf4j; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.web.bind.annotation.*; 10 | import com.hiyj.blog.annotation.PassToken; 11 | import com.hiyj.blog.annotation.Permission; 12 | import com.hiyj.blog.model.request.TokenModel; 13 | import com.hiyj.blog.model.request.TokenTypeModel; 14 | import com.hiyj.blog.object.Msg; 15 | import com.hiyj.blog.services.FileJsonService; 16 | import com.hiyj.blog.services.UserJsonService; 17 | import com.hiyj.blog.utils.JwtUtils; 18 | 19 | import java.util.Map; 20 | 21 | @Slf4j 22 | @RestController 23 | @Api(tags = "文件相关", value = "文件相关") 24 | @RequestMapping(value = "/api/file", produces = "application/json;charset=UTF-8") 25 | public class FileController { 26 | private FileJsonService fileJsonService; 27 | protected UserJsonService userJsonService; 28 | 29 | @Autowired 30 | public void setFileJsonService(FileJsonService fileJsonService) { 31 | this.fileJsonService = fileJsonService; 32 | } 33 | 34 | @Autowired 35 | public void setUserJsonService(UserJsonService userJsonService) { 36 | this.userJsonService = userJsonService; 37 | } 38 | 39 | /** 40 | * 文件上传回调 41 | * 42 | * @return 请求信息 43 | */ 44 | @ApiOperation(value = "文件上传回调") 45 | @ApiResponses({ 46 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 47 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 48 | }) 49 | @PostMapping(value = "callback") 50 | @PassToken 51 | public String uploadCallback(@RequestHeader Map header, @RequestParam Map bodyMap, @RequestBody String ossCallbackBody) { 52 | String fileName = bodyMap.get("fileName"); 53 | header.put("uri", "/api/file/callback"); 54 | int userId = JwtUtils.getTokenUserId(bodyMap.get("token")); 55 | if (fileJsonService.getOssUtils().verifyOssCallbackRequest(header, ossCallbackBody)) { 56 | return Msg.getFailMsg(); 57 | } 58 | userJsonService.addUserFile(userId, fileName); 59 | 60 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, null); 61 | } 62 | 63 | /** 64 | * 用户上传头像 65 | * 66 | * @param tokenModel {"token":"token"} 67 | * @return Msg {urlParams: 上传签名Url对象, GetUrl: Get请求Url} 68 | */ 69 | @ApiOperation(value = "用户上传头像") 70 | @ApiResponses({ 71 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 72 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 73 | }) 74 | @PostMapping(value = "getUploadAvatarUrl") 75 | @Permission(value = {"UPLOAD-FILE"}) 76 | public String getUploadAvatarUrl(@RequestBody TokenModel tokenModel) { 77 | int userId = JwtUtils.getTokenUserId(tokenModel.getToken()); 78 | 79 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, fileJsonService.getUploadAvatarMap(tokenModel.getToken())); 80 | } 81 | 82 | /** 83 | * 文章封面上传 84 | * 85 | * @param tokenModel {"token":"token"} 86 | * @return Msg {host: 上传服务的url, urlParams: 上传签名Url对象, GetUrl: Get请求Url} 87 | */ 88 | @ApiOperation(value = "文章封面上传") 89 | @ApiResponses({ 90 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 91 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 92 | }) 93 | @PostMapping(value = "getUploadArticleCoverImageUrl") 94 | @Permission(value = {"UPLOAD-FILE"}) 95 | public String getUploadArticleCoverImageUrl(@RequestBody TokenModel tokenModel) { 96 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, fileJsonService.getUploadArticleCoverImageUrl(tokenModel.getToken())); 97 | } 98 | 99 | /** 100 | * 获取文章图片上传url和Object路径 101 | * 102 | * @param tokenModel {"token":"token"} 103 | * @return Msg {host: 上传服务的url, urlParams: 上传签名Url对象, GetUrl: Get请求Url} 104 | */ 105 | @ApiOperation(value = "获取文章图片上传url和Object路径") 106 | @ApiResponses({ 107 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 108 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 109 | }) 110 | @PostMapping(value = "getUploadArticleImageUrl") 111 | @Permission(value = {"UPLOAD-FILE"}) 112 | public String getUploadArticleImageUrl(@RequestBody TokenModel tokenModel) { 113 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, fileJsonService.getUploadArticleImageUrl(tokenModel.getToken())); 114 | } 115 | 116 | /** 117 | * 删除文件 118 | * 119 | * @param tokenTypeModel { 120 | * "token":String, 121 | * "content": filePath 122 | * } 123 | */ 124 | @ApiOperation(value = "删除文件") 125 | @ApiResponses({ 126 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 127 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 128 | }) 129 | @PostMapping(value = "deleteObject") 130 | @Permission(value = {"DELETE-FILE"}) 131 | public String deleteObject(@RequestBody TokenTypeModel tokenTypeModel) { 132 | int userId = JwtUtils.getTokenUserId(tokenTypeModel.getToken()); 133 | fileJsonService.deleteUserFile(userId, tokenTypeModel.getContent()); 134 | return Msg.getSuccessMsg(); 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/controller/FriendLinkController.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.controller; 2 | 3 | import com.hiyj.blog.annotation.PassToken; 4 | import com.hiyj.blog.annotation.Permission; 5 | import com.hiyj.blog.model.request.ReqFriendLinkModel; 6 | import com.hiyj.blog.model.request.ReqFriendLinkStatusModel; 7 | import com.hiyj.blog.model.response.RspFriendLinkModel; 8 | import com.hiyj.blog.object.FriendLink; 9 | import com.hiyj.blog.object.Msg; 10 | import com.hiyj.blog.services.FriendLinkJsonService; 11 | import io.swagger.annotations.Api; 12 | import io.swagger.annotations.ApiOperation; 13 | import io.swagger.annotations.ApiResponse; 14 | import io.swagger.annotations.ApiResponses; 15 | import lombok.extern.slf4j.Slf4j; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.web.bind.annotation.PostMapping; 18 | import org.springframework.web.bind.annotation.RequestBody; 19 | import org.springframework.web.bind.annotation.RequestMapping; 20 | import org.springframework.web.bind.annotation.RestController; 21 | 22 | @Slf4j 23 | @RestController 24 | @Api(tags = "链接相关", value = "链接相关") 25 | @RequestMapping(value = "/api/links", produces = "application/json;charset=UTF-8") 26 | public class FriendLinkController { 27 | private FriendLinkJsonService friendLinkJsonService; 28 | 29 | @Autowired 30 | public void setFriendLinkJsonService(FriendLinkJsonService friendLinkJsonService) { 31 | this.friendLinkJsonService = friendLinkJsonService; 32 | } 33 | 34 | /** 35 | * 获取友链列表 36 | * 37 | * @param fiendLinkStatusModel 友链状态 {status: FiendLink.Status} 38 | * @return Msg 39 | */ 40 | @ApiOperation(value = "获取友链列表") 41 | @ApiResponses({ 42 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS, response = RspFriendLinkModel.class), 43 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 44 | }) 45 | @PostMapping(value = "getFriendLinks") 46 | @PassToken 47 | public String getFriendLinks(@RequestBody ReqFriendLinkStatusModel fiendLinkStatusModel) { 48 | return friendLinkJsonService.getFriendLinksJson(fiendLinkStatusModel.getStatus()); 49 | } 50 | 51 | /** 52 | * 申请友链 53 | * 54 | * @param fiendLinkModel 友链信息 55 | * @return Msg 56 | */ 57 | @ApiOperation(value = "申请友链") 58 | @ApiResponses({ 59 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 60 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 61 | }) 62 | @PostMapping(value = "applyFriendLink") 63 | @PassToken 64 | public String applyFriendLink(@RequestBody ReqFriendLinkModel fiendLinkModel) { 65 | return friendLinkJsonService.applyFriendLinkJson(fiendLinkModel); 66 | } 67 | 68 | /** 69 | * 设置友链整体对象 70 | * 71 | * @param friendLink 友链对象 72 | * @return Msg 73 | */ 74 | @ApiOperation(value = "设置友链状") 75 | @ApiResponses({ 76 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 77 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 78 | }) 79 | @PostMapping(value = "setFriendLink") 80 | @Permission(value = {"VERIFY-LINK"}) 81 | public String setFriendLink(@RequestBody FriendLink friendLink) { 82 | return friendLinkJsonService.setFriendLinkJson(friendLink); 83 | } 84 | } -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/controller/OtherController.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.controller; 2 | 3 | import com.hiyj.blog.annotation.Permission; 4 | import com.hiyj.blog.object.Msg; 5 | import com.hiyj.blog.services.OtherJsonService; 6 | import io.swagger.annotations.Api; 7 | import io.swagger.annotations.ApiOperation; 8 | import io.swagger.annotations.ApiResponse; 9 | import io.swagger.annotations.ApiResponses; 10 | import lombok.extern.slf4j.Slf4j; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.web.bind.annotation.PostMapping; 13 | import org.springframework.web.bind.annotation.RequestMapping; 14 | import org.springframework.web.bind.annotation.RestController; 15 | 16 | @Slf4j 17 | @RestController 18 | @Api(tags = "后台杂项", value = "后台杂项") 19 | @RequestMapping(value = "/api/other", produces = "application/json;charset=UTF-8") 20 | @Permission(value = {"BACKGROUND-LOGIN"}) 21 | public class OtherController { 22 | private OtherJsonService otherJsonService; 23 | 24 | @Autowired 25 | public void setOtherJsonService(OtherJsonService otherJsonService) { 26 | this.otherJsonService = otherJsonService; 27 | } 28 | 29 | /** 30 | * 获取仪表盘折线图和panel-group部分 31 | * 32 | * @return Msg 33 | */ 34 | @ApiOperation(value = "获取仪表盘折线图和panel-group部分") 35 | @ApiResponses({ 36 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 37 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 38 | }) 39 | @PostMapping(value = "getPanel") 40 | public String getPanel() { 41 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, otherJsonService.getPanel()); 42 | } 43 | 44 | /** 45 | * 获取图表信息 46 | * 47 | * @return Msg 48 | */ 49 | @ApiOperation(value = "获取图表信息") 50 | @ApiResponses({ 51 | @ApiResponse(code = 20000, message = Msg.MSG_SUCCESS), 52 | @ApiResponse(code = -1, message = Msg.MSG_FAIL) 53 | }) 54 | @PostMapping(value = "getChart") 55 | public String getChart() { 56 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, otherJsonService.getChart()); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/doc/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.doc; 2 | 3 | import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver; 4 | import com.hiyj.blog.object.Msg; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.context.annotation.Profile; 9 | import org.springframework.http.HttpMethod; 10 | import springfox.documentation.builders.ApiInfoBuilder; 11 | import springfox.documentation.builders.PathSelectors; 12 | import springfox.documentation.builders.RequestHandlerSelectors; 13 | import springfox.documentation.builders.ResponseBuilder; 14 | import springfox.documentation.service.Contact; 15 | import springfox.documentation.service.Response; 16 | import springfox.documentation.spi.DocumentationType; 17 | import springfox.documentation.spring.web.plugins.Docket; 18 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 19 | 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | @Configuration 24 | @EnableSwagger2 25 | @Profile("dev") 26 | public class SwaggerConfig { 27 | 28 | private OpenApiExtensionResolver openApiExtensionResolver; 29 | 30 | @Autowired 31 | public void setOpenApiExtensionResolver(OpenApiExtensionResolver openApiExtensionResolver) { 32 | this.openApiExtensionResolver = openApiExtensionResolver; 33 | } 34 | 35 | @Bean 36 | public Docket createRestApi() { 37 | List responseMessageList = new ArrayList<>(); 38 | responseMessageList.add( 39 | new ResponseBuilder().code(String.valueOf(Msg.CODE_SUCCESS)).build() 40 | ); 41 | 42 | responseMessageList.add( 43 | new ResponseBuilder().code(String.valueOf(Msg.CODE_FAIL)).build() 44 | ); 45 | 46 | return new Docket(DocumentationType.SWAGGER_2) 47 | .useDefaultResponseMessages(false) 48 | .globalResponses(HttpMethod.POST, responseMessageList) 49 | .globalResponses(HttpMethod.GET, responseMessageList) 50 | .globalResponses(HttpMethod.PUT, responseMessageList) 51 | .globalResponses(HttpMethod.DELETE, responseMessageList) 52 | .pathMapping("/") 53 | .select() 54 | .apis(RequestHandlerSelectors.any()) 55 | .paths(PathSelectors.any()) 56 | .build().host("http://127.0.0.1:8888") 57 | .apiInfo(new ApiInfoBuilder() 58 | .title("w-blog-api接口") 59 | .description("w-blog-api接口") 60 | .contact(new Contact("WindSnowLi", "https://www.blog.firstmeet.xyz/", "windsnowli@163.com")) 61 | .license("The MIT License") 62 | .licenseUrl("https://github.com/WindSnowLi/w-blog-api/blob/master/LICENSE") 63 | .version("0.0.1") 64 | .build()) 65 | .extensions(openApiExtensionResolver.buildExtensions("md")); 66 | } 67 | } -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/exception/GlobalExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.exception; 2 | 3 | import com.auth0.jwt.exceptions.JWTVerificationException; 4 | import org.springframework.web.bind.annotation.ExceptionHandler; 5 | import com.hiyj.blog.object.Msg; 6 | 7 | public class GlobalExceptionHandler { 8 | 9 | /** 10 | * 处理JWTVerificationException异常 11 | */ 12 | @ExceptionHandler(JWTVerificationException.class) 13 | public Msg jWTVerificationExceptionHandler() { 14 | return Msg.makeMsg(Msg.CODE_FAIL, Msg.MSG_FAIL, null); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/interceptor/CodeInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.interceptor; 2 | 3 | import org.springframework.stereotype.Component; 4 | import org.springframework.web.servlet.HandlerInterceptor; 5 | import org.springframework.web.servlet.ModelAndView; 6 | 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | /** 11 | * @author windSnowLi 12 | */ 13 | @Component 14 | public class CodeInterceptor implements HandlerInterceptor { 15 | @Override 16 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 17 | request.setCharacterEncoding("utf-8"); 18 | response.setCharacterEncoding("utf-8"); 19 | return true; 20 | } 21 | 22 | @Override 23 | public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { 24 | request.setCharacterEncoding("utf-8"); 25 | response.setCharacterEncoding("utf-8"); 26 | } 27 | 28 | @Override 29 | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { 30 | request.setCharacterEncoding("utf-8"); 31 | response.setCharacterEncoding("utf-8"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/interceptor/CrosInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.interceptor; 2 | 3 | import org.springframework.stereotype.Component; 4 | import org.springframework.web.servlet.HandlerInterceptor; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | /** 10 | * 拦截器,允许跨域 11 | * 12 | * @author windSnowLi 13 | */ 14 | @Component 15 | public class CrosInterceptor implements HandlerInterceptor { 16 | final public static String OPTIONS = "OPTIONS"; 17 | 18 | @Override 19 | public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { 20 | 21 | 22 | httpServletResponse.setHeader("Access-Control-Allow-Origin", "*"); 23 | 24 | httpServletResponse.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization,token,Accept,X-Requested-With"); 25 | 26 | httpServletResponse.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); 27 | 28 | httpServletResponse.setHeader("Access-Control-Max-Age", "3600"); 29 | String method = httpServletRequest.getMethod(); 30 | 31 | if (OPTIONS.equals(method)) { 32 | httpServletResponse.setStatus(200); 33 | return false; 34 | } 35 | 36 | return true; 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/interceptor/MappingInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.interceptor; 2 | 3 | import org.springframework.stereotype.Component; 4 | import org.springframework.web.method.HandlerMethod; 5 | import org.springframework.web.servlet.HandlerInterceptor; 6 | 7 | import javax.servlet.http.HttpServletRequest; 8 | import javax.servlet.http.HttpServletResponse; 9 | 10 | /** 11 | * @author windSnowLi 12 | */ 13 | @Component 14 | public class MappingInterceptor implements HandlerInterceptor { 15 | @Override 16 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 17 | // 如果不是映射到方法直接拒绝 18 | return handler instanceof HandlerMethod; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/interceptor/PermissionsInterceptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * PermissionsInterceptor.java, 2021-08-26 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.interceptor; 9 | 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Component; 12 | import org.springframework.web.method.HandlerMethod; 13 | import org.springframework.web.servlet.HandlerInterceptor; 14 | import com.hiyj.blog.annotation.PassToken; 15 | import com.hiyj.blog.annotation.Permission; 16 | import com.hiyj.blog.object.Msg; 17 | import com.hiyj.blog.object.Role; 18 | import com.hiyj.blog.object.User; 19 | import com.hiyj.blog.object.base.PermissionRole; 20 | import com.hiyj.blog.services.base.PermissionService; 21 | import com.hiyj.blog.services.base.RoleService; 22 | import com.hiyj.blog.services.base.UserService; 23 | import com.hiyj.blog.utils.JwtUtils; 24 | 25 | import javax.servlet.http.HttpServletRequest; 26 | import javax.servlet.http.HttpServletResponse; 27 | import java.lang.reflect.Method; 28 | import java.util.List; 29 | 30 | /** 31 | * @author windSnowLi 32 | */ 33 | @Component 34 | public class PermissionsInterceptor implements HandlerInterceptor { 35 | 36 | private UserService userService; 37 | 38 | @Autowired 39 | public void setUserService(UserService userService) { 40 | this.userService = userService; 41 | } 42 | 43 | private PermissionService permissionService; 44 | 45 | @Autowired 46 | public void setPermissionService(PermissionService permissionService) { 47 | this.permissionService = permissionService; 48 | } 49 | 50 | private RoleService roleService; 51 | 52 | @Autowired 53 | public void setRoleService(RoleService roleService) { 54 | this.roleService = roleService; 55 | } 56 | @Override 57 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception { 58 | HandlerMethod handlerMethod = (HandlerMethod) object; 59 | Method method = handlerMethod.getMethod(); 60 | //检查是否有passToken注释,有则跳过认证 61 | if (method.isAnnotationPresent(PassToken.class)) { 62 | PassToken passToken = method.getAnnotation(PassToken.class); 63 | if (passToken.required()) { 64 | return true; 65 | } 66 | } 67 | if (method.isAnnotationPresent(Permission.class) || method.getDeclaringClass().isAnnotationPresent(Permission.class)) { 68 | Permission permissionClass = method.getAnnotation(Permission.class) == null ? 69 | method.getDeclaringClass().getAnnotation(Permission.class) : 70 | method.getAnnotation(Permission.class); 71 | if (permissionClass.value().length == 0) { 72 | response.getWriter().println(Msg.getFailMsg("没有相应权限")); 73 | return false; 74 | } else { 75 | // 从 http 请求头中取出 token 76 | String token = request.getHeader("token"); 77 | List rolePermissionsName; 78 | if (token == null || token.equals("")) { 79 | //获取默认的访问角色权限 80 | rolePermissionsName = permissionService.getRolePermissionsName( 81 | roleService.getRoleByName(Role.VISITOR).getId(), PermissionRole.Status.NORMAL); 82 | } else { 83 | // 获取 token 中的 user id 84 | int userId = JwtUtils.getTokenUserId(token); 85 | User user = userService.findUserById(userId); 86 | String userAccount = JwtUtils.getTokenUserAccount(token); 87 | String userPassword = JwtUtils.getTokenUserPassword(token); 88 | if (user == null || !user.getPassword().equals(userPassword) || !user.getAccount().equals(userAccount)) { 89 | response.getWriter().println(Msg.getFailMsg("身份验证错误")); 90 | return false; 91 | } 92 | rolePermissionsName = permissionService.getUserPermissionsName(userId); 93 | } 94 | 95 | for (String value : permissionClass.value()) { 96 | // 如果用户不包含所需权限则不允许访问 97 | if (!rolePermissionsName.contains(value)) { 98 | response.getWriter().println(Msg.getFailMsg("没有相应权限")); 99 | return false; 100 | } 101 | } 102 | } 103 | } 104 | return true; 105 | } 106 | } -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/mapper/ArticleLabelMapper.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.mapper; 2 | 3 | import com.hiyj.blog.object.ArticleLabel; 4 | import com.hiyj.blog.object.ArticleType; 5 | import com.hiyj.blog.object.base.LabelBase; 6 | import org.apache.ibatis.annotations.Delete; 7 | import org.apache.ibatis.annotations.Mapper; 8 | import org.apache.ibatis.annotations.Param; 9 | import org.apache.ibatis.annotations.Select; 10 | import org.springframework.stereotype.Repository; 11 | 12 | import java.util.List; 13 | import java.util.Map; 14 | 15 | @Mapper 16 | @Repository 17 | public interface ArticleLabelMapper { 18 | /** 19 | * 通过类型ID获取类型 20 | * 21 | * @param typeId 标签ID 22 | * @return 类型对象 23 | */ 24 | LabelBase getTypeById(@Param("typeId") int typeId); 25 | 26 | /** 27 | * 通过类型名获取类型 28 | * 29 | * @param typeName 类型名 30 | * @return 类型对象 31 | */ 32 | ArticleType getTypeByName(@Param("typeName") String typeName); 33 | 34 | /** 35 | * 通过标签ID获取标签 36 | * 37 | * @param labelId 标签ID 38 | * @return 标签对象 39 | */ 40 | ArticleLabel getLabelById(@Param("labelId") int labelId); 41 | 42 | /** 43 | * 通过文章ID获取文章所属类型ID 44 | * 45 | * @param articleId 文章ID 46 | * @return 文章类型ID 47 | */ 48 | int getArticleTypeById(@Param("articleId") int articleId); 49 | 50 | /** 51 | * 添加文章——分类映射 52 | * 53 | * @param articleId 文章ID 54 | * @param typeId 所属类型 55 | */ 56 | void addArticleMapType(@Param("articleId") int articleId, @Param("typeId") int typeId); 57 | 58 | /** 59 | * 获取所有标签 60 | * 61 | * @return List ArticleLabel 62 | */ 63 | List getAllLabel(); 64 | 65 | /** 66 | * 清空文章标签 67 | * 68 | * @param articleId 文章标签 69 | */ 70 | @Delete("DELETE FROM article_map_label WHERE article_id=#{articleId}") 71 | void deleteLabels(@Param("articleId") int articleId); 72 | 73 | /** 74 | * 清空文章分类 75 | * 76 | * @param articleId 文章标签 77 | */ 78 | @Delete("DELETE FROM article_map_type WHERE article_id=#{articleId}") 79 | void deleteType(@Param("articleId") int articleId); 80 | 81 | /** 82 | * 按分类获取每个分类多少文章 83 | * 84 | * @param limit 取最多的前几条 85 | * @return [{name=String, value=Object}] 86 | */ 87 | @Select("select t.num as value, al.name from " + 88 | "( " + 89 | "select count(*) as num, amt.type_id from article_map_type amt group by type_id order by num desc limit #{limit} " + 90 | ") " + 91 | "t " + 92 | "LEFT JOIN article_label al on t.type_id = al.id " + 93 | "order by num desc ") 94 | List> getArticleCountByType(int limit); 95 | 96 | /** 97 | * 获取所有分类 98 | * 99 | * @return List ArticleLabel 100 | */ 101 | List getTypes(); 102 | 103 | /** 104 | * 添加新标签 105 | * 106 | * @param articleLabels 标签列表 107 | */ 108 | void addLabels(@Param("articleLabels") List articleLabels); 109 | 110 | /** 111 | * 根据名字批量检查已经存在的标签 112 | * 113 | * @param articleLabels 文章标签对象列表 114 | * @return 已经存在的标签 115 | */ 116 | List batchCheckLabelByNames(@Param("articleLabels") List articleLabels); 117 | 118 | /** 119 | * 根据标签名批量查询标签对象 120 | * 121 | * @param articleLabels 文章标签对象列表 122 | * @return 标签对象列表 123 | */ 124 | List batchFindLabelByNames(@Param("articleLabels") List articleLabels); 125 | 126 | /** 127 | * 分页获取标签 128 | * 129 | * @param limit 限制数 130 | * @param offset 偏移量 131 | * @return List 132 | */ 133 | List getLabelByPage(@Param("limit") int limit, @Param("offset") int offset); 134 | 135 | /** 136 | * 设置标签内容 137 | * 138 | * @param articleLabel 标签对象 139 | */ 140 | void setLabel(@Param("articleLabel") ArticleLabel articleLabel); 141 | 142 | /** 143 | * 获取标签所属文章总数 144 | * 145 | * @param id 标签ID 146 | * @return 总数 147 | */ 148 | int getArtSumLabel(@Param("id") int id); 149 | 150 | /** 151 | * 获取分类所属文章总数 152 | * 153 | * @param id 分类ID 154 | * @return 总数 155 | */ 156 | int getArtSumType(@Param("id") int id); 157 | 158 | /** 159 | * 通过标签ID获取标签访问量 160 | * 161 | * @param id 标签ID 162 | * @return 总数 163 | */ 164 | int getLabelPV(@Param("id") int id); 165 | 166 | /** 167 | * 通过分类ID获取分类访问量 168 | * 169 | * @param id 分类ID 170 | * @return 总数 171 | */ 172 | int getTypePV(@Param("id") int id); 173 | 174 | } 175 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/mapper/CommentMapper.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.mapper; 2 | 3 | import com.hiyj.blog.object.Comment; 4 | import com.hiyj.blog.object.base.CommentBase; 5 | import org.apache.ibatis.annotations.*; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | @Mapper 12 | @Repository 13 | public interface CommentMapper { 14 | /** 15 | * 添加评论 16 | * 17 | * @param comment 评论对象 18 | */ 19 | void addComment(@Param("comment") Comment comment); 20 | 21 | /** 22 | * 获取目标评论 23 | * 24 | * @param targetId 评论的目标ID 25 | * @param sessionType 会话类型 26 | * @param status 评论状态 27 | * @return 评论对象列表 28 | */ 29 | List getTargetComments(@Param("targetId") Integer targetId, @Param("sessionType") CommentBase.SessionType sessionType, @Param("status") CommentBase.Status status); 30 | 31 | 32 | /** 33 | * 根据记录ID查找某一条记录 34 | * 35 | * @param commentId 记录ID 36 | * @return Comment对象 37 | */ 38 | Comment findComment(@Param("commentId") int commentId); 39 | 40 | /** 41 | * 设置评论状态 42 | * 43 | * @param commentId 评论ID 44 | * @param status 新状态 45 | */ 46 | void setCommentStatus(@Param("commentId") int commentId, @Param("status") CommentBase.Status status); 47 | 48 | /** 49 | * 逆序分页获取文章 50 | * 51 | * @param limit 限制量 52 | * @param offset 偏移量 53 | * @param sort 排序方式 默认-id, 54 | * @param status 评论状态 55 | * @return 评论列表 56 | */ 57 | List getCommentList(@Param("limit") int limit, @Param("offset") int offset, @Param("sort") String sort, @Param("status") CommentBase.Status status); 58 | 59 | /** 60 | * 获取最近评论趋势 61 | * 62 | * @param limit 限制最近多少天,没评论的天忽略不计 63 | * @param status 查询的评论状态 64 | * @return List, 日期数值键值对{total=int, day_time=String} 65 | */ 66 | List> getCommentLogByDay(@Param("limit") int limit, @Param("status") CommentBase.Status status); 67 | 68 | /** 69 | * 获取当前状态评论总量 70 | * 71 | * @param status 评论状态 72 | * @return 总数 73 | */ 74 | int getCommentCount(@Param("status") CommentBase.Status status); 75 | 76 | /** 77 | * 获取所有文章最新的评论 78 | * 79 | * @param limit 条数限制 80 | * @return Comment list 81 | */ 82 | List getRecentComment(int limit); 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/mapper/FileMapper.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.mapper; 2 | 3 | import org.apache.ibatis.annotations.*; 4 | import org.springframework.stereotype.Repository; 5 | 6 | @Mapper 7 | @Repository 8 | public interface FileMapper { 9 | /** 10 | * 添加用户文件映射 11 | * 12 | * @param userId 用户ID 13 | * @param fileName 文件名 14 | */ 15 | @Insert("INSERT INTO user_map_file (user_id, file_name) VALUES(#{userId}, #{fileName});") 16 | void addUserFile(@Param("userId") int userId, @Param("fileName") String fileName); 17 | 18 | /** 19 | * 删除用户文件映射 20 | * 21 | * @param userId 用户ID 22 | * @param fileName 文件名 23 | */ 24 | @Delete("DELETE FROM user_map_file" + 25 | "WHERE user_id=#{userId} AND file_name=#{fileName}") 26 | void deleteUserFile(@Param("userId") int userId, @Param("fileName") String fileName); 27 | 28 | 29 | /** 30 | * 检查文件映射 31 | * 32 | * @param userId 用户ID 33 | * @param object 文件路径 34 | * @return 是否映射正确 35 | */ 36 | @Select("SELECT COUNT(*) FROM user_map_file WHERE " + 37 | "user_map_file.user_id = #{userId} and " + 38 | "user_map_file.file_name = #{object}") 39 | boolean checkFileMap(@Param("userId") int userId, @Param("object") String object); 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/mapper/LinkMapper.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.mapper; 2 | 3 | import com.hiyj.blog.object.FriendLink; 4 | import org.apache.ibatis.annotations.Mapper; 5 | import org.apache.ibatis.annotations.Param; 6 | import org.apache.ibatis.annotations.Update; 7 | 8 | import java.util.List; 9 | 10 | @Mapper 11 | public interface LinkMapper { 12 | /** 13 | * 获取友链列表 14 | * 15 | * @param status 友链状态 16 | * @return List 17 | */ 18 | List getFriendLinks(@Param("status") FriendLink.Status status); 19 | 20 | /** 21 | * 申请友链 22 | * 23 | * @param friendLink 友链对象 24 | */ 25 | void applyFriendLink(@Param("friendLink") FriendLink friendLink); 26 | 27 | /** 28 | * 设置友链整体对象 29 | * 30 | * @param friendLink 友链对象 31 | */ 32 | void setFriendLink(@Param("friendLink") FriendLink friendLink); 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/mapper/PermissionMapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * PermissionMapper.java, 2021-08-26 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.mapper; 9 | 10 | import com.hiyj.blog.object.Permission; 11 | import com.hiyj.blog.object.base.PermissionRole; 12 | import org.apache.ibatis.annotations.Mapper; 13 | import org.apache.ibatis.annotations.Param; 14 | 15 | import java.util.List; 16 | 17 | @Mapper 18 | public interface PermissionMapper { 19 | 20 | /** 21 | * 获取角色权限对象列表 22 | * 23 | * @param roleId 角色ID 24 | * @param status 权限状态 25 | * @return 权限对象List 26 | */ 27 | List getRolePermissions(@Param("roleId") int roleId, @Param("status") PermissionRole.Status status); 28 | 29 | /** 30 | * 获取角色权限ID列表 31 | * 32 | * @param roleId 角色ID 33 | * @param status 权限状态 34 | * @return 权限ID List 35 | */ 36 | List getRolePermissionsId(@Param("roleId") int roleId, @Param("status") PermissionRole.Status status); 37 | 38 | /** 39 | * 获取角色权限name列表 40 | * 41 | * @param roleId 角色ID 42 | * @param status 权限状态 43 | * @return 权限name List 44 | */ 45 | List getRolePermissionsName(@Param("roleId") int roleId, @Param("status") PermissionRole.Status status); 46 | 47 | /** 48 | * 获取用户可用权限对象列表 49 | * 50 | * @param userId 角色ID 51 | * @return 权限对象List 52 | */ 53 | List getUserPermissions(@Param("userId") int userId); 54 | 55 | /** 56 | * 获取用户可用权限ID列表 57 | * 58 | * @param userId 角色ID 59 | * @return 权限ID List 60 | */ 61 | List getUserPermissionsId(@Param("userId") int userId); 62 | 63 | /** 64 | * 获取用户可用权限name列表 65 | * 66 | * @param userId 角色ID 67 | * @return 权限name List 68 | */ 69 | List getUserPermissionsName(@Param("userId") int userId); 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/mapper/RoleMapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * RoleMapper.java, 2021-08-26 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.mapper; 9 | 10 | import com.hiyj.blog.object.Role; 11 | import com.hiyj.blog.object.base.PermissionRole; 12 | import org.apache.ibatis.annotations.Mapper; 13 | import org.apache.ibatis.annotations.Param; 14 | import org.apache.ibatis.annotations.Select; 15 | 16 | import java.util.List; 17 | 18 | @Mapper 19 | public interface RoleMapper { 20 | /** 21 | * 根据用户ID和角色状态获取用户角色表 22 | * 23 | * @param userId 用户ID 24 | * @param status 角色状态 25 | * @return List 26 | */ 27 | List getRoles(@Param("userId") int userId, @Param("status") PermissionRole.Status status); 28 | 29 | /** 30 | * 获取用户角色ID列表 31 | * 32 | * @param userId 用户ID 33 | * @param status 角色状态 34 | * @return 角色ID List 35 | */ 36 | List getRolesId(@Param("userId") int userId, @Param("status") PermissionRole.Status status); 37 | 38 | /** 39 | * 获取用户角色name列表 40 | * 41 | * @param userId 用户ID 42 | * @param status 角色状态 43 | * @return 角色name List 44 | */ 45 | List getRolesName(@Param("userId") int userId, @Param("status") PermissionRole.Status status); 46 | 47 | /** 48 | * 根据Role名字获取角色对象 49 | * 50 | * @param name 角色名 51 | * @return 角色对象 52 | */ 53 | @Select("select * from `role` r where r.name = #{name}") 54 | Role getRoleByName(@Param("name") String name); 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/mapper/SysConfigMapper.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.mapper; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.hiyj.blog.model.share.ClientModel; 5 | import org.apache.ibatis.annotations.*; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | @Mapper 12 | @Repository 13 | public interface SysConfigMapper { 14 | 15 | /** 16 | * 获取用户配置表 17 | * 18 | * @param userId 用户ID 19 | * @return 配置表 20 | */ 21 | List> getUiConfigByUserId(@Param("userId") int userId); 22 | 23 | /** 24 | * 删除用户的所有UI配置 25 | * 26 | * @param userId 用户和ID 27 | */ 28 | void deleteUiConfigByUserId(@Param("userId") int userId); 29 | 30 | /** 31 | * 设置某个用户的UI配置 32 | * 33 | * @param userId 用户ID 34 | * @param configMap 配置表 35 | */ 36 | void setUiConfigByUserId(@Param("userId") int userId, @Param("configMap") Map configMap); 37 | 38 | /** 39 | * 获取OSS的相关配置 40 | * 41 | * @return json配置串 42 | */ 43 | @Select("SELECT value FROM sys_setting WHERE item = \"oss\"") 44 | String getOSSConfig(); 45 | 46 | /** 47 | * 获取系统配置,其中OSS配置信息只有在系统专属设置存储信息时才允许单独查询 48 | * 49 | * @return 系统配置表 50 | */ 51 | @Select("SELECT value FROM sys_setting WHERE item = 'base_sys'") 52 | String getFixedConfig(); 53 | 54 | /** 55 | * 设置系统配置 56 | * 57 | * @param config 配置表 58 | */ 59 | void setFixedConfig(@Param("config") String config); 60 | 61 | /** 62 | * 设置系统存储配置文件 63 | * 64 | * @param config 存储设置整体对象 65 | */ 66 | @Insert("UPDATE sys_setting SET value=#{config} WHERE item='oss'") 67 | void setStorageConfig(@Param("config") String config); 68 | 69 | /** 70 | * 获取第三方登录配置信息 71 | * 72 | * @return JSON格式配置信息 73 | */ 74 | @Select("SELECT value FROM sys_setting WHERE item = 'other_login'") 75 | String getOtherLoginConfig(); 76 | 77 | /** 78 | * 设置Gitee登录配置 79 | */ 80 | @Update("UPDATE sys_setting SET value=#{config} WHERE item='other_login'") 81 | void setGiteeConfig(@Param("config") String config); 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.mapper; 2 | 3 | import com.hiyj.blog.object.OtherUser; 4 | import com.hiyj.blog.object.User; 5 | import org.apache.ibatis.annotations.*; 6 | import org.springframework.stereotype.Repository; 7 | 8 | import java.util.List; 9 | 10 | @Mapper 11 | @Repository 12 | public interface UserMapper { 13 | /** 14 | * 查找用户 15 | * 16 | * @param account 账号 17 | * @return 对象 18 | */ 19 | @Select("select * from user where user.account=#{account}") 20 | User findUserAccount(@Param("account") String account); 21 | 22 | /** 23 | * 查找用户 24 | * 25 | * @param id 账号 26 | * @return 对象 27 | */ 28 | @Select("select * from user where user.id=#{id}") 29 | User findUserId(@Param("id") int id); 30 | 31 | /** 32 | * 设置用户信息 33 | * 34 | * @param user 用户新对象 35 | */ 36 | @Update("UPDATE `user` SET nickname=#{user.nickname}, qq=#{user.qq}, introduction=#{user.introduction} WHERE id=#{user.id};") 37 | void setInfo(@Param("user") User user); 38 | 39 | /** 40 | * 获取用户喜好分类占比 41 | * 42 | * @param userId 用户ID 43 | * @return List [{all_count=int, name=String}] 44 | */ 45 | List getActivityByUserId(@Param("userId") int userId); 46 | 47 | /** 48 | * 设置用户头像 49 | * 50 | * @param userId 用户ID 51 | * @param avatarUrl 头像链接 52 | */ 53 | @Select("UPDATE `user` SET avatar=#{avatarUrl} WHERE id=#{userId};") 54 | void setAvatar(@Param("userId") int userId, @Param("avatarUrl") String avatarUrl); 55 | 56 | /** 57 | * 获取作者关于信息 58 | * 59 | * @param userId 用户ID 60 | * @return String 61 | */ 62 | @Select("SELECT value FROM ui_config WHERE user_id = #{userId} AND item = \"about\"") 63 | String getAboutByUserId(@Param("userId") int userId); 64 | 65 | /** 66 | * 设置作者关于信息 67 | * 68 | * @param userId 用户ID 69 | * @param content 内容 70 | */ 71 | @Insert("REPLACE INTO ui_config(user_id, item, value) VALUES (#{userId}, \"about\", #{content})") 72 | void setAbout(int userId, String content); 73 | 74 | /** 75 | * 添加用户 76 | * 77 | * @param user 用户对象 78 | */ 79 | void addUser(@Param("user") User user); 80 | 81 | /** 82 | * 添加第三方平台登录用户 83 | * 84 | * @param otherUser 第三方对象 85 | */ 86 | @Insert("INSERT INTO " + 87 | "other_user (other_id, other_platform, user_id, " + 88 | "access_token, expires_in, refresh_token, scope, created_at) " + 89 | "VALUES(#{otherUser.other_id}, #{otherUser.other_platform}, #{otherUser.user_id}," + 90 | "#{otherUser.access_token},#{otherUser.expires_in}," + 91 | "#{otherUser.refresh_token},#{otherUser.scope},#{otherUser.created_at})") 92 | void addOtherUser(@Param("otherUser") OtherUser otherUser); 93 | 94 | /** 95 | * 刷新第三方用户验证信息 96 | * 97 | * @param otherUser 第三方信息对象 98 | */ 99 | @Update("UPDATE other_user " + 100 | "SET access_token=#{otherUser.access_token}, refresh_token=#{otherUser.refresh_token}, " + 101 | "`scope`=#{otherUser.scope}," + 102 | "created_at=#{otherUser.created_at}, expires_in=#{otherUser.expires_in} " + 103 | "WHERE other_id=#{otherUser.other_id} AND other_platform=#{otherUser.other_platform}") 104 | void refreshInfo(@Param("otherUser") OtherUser otherUser); 105 | 106 | /** 107 | * 查找第三方登录账户 108 | * 109 | * @param other_id 第三方识别码 110 | * @param platform 平台 111 | * @return 第三方对象 112 | */ 113 | @Select("SELECT id, other_id, other_platform, user_id FROM other_user WHERE other_id=#{other_id} AND other_platform=#{platform}") 114 | OtherUser getOtherUser(@Param("other_id") String other_id, @Param("platform") User.Platform platform); 115 | 116 | /** 117 | * 添加用户-角色映射 118 | * 119 | * @param userId 用户ID 120 | * @param roleId 角色ID 121 | */ 122 | @Insert("INSERT INTO user_map_role (user_id, role_id) VALUES(#{userId}, #{roleId})") 123 | void addUserMapRole(@Param("userId") int userId, @Param("roleId") int roleId); 124 | } -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/request/ArticlePageModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.request; 2 | 3 | import com.alibaba.fastjson.annotation.JSONField; 4 | import com.hiyj.blog.object.Article; 5 | import io.swagger.annotations.ApiModel; 6 | import io.swagger.annotations.ApiModelProperty; 7 | import lombok.Getter; 8 | import lombok.Setter; 9 | import lombok.ToString; 10 | 11 | @ApiModel 12 | @ToString 13 | @Setter 14 | @Getter 15 | public class ArticlePageModel extends PageModel { 16 | //用户ID 17 | @ApiModelProperty(value = "所查用户ID") 18 | @JSONField(defaultValue = "-1") 19 | protected int userId; 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/request/CodeUrlModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.request; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | import lombok.ToString; 8 | 9 | @ApiModel 10 | @ToString 11 | @Setter 12 | @Getter 13 | public class CodeUrlModel { 14 | // 授权码 15 | @ApiModelProperty(value = "授权码", required = true) 16 | private String code; 17 | 18 | // 重定向URL 19 | @ApiModelProperty(value = "重定向URL", required = true) 20 | private String redirect; 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/request/ContentModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.request; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | import lombok.ToString; 8 | 9 | @ApiModel 10 | @ToString 11 | @Setter 12 | @Getter 13 | public class ContentModel { 14 | //负载 15 | @ApiModelProperty(value = "负载", required = true) 16 | protected T content; 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/request/IdModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.request; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | import lombok.ToString; 8 | 9 | @ApiModel 10 | @Getter 11 | @Setter 12 | @ToString 13 | public class IdModel { 14 | //ID 15 | @ApiModelProperty(value = "ID信息", required = true) 16 | protected int id; 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/request/IdTokenModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * IdTokenModel.java, 2021-08-26 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.model.request; 9 | 10 | import io.swagger.annotations.ApiModel; 11 | import io.swagger.annotations.ApiModelProperty; 12 | import lombok.Getter; 13 | import lombok.Setter; 14 | import lombok.ToString; 15 | 16 | @ApiModel 17 | @ToString 18 | @Setter 19 | @Getter 20 | public class IdTokenModel extends IdModel { 21 | // token 22 | @ApiModelProperty(value = "身份验证信息", required = true) 23 | protected String token; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/request/IdTokenTypeModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * IdTokenTypeModel.java, 2021-08-26 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.model.request; 9 | 10 | import io.swagger.annotations.ApiModel; 11 | import io.swagger.annotations.ApiModelProperty; 12 | import lombok.Getter; 13 | import lombok.Setter; 14 | import lombok.ToString; 15 | 16 | @ApiModel 17 | @ToString 18 | @Setter 19 | @Getter 20 | public class IdTokenTypeModel extends IdTokenModel { 21 | // 负载 22 | @ApiModelProperty(value = "负载信息", required = true) 23 | private T content; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/request/IdTypeModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.request; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | import lombok.ToString; 8 | 9 | @ApiModel 10 | @ToString 11 | @Setter 12 | @Getter 13 | public class IdTypeModel extends IdModel { 14 | // 负载 15 | @ApiModelProperty(value = "负载信息", required = true) 16 | private T content; 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/request/PageBaseModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * PageBaseModel.java, 2021-08-26 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.model.request; 9 | 10 | import com.alibaba.fastjson.annotation.JSONField; 11 | import io.swagger.annotations.ApiModel; 12 | import io.swagger.annotations.ApiModelProperty; 13 | import lombok.Getter; 14 | import lombok.Setter; 15 | import lombok.ToString; 16 | 17 | @ApiModel 18 | @ToString 19 | @Setter 20 | @Getter 21 | public class PageBaseModel { 22 | //页数 23 | @ApiModelProperty(value = "页数") 24 | @JSONField(defaultValue = "1") 25 | protected int page; 26 | 27 | //一页行数 28 | @ApiModelProperty(value = "一页行数") 29 | @JSONField(defaultValue = "10") 30 | protected int limit; 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/request/PageModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.request; 2 | 3 | import com.alibaba.fastjson.annotation.JSONField; 4 | import io.swagger.annotations.ApiModel; 5 | import io.swagger.annotations.ApiModelProperty; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | import lombok.ToString; 9 | 10 | @ApiModel 11 | @ToString 12 | @Setter 13 | @Getter 14 | public class PageModel extends PageBaseModel{ 15 | 16 | //排序方式,默认逆序 17 | @ApiModelProperty(value = "排序方式,默认逆序,+id正序,-id逆序") 18 | @JSONField(defaultValue = "-id") 19 | protected String sort; 20 | 21 | //分页查询状态 22 | @JSONField(defaultValue = "状态,ALL为全部") 23 | protected T status; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/request/ReqCommentModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.request; 2 | 3 | import com.hiyj.blog.object.base.CommentBase; 4 | import io.swagger.annotations.ApiModel; 5 | import io.swagger.annotations.ApiModelProperty; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | import lombok.ToString; 9 | 10 | @ApiModel 11 | @Getter 12 | @Setter 13 | @ToString 14 | public class ReqCommentModel extends CommentBase { 15 | //获取的评论状态 16 | @ApiModelProperty(name = "status", value = "获取的评论状态", required = true) 17 | protected CommentBase.Status status; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/request/ReqFriendLinkModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.request; 2 | 3 | import com.hiyj.blog.object.FriendLink; 4 | import io.swagger.annotations.ApiModel; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | import lombok.ToString; 8 | 9 | @ApiModel 10 | @ToString 11 | @Setter 12 | @Getter 13 | public class ReqFriendLinkModel extends FriendLink { 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/request/ReqFriendLinkStatusModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.request; 2 | 3 | import com.hiyj.blog.object.FriendLink; 4 | import io.swagger.annotations.ApiModel; 5 | import io.swagger.annotations.ApiModelProperty; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | import lombok.ToString; 9 | 10 | @ApiModel 11 | @ToString 12 | @Setter 13 | @Getter 14 | public class ReqFriendLinkStatusModel { 15 | // 状态 16 | @ApiModelProperty(value = "友链状态", required = true) 17 | private FriendLink.Status status; 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/request/TokenModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.request; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | import lombok.ToString; 8 | 9 | @ApiModel 10 | @ToString 11 | @Setter 12 | @Getter 13 | public class TokenModel { 14 | public TokenModel(String token) { 15 | this.token = token; 16 | } 17 | 18 | public TokenModel() { 19 | } 20 | 21 | // token 22 | @ApiModelProperty(value = "身份验证信息", required = true) 23 | protected String token; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/request/TokenTypeModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.request; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | import lombok.ToString; 8 | 9 | @ApiModel 10 | @ToString 11 | @Setter 12 | @Getter 13 | public class TokenTypeModel extends TokenModel{ 14 | // 负载 15 | @ApiModelProperty(value = "负载信息", required = true) 16 | protected T content; 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/response/ClientIdModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.response; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | import lombok.ToString; 8 | 9 | @ApiModel 10 | @ToString 11 | @Setter 12 | @Getter 13 | public class ClientIdModel { 14 | public ClientIdModel() { 15 | } 16 | 17 | public ClientIdModel(String clientId) { 18 | this.clientId = clientId; 19 | } 20 | 21 | // token 22 | @ApiModelProperty(value = "客户端ID", required = true) 23 | protected String clientId; 24 | 25 | //是否开启Gitee登录 26 | @ApiModelProperty(value = "是否开启Gitee登录", required = true) 27 | protected boolean status; 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/response/RspCommentsModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.response; 2 | 3 | import com.alibaba.fastjson.annotation.JSONField; 4 | import com.alibaba.fastjson.serializer.SerializerFeature; 5 | import com.hiyj.blog.object.Comment; 6 | import io.swagger.annotations.ApiModel; 7 | import io.swagger.annotations.ApiModelProperty; 8 | import lombok.Getter; 9 | import lombok.Setter; 10 | import lombok.ToString; 11 | 12 | import java.util.List; 13 | 14 | @ApiModel 15 | @Getter 16 | @Setter 17 | @ToString 18 | public class RspCommentsModel extends Comment { 19 | 20 | //子评论列表,用于应答时生成记录表 21 | @JSONField(serialzeFeatures = {SerializerFeature.WriteMapNullValue}) 22 | @ApiModelProperty(name = "childList", value = "子评论列表,用于应答时生成记录表") 23 | protected List childList; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/response/RspFriendLinkModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.response; 2 | 3 | import com.hiyj.blog.object.FriendLink; 4 | import io.swagger.annotations.ApiModel; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | import lombok.ToString; 8 | 9 | @ApiModel 10 | @ToString 11 | @Setter 12 | @Getter 13 | public class RspFriendLinkModel extends FriendLink { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/response/RspListType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * RspListType.java, 2021-08-25 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.model.response; 9 | 10 | import io.swagger.annotations.ApiModel; 11 | import io.swagger.annotations.ApiModelProperty; 12 | import lombok.Getter; 13 | import lombok.Setter; 14 | import lombok.ToString; 15 | 16 | import java.util.List; 17 | 18 | @ApiModel 19 | @ToString 20 | @Setter 21 | @Getter 22 | public class RspListType { 23 | // 总数 24 | @ApiModelProperty(name = "total", value = "总数") 25 | protected int total; 26 | 27 | @ApiModelProperty(name = "items", value = "列表数据") 28 | protected List items; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/model/share/ClientModel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.model.share; 2 | 3 | import com.hiyj.blog.model.response.ClientIdModel; 4 | import io.swagger.annotations.ApiModel; 5 | import io.swagger.annotations.ApiModelProperty; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | import lombok.ToString; 9 | 10 | @ApiModel 11 | @ToString 12 | @Setter 13 | @Getter 14 | public class ClientModel extends ClientIdModel { 15 | public ClientModel(String clientSecret) { 16 | this.clientSecret = clientSecret; 17 | } 18 | 19 | public ClientModel(String clientId, String clientSecret) { 20 | super(clientId); 21 | this.clientSecret = clientSecret; 22 | } 23 | 24 | //客户端秘钥 25 | @ApiModelProperty(value = "客户端秘钥", required = true) 26 | protected String clientSecret; 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/Article.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.object; 2 | 3 | import com.alibaba.fastjson.annotation.JSONField; 4 | import com.hiyj.blog.object.base.LabelBase; 5 | import lombok.EqualsAndHashCode; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | import lombok.ToString; 9 | 10 | import java.io.Serializable; 11 | import java.util.Date; 12 | import java.util.List; 13 | 14 | @Getter 15 | @Setter 16 | @ToString 17 | @EqualsAndHashCode 18 | public class Article implements Serializable { 19 | public enum Status { 20 | PUBLISHED, 21 | DRAFT, 22 | DELETED, 23 | ALL 24 | } 25 | 26 | public enum PublishType { 27 | //原创 28 | ORIGINAL, 29 | //转载 30 | REPRINT, 31 | //翻译 32 | TRANSLATE 33 | } 34 | 35 | //文章ID 36 | private int id; 37 | //文章标题 38 | private String title; 39 | //摘要 40 | private String summary; 41 | //内容 42 | private String content; 43 | //头像链接 44 | private String coverPic; 45 | //文章分类 46 | private LabelBase articleType; 47 | //文章标签 48 | private List labels; 49 | //发布时间 50 | @JSONField(format = "yyyy-MM-dd HH:mm:ss") 51 | private Date createTime; 52 | //浏览次数 53 | private int pv; 54 | //最后更新时间 55 | @JSONField(format = "yyyy-MM-dd HH:mm:ss") 56 | private Date updateTime; 57 | //状态 PUBLISHED发布、DRAFT草稿 58 | private Status status; 59 | //文章是否禁用评论,默认不禁用 60 | private boolean commentDisabled; 61 | //发布类型 62 | private PublishType publishType; 63 | } -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/ArticleLabel.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.object; 2 | 3 | import com.hiyj.blog.object.base.LabelBase; 4 | import io.swagger.annotations.ApiModel; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | import lombok.ToString; 8 | 9 | import java.io.Serializable; 10 | 11 | @ApiModel 12 | @Getter 13 | @Setter 14 | @ToString 15 | public class ArticleLabel extends LabelBase implements Serializable { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/ArticleType.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.object; 2 | 3 | import com.hiyj.blog.object.base.LabelBase; 4 | import io.swagger.annotations.ApiModel; 5 | import lombok.Getter; 6 | import lombok.Setter; 7 | import lombok.ToString; 8 | 9 | import java.io.Serializable; 10 | 11 | @ApiModel 12 | @Getter 13 | @Setter 14 | @ToString 15 | public class ArticleType extends LabelBase implements Serializable { 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/CakeChartData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * CakeChartData.java, 2021-08-25 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.object; 9 | 10 | import lombok.Getter; 11 | import lombok.Setter; 12 | import lombok.ToString; 13 | 14 | import java.io.Serializable; 15 | import java.util.List; 16 | import java.util.Map; 17 | 18 | @Getter 19 | @Setter 20 | @ToString 21 | public class CakeChartData implements Serializable { 22 | private List dataName; 23 | private List> data; 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/Comment.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.object; 2 | 3 | import com.alibaba.fastjson.annotation.JSONField; 4 | import com.hiyj.blog.object.base.CommentBase; 5 | import io.swagger.annotations.ApiModel; 6 | import io.swagger.annotations.ApiModelProperty; 7 | import lombok.Getter; 8 | import lombok.Setter; 9 | import lombok.ToString; 10 | 11 | import java.io.Serializable; 12 | import java.util.Date; 13 | 14 | @ApiModel 15 | @Getter 16 | @Setter 17 | @ToString 18 | public class Comment extends CommentBase implements Serializable { 19 | 20 | //评论人 21 | @ApiModelProperty(name = "fromUser", value = "评论人", required = true) 22 | protected int fromUser; 23 | 24 | //所属根会话ID,为空则为根会话 25 | @ApiModelProperty(name = "parentId", value = "所属根会话ID,为空则为根会话") 26 | protected Integer parentId; 27 | 28 | //评论内容 29 | @ApiModelProperty(name = "content", value = "评论内容", required = true) 30 | protected String content; 31 | 32 | //评论时间 33 | @JSONField(format = "yyyy-MM-dd HH:mm:ss") 34 | @ApiModelProperty(hidden = true) 35 | protected Date time; 36 | 37 | //给谁的评论回复,为空则为根会话 38 | @ApiModelProperty(name = "toUser", value = "给谁的评论回复,为空则为根会话") 39 | protected Integer toUser; 40 | 41 | //评论状态,评论需要审核 42 | @ApiModelProperty(hidden = true) 43 | protected CommentBase.Status status; 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/FriendLink.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.object; 2 | 3 | import com.hiyj.blog.object.base.Link; 4 | import io.swagger.annotations.ApiModel; 5 | import io.swagger.annotations.ApiModelProperty; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | import lombok.ToString; 9 | 10 | import java.io.Serializable; 11 | 12 | @ApiModel 13 | @Setter 14 | @Getter 15 | @ToString 16 | public class FriendLink extends Link implements Serializable { 17 | public enum Status { 18 | // 通过 19 | PASS, 20 | // 申请中 21 | APPLY, 22 | // 拒绝 23 | REFUSE, 24 | //所有 25 | ALL, 26 | // 隐藏状态 27 | HIDE, 28 | // 逻辑删除 29 | DELETE 30 | } 31 | 32 | // 申请者邮箱 33 | @ApiModelProperty(name = "email", value = "申请者邮箱") 34 | protected String email; 35 | // 友链状态 36 | @ApiModelProperty(name = "status", value = "友链状态") 37 | protected FriendLink.Status status; 38 | 39 | // 链接封面 40 | @ApiModelProperty(name = "coverPic", value = "链接封面") 41 | protected String coverPic; 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/Msg.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.object; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import lombok.Getter; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author windSnowLi 10 | */ 11 | public class Msg implements Serializable { 12 | public final static int CODE_SUCCESS = 20000; 13 | public final static int CODE_FAIL = -1; 14 | public final static String MSG_SUCCESS = "请求成功"; 15 | public final static String MSG_FAIL = "请求失败"; 16 | public final static String LOGIN_PASSWORD_ORNUMBER_FAIL = "账户或密码错误"; 17 | /** 18 | * 返回状态码 19 | */ 20 | @Getter 21 | private int code; 22 | /** 23 | * 返回信息 24 | */ 25 | @Getter 26 | private String message; 27 | /** 28 | * 返回信息内容 29 | */ 30 | @Getter 31 | private String data; 32 | 33 | public Msg setCode(int code) { 34 | this.code = code; 35 | return this; 36 | } 37 | 38 | public Msg setMessage(String message) { 39 | this.message = message; 40 | return this; 41 | } 42 | 43 | public Msg setData(String data) { 44 | this.data = data; 45 | return this; 46 | } 47 | 48 | public Msg(int code, String message, String data) { 49 | this.code = code; 50 | this.message = message; 51 | this.data = data; 52 | } 53 | 54 | private Msg(int code, String message) { 55 | this.code = code; 56 | this.message = message; 57 | } 58 | 59 | private Msg() { 60 | } 61 | 62 | public static Msg makeMsg(int code, String msg, String content) { 63 | return new Msg(code, msg, content); 64 | } 65 | 66 | public static String makeJsonMsg(int code, String msg, Object content) { 67 | JSONObject jsonObject=new JSONObject(); 68 | jsonObject.put("code",code); 69 | jsonObject.put("message",msg); 70 | jsonObject.put("data",content); 71 | return jsonObject.toJSONString(); 72 | } 73 | 74 | public static String getFailMsg() { 75 | return Msg.makeJsonMsg(Msg.CODE_FAIL, Msg.MSG_FAIL, null); 76 | } 77 | 78 | public static String getFailMsg(String msg) { 79 | return Msg.makeJsonMsg(Msg.CODE_FAIL, msg, null); 80 | } 81 | 82 | public static String getSuccessMsg() { 83 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, null); 84 | } 85 | 86 | public static Msg parseMsg(String msg) { 87 | return JSONObject.parseObject(msg, Msg.class); 88 | } 89 | 90 | @Override 91 | public String toString() { 92 | return JSONObject.toJSONString(this); 93 | } 94 | 95 | 96 | public static String getSuccessMsg(Object object) { 97 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, MSG_SUCCESS, object); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/OtherUser.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.object; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | import lombok.ToString; 6 | 7 | import java.io.Serializable; 8 | 9 | @Setter 10 | @Getter 11 | @ToString 12 | public class OtherUser implements Serializable { 13 | private int id; 14 | //其他平台身份识别ID 15 | private String other_id; 16 | //平台 17 | private User.Platform other_platform; 18 | //用户ID 19 | private int user_id; 20 | //密钥信息 21 | private String access_token; 22 | //密钥过期时刷新密钥 23 | private String refresh_token; 24 | //授权类别 25 | private String scope; 26 | //密钥有效期 27 | private int expires_in; 28 | //密钥创建时间 29 | private int created_at; 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/PanelData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * PanelData.java, 2021-08-25 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.object; 9 | 10 | import lombok.Getter; 11 | import lombok.Setter; 12 | import lombok.ToString; 13 | 14 | import java.io.Serializable; 15 | import java.util.List; 16 | 17 | @Getter 18 | @Setter 19 | @ToString 20 | public class PanelData implements Serializable { 21 | private String title; 22 | private int total; 23 | private List X; 24 | private List Y; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/Permission.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Permission.java, 2021-08-26 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.object; 9 | 10 | import com.hiyj.blog.object.base.PermissionRole; 11 | import lombok.Getter; 12 | import lombok.Setter; 13 | import lombok.ToString; 14 | 15 | import java.io.Serializable; 16 | 17 | @Getter 18 | @Setter 19 | @ToString 20 | public class Permission extends PermissionRole implements Serializable { 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/Role.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Role.java, 2021-08-26 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.object; 9 | 10 | import com.hiyj.blog.object.base.PermissionRole; 11 | import lombok.Getter; 12 | import lombok.Setter; 13 | import lombok.ToString; 14 | 15 | import java.io.Serializable; 16 | 17 | @Getter 18 | @Setter 19 | @ToString 20 | public class Role extends PermissionRole implements Serializable { 21 | // 这里列举三个最常用的角色类型 22 | public static String ADMIN = "admin"; 23 | public static String USER = "user"; 24 | public static String VISITOR = "visitor"; 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/SysUiConfig.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.object; 2 | 3 | import com.alibaba.fastjson.annotation.JSONField; 4 | import io.swagger.annotations.ApiModel; 5 | import io.swagger.annotations.ApiModelProperty; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | import lombok.ToString; 9 | 10 | @ApiModel 11 | @ToString 12 | @Setter 13 | @Getter 14 | public class SysUiConfig { 15 | @JSONField(name = "topbar_title") 16 | @ApiModelProperty(name = "topbar_title", value = "顶栏标题") 17 | private String topBarTitle; 18 | 19 | @JSONField(name = "footer") 20 | @ApiModelProperty(name = "footer", value = "页脚代码块") 21 | private String footer; 22 | 23 | @JSONField(name = "main_title") 24 | @ApiModelProperty(name = "main_title", value = "浏览器新页标签标题") 25 | private String mainTitle; 26 | 27 | @JSONField(name = "background_list") 28 | @ApiModelProperty(name = "background_list", value = "背景图片链接,一行一个") 29 | private String backgroundList; 30 | 31 | @JSONField(name = "include") 32 | @ApiModelProperty(name = "include", value = "全局引入") 33 | private String include; 34 | 35 | //ICP备案信息 36 | @JSONField(name = "filing_icp") 37 | @ApiModelProperty(name = "filing_icp", value = "ICP备案信息") 38 | private String filing_icp; 39 | 40 | //网安备案信息 41 | @JSONField(name = "filing_security") 42 | @ApiModelProperty(name = "filing_security", value = "网安备案信息") 43 | private String filing_security; 44 | 45 | //后台URL 46 | @JSONField(name = "admin_url") 47 | @ApiModelProperty(name = "admin_url", value = "后台URL") 48 | private String admin_url; 49 | 50 | //关于信息 51 | @JSONField(name = "about") 52 | @ApiModelProperty(name = "about", value = "关于信息") 53 | private String about; 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/User.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.object; 2 | 3 | import com.alibaba.fastjson.annotation.JSONField; 4 | import io.swagger.annotations.ApiModel; 5 | import lombok.EqualsAndHashCode; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | import lombok.ToString; 9 | 10 | import java.io.Serializable; 11 | import java.util.ArrayList; 12 | 13 | @Getter 14 | @Setter 15 | @ToString 16 | @EqualsAndHashCode 17 | @ApiModel 18 | public class User implements Serializable { 19 | 20 | //账户平台 21 | public enum Platform { 22 | GITEE, 23 | GITHUB, 24 | LOCAL 25 | } 26 | 27 | //ID 28 | protected int id; 29 | //账户 30 | private String account; 31 | //密码 32 | @JSONField(serialize = false) 33 | private String password; 34 | //昵称 35 | private String nickname; 36 | //头像链接 37 | private String avatar; 38 | //QQ 39 | private String qq; 40 | //个人介绍 41 | private String introduction; 42 | //角色 43 | private ArrayList roles; 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/base/CommentBase.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.object.base; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.EqualsAndHashCode; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | import lombok.ToString; 9 | 10 | import java.io.Serializable; 11 | 12 | @ApiModel 13 | @Getter 14 | @Setter 15 | @ToString 16 | @EqualsAndHashCode 17 | public class CommentBase implements Serializable { 18 | 19 | public enum SessionType { 20 | //文章评论 21 | ARTICLE, 22 | //标签评论 23 | TAG, 24 | //分类评论 25 | TYPE, 26 | //关于信息评论 27 | ABOUT, 28 | //留言信息 29 | MESSAGE, 30 | //所有 31 | ALL 32 | } 33 | 34 | public enum Status { 35 | //通过 36 | PASS, 37 | //审核 38 | VERIFY, 39 | //删除 40 | DELETE, 41 | //所有 42 | ALL 43 | } 44 | 45 | //评论ID 46 | @ApiModelProperty(hidden = true) 47 | protected int id; 48 | 49 | //评论的目标ID 50 | @ApiModelProperty(name = "targetId", value = "评论的目标ID", required = true) 51 | protected Integer targetId; 52 | 53 | //会话类型 54 | @ApiModelProperty(name = "sessionType", value = "会话类型", required = true) 55 | protected CommentBase.SessionType sessionType; 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/base/LabelBase.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.object.base; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.EqualsAndHashCode; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | import lombok.ToString; 9 | 10 | import java.io.Serializable; 11 | 12 | @ApiModel 13 | @Getter 14 | @Setter 15 | @ToString 16 | @EqualsAndHashCode 17 | public class LabelBase implements Serializable { 18 | //label ID 19 | @ApiModelProperty(name = "id", value = "label ID") 20 | protected int id; 21 | //label名称 22 | @ApiModelProperty(name = "name", value = "label名称") 23 | protected String name; 24 | //封面 25 | @ApiModelProperty(name = "coverPic", value = "封面") 26 | protected String coverPic; 27 | //描述 28 | @ApiModelProperty(name = "describe", value = "描述") 29 | protected String describe; 30 | //浏览次数 31 | @ApiModelProperty(name = "pv", value = "浏览次数") 32 | protected int pv; 33 | //所属文章总数 34 | @ApiModelProperty(name = "num", value = "所属文章总数") 35 | protected int num; 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/base/Link.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.object.base; 2 | 3 | import com.alibaba.fastjson.annotation.JSONField; 4 | import io.swagger.annotations.ApiModel; 5 | import io.swagger.annotations.ApiModelProperty; 6 | import lombok.Getter; 7 | import lombok.Setter; 8 | import lombok.ToString; 9 | 10 | import java.io.Serializable; 11 | import java.util.Date; 12 | 13 | @ApiModel 14 | @Setter 15 | @Getter 16 | @ToString 17 | public class Link implements Serializable { 18 | @ApiModelProperty(name = "id", value = "链接ID") 19 | protected int id; 20 | // url 21 | @ApiModelProperty(name = "link", value = "链接值") 22 | protected String link; 23 | // 主题 24 | @ApiModelProperty(name = "title", value = "链接主题") 25 | protected String title; 26 | // 描述 27 | @ApiModelProperty(name = "describe", value = "链接描述") 28 | protected String describe; 29 | // 发布时间 30 | @JSONField(format = "yyyy-MM-dd HH:mm:ss") 31 | private Date createTime; 32 | // 最后更新时间 33 | @JSONField(format = "yyyy-MM-dd HH:mm:ss") 34 | private Date updateTime; 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/object/base/PermissionRole.java: -------------------------------------------------------------------------------- 1 | /* 2 | * PermissionRole.java, 2021-08-26 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.object.base; 9 | 10 | 11 | import com.alibaba.fastjson.annotation.JSONField; 12 | import lombok.Getter; 13 | import lombok.Setter; 14 | import lombok.ToString; 15 | 16 | import java.io.Serializable; 17 | import java.util.Date; 18 | 19 | @Getter 20 | @Setter 21 | @ToString 22 | public class PermissionRole implements Serializable { 23 | public enum Status { 24 | //正常 25 | NORMAL, 26 | //忽略 27 | IGNORE, 28 | //所有 29 | ALL 30 | } 31 | 32 | // 权限ID 33 | protected int id; 34 | // 权限命名 35 | protected String name; 36 | // 权限介绍 37 | protected String info; 38 | // 权限创建时间 39 | @JSONField(format = "yyyy-MM-dd HH:mm:ss") 40 | protected Date createTime; 41 | // 权限更新时间 42 | @JSONField(format = "yyyy-MM-dd HH:mm:ss") 43 | protected Date updateTime; 44 | //权限状态 45 | protected Status status; 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/services/ArticleLabelJsonService.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import com.hiyj.blog.object.Msg; 4 | import com.hiyj.blog.services.base.ArticleLabelService; 5 | import org.springframework.stereotype.Service; 6 | 7 | @Service("articleLabelJsonService") 8 | public class ArticleLabelJsonService extends ArticleLabelService { 9 | 10 | /** 11 | * 通过类型ID获取类型 12 | * 13 | * @param id 类型ID 14 | * @return 标签Json信息 15 | */ 16 | public String getTypeByIdJson(int id) { 17 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, this.getTypeById(id)); 18 | } 19 | 20 | /** 21 | * 获取所有标签 22 | * 23 | * @return ArticleLabel 24 | */ 25 | public String getAllLabelJson() { 26 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, this.getAllLabel()); 27 | } 28 | 29 | /** 30 | * 通过标签ID获取标签 31 | * 32 | * @param labelId 标签ID 33 | * @return 标签对象JSON 34 | */ 35 | public String getLabelByIdJson(int labelId) { 36 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.getSuccessMsg(), this.getLabelById(labelId)); 37 | } 38 | 39 | /** 40 | * 获取所有分类信息 41 | * 42 | * @return Msg 43 | */ 44 | public String getTypes() { 45 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, articleLabelMapper.getTypes()); 46 | } 47 | 48 | /** 49 | * 分页获取标签 50 | * 51 | * @param limit 限制数 52 | * @param page 偏移量量 53 | * @return Msg 54 | */ 55 | public String getLabelByPageJson(int limit, int page) { 56 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, 57 | getLabelByPage(limit, (page - 1) * limit)); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/services/CommentJsonService.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.JSONArray; 5 | import com.alibaba.fastjson.JSONObject; 6 | import com.alibaba.fastjson.serializer.SerializerFeature; 7 | import com.hiyj.blog.services.base.CommentService; 8 | import org.springframework.stereotype.Service; 9 | import com.hiyj.blog.model.response.RspListType; 10 | import com.hiyj.blog.object.Comment; 11 | import com.hiyj.blog.object.base.CommentBase; 12 | 13 | import java.util.List; 14 | 15 | @Service("commentJsonService") 16 | public class CommentJsonService extends CommentService { 17 | 18 | //无奈的回调 19 | interface Call { 20 | //回调 21 | void callback(Object o); 22 | } 23 | 24 | /** 25 | * 评论对象列表并把用户ID转换为对象 26 | * 27 | * @param targetId 评论的目标ID 28 | * @param sessionType 会话类型 29 | * @param status 评论状态 30 | * @return 评论对象列表 Json 31 | */ 32 | public JSONArray getTargetCommentsJson(Integer targetId, CommentBase.SessionType sessionType, CommentBase.Status status) { 33 | JSONArray jsonArray = JSONArray.parseArray(JSONObject.toJSONString(getTargetComments(targetId, sessionType, status), SerializerFeature.WriteMapNullValue)); 34 | //Test使用ValueFilter一直没问题,但是从控制层调用一直错误,无奈自己回调 35 | Call call = new Call() { 36 | @Override 37 | public void callback(Object o) { 38 | JSONObject comment = (JSONObject) o; 39 | if (comment.containsKey("fromUser") && comment.getInteger("fromUser") != null) { 40 | comment.put("fromUser", userService.findUserById(comment.getInteger("fromUser"))); 41 | } 42 | if (comment.containsKey("toUser") && comment.getInteger("toUser") != null) { 43 | comment.put("toUser", userService.findUserById(comment.getInteger("toUser"))); 44 | } 45 | if (comment.containsKey("parentId") && comment.get("parentId") != null) { 46 | comment.put("parentId", findComment(comment.getInteger("parentId"))); 47 | } 48 | if (comment.containsKey("childList") && comment.getJSONArray("childList").size() != 0) { 49 | for (Object temp : comment.getJSONArray("childList")) { 50 | JSONObject tempComment = (JSONObject) temp; 51 | this.callback(tempComment); 52 | } 53 | } 54 | } 55 | }; 56 | for (Object o : jsonArray) { 57 | JSONObject comment = (JSONObject) o; 58 | call.callback(comment); 59 | } 60 | return jsonArray; 61 | } 62 | 63 | /** 64 | * 逆序分页获取文章 65 | * 66 | * @param limit 限制量 67 | * @param offset 偏移量 68 | * @param sort 排序方式 默认-id, 69 | * @param status 评论状态 70 | * @return 评论列表 71 | */ 72 | public RspListType getCommentListJson(int limit, int offset, String sort, CommentBase.Status status) { 73 | List commentList = getCommentList(limit, offset, sort, status); 74 | JSONArray jsonArray = JSONArray.parseArray(JSONArray.toJSONString(commentList)); 75 | for (Object o : jsonArray) { 76 | JSONObject temp = ((JSONObject) o); 77 | temp.put("fromUser", userService.findUserById(temp.getInteger("fromUser"))); 78 | if (temp.containsKey("toUser") && ((JSONObject) o).getInteger("toUser") != null) { 79 | temp.put("toUser", userService.findUserById(temp.getInteger("toUser"))); 80 | } 81 | String title = ""; 82 | switch (JSON.parseObject("\"" + temp.getString("sessionType") + "\"", CommentBase.SessionType.class)) { 83 | case TYPE: 84 | title = "分类页"; 85 | break; 86 | case TAG: 87 | title = "标签页"; 88 | break; 89 | case ARTICLE: 90 | title = articleService.findArticle(temp.getInteger("targetId")).getTitle(); 91 | break; 92 | case MESSAGE: 93 | title = "留言信息"; 94 | break; 95 | case ABOUT: 96 | title = "关于信息评论"; 97 | break; 98 | } 99 | JSONObject target = new JSONObject(); 100 | target.put("title", title); 101 | temp.put("target", target); 102 | } 103 | RspListType rspListType = new RspListType<>(); 104 | rspListType.setTotal(jsonArray.size()); 105 | rspListType.setItems(jsonArray.toJavaList(JSONObject.class)); 106 | return rspListType; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/services/FileJsonService.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import com.hiyj.blog.services.base.FileService; 4 | import org.springframework.stereotype.Service; 5 | 6 | @Service("fileJsonService") 7 | public class FileJsonService extends FileService { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/services/FriendLinkJsonService.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import com.hiyj.blog.services.base.FriendLinkService; 4 | import org.springframework.stereotype.Service; 5 | import com.hiyj.blog.object.FriendLink; 6 | import com.hiyj.blog.object.Msg; 7 | 8 | @Service("friendLinkJsonService") 9 | public class FriendLinkJsonService extends FriendLinkService { 10 | /** 11 | * 获取友链列表 12 | * 13 | * @param status 友链状态 14 | * @return Msg 15 | */ 16 | public String getFriendLinksJson(FriendLink.Status status) { 17 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, getFriendLinks(status)); 18 | } 19 | 20 | /** 21 | * 申请友链 22 | * 23 | * @param friendLink 友链对象 24 | * @return Msg 25 | */ 26 | public String applyFriendLinkJson(FriendLink friendLink) { 27 | applyFriendLink(friendLink); 28 | return Msg.getSuccessMsg(); 29 | } 30 | 31 | /** 32 | * 设置友链整体对象 33 | * 34 | * @param friendLink 友链对象 35 | * @return Msg 36 | */ 37 | public String setFriendLinkJson(FriendLink friendLink) { 38 | setFriendLink(friendLink); 39 | return Msg.getSuccessMsg(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/services/OtherJsonService.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import com.hiyj.blog.services.base.OtherService; 4 | import org.springframework.stereotype.Service; 5 | 6 | @Service("otherJsonService") 7 | public class OtherJsonService extends OtherService { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/services/SysConfigJsonService.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.alibaba.fastjson.TypeReference; 5 | import com.hiyj.blog.object.Msg; 6 | import com.hiyj.blog.services.base.SysConfigService; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.HashMap; 10 | 11 | @Service("sysConfigJsonService") 12 | public class SysConfigJsonService extends SysConfigService { 13 | 14 | /** 15 | * 设置某个用户的UI配置 16 | * 17 | * @param user_id 用户ID 18 | * @param systemConfigJson 配置表 19 | * @return Msg 20 | */ 21 | public String setUiConfigByUserIdJson(int user_id, JSONObject systemConfigJson) { 22 | HashMap systemConfig = JSONObject.parseObject(systemConfigJson.toJSONString(), new TypeReference<>() { 23 | }); 24 | HashMap configTable = new HashMap<>(); 25 | 26 | String[] ready = {"main_title", "topbar_title", "footer", "background_list", "include"}; 27 | 28 | for (String key : ready) { 29 | configTable.put(key, systemConfig.get(key)); 30 | } 31 | this.setUiConfigByUserId(user_id, configTable); 32 | 33 | return Msg.getSuccessMsg(); 34 | } 35 | 36 | /** 37 | * 获取系统存储配置文件 38 | * 39 | * @return Msg 40 | */ 41 | public String getStorageConfigJson() { 42 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, this.getStorageConfig()); 43 | } 44 | 45 | /** 46 | * 设置系统存储配置文件 47 | * 48 | * @param storage 存储设置Json对象 49 | * @return Msg 50 | */ 51 | public String setStorageConfigJson(JSONObject storage) { 52 | setStorageConfig(storage); 53 | return Msg.getSuccessMsg(); 54 | } 55 | 56 | /** 57 | * 获取系统配置信息 58 | * 59 | * @return Msg 60 | */ 61 | public String getFixedConfigJson() { 62 | //对前端的提交格式描述,OSS需特殊处理,直接合并到了本身配置里 63 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, getFixedConfig()); 64 | } 65 | 66 | /** 67 | * 设置系统配置 68 | * 69 | * @param config 系统配置表 70 | * @return Msg 71 | */ 72 | public String setFixedConfigJson(JSONObject config) { 73 | setFixedConfig(config); 74 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, null); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/services/UserJsonService.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.hiyj.blog.services.base.UserService; 5 | import org.springframework.stereotype.Service; 6 | import com.hiyj.blog.object.Msg; 7 | import com.hiyj.blog.object.User; 8 | import com.hiyj.blog.utils.JwtUtils; 9 | 10 | import java.math.BigDecimal; 11 | import java.util.ArrayList; 12 | import java.util.HashMap; 13 | import java.util.List; 14 | 15 | @Service("userJsonService") 16 | public class UserJsonService extends UserService { 17 | 18 | /** 19 | * 获取用户信息 20 | * 21 | * @param token 用户token 22 | * @return 用户对象Msg 23 | */ 24 | public String getInfoJson(String token) { 25 | int userId = JwtUtils.getTokenUserId(token); 26 | User user = findUserById(userId); 27 | if (user == null) { 28 | return Msg.getFailMsg(); 29 | } 30 | String userAccount = JwtUtils.getTokenUserAccount(token); 31 | String userPassword = JwtUtils.getTokenUserPassword(token); 32 | if (!user.getAccount().equals(userAccount) || !user.getPassword().equals(userPassword)) { 33 | return Msg.getFailMsg(); 34 | } 35 | ArrayList roles = new ArrayList<>(); 36 | roles.add("admin"); 37 | JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(user)); 38 | jsonObject.put("roles", roles); 39 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, jsonObject); 40 | } 41 | 42 | public String loginJson(User user) { 43 | User tempUser = userMapper.findUserAccount(user.getAccount()); 44 | if (tempUser == null || !tempUser.getPassword().equals(encryptPasswd(user.getPassword()))) { 45 | return Msg.makeJsonMsg(Msg.CODE_FAIL, Msg.LOGIN_PASSWORD_ORNUMBER_FAIL, null); 46 | } 47 | JSONObject jsonObject = new JSONObject(); 48 | jsonObject.put("token", JwtUtils.getToken(tempUser)); 49 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, jsonObject); 50 | } 51 | 52 | /** 53 | * 访客获取作者信息 54 | * 55 | * @param userId 作者ID 56 | * @return 作者信息 57 | */ 58 | public String visitorGetAuthorInfo(int userId) { 59 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, findUserById(userId)); 60 | } 61 | 62 | /** 63 | * 设置用户信息 64 | * 65 | * @param user 用户新对象 66 | * @return Msg 67 | */ 68 | public String setInfoJson(User user) { 69 | if (user.getNickname().isEmpty()) { 70 | return Msg.makeJsonMsg(Msg.CODE_FAIL, "昵称不可为空", null); 71 | } 72 | setInfo(user); 73 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, "保存成功", null); 74 | } 75 | 76 | /** 77 | * 获取用户喜好分类占比 78 | * 79 | * @param userId 用户ID 80 | * @return List 81 | */ 82 | public String getWorkByUserIdJson(int userId) { 83 | final List activityByUserId = this.getWorkByUserId(userId); 84 | int allArticleCount = articleMapper.getArticleCount(); 85 | List rs = new ArrayList<>(); 86 | for (Object temp : activityByUserId) { 87 | final HashMap temp1 = (HashMap) temp; 88 | JSONObject tempJson = new JSONObject(); 89 | tempJson.put("name", temp1.get("name")); 90 | tempJson.put("value", ((BigDecimal) temp1.getOrDefault("value", new BigDecimal(0))).intValue() * 100 / allArticleCount); 91 | rs.add(tempJson); 92 | } 93 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, rs); 94 | } 95 | 96 | /** 97 | * 设置用户头像 98 | * 99 | * @param userId 用户ID 100 | * @param avatarUrl 头像链接 101 | */ 102 | public String setAvatarJson(int userId, String avatarUrl) { 103 | setAvatar(userId, avatarUrl); 104 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, null); 105 | } 106 | 107 | /** 108 | * 获取作者关于信息 109 | * 110 | * @param userId 用户ID 111 | * @return Msg 112 | */ 113 | public String getAboutByUserIdJson(int userId) { 114 | return Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, getAboutByUserId(userId)); 115 | } 116 | 117 | /** 118 | * 设置作者关于信息 119 | * 120 | * @param userId 用户ID 121 | * @param content 内容 122 | * @return Msg 123 | */ 124 | public String setAboutByUserTokenJson(int userId, String content) { 125 | setAboutByUserToken(userId, content); 126 | return Msg.getSuccessMsg(); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/services/base/CommentService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * CommentService.java, 2021-08-23 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.services.base; 9 | 10 | import com.alibaba.fastjson.JSON; 11 | import com.alibaba.fastjson.JSONObject; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.stereotype.Service; 14 | import com.hiyj.blog.mapper.CommentMapper; 15 | import com.hiyj.blog.model.response.RspCommentsModel; 16 | import com.hiyj.blog.object.Comment; 17 | import com.hiyj.blog.object.base.CommentBase; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | import java.util.Map; 22 | import java.util.stream.Collectors; 23 | 24 | @Service("commentService") 25 | public class CommentService { 26 | protected CommentMapper commentMapper; 27 | 28 | @Autowired 29 | public void setCommentMapper(CommentMapper commentMapper) { 30 | this.commentMapper = commentMapper; 31 | } 32 | 33 | protected UserService userService; 34 | 35 | @Autowired 36 | public void setUserService(UserService userService) { 37 | this.userService = userService; 38 | } 39 | 40 | protected ArticleService articleService; 41 | 42 | @Autowired 43 | public void setArticleService(ArticleService articleService) { 44 | this.articleService = articleService; 45 | } 46 | 47 | /** 48 | * 添加评论 49 | * 50 | * @param comment 评论对象 51 | */ 52 | public void addComment(Comment comment) { 53 | commentMapper.addComment(comment); 54 | } 55 | 56 | /** 57 | * @param targetId 评论的目标ID 58 | * @param sessionType 会话类型 59 | * @param status 评论状态 60 | * @return 评论对象列表 61 | */ 62 | public List getTargetComments(Integer targetId, CommentBase.SessionType sessionType, CommentBase.Status status) { 63 | List allComments = commentMapper.getTargetComments(targetId, sessionType, status); 64 | //筛选独立评论 65 | List parentComments = allComments.stream().filter((Comment comment) -> 66 | comment.getParentId() == null 67 | ).collect(Collectors.toList()); 68 | //筛选各独立评论的子评论 69 | ArrayList rspCommentsModels = new ArrayList<>(); 70 | for (Comment comment : parentComments) { 71 | RspCommentsModel rspCommentsModel = JSONObject.parseObject(JSONObject.toJSONString(comment), RspCommentsModel.class); 72 | rspCommentsModel.setChildList( 73 | allComments.stream() 74 | .filter((Comment temp) -> temp.getParentId() != null && 75 | temp.getParentId() == comment.getId()) 76 | .collect(Collectors.toList())); 77 | rspCommentsModels.add(rspCommentsModel); 78 | } 79 | return rspCommentsModels; 80 | } 81 | 82 | /** 83 | * 根据记录ID查找某一条记录 84 | * 85 | * @param commentId 记录ID 86 | * @return Comment对象 87 | */ 88 | public Comment findComment(int commentId) { 89 | return commentMapper.findComment(commentId); 90 | } 91 | 92 | /** 93 | * 设置评论状态 94 | * 95 | * @param commentId 评论ID 96 | * @param status 新状态 97 | */ 98 | public void setCommentStatus(int commentId, CommentBase.Status status) { 99 | commentMapper.setCommentStatus(commentId, status); 100 | } 101 | 102 | /** 103 | * 逆序分页获取文章 104 | * 105 | * @param limit 限制量 106 | * @param offset 偏移量 107 | * @param sort 排序方式 默认-id, 108 | * @param status 评论状态 109 | * @return 评论列表 110 | */ 111 | public List getCommentList(int limit, int offset, String sort, CommentBase.Status status) { 112 | return commentMapper.getCommentList(limit, offset, sort, status); 113 | } 114 | 115 | /** 116 | * 获取评论历史 117 | * 118 | * @return List, 日期数值键值对{total=int, day_time=String} 119 | */ 120 | public List> getCommentLogByDay(int limit, CommentBase.Status status) { 121 | return commentMapper.getCommentLogByDay(limit, status); 122 | } 123 | 124 | /** 125 | * 获取当前状态评论总量 126 | * 127 | * @param status 评论状态 128 | * @return 总数 129 | */ 130 | public int getCommentCount(CommentBase.Status status) { 131 | return commentMapper.getCommentCount(status); 132 | } 133 | 134 | /** 135 | * 获取所有文章最新的评论 136 | * 137 | * @param limit 条数限制 138 | * @return Comment json list 139 | */ 140 | public List getRecentComment(int limit) { 141 | ArrayList commentList = new ArrayList<>(); 142 | for (Comment c : commentMapper.getRecentComment(limit)) { 143 | JSONObject comment = (JSONObject) JSONObject.toJSON(c); 144 | comment.put("user", userService.findUserById(comment.getInteger("fromUser"))); 145 | comment.put("target", articleService.findArticle(comment.getInteger("targetId"))); 146 | commentList.add(comment); 147 | } 148 | return commentList; 149 | } 150 | 151 | } 152 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/services/base/FileService.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services.base; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import lombok.Getter; 5 | import lombok.Setter; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | import com.hiyj.blog.mapper.FileMapper; 9 | import com.hiyj.blog.oss.OssUtils; 10 | import com.hiyj.blog.utils.CodeUtils; 11 | 12 | import java.util.HashMap; 13 | import java.util.Map; 14 | 15 | @Getter 16 | @Setter 17 | @Service("fileService") 18 | public class FileService { 19 | protected SysConfigService sysConfigService; 20 | 21 | @Autowired 22 | public void setSysConfigService(SysConfigService sysConfigService) { 23 | this.sysConfigService = sysConfigService; 24 | } 25 | 26 | protected FileMapper fileMapper; 27 | 28 | @Autowired 29 | public void setFileMapper(FileMapper fileMapper) { 30 | this.fileMapper = fileMapper; 31 | } 32 | 33 | //项目文件存储根路径 34 | protected String rootPath; 35 | //文章封面存储路径 36 | protected String articleCoverImagePath; 37 | //头像存储路径 38 | protected String avatarImagePath; 39 | //文章内容图片路径 40 | protected String articleImagePath; 41 | //是否开启上传 42 | protected boolean status; 43 | 44 | /** 45 | * 获取OSS工具对象 46 | * 47 | * @return OssUtils 48 | */ 49 | public OssUtils getOssUtils() { 50 | final JSONObject ossConfigJson = JSONObject.parseObject(sysConfigService.getStorageConfig().getString("storage")); 51 | rootPath = ossConfigJson.getString("rootPath"); 52 | ossConfigJson.remove("ossConfigJson"); 53 | articleCoverImagePath = ossConfigJson.getString("articleCoverImagePath"); 54 | ossConfigJson.remove("imagePath"); 55 | avatarImagePath = ossConfigJson.getString("avatarImagePath"); 56 | ossConfigJson.remove("avatarImagePath"); 57 | articleImagePath = ossConfigJson.getString("articleImagePath"); 58 | ossConfigJson.remove("articleImagePath"); 59 | status = ossConfigJson.getBoolean("status"); 60 | ossConfigJson.remove("status"); 61 | return JSONObject.parseObject(ossConfigJson.toJSONString(), OssUtils.class); 62 | } 63 | 64 | /** 65 | * 获取OSS上传参数表 66 | * 67 | * @param path 存储路径 68 | * @param token 验证信息 69 | * @return 返回链接和Object路径 70 | */ 71 | protected Map getUploadMap(String path, String token) { 72 | OssUtils ossUtils = getOssUtils(); 73 | Map map = new HashMap<>(); 74 | map.put("status", status); 75 | if (status) { 76 | map.putAll(ossUtils.getUploadUrl(path + "/" + CodeUtils.getUUID(), "&token=" + token, true)); 77 | } 78 | return map; 79 | } 80 | 81 | /** 82 | * 获取上传头像的url和Object路径 83 | * 84 | * @return 返回上传的头像链接和Object路径 85 | */ 86 | public Map getUploadAvatarMap(String token) { 87 | return getUploadMap(avatarImagePath, token); 88 | } 89 | 90 | /** 91 | * 获取上传文章封面的url和Object路径 92 | * 93 | * @return 返回上传的文章封面链接和Object路径 94 | */ 95 | public Map getUploadArticleCoverImageUrl(String token) { 96 | return getUploadMap(articleCoverImagePath, token); 97 | } 98 | 99 | /** 100 | * 获取文章图片上传url和Object路径 101 | * 102 | * @return 返回上传的文章封面链接和Object路径 103 | */ 104 | public Map getUploadArticleImageUrl(String token) { 105 | return getUploadMap(articleImagePath, token); 106 | } 107 | 108 | /** 109 | * 删除文件 110 | * 111 | * @param objectName 文件路径 112 | */ 113 | public void deleteObject(String objectName) { 114 | getOssUtils().deleteObject(objectName); 115 | } 116 | 117 | /** 118 | * 检查文件映射 119 | * 120 | * @param userId 用户ID 121 | * @param object 文件路径 122 | * @return 是否映射正确 123 | */ 124 | public boolean checkFileMap(int userId, String object) { 125 | return fileMapper.checkFileMap(userId, object); 126 | } 127 | 128 | /** 129 | * 删除用户文件映射 130 | * 131 | * @param userId 用户ID 132 | * @param fileName 文件名 133 | */ 134 | public void deleteUserFile(int userId, String fileName) { 135 | if (fileName != null && checkFileMap(userId, fileName)) { 136 | deleteObject(fileName); 137 | } 138 | } 139 | 140 | /** 141 | * 添加用户文件映射 142 | * 143 | * @param userId 用户ID 144 | * @param fileName 文件名 145 | */ 146 | public void addUserFile(int userId, String fileName) { 147 | fileMapper.addUserFile(userId, fileName); 148 | } 149 | 150 | } -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/services/base/FriendLinkService.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services.base; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | import com.hiyj.blog.mapper.LinkMapper; 6 | import com.hiyj.blog.object.FriendLink; 7 | 8 | import java.util.List; 9 | 10 | @Service("friendLinkService") 11 | public class FriendLinkService { 12 | private LinkMapper linkMapper; 13 | 14 | @Autowired 15 | public void setLinkMapper(LinkMapper linkMapper) { 16 | this.linkMapper = linkMapper; 17 | } 18 | 19 | /** 20 | * 获取友链列表 21 | * 22 | * @param status 友链状态 23 | * @return List 24 | */ 25 | public List getFriendLinks(FriendLink.Status status) { 26 | return linkMapper.getFriendLinks(status); 27 | } 28 | 29 | /** 30 | * 申请友链 31 | * 32 | * @param friendLink 友链对象 33 | */ 34 | public void applyFriendLink(FriendLink friendLink) { 35 | linkMapper.applyFriendLink(friendLink); 36 | } 37 | 38 | /** 39 | * 设置友链整体对象 40 | * 41 | * @param friendLink 友链对象 42 | */ 43 | public void setFriendLink(FriendLink friendLink) { 44 | linkMapper.setFriendLink(friendLink); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/services/base/PermissionService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * PermissionService.java, 2021-08-26 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.services.base; 9 | 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Service; 12 | import com.hiyj.blog.mapper.PermissionMapper; 13 | import com.hiyj.blog.object.Permission; 14 | import com.hiyj.blog.object.base.PermissionRole; 15 | 16 | import java.util.List; 17 | 18 | @Service("permissionService") 19 | public class PermissionService { 20 | private PermissionMapper permissionMapper; 21 | 22 | @Autowired 23 | public void setPermissionMapper(PermissionMapper permissionMapper) { 24 | this.permissionMapper = permissionMapper; 25 | } 26 | 27 | /** 28 | * 获取角色权限对象列表 29 | * 30 | * @param roleId 角色ID 31 | * @param status 权限状态 32 | * @return 权限对象List 33 | */ 34 | public List getRolePermissions(int roleId, PermissionRole.Status status) { 35 | return permissionMapper.getRolePermissions(roleId, status); 36 | } 37 | 38 | /** 39 | * 获取角色权限ID列表 40 | * 41 | * @param roleId 角色ID 42 | * @param status 权限状态 43 | * @return 权限ID List 44 | */ 45 | public List getRolePermissionsId(int roleId, PermissionRole.Status status) { 46 | return permissionMapper.getRolePermissionsId(roleId, status); 47 | } 48 | 49 | /** 50 | * 获取角色权限name列表 51 | * 52 | * @param roleId 角色ID 53 | * @param status 权限状态 54 | * @return 权限name List 55 | */ 56 | public List getRolePermissionsName(int roleId, PermissionRole.Status status) { 57 | return permissionMapper.getRolePermissionsName(roleId, status); 58 | } 59 | 60 | /** 61 | * 获取用户可用权限对象列表 62 | * 63 | * @param userId 角色ID 64 | * @return 权限对象List 65 | */ 66 | public List getUserPermissions(int userId) { 67 | return permissionMapper.getUserPermissions(userId); 68 | } 69 | 70 | /** 71 | * 获取用户可用权限ID列表 72 | * 73 | * @param userId 角色ID 74 | * @return 权限ID List 75 | */ 76 | public List getUserPermissionsId(int userId) { 77 | return permissionMapper.getUserPermissionsId(userId); 78 | } 79 | 80 | /** 81 | * 获取用户可用权限name列表 82 | * 83 | * @param userId 角色ID 84 | * @return 权限name List 85 | */ 86 | public List getUserPermissionsName(int userId) { 87 | return permissionMapper.getUserPermissionsName(userId); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/services/base/RoleService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * RoleService.java, 2021-08-26 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.services.base; 9 | 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Service; 12 | import com.hiyj.blog.mapper.RoleMapper; 13 | import com.hiyj.blog.object.Role; 14 | import com.hiyj.blog.object.base.PermissionRole; 15 | 16 | import java.util.List; 17 | 18 | @Service("roleService") 19 | public class RoleService { 20 | private RoleMapper roleMapper; 21 | 22 | @Autowired 23 | public void setRoleMapper(RoleMapper roleMapper) { 24 | this.roleMapper = roleMapper; 25 | } 26 | 27 | /** 28 | * 根据用户ID和角色状态获取用户角色表 29 | * 30 | * @param userId 用户ID 31 | * @param status 角色状态 32 | * @return List 33 | */ 34 | public List getUserRoles(int userId, PermissionRole.Status status) { 35 | return roleMapper.getRoles(userId, status); 36 | } 37 | 38 | /** 39 | * 获取用户角色ID列表 40 | * 41 | * @param userId 用户ID 42 | * @param status 角色状态 43 | * @return 角色ID List 44 | */ 45 | public List getUserRolesById(int userId, PermissionRole.Status status) { 46 | return roleMapper.getRolesId(userId, status); 47 | } 48 | 49 | /** 50 | * 获取用户角色name列表 51 | * 52 | * @param userId 用户ID 53 | * @param status 角色状态 54 | * @return 角色name List 55 | */ 56 | public List getUserRolesByName(int userId, PermissionRole.Status status) { 57 | return roleMapper.getRolesName(userId, status); 58 | } 59 | 60 | /** 61 | * 根据Role名字获取角色对象 62 | * 63 | * @param name 角色名 64 | * @return 角色对象 65 | */ 66 | public Role getRoleByName(String name) { 67 | return roleMapper.getRoleByName(name); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/services/base/SysConfigService.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services.base; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.alibaba.fastjson.parser.Feature; 5 | import com.hiyj.blog.mapper.SysConfigMapper; 6 | import com.hiyj.blog.mapper.UserMapper; 7 | import com.hiyj.blog.model.response.ClientIdModel; 8 | import com.hiyj.blog.object.SysUiConfig; 9 | import com.hiyj.blog.model.share.ClientModel; 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.stereotype.Service; 12 | 13 | import java.util.HashMap; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | @Service("sysConfigService") 18 | public class SysConfigService { 19 | protected SysConfigMapper sysConfigMapper; 20 | protected UserMapper userMapper; 21 | 22 | @Autowired 23 | public void setSystemConfigMapper(SysConfigMapper sysConfigMapper) { 24 | this.sysConfigMapper = sysConfigMapper; 25 | } 26 | 27 | @Autowired 28 | public void setUserMapper(UserMapper userMapper) { 29 | this.userMapper = userMapper; 30 | } 31 | 32 | /** 33 | * 获取用户UI配置 34 | * 35 | * @param user_id 用户ID 36 | * @return 配置表 37 | */ 38 | public SysUiConfig getUiConfigByUserId(int user_id) { 39 | final List> sysUiConfigByUserId = sysConfigMapper.getUiConfigByUserId(user_id); 40 | HashMap rs = new HashMap<>(); 41 | for (Map map : sysUiConfigByUserId) { 42 | rs.put(map.get("item"), map.get("value")); 43 | } 44 | JSONObject baseSysConfig = getFixedConfig().getJSONObject("sys"); 45 | rs.put("filing_icp", baseSysConfig.getOrDefault("filing_icp", "").toString()); 46 | rs.put("filing_security", baseSysConfig.getOrDefault("filing_security", "").toString()); 47 | rs.put("admin_url", baseSysConfig.getOrDefault("admin_url", "").toString()); 48 | return JSONObject.parseObject(JSONObject.toJSONString(rs), SysUiConfig.class); 49 | } 50 | 51 | /** 52 | * 获取系统默认配置 53 | * 54 | * @return 系统配置表 55 | */ 56 | public JSONObject getFixedConfig() { 57 | return JSONObject.parseObject(sysConfigMapper.getFixedConfig()); 58 | } 59 | 60 | /** 61 | * 设置系统配置 62 | * 63 | * @param config 配置表 64 | */ 65 | public void setFixedConfig(JSONObject config) { 66 | JSONObject configJson = getFixedConfig(); 67 | configJson.put("sys", config); 68 | sysConfigMapper.setFixedConfig(configJson.toJSONString()); 69 | } 70 | 71 | /** 72 | * 设置某个用户的UI配置 73 | * 74 | * @param user_id 用户ID 75 | * @param configMap 配置表 76 | */ 77 | public void setUiConfigByUserId(int user_id, Map configMap) { 78 | sysConfigMapper.setUiConfigByUserId(user_id, configMap); 79 | } 80 | 81 | /** 82 | * 获取系统存储配置文件 83 | * 84 | * @return SystemConfig 85 | */ 86 | public JSONObject getStorageConfig() { 87 | return JSONObject.parseObject(sysConfigMapper.getOSSConfig(), Feature.OrderedField); 88 | } 89 | 90 | /** 91 | * 设置系统存储配置文件 92 | * 93 | * @param storage 存储设置Json对象 94 | */ 95 | public void setStorageConfig(JSONObject storage) { 96 | JSONObject storageConfig = getStorageConfig(); 97 | storageConfig.put("storage", storage); 98 | sysConfigMapper.setStorageConfig(storageConfig.toJSONString()); 99 | } 100 | 101 | /** 102 | * 获取Gitee登录配置信息 103 | * 104 | * @return Gitee的JSON格式信息 105 | */ 106 | public JSONObject getGiteeLoginConfig() { 107 | return JSONObject.parseObject(sysConfigMapper.getOtherLoginConfig()).getJSONObject("gitee"); 108 | } 109 | 110 | /** 111 | * 获取Gitee应用程序ID,用于Gitee登录 112 | * 113 | * @return client_id 114 | */ 115 | public ClientIdModel getGiteeClientId() { 116 | return getGiteeLoginConfig().getJSONObject("client").toJavaObject(ClientIdModel.class); 117 | } 118 | 119 | /** 120 | * 设置Gitee登录配置 121 | */ 122 | public void setGiteeConfig(ClientModel clientModel) { 123 | JSONObject config = JSONObject.parseObject(sysConfigMapper.getOtherLoginConfig()); 124 | config.getJSONObject("gitee").put("client", clientModel); 125 | sysConfigMapper.setGiteeConfig(config.toJSONString()); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/services/base/UserService.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services.base; 2 | 3 | 4 | import com.hiyj.blog.mapper.ArticleMapper; 5 | import com.hiyj.blog.mapper.UserMapper; 6 | import com.hiyj.blog.object.OtherUser; 7 | import com.hiyj.blog.object.User; 8 | import com.hiyj.blog.utils.CodeUtils; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.util.List; 13 | 14 | @Service("userService") 15 | public class UserService { 16 | 17 | protected UserMapper userMapper; 18 | 19 | protected ArticleMapper articleMapper; 20 | 21 | protected FileService fileService; 22 | 23 | protected ArticleLabelService articleLabelService; 24 | 25 | @Autowired 26 | public void setUserMapper(UserMapper userMapper) { 27 | this.userMapper = userMapper; 28 | } 29 | 30 | @Autowired 31 | public void setArticleMapper(ArticleMapper articleMapper) { 32 | this.articleMapper = articleMapper; 33 | } 34 | 35 | @Autowired 36 | public void setFileService(FileService fileService) { 37 | this.fileService = fileService; 38 | } 39 | 40 | @Autowired 41 | public void setArticleLabelService(ArticleLabelService articleLabelService) { 42 | this.articleLabelService = articleLabelService; 43 | } 44 | 45 | public User findUserByAccount(String account) { 46 | return userMapper.findUserAccount(account); 47 | } 48 | 49 | public User findUserById(int id) { 50 | return userMapper.findUserId(id); 51 | } 52 | 53 | /** 54 | * 设置用户信息 55 | * 56 | * @param user 用户新对象 57 | */ 58 | public void setInfo(User user) { 59 | userMapper.setInfo(user); 60 | } 61 | 62 | /** 63 | * 获取用户喜好分类占比 64 | * 65 | * @param userId 用户ID 66 | * @return List [{all_count=int, name=String}] 67 | */ 68 | public List getWorkByUserId(int userId) { 69 | return userMapper.getActivityByUserId(userId); 70 | } 71 | 72 | /** 73 | * 设置用户头像 74 | * 75 | * @param userId 用户ID 76 | * @param avatarUrl 头像链接 77 | */ 78 | public void setAvatar(int userId, String avatarUrl) { 79 | userMapper.setAvatar(userId, avatarUrl); 80 | } 81 | 82 | /** 83 | * 添加用户文件映射 84 | * 85 | * @param userId 用户ID 86 | * @param fileName 文件名 87 | */ 88 | public void addUserFile(int userId, String fileName) { 89 | fileService.addUserFile(userId, fileName); 90 | } 91 | 92 | /** 93 | * 获取作者关于信息 94 | * 95 | * @param userId 用户ID 96 | * @return String 97 | */ 98 | public String getAboutByUserId(int userId) { 99 | return userMapper.getAboutByUserId(userId); 100 | } 101 | 102 | /** 103 | * 设置作者关于信息 104 | * 105 | * @param userId 用户ID 106 | * @param content 内容 107 | */ 108 | public void setAboutByUserToken(int userId, String content) { 109 | userMapper.setAbout(userId, content); 110 | } 111 | 112 | /** 113 | * 添加用户 114 | * 115 | * @param user 用户对象 116 | */ 117 | public void addUser(User user) { 118 | userMapper.addUser(user); 119 | } 120 | 121 | /** 122 | * 添加第三方平台登录用户 123 | * 124 | * @param otherUser 第三方对象 125 | */ 126 | public void addOtherUser(OtherUser otherUser) { 127 | userMapper.addOtherUser(otherUser); 128 | } 129 | 130 | /** 131 | * 查找第三方登录账户 132 | * 133 | * @param other_id 第三方识别码 134 | * @param platform 平台 135 | * @return 第三方对象 136 | */ 137 | public OtherUser getOtherUser(String other_id, User.Platform platform) { 138 | return userMapper.getOtherUser(other_id, platform); 139 | } 140 | 141 | /** 142 | * 刷新第三方用户验证信息 143 | * 144 | * @param otherUser 第三方信息对象 145 | */ 146 | public void refreshKeyInfo(OtherUser otherUser) { 147 | userMapper.refreshInfo(otherUser); 148 | } 149 | 150 | /** 151 | * 由明文密码计算密文密码 152 | * 153 | * @param text 明文 154 | * @return MD5(SHA512 ( 明文)+明文) 155 | */ 156 | public String encryptPasswd(String text) { 157 | return CodeUtils.strWithMd5(CodeUtils.strWithSha512(text)); 158 | } 159 | 160 | /** 161 | * 添加用户-角色映射 162 | * 163 | * @param userId 用户ID 164 | * @param roleId 角色ID 165 | */ 166 | public void addUserMapRole(int userId, int roleId) { 167 | userMapper.addUserMapRole(userId, roleId); 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/utils/CodeUtils.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.utils; 2 | 3 | 4 | import com.aliyun.oss.common.utils.BinaryUtil; 5 | import com.fasterxml.uuid.Generators; 6 | 7 | import java.io.File; 8 | import java.io.FileInputStream; 9 | import java.io.IOException; 10 | import java.nio.charset.StandardCharsets; 11 | import java.security.KeyFactory; 12 | import java.security.MessageDigest; 13 | import java.security.NoSuchAlgorithmException; 14 | import java.security.PublicKey; 15 | import java.security.spec.X509EncodedKeySpec; 16 | 17 | public class CodeUtils { 18 | /** 19 | * @return UUID 20 | */ 21 | public static String getUUID() { 22 | return Generators.timeBasedGenerator().generate().toString(); 23 | } 24 | 25 | /** 26 | * 验证RSA 27 | * 28 | * @param content 内容 29 | * @param sign 签名 30 | * @param publicKey 公开密钥 31 | * @return 验证状态 32 | */ 33 | public static boolean rsaCheck(String content, byte[] sign, String publicKey) { 34 | try { 35 | KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 36 | byte[] encodedKey = BinaryUtil.fromBase64String(publicKey); 37 | PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); 38 | java.security.Signature signature = java.security.Signature.getInstance("MD5withRSA"); 39 | signature.initVerify(pubKey); 40 | signature.update(content.getBytes()); 41 | return signature.verify(sign); 42 | } catch (Exception e) { 43 | e.printStackTrace(); 44 | } 45 | return false; 46 | } 47 | 48 | /** 49 | * 获取字符串SHA521 50 | * 51 | * @param content 内容 52 | * @return SHA512 53 | */ 54 | public static String strWithSha512(final String content) { 55 | try { 56 | //创建SHA512类型的加密对象 57 | MessageDigest messageDigest = MessageDigest.getInstance("SHA-512"); 58 | messageDigest.update(content.getBytes()); 59 | byte[] bytes = messageDigest.digest(); 60 | StringBuilder strHexString = new StringBuilder(); 61 | for (byte aByte : bytes) { 62 | String hex = Integer.toHexString(0xff & aByte); 63 | if (hex.length() == 1) { 64 | strHexString.append('0'); 65 | } 66 | strHexString.append(hex); 67 | } 68 | return strHexString.toString(); 69 | } catch (NoSuchAlgorithmException e) { 70 | e.printStackTrace(); 71 | return null; 72 | } 73 | } 74 | 75 | /** 76 | * 获取字符串SHA1 77 | * 78 | * @param content 内容 79 | * @return SHA1 80 | */ 81 | public static String strWithSha1(final String content) { 82 | try { 83 | if (content == null) { 84 | return null; 85 | } 86 | 87 | final MessageDigest messageDigest = MessageDigest.getInstance("SHA"); 88 | final byte[] digests = messageDigest.digest(content.getBytes()); 89 | 90 | final StringBuilder stringBuilder = new StringBuilder(); 91 | for (byte digest : digests) { 92 | int halfbyte = (digest >>> 4) & 0x0F; 93 | for (int j = 0; j <= 1; j++) { 94 | stringBuilder.append( 95 | halfbyte <= 9 96 | ? (char) ('0' + halfbyte) 97 | : (char) ('a' + halfbyte - 10)); 98 | halfbyte = digest & 0x0F; 99 | } 100 | } 101 | 102 | return stringBuilder.toString(); 103 | } catch (final Throwable throwable) { 104 | return null; 105 | } 106 | } 107 | 108 | public static String strWithMd5(final String content) { 109 | try { 110 | MessageDigest md5 = MessageDigest.getInstance("MD5"); 111 | md5.update(content.getBytes(StandardCharsets.UTF_8)); 112 | byte[] hexBuff = md5.digest(); 113 | return hexToStr(hexBuff); 114 | } catch (Exception e) { 115 | e.printStackTrace(); 116 | } 117 | return ""; 118 | } 119 | 120 | public static String getFileMd5(File file) { 121 | FileInputStream fileInputStream = null; 122 | try { 123 | MessageDigest md5 = MessageDigest.getInstance("md5"); 124 | fileInputStream = new FileInputStream(file); 125 | byte[] buffer = new byte[8192]; 126 | int length; 127 | while ((length = fileInputStream.read(buffer)) != -1) { 128 | md5.update(buffer, 0, length); 129 | } 130 | return hexToStr(md5.digest()); 131 | } catch (Exception e) { 132 | e.printStackTrace(); 133 | return null; 134 | } finally { 135 | try { 136 | if (fileInputStream != null) { 137 | fileInputStream.close(); 138 | } 139 | } catch (IOException e) { 140 | e.printStackTrace(); 141 | } 142 | } 143 | } 144 | 145 | /** 146 | * 十六进制转字符串 147 | * 148 | * @param hexStr 十六进制数组 149 | * @return 字符串 150 | */ 151 | private static String hexToStr(byte[] hexStr) { 152 | StringBuilder result = new StringBuilder(); 153 | for (byte b : hexStr) { 154 | result.append(Integer.toHexString((0x000000FF & b) | 0xFFFFFF00).substring(6)); 155 | } 156 | return result.toString(); 157 | } 158 | 159 | } 160 | -------------------------------------------------------------------------------- /src/main/java/com/hiyj/blog/utils/JwtUtils.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.utils; 2 | 3 | import com.alibaba.druid.util.StringUtils; 4 | import com.auth0.jwt.JWT; 5 | import com.auth0.jwt.JWTVerifier; 6 | import com.auth0.jwt.algorithms.Algorithm; 7 | import com.auth0.jwt.interfaces.Claim; 8 | import com.auth0.jwt.interfaces.DecodedJWT; 9 | import com.hiyj.blog.object.User; 10 | import org.springframework.stereotype.Component; 11 | 12 | import java.util.Calendar; 13 | import java.util.Date; 14 | import java.util.HashMap; 15 | import java.util.Map; 16 | 17 | 18 | /** 19 | * @author windSnowLi 20 | */ 21 | @Component 22 | public class JwtUtils { 23 | /** 24 | * token秘钥:WINDSNOWLI-Blog 25 | */ 26 | public static final String SECRET = "WINDSNOWLI-Blog"; 27 | /** 28 | * token 过期时间: 180天 29 | */ 30 | public static final int CALENDAR_FIELD = Calendar.DATE; 31 | public static final int CALENDAR_INTERVAL = 180; 32 | 33 | public static String getToken(User user) { 34 | Date iatDate = new Date(); 35 | // expire time 36 | Calendar nowTime = Calendar.getInstance(); 37 | nowTime.add(CALENDAR_FIELD, CALENDAR_INTERVAL); 38 | Date expiresDate = nowTime.getTime(); 39 | 40 | // header Map 41 | Map map = new HashMap<>(); 42 | map.put("alg", "HS256"); 43 | map.put("typ", "JWT"); 44 | 45 | // build token 46 | // header 47 | return JWT.create().withHeader(map) 48 | // payload 49 | .withClaim("userId", String.valueOf(user.getId())) 50 | .withClaim("userPassword", String.valueOf(user.getPassword())) 51 | .withClaim("userAccount", String.valueOf(user.getAccount())) 52 | // sign time 53 | .withIssuedAt(iatDate) 54 | // expire time 55 | .withExpiresAt(expiresDate) 56 | //signature 57 | .sign(Algorithm.HMAC256(SECRET)); 58 | } 59 | 60 | 61 | /** 62 | * 解密Token 63 | * 64 | * @param token 信息串 65 | * @return 揭秘结果 66 | */ 67 | public static Map verifyToken(String token) { 68 | DecodedJWT jwt; 69 | JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build(); 70 | jwt = verifier.verify(token); 71 | return jwt.getClaims(); 72 | } 73 | 74 | /** 75 | * 根据Token获取userId 76 | * 77 | * @param token 信息串 78 | * @return 用户ID 79 | */ 80 | public static int getTokenUserId(String token) { 81 | Map claims = verifyToken(token); 82 | Claim userIdClaim = claims.get("userId"); 83 | if (null == userIdClaim || StringUtils.isEmpty(userIdClaim.asString())) { 84 | return -1; 85 | } 86 | return Integer.parseInt(userIdClaim.asString()); 87 | } 88 | 89 | /** 90 | * 根据Token获取account 91 | * 92 | * @param token 信息串 93 | * @return 用户账号 94 | */ 95 | public static String getTokenUserAccount(String token) { 96 | Map claims = verifyToken(token); 97 | Claim userIdClaim = claims.get("userAccount"); 98 | if (null == userIdClaim || StringUtils.isEmpty(userIdClaim.asString())) { 99 | return ""; 100 | } 101 | return userIdClaim.asString(); 102 | } 103 | 104 | /** 105 | * 根据Token获取account 106 | * 107 | * @param token 信息串 108 | * @return 用户账号 109 | */ 110 | public static String getTokenUserPassword(String token) { 111 | Map claims = verifyToken(token); 112 | Claim userIdClaim = claims.get("userPassword"); 113 | if (null == userIdClaim || StringUtils.isEmpty(userIdClaim.asString())) { 114 | return ""; 115 | } 116 | return userIdClaim.asString(); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /src/main/resources/application-dev.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | driver-class-name: com.mysql.cj.jdbc.NonRegisteringDriver 4 | url: jdbc:mysql://127.0.0.1/blog?serverTimezone=GMT%2B8 5 | username: root 6 | password: root 7 | name: w-blog-api-dev 8 | knife4j: 9 | enable: true 10 | 11 | redis: 12 | host: 127.0.0.1 13 | port: 6379 14 | connectionTimeout: 10000 15 | soTimeout: 10000 16 | timeout: 10 17 | # password: w-blog 18 | database: 0 -------------------------------------------------------------------------------- /src/main/resources/application-prod.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | driver-class-name: com.mysql.cj.jdbc.NonRegisteringDriver 4 | url: jdbc:mysql://127.0.0.1/blog?serverTimezone=GMT%2B8 5 | username: root 6 | password: root 7 | name: w-blog-api-prod 8 | flyway: 9 | clean-disabled: true 10 | 11 | redis: 12 | host: 127.0.0.1 13 | port: 6379 14 | connectionTimeout: 10000 15 | soTimeout: 10000 16 | timeout: 3600 17 | password: w-blog 18 | database: 0 -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9000 3 | spring: 4 | profiles: 5 | active: dev 6 | datasource: 7 | driver-class-name: com.mysql.cj.jdbc.NonRegisteringDriver 8 | type: com.alibaba.druid.pool.DruidDataSource 9 | filters: stat 10 | initialSize: 2 11 | maxActive: 300 12 | maxWait: 60000 13 | timeBetweenEvictionRunsMillis: 60000 14 | minEvictableIdleTimeMillis: 300000 15 | validationQuery: SELECT 1 16 | testWhileIdle: true 17 | testOnBorrow: false 18 | testOnReturn: false 19 | poolPreparedStatements: false 20 | maxPoolPreparedStatementPerConnectionSize: 200 21 | flyway: 22 | encoding: UTF-8 23 | locations: classpath:db/migration 24 | # placeholder-replacement: false 25 | jackson: 26 | date-format: yyyy-MM-dd HH:mm:ss 27 | time-zone: GMT+8 28 | #配置Mapper.xml映射文件 29 | mybatis: 30 | mapper-locations: classpath*:mybatis/mapper/*.xml 31 | 32 | -------------------------------------------------------------------------------- /src/main/resources/mybatis/mapper/CommentMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | INSERT INTO comment 22 | (target_id, session_type, from_user, parent_id, content, to_user) 23 | VALUES (#{comment.targetId}, #{comment.sessionType}, #{comment.fromUser}, 24 | #{comment.parentId}, #{comment.content}, #{comment.toUser}); 25 | 26 | 27 | 28 | 29 | 69 | 70 | 71 | 72 | 85 | 86 | 87 | 88 | 120 | 121 | 122 | 123 | 143 | 144 | 145 | 146 | 158 | 159 | 160 | 161 | UPDATE comment 162 | SET status=#{status} 163 | WHERE id = #{commentId} 164 | 165 | 166 | 167 | 175 | 176 | -------------------------------------------------------------------------------- /src/main/resources/mybatis/mapper/LinkMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 28 | 29 | 30 | 31 | INSERT INTO friend_links 32 | (link, title, `describe`, email, coverPic) 33 | VALUES (#{friendLink.link}, #{friendLink.title}, 34 | #{friendLink.describe}, #{friendLink.email}, 35 | #{friendLink.coverPic}); 36 | 37 | 38 | 39 | 40 | 41 | UPDATE friend_links 42 | SET link=#{friendLink.link}, 43 | title=#{friendLink.title}, 44 | `describe`=#{friendLink.describe}, 45 | email=#{friendLink.email}, 46 | status=#{friendLink.status}, 47 | coverPic=#{friendLink.coverPic} 48 | WHERE id = #{friendLink.id} 49 | 50 | -------------------------------------------------------------------------------- /src/main/resources/mybatis/mapper/PermissionMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | from 26 | ( 27 | select permission_id 28 | from role_map_permission rmp 29 | where rmp.role_id = #{roleId} 30 | ) 31 | permission_list 32 | left join 33 | permission p on permission_list.permission_id = p.id 34 | where p.status = 35 | #{status} 36 | 37 | 41 | 42 | 43 | 47 | 48 | 49 | 53 | 54 | 55 | 56 | 57 | 58 | from 59 | ( 60 | select rmp.permission_id 61 | from role_map_permission rmp 62 | where rmp.role_id 63 | in 64 | ( 65 | select umr.role_id 66 | from user_map_role umr 67 | left join `role` r on umr.role_id = r.id 68 | where umr.user_id = #{userId} 69 | and r.status = 'NORMAL' 70 | ) 71 | ) 72 | permission_list 73 | left join permission p on permission_list.permission_id = p.id 74 | where p.status = 'NORMAL' 75 | 76 | 80 | 81 | 82 | 86 | 87 | 88 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /src/main/resources/mybatis/mapper/RoleMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | from 25 | ( 26 | select umr.role_id 27 | from user_map_role umr 28 | where umr.user_id = #{userId} 29 | ) 30 | umr 31 | left join 32 | `role` r 33 | on umr.role_id = r.id 34 | 35 | 36 | 37 | where r.status = #{status} 38 | 39 | 40 | 41 | 42 | 43 | 47 | 48 | 49 | 53 | 54 | 55 | 59 | 60 | -------------------------------------------------------------------------------- /src/main/resources/mybatis/mapper/SysConfigMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | DELETE 18 | FROM ui_config 19 | WHERE user_id = #{userId} 20 | 21 | 22 | 23 | 24 | 25 | 26 | REPLACE INTO ui_config(user_id, item, value) 27 | VALUES 28 | 29 | ( 30 | #{userId}, #{key}, #{value} 31 | ) 32 | 33 | ; 34 | 35 | 36 | 37 | 38 | 39 | REPLACE INTO sys_setting(item, value) 40 | VALUES ('base_sys', #{config}) 41 | 42 | -------------------------------------------------------------------------------- /src/main/resources/mybatis/mapper/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 42 | 43 | 44 | 45 | 46 | INSERT INTO user 47 | (account, password, avatar, nickname) 48 | VALUES (#{user.account}, #{user.password}, #{user.avatar}, #{user.nickname}); 49 | 50 | 51 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/LBlogApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class LBlogApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/caches/redis/RedisConfigTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.caches.redis; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | 7 | @SpringBootTest 8 | public class RedisConfigTest { 9 | @Autowired 10 | private RedisConfig redisConfig; 11 | 12 | @Test 13 | public void testRedisConfigTest(){ 14 | System.out.println(redisConfig); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/mapper/ArticleLabelMapperTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.mapper; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.alibaba.fastjson.JSONArray; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | 9 | @SpringBootTest 10 | public class ArticleLabelMapperTest { 11 | private ArticleLabelMapper articleLabelMapper; 12 | 13 | @Autowired 14 | public void setArticleLabelMapper(ArticleLabelMapper articleLabelMapper) { 15 | this.articleLabelMapper = articleLabelMapper; 16 | } 17 | 18 | @Test 19 | public void testGetTypeById() { 20 | System.out.println(JSON.toJSONString(articleLabelMapper.getTypeById(1))); 21 | } 22 | 23 | @Test 24 | public void testGetArticleTypeById() { 25 | System.out.println(articleLabelMapper.getArticleTypeById(1)); 26 | } 27 | 28 | /** 29 | * 按分类获取每个分类多少文章 30 | */ 31 | @Test 32 | public void testGetArticleCountByType() { 33 | System.out.println(articleLabelMapper.getArticleCountByType(10)); 34 | } 35 | 36 | @Test 37 | public void testGetTypes() { 38 | System.out.println(articleLabelMapper.getTypes()); 39 | } 40 | 41 | @Test 42 | public void testGetAllLabel() { 43 | System.out.println(articleLabelMapper.getAllLabel()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/mapper/ArticleMapperTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.mapper; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.hiyj.blog.object.Article; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | 9 | import java.math.BigDecimal; 10 | import java.util.*; 11 | 12 | @SpringBootTest 13 | public class ArticleMapperTest { 14 | private ArticleMapper articleMapper; 15 | 16 | @Autowired 17 | public void setArticleMapper(ArticleMapper articleMapper) { 18 | this.articleMapper = articleMapper; 19 | } 20 | 21 | @Test 22 | public void tesFindArticleJson() { 23 | System.out.println(articleMapper.findArticleAuthor(1)); 24 | } 25 | 26 | @Test 27 | public void testFindArticle() { 28 | System.out.println(articleMapper.findArticleId(1)); 29 | } 30 | 31 | @Test 32 | public void testGetLabelArticlePage() { 33 | System.out.println(articleMapper.getArticlesByLabel(2, 0, 100, "-id", Article.Status.PUBLISHED)); 34 | } 35 | 36 | @Test 37 | public void testGetArticleByType() { 38 | System.out.println(articleMapper.getArticlesByType(2, 1, 100, "-id", Article.Status.ALL)); 39 | } 40 | 41 | @Test 42 | public void testAddPV() { 43 | articleMapper.addPV(1, 1); 44 | } 45 | 46 | @Test 47 | public void testGetArticleCountByUserId() { 48 | System.out.println(articleMapper.getArticleCount()); 49 | } 50 | 51 | @Test 52 | public void testGetVisitsAllCountByUserId() { 53 | System.out.println(articleMapper.getPV()); 54 | } 55 | 56 | /** 57 | * 按天获取访问总量和趋势数据 58 | */ 59 | @Test 60 | public void testGetVisitHistoryCountByDay() { 61 | List> visitHistoryCountByDay = articleMapper.getPVLogByDay(); 62 | JSONObject jsonObject = new JSONObject(); 63 | ArrayList x = new ArrayList<>(); 64 | ArrayList y = new ArrayList<>(); 65 | if (visitHistoryCountByDay != null && !visitHistoryCountByDay.contains(null)) { 66 | for (Map temp : visitHistoryCountByDay) { 67 | x.add((String) temp.get("day_time")); 68 | y.add(((BigDecimal) temp.get("total")).intValue()); 69 | } 70 | } 71 | 72 | jsonObject.put("x", x); 73 | jsonObject.put("y", y); 74 | System.out.println(jsonObject.toJSONString()); 75 | } 76 | 77 | /** 78 | * 获取文章创建历史 79 | */ 80 | @Test 81 | public void testGetArticleCreateHistoryByWeek() { 82 | JSONObject jsonObject = new JSONObject(); 83 | ArrayList x = new ArrayList<>(); 84 | ArrayList y = new ArrayList<>(); 85 | for (Map temp : articleMapper.getArticleCreateLogByWeek()) { 86 | x.add((String) temp.get("week_time")); 87 | y.add(((Long) temp.get("total")).intValue()); 88 | } 89 | jsonObject.put("x", x); 90 | jsonObject.put("y", y); 91 | System.out.println(jsonObject.toJSONString()); 92 | } 93 | 94 | /** 95 | * 添加文章,不含标签和文章所属用户映射 96 | */ 97 | @Test 98 | public void addArticle() { 99 | // Article article = new Article(); 100 | // article.setContent("TEST"); 101 | // article.setSummary("TEST"); 102 | // article.setTitle("TEST"); 103 | // article.setCoverPic("TEST"); 104 | // article.setStatus(Article.Status.PUBLISHED); 105 | // System.out.println(articleMapper.addArticle(article)); 106 | // System.out.println(article.getId()); 107 | } 108 | 109 | /** 110 | * 分页查询文章 111 | */ 112 | @Test 113 | public void testGetArticlesByPage() { 114 | System.out.println(articleMapper.getArticlesByPage(3, 0, null, Article.Status.PUBLISHED)); 115 | System.out.println(articleMapper.getArticlesByPage(3, 1, null, Article.Status.PUBLISHED)); 116 | System.out.println(articleMapper.getArticlesByPage(3, 2, null, Article.Status.PUBLISHED)); 117 | } 118 | 119 | /** 120 | * 分页查询文章Id 121 | */ 122 | @Test 123 | public void testGetArticleIdByPage() { 124 | System.out.println(articleMapper.getArticleIdByPage(3, 0, null, Article.Status.PUBLISHED)); 125 | System.out.println(articleMapper.getArticleIdByPage(3, 1, null, Article.Status.PUBLISHED)); 126 | System.out.println(articleMapper.getArticleIdByPage(3, 2, null, Article.Status.PUBLISHED)); 127 | System.out.println(JSONObject.toJSONString(articleMapper.getArticleIdByPage(3, 2, null, Article.Status.PUBLISHED))); 128 | } 129 | } 130 | 131 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/mapper/CommentMapperTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.mapper; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.hiyj.blog.object.Comment; 5 | import com.hiyj.blog.object.base.CommentBase; 6 | import org.junit.jupiter.api.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | 10 | @SpringBootTest 11 | public class CommentMapperTest { 12 | private CommentMapper commentMapper; 13 | 14 | @Autowired 15 | public void setCommentMapper(CommentMapper commentMapper) { 16 | this.commentMapper = commentMapper; 17 | } 18 | 19 | /** 20 | * 添加评论 21 | */ 22 | @Test 23 | public void testAddComment() { 24 | Comment comment = new Comment(); 25 | comment.setTargetId(90); 26 | comment.setContent("评论测试"); 27 | comment.setFromUser(1); 28 | comment.setStatus(CommentBase.Status.PASS); 29 | comment.setSessionType(CommentBase.SessionType.ARTICLE); 30 | commentMapper.addComment(comment); 31 | } 32 | 33 | /** 34 | * 获取目标评论 35 | */ 36 | @Test 37 | public void testGetTargetComments() { 38 | System.out.println(commentMapper.getTargetComments(90, CommentBase.SessionType.ARTICLE, CommentBase.Status.ALL)); 39 | } 40 | 41 | /** 42 | * 逆序分页获取文章 43 | */ 44 | @Test 45 | public void testGetCommentList() { 46 | System.out.println(JSON.toJSONString(commentMapper.getCommentList(5, 2 * 5, "-id", CommentBase.Status.ALL))); 47 | } 48 | 49 | /** 50 | * 获取最近评论趋势 51 | */ 52 | @Test 53 | public void testGetCommentLogByDay() { 54 | System.out.println(commentMapper.getCommentLogByDay(10, CommentBase.Status.PASS)); 55 | System.out.println(commentMapper.getCommentLogByDay(10, CommentBase.Status.VERIFY)); 56 | System.out.println(commentMapper.getCommentLogByDay(10, CommentBase.Status.DELETE)); 57 | System.out.println(commentMapper.getCommentLogByDay(10, CommentBase.Status.ALL)); 58 | } 59 | 60 | /** 61 | * 获取所有文章最新的评论 62 | */ 63 | @Test 64 | public void testGetRecentComment() { 65 | for (Comment comment : commentMapper.getRecentComment(5)) { 66 | System.out.println(comment); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/mapper/LinkMapperTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.mapper; 2 | 3 | import com.hiyj.blog.object.FriendLink; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | 8 | @SpringBootTest 9 | public class LinkMapperTest { 10 | private LinkMapper linkMapper; 11 | 12 | @Autowired 13 | public void setLinkMapper(LinkMapper linkMapper) { 14 | this.linkMapper = linkMapper; 15 | } 16 | 17 | @Test 18 | public void testGetFriendLinks() { 19 | System.out.println(linkMapper.getFriendLinks(FriendLink.Status.ALL).size()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/mapper/PermissionsServiceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * PermissionsServiceTest.java, 2021-08-26 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.mapper; 9 | 10 | import com.hiyj.blog.object.base.PermissionRole; 11 | import org.junit.jupiter.api.Test; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.boot.test.context.SpringBootTest; 14 | 15 | @SpringBootTest 16 | public class PermissionsServiceTest { 17 | private PermissionMapper permissionMapper; 18 | 19 | @Autowired 20 | public void setPermissionMapper(PermissionMapper permissionMapper) { 21 | this.permissionMapper = permissionMapper; 22 | } 23 | 24 | @Test 25 | public void testGetPermissions() { 26 | System.out.println(permissionMapper.getRolePermissionsName(2, PermissionRole.Status.ALL)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/mapper/RoleMapperTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * RoleMapperTest.java, 2021-08-26 3 | * 4 | * Copyright 2021 by WindSnowLi, Inc. All rights reserved. 5 | * 6 | */ 7 | 8 | package com.hiyj.blog.mapper; 9 | 10 | import com.alibaba.fastjson.JSON; 11 | import com.hiyj.blog.object.base.PermissionRole; 12 | import org.junit.jupiter.api.Test; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.boot.test.context.SpringBootTest; 15 | 16 | @SpringBootTest 17 | public class RoleMapperTest { 18 | private RoleMapper roleMapper; 19 | 20 | @Autowired 21 | public void setRoleMapper(RoleMapper roleMapper) { 22 | this.roleMapper = roleMapper; 23 | } 24 | 25 | @Test 26 | public void testGetRoles() { 27 | System.out.println(JSON.toJSONString(roleMapper.getRoles(1, PermissionRole.Status.ALL))); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/mapper/SysConfigMapperTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.mapper; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | 7 | import java.util.HashMap; 8 | 9 | @SpringBootTest 10 | public class SysConfigMapperTest { 11 | private SysConfigMapper sysConfigMapper; 12 | 13 | @Autowired 14 | public void setSystemSettingMapper(SysConfigMapper sysConfigMapper) { 15 | this.sysConfigMapper = sysConfigMapper; 16 | } 17 | 18 | 19 | /** 20 | * 获取用户配置表 21 | */ 22 | @Test 23 | public void testGetSysUiConfigByUserId() { 24 | System.out.println(sysConfigMapper.getUiConfigByUserId(1)); 25 | } 26 | 27 | /** 28 | * 设置某个用户的UI配置 29 | */ 30 | @Test 31 | public void testSetUiConfigByUserId() { 32 | HashMap config = new HashMap<>(); 33 | config.put("footer", "额滴神"); 34 | sysConfigMapper.setUiConfigByUserId(1, config); 35 | } 36 | 37 | /** 38 | * 获取OSS的相关配置 39 | */ 40 | @Test 41 | public void testGetOSSConfig() { 42 | System.out.println(sysConfigMapper.getOSSConfig()); 43 | } 44 | 45 | /** 46 | * 获取系统默认配置 47 | */ 48 | @Test 49 | public void testGetSysUiConfig() { 50 | System.out.println(sysConfigMapper.getFixedConfig()); 51 | } 52 | 53 | @Test 54 | void testGetOtherLoginConfig() { 55 | System.out.println(sysConfigMapper.getOtherLoginConfig()); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/mapper/UserMapperTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.mapper; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | 7 | @SpringBootTest 8 | public class UserMapperTest { 9 | private UserMapper userMapper; 10 | 11 | @Autowired 12 | public void setUserMapper(UserMapper userMapper) { 13 | this.userMapper = userMapper; 14 | } 15 | 16 | @Test 17 | public void testFindUser() { 18 | System.out.println(userMapper.findUserAccount("111111")); 19 | } 20 | 21 | /** 22 | * 获取用户喜好分类占比 23 | */ 24 | @Test 25 | public void testGetActivityByUserId(){ 26 | System.out.println(userMapper.getActivityByUserId(1)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/oss/OssUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.oss; 2 | 3 | import com.hiyj.blog.services.base.FileService; 4 | import com.hiyj.blog.utils.CodeUtils; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | 9 | import java.util.HashMap; 10 | 11 | @SpringBootTest 12 | public class OssUtilsTest { 13 | private FileService fileService; 14 | 15 | @Autowired 16 | public void setFileService(FileService fileService) { 17 | this.fileService = fileService; 18 | } 19 | 20 | 21 | /** 22 | * 获取PUT上传签名Url 23 | */ 24 | @Test 25 | public void testGetUploadUrl() { 26 | // OssUtils ossUtils = fileService.getOssUtils(); 27 | // HashMap headMap = new HashMap<>(); 28 | // final HashMap uploadUrl = ossUtils.getUploadUrl("Blog/image/avatar/" + CodeUtils.getUUID(), false); 29 | // 30 | // System.out.println(uploadUrl); 31 | // 32 | // String url = "http://windsnowli.oss-cn-beijing.aliyuncs.com/Blog/image/avatar/b5af4dc0-dd32-11eb-973a-43ad923ac904?Expires=1625453143&OSSAccessKeyId=LTAI4GGJHNi11CxiSdxvq3QB&Signature=zek6pEPo5rksfvfVV1tX2ZrW8%2B4%3D"; 33 | // String objectName = uploadUrl.get("GetUrl"); 34 | // try { 35 | // ossUtils.uploadBySignedUrl(url, "E:\\Desktop\\test.txt", headMap); 36 | // } catch (FileNotFoundException | MalformedURLException e) { 37 | // e.printStackTrace(); 38 | // } 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/services/ArticleLabelServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.hiyj.blog.object.base.LabelBase; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | @SpringBootTest 13 | public class ArticleLabelServiceTest { 14 | private ArticleLabelJsonService articleLabelJsonService; 15 | 16 | private ArticleJsonService articleJsonService; 17 | 18 | @Autowired 19 | public void setArticleLabelJsonService(ArticleLabelJsonService articleLabelJsonService) { 20 | this.articleLabelJsonService = articleLabelJsonService; 21 | } 22 | 23 | @Autowired 24 | public void setArticleJsonService(ArticleJsonService articleJsonService) { 25 | this.articleJsonService = articleJsonService; 26 | } 27 | 28 | @Test 29 | public void testGetAllLabelJson() { 30 | System.out.println(articleLabelJsonService.getAllLabelJson()); 31 | } 32 | 33 | @Test 34 | public void testGetTypeOfPVPage() { 35 | final List visitCountByTypeByUserId = articleLabelJsonService.getTypeOfPVPage(10, 0); 36 | System.out.println(JSONObject.toJSONString(visitCountByTypeByUserId)); 37 | JSONObject jsonObject = new JSONObject(); 38 | ArrayList dataName = new ArrayList<>(); 39 | for (LabelBase articleLabel : visitCountByTypeByUserId) { 40 | dataName.add(articleLabel.getName()); 41 | } 42 | final ArrayList pv = new ArrayList<>(); 43 | int top10 = 0; 44 | for (LabelBase articleLabel : visitCountByTypeByUserId) { 45 | JSONObject temp = new JSONObject(); 46 | top10 += articleLabel.getPv(); 47 | temp.put("value", articleLabel.getPv()); 48 | temp.put("name", articleLabel.getName()); 49 | pv.add(temp); 50 | } 51 | int other = articleJsonService.getPV() - top10; 52 | if (pv.size() == 10 && other > 0) { 53 | JSONObject temp = new JSONObject(); 54 | temp.put("value", other); 55 | dataName.add("其它"); 56 | temp.put("name", "其它"); 57 | pv.add(temp); 58 | } 59 | jsonObject.put("dataName", dataName); 60 | jsonObject.put("pv", pv); 61 | System.out.println(jsonObject.toJSONString()); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/services/ArticleServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import com.hiyj.blog.object.Article; 8 | 9 | import java.math.BigDecimal; 10 | import java.util.*; 11 | 12 | @SpringBootTest 13 | public class ArticleServiceTest { 14 | private ArticleJsonService articleJsonService; 15 | 16 | @Autowired 17 | public void setArticleJsonService(ArticleJsonService articleJsonService) { 18 | this.articleJsonService = articleJsonService; 19 | } 20 | 21 | @Test 22 | public void testFindArticleAuthor() { 23 | System.out.println(articleJsonService.findArticleAuthor(1)); 24 | } 25 | 26 | @Test 27 | public void testFindArticleJson() { 28 | System.out.println(articleJsonService.findArticleJson(1)); 29 | } 30 | 31 | @Test 32 | public void testGetLabelArticlePageJson() { 33 | System.out.println(articleJsonService.getArticlesByLabelJson(2, 10, 1, "-id", Article.Status.PUBLISHED)); 34 | } 35 | 36 | /** 37 | * 创建文章 38 | */ 39 | @Test 40 | public void testCreateArticle() { 41 | // Article article = new Article(); 42 | // article.setContent("TEST"); 43 | // article.setSummary("TEST"); 44 | // article.setTitle("TEST"); 45 | // article.setCoverPic("TEST"); 46 | // ArrayList labbels = new ArrayList<>(); 47 | // ArticleLabel label1 = new ArticleLabel(); 48 | // label1.setName("C++"); 49 | // labbels.add(label1); 50 | // ArticleLabel label2 = new ArticleLabel(); 51 | // labbels.add(label2); 52 | // label2.setName("TEST"); 53 | // article.setLabels(labbels); 54 | // articleJsonService.createArticle(article, 1); 55 | } 56 | 57 | /** 58 | * 按天获取访问总量和趋势数据 59 | */ 60 | @Test 61 | public void testGetVisitLogByDay() { 62 | JSONObject jsonObject = new JSONObject(); 63 | ArrayList x = new ArrayList<>(); 64 | ArrayList y = new ArrayList<>(); 65 | List> visitLogByDay = articleJsonService.getVisitLogByDay(); 66 | if (visitLogByDay != null && !visitLogByDay.contains(null)) { 67 | for (Object temp : visitLogByDay) { 68 | x.add((String) ((HashMap) temp).get("day_time")); 69 | y.add(((BigDecimal) ((HashMap) temp).get("total")).intValue()); 70 | } 71 | } 72 | jsonObject.put("visitsAllCount", articleJsonService.getPV()); 73 | jsonObject.put("title", "浏览量"); 74 | //图从左至右,数据应逆序 75 | Collections.reverse(x); 76 | Collections.reverse(y); 77 | jsonObject.put("x", x); 78 | jsonObject.put("y", y); 79 | System.out.println(jsonObject.toJSONString()); 80 | } 81 | 82 | /** 83 | * 分页获取文章ID列表 84 | */ 85 | @Test 86 | public void testGetArticleIdByPageJson() { 87 | System.out.println(articleJsonService.getArticleIdByPageJson(20, 1, "-id", Article.Status.PUBLISHED)); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/services/CommentServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import com.hiyj.blog.object.Comment; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.boot.test.context.SpringBootTest; 8 | import com.hiyj.blog.model.request.ReqCommentModel; 9 | import com.hiyj.blog.object.Msg; 10 | import com.hiyj.blog.object.base.CommentBase; 11 | 12 | @SpringBootTest 13 | public class CommentServiceTest { 14 | private CommentJsonService commentJsonService; 15 | 16 | @Autowired 17 | public void setCommentService(CommentJsonService commentJsonService) { 18 | this.commentJsonService = commentJsonService; 19 | } 20 | 21 | /** 22 | * 获取目标的评论 23 | */ 24 | @Test 25 | public void testGetTargetComments() { 26 | ReqCommentModel reqCommentModel = JSONObject.parseObject("{ \"sessionType\": \"ARTICLE\", \"status\": \"PASS\", \"targetId\": 85 }", ReqCommentModel.class); 27 | System.out.println(Msg.makeJsonMsg(Msg.CODE_SUCCESS, Msg.MSG_SUCCESS, 28 | commentJsonService.getTargetCommentsJson(reqCommentModel.getTargetId(), 29 | reqCommentModel.getSessionType(), 30 | reqCommentModel.getStatus()))); 31 | } 32 | 33 | /** 34 | * 获取评论对象列表 35 | */ 36 | @Test 37 | public void testGetCommentListJson() { 38 | System.out.println(commentJsonService.getCommentListJson(5, 2 * 5, "-id", CommentBase.Status.ALL)); 39 | } 40 | 41 | /** 42 | * 获取所有文章最新的评论 43 | */ 44 | @Test 45 | public void testGetRecentComment() { 46 | for (JSONObject comment : commentJsonService.getRecentComment(5)) { 47 | System.out.println(comment); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/services/FileServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | 7 | @SpringBootTest 8 | public class FileServiceTest { 9 | 10 | private FileJsonService fileJsonService; 11 | 12 | @Autowired 13 | public void setFileJsonService(FileJsonService fileJsonService) { 14 | this.fileJsonService = fileJsonService; 15 | } 16 | 17 | @Test 18 | public void testGetUploadAvatarMap() { 19 | System.out.println(fileJsonService.getUploadAvatarMap("token")); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/services/GiteeServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import com.hiyj.blog.services.otherlogin.GiteeService; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | 8 | @SpringBootTest 9 | public class GiteeServiceTest { 10 | private GiteeService giteeService; 11 | 12 | @Autowired 13 | public void setGiteeLogin(GiteeService giteeService) { 14 | this.giteeService = giteeService; 15 | } 16 | 17 | @Test 18 | public void testGiteeLogin() { 19 | // System.out.println(giteeService.getLocalToken("4c7bdb48805ab9e6d7b90e561a4cc06b4de6c689282a5a67a4b5d548b1b043b9", "http://192.168.10.105:3000/login")); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/services/OtherServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import com.hiyj.blog.services.base.OtherService; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | 8 | @SpringBootTest 9 | public class OtherServiceTest { 10 | private OtherService otherService; 11 | 12 | @Autowired 13 | public void setOtherService(OtherService otherService) { 14 | this.otherService = otherService; 15 | } 16 | 17 | /** 18 | * 获取仪表盘折线图和panel-group部分 19 | */ 20 | @Test 21 | public void testGetPanel() { 22 | System.out.println(otherService.getPanel()); 23 | } 24 | 25 | /** 26 | * 获取图表信息 27 | */ 28 | @Test 29 | public void testGetChart() { 30 | System.out.println(otherService.getChart()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/services/SysConfigServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | 7 | import java.util.HashMap; 8 | 9 | @SpringBootTest 10 | public class SysConfigServiceTest { 11 | private SysConfigJsonService sysConfigJsonService; 12 | 13 | @Autowired 14 | public void setSysConfigJsonService(SysConfigJsonService sysConfigJsonService) { 15 | this.sysConfigJsonService = sysConfigJsonService; 16 | } 17 | 18 | /** 19 | * 设置某个用户的UI配置 20 | */ 21 | @Test 22 | public void testSetUiConfigByUserId() { 23 | HashMap config = new HashMap<>(); 24 | config.put("topbar_title", "纯天然色的菜狗"); 25 | config.put("main_title", "WindSnowLi"); 26 | System.out.println(config); 27 | sysConfigJsonService.setUiConfigByUserId(1, config); 28 | } 29 | 30 | /** 31 | * 获取系统默认配置 32 | */ 33 | @Test 34 | public void testGetSysConfig() { 35 | System.out.println(sysConfigJsonService.getFixedConfigJson()); 36 | } 37 | 38 | /** 39 | * 获取Gitee登录配置信息 40 | */ 41 | @Test 42 | public void testGetGiteeLoginConfig() { 43 | System.out.println(sysConfigJsonService.getGiteeLoginConfig()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/services/UserServiceTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.services; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | 7 | @SpringBootTest 8 | public class UserServiceTest { 9 | private UserJsonService userJsonService; 10 | 11 | @Autowired 12 | public void setUserService(UserJsonService userJsonService) { 13 | this.userJsonService = userJsonService; 14 | } 15 | 16 | @Test 17 | public void testFindUser() { 18 | System.out.println(userJsonService.findUserByAccount("goyujie@163.com")); 19 | } 20 | 21 | /** 22 | * 获取用户喜好分类占比 23 | */ 24 | @Test 25 | public void testGetWorkByUserIdJson() { 26 | System.out.println(userJsonService.getWorkByUserIdJson(1)); 27 | } 28 | 29 | /** 30 | * 初始密码123456,值 b8a1099b57fb53d28fba7d5717e317ea 31 | */ 32 | @Test 33 | public void testEncryptPasswd() { 34 | System.out.println(userJsonService.encryptPasswd("123456")); 35 | } 36 | 37 | @Test 38 | public void testGetUserToken() { 39 | System.out.println(userJsonService.loginJson(userJsonService.findUserById(1))); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/test/HttpRequestTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.test; 2 | 3 | import com.alibaba.fastjson.JSONObject; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.http.HttpEntity; 7 | import org.springframework.http.HttpHeaders; 8 | import org.springframework.http.MediaType; 9 | import org.springframework.web.client.RestTemplate; 10 | 11 | @SpringBootTest 12 | public class HttpRequestTest { 13 | /** 14 | * Spring Boot Post请求 15 | */ 16 | @Test 17 | public void testPostRequest() { 18 | String url = "https://www.blog.hiyj.cn/api/article/getArticleById"; 19 | // 请求表 20 | JSONObject paramMap = new JSONObject(); 21 | paramMap.put("id", 93); 22 | // 请求头 23 | HttpHeaders headers = new HttpHeaders(); 24 | headers.setContentType(MediaType.APPLICATION_JSON); 25 | headers.set("user-agent", "Mozilla/5.0 WindSnowLi-Blog"); 26 | // 整合请求头和请求参数 27 | HttpEntity httpEntity = new HttpEntity<>(paramMap, headers); 28 | // 请求客户端 29 | RestTemplate client = new RestTemplate(); 30 | // 发起请求 31 | JSONObject body = client.postForEntity(url, httpEntity, JSONObject.class).getBody(); 32 | System.out.println("******** POST请求 *********"); 33 | assert body != null; 34 | System.out.println(body.toJSONString()); 35 | } 36 | 37 | /** 38 | * Spring Boot Get请求 39 | */ 40 | @Test 41 | public void testGetRequest() { 42 | String url = "https://www.blog.hiyj.cn/"; 43 | // 请求客户端 44 | RestTemplate client = new RestTemplate(); 45 | // 发起请求 46 | String body = client.getForEntity(url, String.class).getBody(); 47 | System.out.println("******** Get请求 *********"); 48 | assert body != null; 49 | System.out.println(body); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/test/TestEnum.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.test; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | 7 | enum Size { 8 | Big, Small 9 | } 10 | 11 | 12 | @SpringBootTest 13 | public class TestEnum { 14 | @Test 15 | public void testEnum() { 16 | Size size = JSON.parseObject("\"Big\"", Size.class); 17 | Size size1 = JSON.parseObject("\"Large\"", Size.class); 18 | System.out.println(size); 19 | System.out.println(size1); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/test/java/com/hiyj/blog/utils/JwtUtilsTest.java: -------------------------------------------------------------------------------- 1 | package com.hiyj.blog.utils; 2 | 3 | import com.hiyj.blog.services.UserJsonService; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | 8 | @SpringBootTest 9 | public class JwtUtilsTest { 10 | private UserJsonService userJsonService; 11 | 12 | @Autowired 13 | public void setUserService(UserJsonService userJsonService) { 14 | this.userJsonService = userJsonService; 15 | } 16 | 17 | @Test 18 | public void testReadJwt() { 19 | String rs = JwtUtils.getToken(userJsonService.findUserByAccount("admin@163.com")); 20 | System.out.println(rs); 21 | System.out.println(JwtUtils.getTokenUserId(rs)); 22 | System.out.println(JwtUtils.getTokenUserAccount(rs)); 23 | } 24 | } 25 | --------------------------------------------------------------------------------