├── .codacy.yml ├── .editorconfig ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── magic-core-auto ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── xkcoding │ │ └── magic │ │ └── core │ │ └── auto │ │ ├── common │ │ ├── AbstractBladeProcessor.java │ │ ├── MultiSetMap.java │ │ ├── Sets.java │ │ └── TypeHelper.java │ │ ├── factories │ │ ├── AutoFactoriesProcessor.java │ │ └── FactoriesFiles.java │ │ └── service │ │ ├── AutoService.java │ │ ├── AutoServiceProcessor.java │ │ └── ServicesFiles.java │ └── resources │ └── META-INF │ └── services │ └── javax.annotation.processing.Processor ├── magic-core-tool ├── README.md ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── xkcoding │ │ └── magic │ │ └── core │ │ └── tool │ │ ├── api │ │ ├── IResultCode.java │ │ ├── PageQuery.java │ │ ├── PageResult.java │ │ └── R.java │ │ ├── autoconfigure │ │ ├── MagicPropertyConfiguration.java │ │ ├── ToolAutoConfiguration.java │ │ └── ToolProperties.java │ │ ├── constants │ │ ├── MagicConsts.java │ │ └── MimeType.java │ │ ├── enums │ │ ├── CommonResultCode.java │ │ └── SortType.java │ │ ├── exception │ │ └── ServiceException.java │ │ ├── props │ │ ├── MagicPropertySource.java │ │ └── MagicPropertySourcePostProcessor.java │ │ ├── spel │ │ ├── ExpressionEvaluator.java │ │ └── ExpressionRootObject.java │ │ ├── support │ │ ├── ip │ │ │ └── Ip2Region.java │ │ ├── lambda │ │ │ └── Trys.java │ │ ├── lang │ │ │ ├── FastStringWriter.java │ │ │ └── Kv.java │ │ └── wrapper │ │ │ ├── page │ │ │ ├── mybatisplus │ │ │ │ └── PageResultWrapper.java │ │ │ └── pagehelper │ │ │ │ └── PageResultWrapper.java │ │ │ └── vo │ │ │ └── BaseEntityWrapper.java │ │ ├── util │ │ ├── AssertUtil.java │ │ ├── ClassUtil.java │ │ ├── CollUtil.java │ │ ├── ExceptionUtil.java │ │ ├── HashIdUtil.java │ │ ├── IpUtil.java │ │ ├── SpringUtil.java │ │ ├── StrUtil.java │ │ ├── UrlUtil.java │ │ ├── UuidUtil.java │ │ └── WebUtil.java │ │ └── yaml │ │ └── YamlPropertyLoaderFactory.java │ └── resources │ └── ip │ └── ip2region.db ├── magic-starter-bom ├── README.md └── pom.xml ├── magic-starter-id ├── README.md ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── xkcoding │ └── magic │ └── id │ ├── autoconfigure │ ├── IdAutoConfiguration.java │ ├── IdDatabaseProperties.java │ ├── IdRedisProperties.java │ ├── IdSnowflakeProperties.java │ └── base │ │ └── BaseIdProperties.java │ ├── exception │ └── IdException.java │ └── support │ ├── business │ ├── BusinessName.java │ └── impl │ │ ├── DateBusinessName.java │ │ └── DefaultBusinessName.java │ ├── factory │ ├── IdFactory.java │ └── impl │ │ ├── DatabaseIdFactory.java │ │ ├── RedisIdFactory.java │ │ └── SnowflakeIdFactory.java │ ├── id │ ├── Id.java │ ├── RangeId.java │ └── impl │ │ ├── DefaultRangeId.java │ │ └── SnowFlakeId.java │ ├── prefix │ ├── Prefix.java │ └── impl │ │ └── DefaultPrefix.java │ └── range │ ├── RangeManager.java │ ├── impl │ ├── database │ │ ├── BaseDbHelper.java │ │ └── DatabaseRangeManager.java │ └── redis │ │ └── RedisRangeManager.java │ └── model │ └── IdRange.java ├── magic-starter-locker ├── README.md └── pom.xml ├── magic-starter-log ├── README.md ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── xkcoding │ └── magic │ └── log │ ├── annotation │ └── OperateLog.java │ ├── aspect │ ├── OperateLogAspect.java │ └── RequestLogAspect.java │ ├── autoconfigure │ ├── LogAutoConfiguration.java │ └── LogProperties.java │ ├── constants │ └── LogConstants.java │ ├── enums │ └── LogType.java │ ├── logback │ └── logback-spring.xml │ ├── model │ ├── AbstractLogModel.java │ ├── CustomLogModel.java │ ├── ErrorLogModel.java │ └── OperateLogModel.java │ └── support │ ├── LogHandler.java │ ├── OperatorService.java │ ├── event │ ├── CustomLogEvent.java │ ├── ErrorLogEvent.java │ └── OperateLogEvent.java │ ├── listener │ └── LogEventListener.java │ ├── logger │ └── MagicLogger.java │ └── publisher │ └── LogEventPublisher.java ├── magic-starter-message ├── README.md ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── xkcoding │ └── magic │ └── message │ ├── autoconfigure │ ├── EmailMessageTemplateAutoConfiguration.java │ ├── MessageAutoConfiguration.java │ └── MessageProperties.java │ ├── constants │ └── MessageConstants.java │ ├── enums │ ├── DingTalkType.java │ └── EmailType.java │ ├── model │ ├── Message.java │ ├── dingtalk │ │ ├── AbstractDingTalkMessage.java │ │ ├── ActionCardSingleDingTalkMessage.java │ │ ├── ActionCardWholeDingTalkMessage.java │ │ ├── FeedCardDingTalkMessage.java │ │ ├── LinkDingTalkMessage.java │ │ ├── MarkdownDingTalkMessage.java │ │ ├── TextDingTalkMessage.java │ │ └── support │ │ │ ├── Attention.java │ │ │ ├── actioncard │ │ │ ├── ActionCardSingle.java │ │ │ ├── ActionCardWhole.java │ │ │ └── Button.java │ │ │ ├── feedcard │ │ │ ├── FeedCard.java │ │ │ └── FeedCardLink.java │ │ │ ├── link │ │ │ ├── BaseLink.java │ │ │ └── Link.java │ │ │ ├── markdown │ │ │ └── Markdown.java │ │ │ └── text │ │ │ └── Text.java │ ├── email │ │ ├── EmailMessage.java │ │ └── support │ │ │ ├── EmailAttachment.java │ │ │ └── EmailStaticResource.java │ └── sms │ │ └── SmsMessage.java │ └── support │ ├── AbstractMessageSender.java │ ├── MessageSenderService.java │ ├── dingtalk │ └── DingTalkMessageSender.java │ ├── email │ └── EmailMessageSender.java │ └── sms │ └── SmsMessageSender.java ├── magic-starter-oss ├── README.md ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── xkcoding │ └── magic │ └── oss │ ├── AbstractOssTemplate.java │ ├── OssTemplate.java │ ├── autoconfigure │ ├── OssAutoConfiguration.java │ ├── OssProperties.java │ ├── ali │ │ └── AliOssAutoConfiguration.java │ ├── minio │ │ └── MinIoAutoConfiguration.java │ ├── qiniu │ │ └── QiNiuCloudAutoConfiguration.java │ └── tencent │ │ └── TencentCosAutoConfiguration.java │ ├── enums │ └── OssType.java │ ├── model │ ├── OssFile.java │ └── OssFileMetaInfo.java │ └── support │ ├── ali │ └── AliOssTemplate.java │ ├── minio │ ├── MinIoTemplate.java │ └── enums │ │ └── PolicyType.java │ ├── qiniu │ └── QiNiuCloudTemplate.java │ ├── rule │ └── OssRule.java │ └── tencent │ └── TencentCosTemplate.java ├── magic-starter-ratelimiter ├── README.md └── pom.xml ├── magic-starter-secure ├── README.md ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── xkcoding │ └── magic │ └── secure │ ├── annotation │ ├── CurrentUser.java │ └── Secure.java │ ├── aspect │ └── SecureAspect.java │ ├── autoconfigure │ ├── SecureAutoConfiguration.java │ ├── SecureConfiguration.java │ └── SecureProperties.java │ ├── constants │ └── SecureConstants.java │ ├── enums │ └── HttpMethod.java │ ├── exception │ ├── AuthorizationException.java │ ├── ExpiredTokenException.java │ ├── InvalidTokenException.java │ ├── NotFoundTokenException.java │ ├── SecureException.java │ └── TokenException.java │ ├── interceptor │ └── SecureInterceptor.java │ ├── model │ ├── Rule.java │ └── SecureUser.java │ ├── support │ ├── DefaultSecureExpressionHandler.java │ ├── SecureExpressionHandler.java │ ├── SecureRuleRegistry.java │ ├── SecureUserArgumentResolver.java │ └── UserContextHolder.java │ └── util │ ├── JwtUtil.java │ ├── SecureCheckUtil.java │ └── SecureUtil.java └── pom.xml /.codacy.yml: -------------------------------------------------------------------------------- 1 | --- 2 | exclude_paths: 3 | - '**.md' 4 | - '**/**.md' 5 | - '**.sql' 6 | - '**.html' 7 | - '**/static/**' 8 | - '**/templates/**' 9 | - '**/test/**' 10 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | # 空格替代Tab缩进在各种编辑工具下效果一致 5 | [*] 6 | indent_style = space 7 | indent_size = 2 8 | charset = utf-8 9 | end_of_line = lf 10 | trim_trailing_whitespace = true 11 | insert_final_newline = true 12 | 13 | [*.java] 14 | indent_style = tab 15 | indent_size = 4 16 | 17 | [*.md] 18 | insert_final_newline = false 19 | trim_trailing_whitespace = false 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### common-ignore template 3 | ### GIT IGNORE FILE ### 4 | 5 | ### MAC ### 6 | .DS_Store 7 | 8 | ### IDEA ### 9 | .idea 10 | *.iml 11 | 12 | ### ECLIPSE ### 13 | .classpath 14 | .metadata 15 | .settings 16 | .project 17 | .factorypath 18 | 19 | ### JAVA ### 20 | logs 21 | target 22 | 23 | ### JS ### 24 | node_modules 25 | dist 26 | 27 | ### OTHERS ### 28 | build 29 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # 语言 2 | language: java 3 | # 执行脚本 4 | script: "mvn clean package -DskipTests=true -Dmaven.javadoc.skip=true -B -V" 5 | # 通知 6 | notifications: 7 | email: 8 | recipients: 9 | - 237497819@qq.com 10 | on_success: always # default: change 11 | on_failure: always # default: always 12 | # 缓存 13 | cache: 14 | directories: 15 | - '$HOME/.m2/repository' 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Magic-Starter

2 |

3 | Travis-CI 4 | MAVEN 5 | Codacy 6 | author 7 | JDK 8 | Spring Boot 9 | LICENSE 10 |

11 | 12 | ## 简介 13 | 14 | 一组神奇的 starter 套件,只为爱“偷懒”的你~ 15 | 16 | ## 开发进度 17 | 18 | - [x] 自动装配组件模块:magic-core-auto 19 | - [x] 核心工具包模块:magic-core-tool 20 | - [x] 依赖版本控制模块:magic-starter-bom 21 | - [x] 极简的权限控制模块:magic-starter-secure 22 | - [x] 通用操作日志封装模块:magic-starter-log 23 | - [x] 通用消息通知封装模块:magic-starter-message 24 | - [ ] 限流组件模块:magic-starter-ratelimiter 25 | - [ ] 分布式锁模块:magic-starter-locker 26 | - [x] 通用对象存储封装模块:magic-starter-oss 27 | - [x] 分布式主键生成器模块:magic-starter-id 28 | -------------------------------------------------------------------------------- /magic-core-auto/README.md: -------------------------------------------------------------------------------- 1 |

magic-core-auto

2 |

3 | Travis-CI 4 | MAVEN 5 | Codacy 6 | author 7 | JDK 8 | Spring Boot 9 | LICENSE 10 |

11 | 12 | ## 简介 13 | 14 | `magic-core-auto` 借鉴了 [春哥](https://github.com/ChunMengLu) 的 [mica-auto](https://github.com/lets-mica/mica-auto),是 `magic-starter` 的一个基础组件,通过扫描 `@Component`、`@Configuration` 等注解,用自动生成 `Spring Boot Starter` 的一些基础配置,包括 `spring.factories`、`spring-autoconfigure-metadata.properties`、`spring-configuration-metadata.json`、`spring-devtools.properties` 等文件的自动生成。 15 | 16 | ## 使用 17 | 18 | > 注意:如果你项目中使用了 `Lombok` 请将 `magic-auto` 的依赖放置到 `Lombok` 后面。 19 | 20 | ```xml 21 | 22 | com.xkcoding 23 | magic-core-auto 24 | ${magic-starter.version} 25 | provided 26 | 27 | ``` 28 | 29 | ## 参考 30 | 31 | - mica-atuo:https://github.com/lets-mica/mica-auto 32 | - Google Auto: https://github.com/google/auto 33 | - Spring 5 - spring-context-indexer:https://github.com/spring-projects/spring-framework/tree/master/spring-context-indexer 34 | 35 | -------------------------------------------------------------------------------- /magic-core-auto/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 21 | 22 | Magic-Starter 23 | com.xkcoding.magic 24 | 0.0.1-SNAPSHOT 25 | 26 | 4.0.0 27 | 28 | magic-core-auto 29 | ${project.artifactId} 30 | ${magic-starter.version} 31 | jar 32 | 33 | 34 | Magic Starter 核心工具包自动生成 spring.factories、additional-spring-configuration-metadata.json 等文件 35 | 36 | 37 | 38 | 1.0-rc6 39 | 40 | 41 | 42 | 43 | 44 | org.apache.maven.plugins 45 | maven-compiler-plugin 46 | 47 | 48 | 49 | com.google.auto.service 50 | auto-service 51 | ${google-auto-service.version} 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /magic-core-auto/src/main/java/com/xkcoding/magic/core/auto/common/Sets.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2028, DreamLu All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * Neither the name of the dreamlu.net developer nor the names of its 13 | * contributors may be used to endorse or promote products derived from 14 | * this software without specific prior written permission. 15 | * Author: DreamLu 卢春梦 (596392912@qq.com) 16 | */ 17 | package com.xkcoding.magic.core.auto.common; 18 | 19 | import java.util.Objects; 20 | import java.util.Set; 21 | import java.util.stream.Collectors; 22 | import java.util.stream.Stream; 23 | 24 | /** 25 | * 集合 工具类 26 | * 27 | * @author L.cm 28 | */ 29 | public class Sets { 30 | 31 | /** 32 | * 不可变 集合 33 | * 34 | * @param es 对象 35 | * @param 泛型 36 | * @return 集合 37 | */ 38 | @SafeVarargs 39 | public static Set ofImmutableSet(E... es) { 40 | Objects.requireNonNull(es); 41 | return Stream.of(es).collect(Collectors.toSet()); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /magic-core-auto/src/main/java/com/xkcoding/magic/core/auto/factories/FactoriesFiles.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2028, DreamLu All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * Neither the name of the dreamlu.net developer nor the names of its 13 | * contributors may be used to endorse or promote products derived from 14 | * this software without specific prior written permission. 15 | * Author: DreamLu 卢春梦 (596392912@qq.com) 16 | */ 17 | package com.xkcoding.magic.core.auto.factories; 18 | 19 | import com.xkcoding.magic.core.auto.common.MultiSetMap; 20 | import lombok.experimental.UtilityClass; 21 | 22 | import java.io.BufferedWriter; 23 | import java.io.IOException; 24 | import java.io.OutputStream; 25 | import java.io.OutputStreamWriter; 26 | import java.nio.charset.Charset; 27 | import java.nio.charset.StandardCharsets; 28 | import java.util.Set; 29 | import java.util.StringJoiner; 30 | 31 | /** 32 | * spring boot 自动化配置工具类 33 | * 34 | * @author L.cm 35 | */ 36 | @UtilityClass 37 | class FactoriesFiles { 38 | private static final Charset UTF_8 = StandardCharsets.UTF_8; 39 | 40 | /** 41 | * 写出 spring.factories 文件 42 | * 43 | * @param factories factories 信息 44 | * @param output 输出流 45 | * @throws IOException 异常信息 46 | */ 47 | static void writeFactoriesFile(MultiSetMap factories, OutputStream output) throws IOException { 48 | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, UTF_8)); 49 | Set keySet = factories.keySet(); 50 | for (String key : keySet) { 51 | Set values = factories.get(key); 52 | if (values == null || values.isEmpty()) { 53 | continue; 54 | } 55 | writer.write(key); 56 | writer.write("=\\\n "); 57 | StringJoiner joiner = new StringJoiner(",\\\n "); 58 | for (String value : values) { 59 | joiner.add(value); 60 | } 61 | writer.write(joiner.toString()); 62 | writer.newLine(); 63 | } 64 | writer.flush(); 65 | output.close(); 66 | } 67 | 68 | /** 69 | * 写出 spring-devtools.properties 70 | * 71 | * @param projectName 项目名 72 | * @param output 输出流 73 | * @throws IOException 异常信息 74 | */ 75 | static void writeDevToolsFile(String projectName, OutputStream output) throws IOException { 76 | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, UTF_8)); 77 | // restart.include.blade-cloud=/blade-cloud[\\w-]+\.jar 78 | String format = "restart.include.%s=/%s[\\\\w-]+\\.jar"; 79 | writer.write(String.format(format, projectName, projectName)); 80 | writer.flush(); 81 | output.close(); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /magic-core-auto/src/main/java/com/xkcoding/magic/core/auto/service/AutoService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2028, DreamLu All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * Neither the name of the dreamlu.net developer nor the names of its 13 | * contributors may be used to endorse or promote products derived from 14 | * this software without specific prior written permission. 15 | * Author: DreamLu 卢春梦 (596392912@qq.com) 16 | */ 17 | package com.xkcoding.magic.core.auto.service; 18 | 19 | import java.lang.annotation.*; 20 | 21 | /** 22 | * An annotation for service providers as described in {@link java.util.ServiceLoader}. The {@link 23 | * AutoServiceProcessor} generates the configuration files which 24 | * allows service providers to be loaded with {@link java.util.ServiceLoader#load(Class)}. 25 | * 26 | *

Service providers assert that they conform to the service provider specification. 27 | * Specifically, they must: 28 | * 29 | *

    30 | *
  • be a non-inner, non-anonymous, concrete class 31 | *
  • have a publicly accessible no-arg constructor 32 | *
  • implement the interface type returned by {@code value()} 33 | *
34 | */ 35 | @Documented 36 | @Retention(RetentionPolicy.SOURCE) 37 | @Target(ElementType.TYPE) 38 | public @interface AutoService { 39 | /** 40 | * Returns the interfaces implemented by this service provider. 41 | * 42 | * @return interface array 43 | */ 44 | Class[] value(); 45 | } 46 | -------------------------------------------------------------------------------- /magic-core-auto/src/main/java/com/xkcoding/magic/core/auto/service/ServicesFiles.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2028, DreamLu All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * Neither the name of the dreamlu.net developer nor the names of its 13 | * contributors may be used to endorse or promote products derived from 14 | * this software without specific prior written permission. 15 | * Author: DreamLu 卢春梦 (596392912@qq.com) 16 | */ 17 | package com.xkcoding.magic.core.auto.service; 18 | 19 | import lombok.experimental.UtilityClass; 20 | 21 | import java.io.*; 22 | import java.nio.charset.Charset; 23 | import java.nio.charset.StandardCharsets; 24 | import java.util.Collection; 25 | import java.util.HashSet; 26 | import java.util.Set; 27 | 28 | /** 29 | * A helper class for reading and writing Services files. 30 | * 31 | * @author L.cm 32 | */ 33 | @UtilityClass 34 | class ServicesFiles { 35 | private static final Charset UTF_8 = StandardCharsets.UTF_8; 36 | 37 | /** 38 | * Reads the set of service classes from a service file. 39 | * 40 | * @param input not {@code null}. Closed after use. 41 | * @return a not {@code null Set} of service class names. 42 | * @throws IOException 43 | */ 44 | static Set readServiceFile(InputStream input) throws IOException { 45 | HashSet serviceClasses = new HashSet<>(); 46 | try (InputStreamReader isr = new InputStreamReader(input, UTF_8); BufferedReader r = new BufferedReader(isr)) { 47 | String line; 48 | while ((line = r.readLine()) != null) { 49 | int commentStart = line.indexOf('#'); 50 | if (commentStart >= 0) { 51 | line = line.substring(0, commentStart); 52 | } 53 | line = line.trim(); 54 | if (!line.isEmpty()) { 55 | serviceClasses.add(line); 56 | } 57 | } 58 | return serviceClasses; 59 | } 60 | } 61 | 62 | /** 63 | * Writes the set of service class names to a service file. 64 | * 65 | * @param output not {@code null}. Not closed after use. 66 | * @param services a not {@code null Collection} of service class names. 67 | * @throws IOException 68 | */ 69 | static void writeServiceFile(Collection services, OutputStream output) throws IOException { 70 | BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, UTF_8)); 71 | for (String service : services) { 72 | writer.write(service); 73 | writer.newLine(); 74 | } 75 | writer.flush(); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /magic-core-auto/src/main/resources/META-INF/services/javax.annotation.processing.Processor: -------------------------------------------------------------------------------- 1 | com.xkcoding.magic.core.auto.service.AutoServiceProcessor 2 | com.xkcoding.magic.core.auto.factories.AutoFactoriesProcessor 3 | -------------------------------------------------------------------------------- /magic-core-tool/README.md: -------------------------------------------------------------------------------- 1 |

magic-core-tool

2 |

3 | Travis-CI 4 | MAVEN 5 | Codacy 6 | author 7 | JDK 8 | Spring Boot 9 | LICENSE 10 |

11 | 12 | ## 简介 13 | 14 | `magic-core-tool` 主要提供常用的 API 返回参数封装,分页条件封装,分页结果封装,通用异常封装,常用工具类封装,同时集成 `Google Guava`、`Hutool`等工具包。 15 | 16 | ## 使用 17 | 18 | ```xml 19 | 20 | com.xkcoding 21 | magic-core-tool 22 | ${magic-starter.version} 23 | 24 | ``` 25 | 26 | ## 致谢 27 | 28 | - Google Guava:https://github.com/google/guava 29 | - Hutool:https://github.com/looly/hutool 30 | 31 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/api/IResultCode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.core.tool.api; 18 | 19 | import java.io.Serializable; 20 | 21 | /** 22 | *

23 | * 返回值接口 24 | *

25 | * 26 | * @author yangkai.shen 27 | * @date Created in 2019/9/19 21:44 28 | */ 29 | public interface IResultCode extends Serializable { 30 | /** 31 | * 返回消息 32 | * 33 | * @return String 34 | */ 35 | String getMessage(); 36 | 37 | /** 38 | * 状态码 39 | * 40 | * @return int 41 | */ 42 | int getCode(); 43 | } 44 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/api/PageQuery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.core.tool.api; 18 | 19 | import com.xkcoding.magic.core.tool.enums.SortType; 20 | import io.swagger.annotations.ApiModel; 21 | import io.swagger.annotations.ApiModelProperty; 22 | import lombok.AllArgsConstructor; 23 | import lombok.Data; 24 | import lombok.NoArgsConstructor; 25 | 26 | /** 27 | *

28 | * 通用分页参数封装 29 | *

30 | * 31 | * @author yangkai.shen 32 | * @date Created in 2019/9/19 22:05 33 | */ 34 | @Data 35 | @NoArgsConstructor 36 | @AllArgsConstructor 37 | @ApiModel(description = "分页参数") 38 | public class PageQuery { 39 | /** 40 | * 当前页码 41 | */ 42 | @ApiModelProperty(value = "当前页码", required = true) 43 | private Integer currentPage; 44 | /** 45 | * 每页条数 46 | */ 47 | @ApiModelProperty(value = "每页条数", required = true) 48 | private Integer pageSize; 49 | /** 50 | * 排序字段 51 | */ 52 | @ApiModelProperty(value = "排序字段") 53 | private String sortBy; 54 | /** 55 | * 排序方式 56 | * 57 | * @see SortType 58 | */ 59 | @ApiModelProperty(value = "排序方式") 60 | private SortType sortType; 61 | } 62 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/autoconfigure/MagicPropertyConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2028, DreamLu All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * Neither the name of the dreamlu.net developer nor the names of its 13 | * contributors may be used to endorse or promote products derived from 14 | * this software without specific prior written permission. 15 | * Author: DreamLu 卢春梦 (596392912@qq.com) 16 | */ 17 | 18 | package com.xkcoding.magic.core.tool.autoconfigure; 19 | 20 | import com.xkcoding.magic.core.tool.props.MagicPropertySourcePostProcessor; 21 | import org.springframework.context.annotation.Bean; 22 | import org.springframework.context.annotation.Configuration; 23 | import org.springframework.core.Ordered; 24 | import org.springframework.core.annotation.Order; 25 | 26 | /** 27 | * blade property config 28 | * 29 | * @author L.cm 30 | */ 31 | @Configuration 32 | @Order(Ordered.HIGHEST_PRECEDENCE) 33 | public class MagicPropertyConfiguration { 34 | 35 | @Bean 36 | public MagicPropertySourcePostProcessor magicPropertySourcePostProcessor() { 37 | return new MagicPropertySourcePostProcessor(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/autoconfigure/ToolAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.core.tool.autoconfigure; 18 | 19 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 20 | import org.springframework.context.annotation.Configuration; 21 | 22 | /** 23 | *

24 | * 自动装配类 25 | *

26 | * 27 | * @author yangkai.shen 28 | * @date Created in 2019/9/24 17:57 29 | */ 30 | @Configuration 31 | @EnableConfigurationProperties({ToolProperties.class}) 32 | public class ToolAutoConfiguration { 33 | } 34 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/autoconfigure/ToolProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.core.tool.autoconfigure; 18 | 19 | import lombok.Data; 20 | import org.springframework.boot.context.properties.ConfigurationProperties; 21 | 22 | /** 23 | *

24 | * 配置属性 25 | *

26 | * 27 | * @author yangkai.shen 28 | * @date Created in 2019/9/24 17:58 29 | */ 30 | @Data 31 | @ConfigurationProperties(prefix = "magic.tool") 32 | public class ToolProperties { 33 | /** 34 | * ip2region 配置. 35 | */ 36 | private Ip2RegionProperties ipRegion = new Ip2RegionProperties(); 37 | 38 | @Data 39 | public static class Ip2RegionProperties { 40 | /** 41 | * ip 数据文件,默认读取 classpath 下的 ip/ip2region.db 文件. 42 | * 该文件可以从 https://github.com/xkcoding/magic-starter/tree/master/magic-core-tool/src/main/resources/ip/ip2region.db 下载 43 | */ 44 | private String dbFile = "ip/ip2region.db"; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/constants/MagicConsts.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.core.tool.constants; 18 | 19 | /** 20 | *

21 | * Magic Starter 常量池 22 | *

23 | * 24 | * @author yangkai.shen 25 | * @date Created in 2019/9/23 14:26 26 | */ 27 | public interface MagicConsts { 28 | /** 29 | * 项目名称常量 30 | */ 31 | String MAGIC = "magic"; 32 | /** 33 | * 注解 @Secure AOP顺序 34 | */ 35 | int AOP_ORDER_SECURE = 0; 36 | 37 | /** 38 | * 注解 @Log AOP顺序 39 | */ 40 | int AOP_ORDER_LOG = AOP_ORDER_SECURE + 1; 41 | 42 | /** 43 | * web 请求 AOP 顺序 44 | */ 45 | int AOP_ORDER_REQUEST_LOG = AOP_ORDER_SECURE - 1; 46 | } 47 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/enums/CommonResultCode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.core.tool.enums; 18 | 19 | import com.xkcoding.magic.core.tool.api.IResultCode; 20 | import lombok.AllArgsConstructor; 21 | import lombok.Getter; 22 | 23 | import javax.servlet.http.HttpServletResponse; 24 | 25 | /** 26 | *

27 | * 通用业务代码枚举 28 | *

29 | * 30 | * @author yangkai.shen 31 | * @date Created in 2019/9/19 22:02 32 | */ 33 | @Getter 34 | @AllArgsConstructor 35 | public enum CommonResultCode implements IResultCode { 36 | 37 | /** 38 | * 操作成功 39 | */ 40 | SUCCESS(HttpServletResponse.SC_OK, "操作成功"), 41 | 42 | /** 43 | * 业务异常 44 | */ 45 | FAILURE(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "业务异常"), 46 | 47 | /** 48 | * 请求未授权 49 | */ 50 | UN_AUTHORIZED(HttpServletResponse.SC_UNAUTHORIZED, "请求未授权"), 51 | 52 | /** 53 | * 404 没找到请求 54 | */ 55 | NOT_FOUND(HttpServletResponse.SC_NOT_FOUND, "404 没找到请求"), 56 | 57 | /** 58 | * 消息不能读取 59 | */ 60 | MSG_NOT_READABLE(HttpServletResponse.SC_BAD_REQUEST, "消息不能读取"), 61 | 62 | /** 63 | * 不支持当前请求方法 64 | */ 65 | METHOD_NOT_SUPPORTED(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "不支持当前请求方法"), 66 | 67 | /** 68 | * 不支持当前媒体类型 69 | */ 70 | MEDIA_TYPE_NOT_SUPPORTED(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, "不支持当前媒体类型"), 71 | 72 | /** 73 | * 请求被拒绝 74 | */ 75 | REQ_REJECT(HttpServletResponse.SC_FORBIDDEN, "请求被拒绝"), 76 | 77 | /** 78 | * 服务器异常 79 | */ 80 | INTERNAL_SERVER_ERROR(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "服务器异常"), 81 | 82 | /** 83 | * 缺少必要的请求参数 84 | */ 85 | PARAM_MISS(HttpServletResponse.SC_BAD_REQUEST, "缺少必要的请求参数"), 86 | 87 | /** 88 | * 请求参数类型错误 89 | */ 90 | PARAM_TYPE_ERROR(HttpServletResponse.SC_BAD_REQUEST, "请求参数类型错误"), 91 | 92 | /** 93 | * 请求参数绑定错误 94 | */ 95 | PARAM_BIND_ERROR(HttpServletResponse.SC_BAD_REQUEST, "请求参数绑定错误"), 96 | 97 | /** 98 | * 参数校验失败 99 | */ 100 | PARAM_VALID_ERROR(HttpServletResponse.SC_BAD_REQUEST, "参数校验未通过"), 101 | 102 | /** 103 | * 该服务已下线 104 | */ 105 | SERVICES_DISABLED(1, "服务未启用"), 106 | ; 107 | 108 | /** 109 | * 状态码 110 | */ 111 | private final int code; 112 | /** 113 | * 返回消息 114 | */ 115 | private final String message; 116 | 117 | } 118 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/enums/SortType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.core.tool.enums; 18 | 19 | import cn.hutool.core.util.StrUtil; 20 | import lombok.AllArgsConstructor; 21 | import lombok.Getter; 22 | 23 | /** 24 | *

25 | * 排序枚举 26 | *

27 | * 28 | * @author yangkai.shen 29 | * @date Created in 2019/9/19 22:06 30 | */ 31 | @Getter 32 | @AllArgsConstructor 33 | public enum SortType { 34 | /** 35 | * 升序 36 | */ 37 | ASC(1), 38 | /** 39 | * 降序 40 | */ 41 | DESC(-1); 42 | 43 | int value; 44 | 45 | /** 46 | * 通过参数转换 47 | * 48 | * @param value 值 49 | * @return 枚举对象 50 | */ 51 | public static SortType fromInt(int value) { 52 | switch (value) { 53 | case -1: 54 | return DESC; 55 | case 1: 56 | return ASC; 57 | default: 58 | throw new IllegalArgumentException(value + " is not a valid"); 59 | } 60 | } 61 | 62 | /** 63 | * 通过字符串转化 64 | * 65 | * @param value 值 66 | * @return 枚举对象 67 | */ 68 | public static SortType fromStr(String value) { 69 | if (StrUtil.isNotBlank(value)) { 70 | return SortType.valueOf(value.toUpperCase()); 71 | } 72 | throw new IllegalArgumentException(value + " is not a valid"); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/exception/ServiceException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.core.tool.exception; 18 | 19 | import com.xkcoding.magic.core.tool.api.IResultCode; 20 | import com.xkcoding.magic.core.tool.enums.CommonResultCode; 21 | import lombok.Getter; 22 | 23 | /** 24 | *

25 | * 通用业务异常 26 | *

27 | * 28 | * @author yangkai.shen 29 | * @date Created in 2019/9/19 22:21 30 | */ 31 | public class ServiceException extends RuntimeException { 32 | private static final long serialVersionUID = 2359767895161832954L; 33 | 34 | @Getter 35 | private final IResultCode resultCode; 36 | @Getter 37 | private final Object data; 38 | 39 | public ServiceException(String message) { 40 | super(message); 41 | this.resultCode = CommonResultCode.INTERNAL_SERVER_ERROR; 42 | data = null; 43 | } 44 | 45 | public ServiceException(IResultCode resultCode) { 46 | super(resultCode.getMessage()); 47 | this.resultCode = resultCode; 48 | data = null; 49 | } 50 | 51 | public ServiceException(IResultCode resultCode, Object data) { 52 | super(resultCode.getMessage()); 53 | this.resultCode = resultCode; 54 | this.data = data; 55 | } 56 | 57 | public ServiceException(IResultCode resultCode, Throwable cause) { 58 | super(cause); 59 | this.resultCode = resultCode; 60 | data = null; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/props/MagicPropertySource.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2028, DreamLu All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * Neither the name of the dreamlu.net developer nor the names of its 13 | * contributors may be used to endorse or promote products derived from 14 | * this software without specific prior written permission. 15 | * Author: DreamLu 卢春梦 (596392912@qq.com) 16 | */ 17 | 18 | package com.xkcoding.magic.core.tool.props; 19 | 20 | import org.springframework.core.Ordered; 21 | 22 | import java.lang.annotation.*; 23 | 24 | /** 25 | * 自定义资源文件读取,优先级最低 26 | * 27 | * @author L.cm 28 | */ 29 | @Target(ElementType.TYPE) 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Documented 32 | public @interface MagicPropertySource { 33 | 34 | /** 35 | * Indicate the resource location(s) of the properties file to be loaded. 36 | * for example, {@code "classpath:/com/example/app.yml"} 37 | * 38 | * @return location(s) 39 | */ 40 | String value(); 41 | 42 | /** 43 | * load app-{activeProfile}.yml 44 | * 45 | * @return {boolean} 46 | */ 47 | boolean loadActiveProfile() default true; 48 | 49 | /** 50 | * Get the order value of this resource. 51 | * 52 | * @return order 53 | */ 54 | int order() default Ordered.LOWEST_PRECEDENCE; 55 | 56 | } 57 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/spel/ExpressionRootObject.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2028, DreamLu All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * Neither the name of the dreamlu.net developer nor the names of its 13 | * contributors may be used to endorse or promote products derived from 14 | * this software without specific prior written permission. 15 | * Author: DreamLu 卢春梦 (596392912@qq.com) 16 | */ 17 | 18 | package com.xkcoding.magic.core.tool.spel; 19 | 20 | import lombok.AllArgsConstructor; 21 | import lombok.Getter; 22 | 23 | import java.lang.reflect.Method; 24 | 25 | /** 26 | * ExpressionRootObject 27 | * 28 | * @author L.cm 29 | */ 30 | @Getter 31 | @AllArgsConstructor 32 | public class ExpressionRootObject { 33 | private final Method method; 34 | 35 | private final Object[] args; 36 | 37 | private final Object target; 38 | 39 | private final Class targetClass; 40 | 41 | private final Method targetMethod; 42 | } 43 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/support/wrapper/page/mybatisplus/PageResultWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.core.tool.support.wrapper.page.mybatisplus; 18 | 19 | import com.baomidou.mybatisplus.core.metadata.IPage; 20 | import com.xkcoding.magic.core.tool.api.PageResult; 21 | 22 | /** 23 | *

24 | * Mybatis-Plus 分页结果集包装 25 | *

26 | * 27 | * @author yangkai.shen 28 | * @date Created in 2019/11/6 10:45 29 | */ 30 | public class PageResultWrapper { 31 | /** 32 | * 构造分页对象 33 | * 34 | * @param pageData 当前页数据 35 | * @param 泛型 36 | * @return 分页对象 37 | */ 38 | public static PageResult wrapper(IPage pageData) { 39 | return new PageResult<>((int) pageData.getCurrent(), (int) pageData.getSize(), (int) pageData.getPages(), pageData.getTotal(), pageData.getRecords()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/support/wrapper/page/pagehelper/PageResultWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.core.tool.support.wrapper.page.pagehelper; 18 | 19 | import com.github.pagehelper.Page; 20 | import com.xkcoding.magic.core.tool.api.PageResult; 21 | 22 | /** 23 | *

24 | * PageHelper 分页结果集包装 25 | *

26 | * 27 | * @author yangkai.shen 28 | * @date Created in 2019/11/6 10:45 29 | */ 30 | public class PageResultWrapper { 31 | /** 32 | * 构造分页对象 33 | * 34 | * @param pageData 当前页数据 35 | * @param 泛型 36 | * @return 分页对象 37 | */ 38 | public static PageResult wrapper(Page pageData) { 39 | return new PageResult<>(pageData.getPageNum(), pageData.getPageSize(), pageData.getPages(), pageData.getTotal(), pageData.getResult()); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/support/wrapper/vo/BaseEntityWrapper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.core.tool.support.wrapper.vo; 18 | 19 | import com.xkcoding.magic.core.tool.api.PageResult; 20 | 21 | import java.util.List; 22 | import java.util.stream.Collectors; 23 | 24 | /** 25 | *

26 | * VO对象包装器 27 | *

28 | * 29 | * @author yangkai.shen 30 | * @date Created in 2019/10/25 15:06 31 | */ 32 | public abstract class BaseEntityWrapper { 33 | /** 34 | * 单个实体类包装VO对象 35 | * 36 | * @param entity 实体对象 37 | * @return VO 对象 38 | */ 39 | public abstract V toVO(E entity); 40 | 41 | /** 42 | * 实体类集合包装VO集合 43 | * 44 | * @param list 集合 45 | * @return VO 集合对象 46 | */ 47 | public List listVO(List list) { 48 | return list.stream().map(this::toVO).collect(Collectors.toList()); 49 | } 50 | 51 | /** 52 | * 分页实体类集合包装VO分页 53 | * 54 | * @param pageResult 分页数据 55 | * @return VO 分页对象 56 | */ 57 | public PageResult pageVO(PageResult pageResult) { 58 | List records = listVO(pageResult.getList()); 59 | return new PageResult<>(pageResult.getCurrentPage(), pageResult.getPageSize(), pageResult.getTotalPage(), pageResult.getTotal(), records); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/util/CollUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.core.tool.util; 18 | 19 | import cn.hutool.core.util.ArrayUtil; 20 | import org.springframework.lang.Nullable; 21 | 22 | /** 23 | *

24 | * 对 hutool CollUtil 的扩展 25 | *

26 | * 27 | * @author yangkai.shen 28 | * @date Created in 2020/1/2 10:49 29 | */ 30 | public class CollUtil extends cn.hutool.core.collection.CollUtil { 31 | /** 32 | * 数组中是否包含元素 33 | * 34 | * @param 数组元素类型 35 | * @param array 数组 36 | * @param value 被检查的元素 37 | * @return 是否包含 38 | */ 39 | public static boolean contains(@Nullable T[] array, T value) { 40 | return ArrayUtil.isNotEmpty(array) && ArrayUtil.contains(array, value); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/util/UrlUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2028, DreamLu All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * 7 | * Redistributions of source code must retain the above copyright notice, 8 | * this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | * notice, this list of conditions and the following disclaimer in the 11 | * documentation and/or other materials provided with the distribution. 12 | * Neither the name of the dreamlu.net developer nor the names of its 13 | * contributors may be used to endorse or promote products derived from 14 | * this software without specific prior written permission. 15 | * Author: DreamLu 卢春梦 (596392912@qq.com) 16 | */ 17 | package com.xkcoding.magic.core.tool.util; 18 | 19 | import java.net.URI; 20 | import java.net.URISyntaxException; 21 | import java.nio.charset.Charset; 22 | 23 | /** 24 | * url处理工具类 25 | * 26 | * @author L.cm 27 | */ 28 | public class UrlUtil extends org.springframework.web.util.UriUtils { 29 | 30 | /** 31 | * url 编码,同js decodeURIComponent 32 | * 33 | * @param source url 34 | * @param charset 字符集 35 | * @return 编码后的url 36 | */ 37 | public static String encodeURL(String source, Charset charset) { 38 | return UrlUtil.encode(source, charset.name()); 39 | } 40 | 41 | /** 42 | * url 解码 43 | * 44 | * @param source url 45 | * @param charset 字符集 46 | * @return 解码url 47 | */ 48 | public static String decodeURL(String source, Charset charset) { 49 | return UrlUtil.decode(source, charset.name()); 50 | } 51 | 52 | /** 53 | * 获取url路径 54 | * 55 | * @param uriStr 路径 56 | * @return url路径 57 | */ 58 | public static String getPath(String uriStr) { 59 | URI uri; 60 | 61 | try { 62 | uri = new URI(uriStr); 63 | } catch (URISyntaxException var3) { 64 | throw new RuntimeException(var3); 65 | } 66 | 67 | return uri.getPath(); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/java/com/xkcoding/magic/core/tool/yaml/YamlPropertyLoaderFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.core.tool.yaml; 18 | 19 | import com.xkcoding.magic.core.tool.util.StrUtil; 20 | import org.springframework.boot.env.OriginTrackedMapPropertySource; 21 | import org.springframework.boot.env.YamlPropertySourceLoader; 22 | import org.springframework.core.env.MapPropertySource; 23 | import org.springframework.core.env.PropertySource; 24 | import org.springframework.core.io.Resource; 25 | import org.springframework.core.io.support.DefaultPropertySourceFactory; 26 | import org.springframework.core.io.support.EncodedResource; 27 | import org.springframework.lang.Nullable; 28 | 29 | import java.io.IOException; 30 | import java.util.Collections; 31 | import java.util.HashMap; 32 | import java.util.List; 33 | import java.util.Map; 34 | import java.util.stream.Stream; 35 | 36 | /** 37 | * Yaml配置文件加载 38 | * 39 | * @author lcm 40 | */ 41 | public class YamlPropertyLoaderFactory extends DefaultPropertySourceFactory { 42 | 43 | @Override 44 | public PropertySource createPropertySource(@Nullable String name, EncodedResource encodedResource) throws IOException { 45 | if (encodedResource == null) { 46 | return emptyPropertySource(name); 47 | } 48 | Resource resource = encodedResource.getResource(); 49 | String fileName = resource.getFilename(); 50 | List> sources = new YamlPropertySourceLoader().load(fileName, resource); 51 | if (sources.isEmpty()) { 52 | return emptyPropertySource(fileName); 53 | } 54 | // yaml 数据存储,合成一个 PropertySource 55 | Map ymlDataMap = new HashMap<>(32); 56 | for (PropertySource source : sources) { 57 | ymlDataMap.putAll(((MapPropertySource) source).getSource()); 58 | } 59 | return new OriginTrackedMapPropertySource(getSourceName(fileName, name), ymlDataMap); 60 | } 61 | 62 | private static PropertySource emptyPropertySource(@Nullable String name) { 63 | return new MapPropertySource(getSourceName(name), Collections.emptyMap()); 64 | } 65 | 66 | private static String getSourceName(String... names) { 67 | return Stream.of(names).filter(StrUtil::isNotBlank).findFirst().orElse("MagicYamlPropertySource"); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /magic-core-tool/src/main/resources/ip/ip2region.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xkcoding/magic-starter/d7c4529464cec03e876aeef9c14d5f2d9af8c0d2/magic-core-tool/src/main/resources/ip/ip2region.db -------------------------------------------------------------------------------- /magic-starter-bom/README.md: -------------------------------------------------------------------------------- 1 |

magic-starter-bom

2 |

3 | Travis-CI 4 | MAVEN 5 | Codacy 6 | author 7 | JDK 8 | Spring Boot 9 | LICENSE 10 |

11 | 12 | ## 简介 13 | 14 | `magic-starter-bom` 主要是对 `Magic-Starter` 下各个模块的版本管理。 15 | 16 | ## 使用 17 | 18 | 在 pom.xml 文件中配置依赖管理,后续引入 `Magic-Starter` 其余模块,均可以不配置具体版本号信息。 19 | 20 | ```xml 21 | 22 | 23 | 24 | com.xkcoding 25 | magic-starter-bom 26 | ${magic-starter.version} 27 | pom 28 | import 29 | 30 | 31 | 32 | ``` 33 | 34 | -------------------------------------------------------------------------------- /magic-starter-id/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 21 | 22 | Magic-Starter 23 | com.xkcoding.magic 24 | 0.0.1-SNAPSHOT 25 | 26 | 4.0.0 27 | 28 | magic-starter-id 29 | ${project.artifactId} 30 | ${magic-starter.version} 31 | jar 32 | 33 | 34 | Magic Id 分布式主键生成器 35 | 36 | 37 | 38 | 39 | com.xkcoding.magic 40 | magic-core-tool 41 | ${magic-starter.version} 42 | true 43 | 44 | 45 | 46 | redis.clients 47 | jedis 48 | provided 49 | 50 | 51 | 52 | com.xkcoding.magic 53 | magic-core-auto 54 | ${magic-starter.version} 55 | true 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/autoconfigure/IdAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.autoconfigure; 18 | 19 | import com.xkcoding.magic.id.support.factory.impl.SnowflakeIdFactory; 20 | import com.xkcoding.magic.id.support.id.Id; 21 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 22 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 23 | import org.springframework.context.annotation.Bean; 24 | import org.springframework.context.annotation.Configuration; 25 | 26 | /** 27 | *

28 | * 分布式主键自动装配类 29 | *

30 | * 31 | * @author yangkai.shen 32 | * @date Created in 2019/10/29 15:42 33 | */ 34 | @Configuration 35 | @EnableConfigurationProperties({IdSnowflakeProperties.class, IdDatabaseProperties.class, IdRedisProperties.class}) 36 | public class IdAutoConfiguration { 37 | 38 | /** 39 | * 默认使用雪花算法实现 40 | */ 41 | @Bean 42 | @ConditionalOnMissingBean 43 | public Id snowflakeId(IdSnowflakeProperties properties) { 44 | return SnowflakeIdFactory.create().dataCenterId(properties.getDataCenterId()).workerId(properties.getWorkerId()).prefix(properties::getPrefix).getInstance(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/autoconfigure/IdDatabaseProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.autoconfigure; 18 | 19 | import com.xkcoding.magic.id.autoconfigure.base.BaseIdProperties; 20 | import lombok.Data; 21 | import lombok.EqualsAndHashCode; 22 | import org.springframework.boot.context.properties.ConfigurationProperties; 23 | 24 | /** 25 | *

26 | * 数据库步长ID生成器配置 27 | *

28 | * 29 | * @author yangkai.shen 30 | * @date Created in 2019/10/29 17:53 31 | */ 32 | @Data 33 | @EqualsAndHashCode(callSuper = true) 34 | @ConfigurationProperties(prefix = "magic.id.database") 35 | public class IdDatabaseProperties extends BaseIdProperties { 36 | /** 37 | * 表名称 38 | */ 39 | private String tableName = "magic_id"; 40 | /** 41 | * 重试次数 42 | */ 43 | private int retryTimes = 1; 44 | 45 | /** 46 | * 获取range步长[可选,默认:1000] 47 | */ 48 | private int step = 1000; 49 | 50 | /** 51 | * 序列号分配起始值[可选:默认:0] 52 | */ 53 | private long stepStart = 0; 54 | 55 | /** 56 | * 业务名称 57 | */ 58 | private String businessName = "magic-id"; 59 | 60 | } 61 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/autoconfigure/IdRedisProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.autoconfigure; 18 | 19 | import com.xkcoding.magic.id.autoconfigure.base.BaseIdProperties; 20 | import lombok.Data; 21 | import lombok.EqualsAndHashCode; 22 | import org.springframework.boot.context.properties.ConfigurationProperties; 23 | 24 | /** 25 | *

26 | * Redis步长ID生成器配置 27 | *

28 | * 29 | * @author yangkai.shen 30 | * @date Created in 2019/10/29 17:37 31 | */ 32 | @Data 33 | @EqualsAndHashCode(callSuper = true) 34 | @ConfigurationProperties(prefix = "magic.id.redis") 35 | public class IdRedisProperties extends BaseIdProperties { 36 | /** 37 | * Redis IP 38 | */ 39 | private String ip; 40 | 41 | /** 42 | * Redis 端口 43 | */ 44 | private int port; 45 | 46 | /** 47 | * Redis 密码 48 | */ 49 | private String password; 50 | 51 | /** 52 | * 获取range步长[可选,默认:1000] 53 | */ 54 | private int step = 1000; 55 | 56 | /** 57 | * 序列号分配起始值[可选:默认:0] 58 | */ 59 | private long stepStart = 0; 60 | 61 | /** 62 | * 业务名称 63 | */ 64 | private String businessName = "magic-id"; 65 | } 66 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/autoconfigure/IdSnowflakeProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.autoconfigure; 18 | 19 | import com.xkcoding.magic.id.autoconfigure.base.BaseIdProperties; 20 | import lombok.Data; 21 | import lombok.EqualsAndHashCode; 22 | import org.springframework.boot.context.properties.ConfigurationProperties; 23 | 24 | /** 25 | *

26 | * 雪花算法ID生成器配置 27 | *

28 | * 29 | * @author yangkai.shen 30 | * @date Created in 2019/10/29 17:52 31 | */ 32 | @Data 33 | @EqualsAndHashCode(callSuper = true) 34 | @ConfigurationProperties(prefix = "magic.id.snowflake") 35 | public class IdSnowflakeProperties extends BaseIdProperties { 36 | /** 37 | * 数据中心ID,值的范围在[0,31]之间,一般可以设置机房的IDC[必选] 38 | */ 39 | private long dataCenterId; 40 | /** 41 | * 工作机器ID,值的范围在[0,31]之间,一般可以设置机器编号[必选] 42 | */ 43 | private long workerId; 44 | } 45 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/autoconfigure/base/BaseIdProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.autoconfigure.base; 18 | 19 | import lombok.Data; 20 | 21 | /** 22 | *

23 | * 配置基类 24 | *

25 | * 26 | * @author yangkai.shen 27 | * @date Created in 2019/10/29 17:34 28 | */ 29 | @Data 30 | public class BaseIdProperties { 31 | /** 32 | * 前缀 33 | */ 34 | private String prefix = ""; 35 | } 36 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/exception/IdException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.exception; 18 | 19 | /** 20 | *

21 | * 分布式主键生成异常 22 | *

23 | * 24 | * @author yangkai.shen 25 | * @date Created in 2019/10/29 15:44 26 | */ 27 | public class IdException extends RuntimeException { 28 | public IdException(Throwable cause) { 29 | super(cause); 30 | } 31 | 32 | public IdException(String message) { 33 | super(message); 34 | } 35 | 36 | public IdException(String message, Throwable cause) { 37 | super(message, cause); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/support/business/BusinessName.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.support.business; 18 | 19 | /** 20 | *

21 | * 业务名称接口 22 | *

23 | * 24 | * @author yangkai.shen 25 | * @date Created in 2019/10/29 16:16 26 | */ 27 | public interface BusinessName { 28 | /** 29 | * 业务名称 30 | * 31 | * @return 业务名称 32 | */ 33 | String get(); 34 | } 35 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/support/business/impl/DateBusinessName.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.support.business.impl; 18 | 19 | import cn.hutool.core.date.DateUtil; 20 | import com.xkcoding.magic.core.tool.util.StrUtil; 21 | import com.xkcoding.magic.id.support.business.BusinessName; 22 | import lombok.AllArgsConstructor; 23 | import lombok.NoArgsConstructor; 24 | 25 | /** 26 | *

27 | * 日期业务名称 28 | *

29 | * 30 | * @author yangkai.shen 31 | * @date Created in 2019/10/29 16:26 32 | */ 33 | @NoArgsConstructor 34 | @AllArgsConstructor 35 | public class DateBusinessName implements BusinessName { 36 | /** 37 | * 业务名称 38 | */ 39 | private String businessName; 40 | 41 | /** 42 | * 业务名称 43 | * 44 | * @return 业务名称 45 | */ 46 | @Override 47 | public String get() { 48 | if (StrUtil.isBlank(businessName)) { 49 | this.businessName = StrUtil.EMPTY; 50 | } 51 | return this.businessName + DateUtil.today(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/support/business/impl/DefaultBusinessName.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.support.business.impl; 18 | 19 | import com.xkcoding.magic.id.support.business.BusinessName; 20 | import lombok.AllArgsConstructor; 21 | 22 | /** 23 | *

24 | * 默认业务名称 25 | *

26 | * 27 | * @author yangkai.shen 28 | * @date Created in 2019/10/29 16:29 29 | */ 30 | @AllArgsConstructor 31 | public class DefaultBusinessName implements BusinessName { 32 | /** 33 | * 业务名称 34 | */ 35 | private String businessName; 36 | 37 | /** 38 | * 业务名称 39 | * 40 | * @return 业务名称 41 | */ 42 | @Override 43 | public String get() { 44 | return this.businessName; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/support/factory/IdFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.support.factory; 18 | 19 | import com.xkcoding.magic.id.support.id.Id; 20 | 21 | /** 22 | *

23 | * 主键生成器工厂接口 24 | *

25 | * 26 | * @author yangkai.shen 27 | * @date Created in 2019/10/29 16:46 28 | */ 29 | public interface IdFactory { 30 | /** 31 | * 返回一个主键生成器对象 32 | * 33 | * @return 主键生成器 34 | */ 35 | Id getInstance(); 36 | } 37 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/support/factory/impl/SnowflakeIdFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.support.factory.impl; 18 | 19 | import com.xkcoding.magic.id.support.factory.IdFactory; 20 | import com.xkcoding.magic.id.support.id.Id; 21 | import com.xkcoding.magic.id.support.id.impl.SnowFlakeId; 22 | import com.xkcoding.magic.id.support.prefix.Prefix; 23 | import com.xkcoding.magic.id.support.prefix.impl.DefaultPrefix; 24 | 25 | /** 26 | *

27 | * 雪花算法生成器,主键生成器工厂类 28 | *

29 | * 30 | * @author yangkai.shen 31 | * @date Created in 2019/10/29 16:48 32 | */ 33 | public class SnowflakeIdFactory implements IdFactory { 34 | /** 35 | * 数据中心ID,值的范围在[0,31]之间,一般可以设置机房的IDC[必选] 36 | */ 37 | private long dataCenterId; 38 | /** 39 | * 工作机器ID,值的范围在[0,31]之间,一般可以设置机器编号[必选] 40 | */ 41 | private long workerId; 42 | /** 43 | * 前缀,可选,默认为空 44 | */ 45 | private Prefix prefix = new DefaultPrefix(); 46 | 47 | public static SnowflakeIdFactory create() { 48 | return new SnowflakeIdFactory(); 49 | } 50 | 51 | public SnowflakeIdFactory dataCenterId(long dataCenterId) { 52 | this.dataCenterId = dataCenterId; 53 | return this; 54 | } 55 | 56 | public SnowflakeIdFactory workerId(long workerId) { 57 | this.workerId = workerId; 58 | return this; 59 | } 60 | 61 | public SnowflakeIdFactory prefix(Prefix prefix) { 62 | this.prefix = prefix; 63 | return this; 64 | } 65 | 66 | /** 67 | * 返回一个主键生成器对象 68 | * 69 | * @return 主键生成器 70 | */ 71 | @Override 72 | public Id getInstance() { 73 | return new SnowFlakeId(this.dataCenterId, this.workerId, this.prefix); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/support/id/Id.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.support.id; 18 | 19 | import com.xkcoding.magic.id.exception.IdException; 20 | import com.xkcoding.magic.id.support.business.BusinessName; 21 | import com.xkcoding.magic.id.support.prefix.Prefix; 22 | 23 | /** 24 | *

25 | * 主键生成接口 26 | *

27 | * 28 | * @author yangkai.shen 29 | * @date Created in 2019/10/29 15:47 30 | */ 31 | public interface Id { 32 | /** 33 | * 生成下一个主键 34 | * 35 | * @return 主键 36 | * @throws IdException 主键生成异常 37 | */ 38 | long nextId() throws IdException; 39 | 40 | /** 41 | * (根据业务名称)生成下一个主键 42 | * 43 | * @param businessName 业务名称 44 | * @return 主键 45 | * @throws IdException 主键生成异常 46 | */ 47 | long nextId(BusinessName businessName) throws IdException; 48 | 49 | /** 50 | * 生成下一个主键(带格式) 51 | * 52 | * @return 主键(带格式) 53 | * @throws IdException 主键生成异常 54 | */ 55 | String nextIdStr() throws IdException; 56 | 57 | /** 58 | * (根据业务名称)生成下一个主键(带格式) 59 | * 60 | * @param businessName 业务名称 61 | * @param prefix 前缀 62 | * @return 主键(带格式) 63 | * @throws IdException 主键生成异常 64 | */ 65 | String nextIdStr(BusinessName businessName, Prefix prefix) throws IdException; 66 | } 67 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/support/id/RangeId.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.support.id; 18 | 19 | import com.xkcoding.magic.id.support.business.BusinessName; 20 | import com.xkcoding.magic.id.support.prefix.Prefix; 21 | import com.xkcoding.magic.id.support.range.RangeManager; 22 | 23 | /** 24 | *

25 | * 区间范围主键生成器接口 26 | *

27 | * 28 | * @author yangkai.shen 29 | * @date Created in 2019/10/29 15:50 30 | */ 31 | public interface RangeId extends Id { 32 | 33 | /** 34 | * 设置主键区间范围管理器 35 | * 36 | * @param rangeManager 区间范围管理器 37 | */ 38 | void setRangeManager(RangeManager rangeManager); 39 | 40 | /** 41 | * 设置主键获取名称 42 | * 43 | * @param prefix 前缀 44 | */ 45 | void setPrefix(Prefix prefix); 46 | 47 | /** 48 | * 设置业务名称 49 | * 50 | * @param businessName 业务名称 51 | */ 52 | void setBusinessName(BusinessName businessName); 53 | } 54 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/support/prefix/Prefix.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.support.prefix; 18 | 19 | /** 20 | *

21 | * 携带前缀 22 | *

23 | * 24 | * @author yangkai.shen 25 | * @date Created in 2019/10/29 16:59 26 | */ 27 | public interface Prefix { 28 | /** 29 | * 前缀 30 | * 31 | * @return 前缀 32 | */ 33 | String get(); 34 | } 35 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/support/prefix/impl/DefaultPrefix.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.support.prefix.impl; 18 | 19 | import com.xkcoding.magic.core.tool.util.StrUtil; 20 | import com.xkcoding.magic.id.support.prefix.Prefix; 21 | import lombok.AllArgsConstructor; 22 | import lombok.NoArgsConstructor; 23 | 24 | /** 25 | *

26 | * 默认携带前缀 27 | *

28 | * 29 | * @author yangkai.shen 30 | * @date Created in 2019/10/29 17:00 31 | */ 32 | @NoArgsConstructor 33 | @AllArgsConstructor 34 | public class DefaultPrefix implements Prefix { 35 | private String prefix; 36 | 37 | /** 38 | * 前缀 39 | * 40 | * @return 前缀 41 | */ 42 | @Override 43 | public String get() { 44 | if (StrUtil.isBlank(this.prefix)) { 45 | return StrUtil.EMPTY; 46 | } 47 | return this.prefix; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/support/range/RangeManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.support.range; 18 | 19 | import com.xkcoding.magic.id.exception.IdException; 20 | import com.xkcoding.magic.id.support.range.model.IdRange; 21 | 22 | /** 23 | *

24 | * 区间范围管理器接口 25 | *

26 | * 27 | * @author yangkai.shen 28 | * @date Created in 2019/10/29 16:22 29 | */ 30 | public interface RangeManager { 31 | /** 32 | * 获取指定区间名的下一个区间 33 | * 34 | * @param name 区间名 35 | * @return 返回区间 36 | * @throws IdException 异常 37 | */ 38 | IdRange nextRange(String name) throws IdException; 39 | 40 | /** 41 | * 初始化 42 | */ 43 | void init(); 44 | } 45 | -------------------------------------------------------------------------------- /magic-starter-id/src/main/java/com/xkcoding/magic/id/support/range/model/IdRange.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.id.support.range.model; 18 | 19 | import java.util.concurrent.atomic.AtomicLong; 20 | 21 | /** 22 | *

23 | * 主键区间对象模型 24 | *

25 | * 26 | * @author yangkai.shen 27 | * @date Created in 2019/10/29 16:23 28 | */ 29 | public class IdRange { 30 | 31 | /** 32 | * 区间的序列号开始值 33 | */ 34 | private final long min; 35 | /** 36 | * 区间的序列号结束值 37 | */ 38 | private final long max; 39 | /** 40 | * 区间的序列号当前值 41 | */ 42 | private final AtomicLong value; 43 | /** 44 | * 区间的序列号是否分配完毕,每次分配完毕就会去重新获取一个新的区间 45 | */ 46 | private volatile boolean over = false; 47 | 48 | public IdRange(long min, long max) { 49 | this.min = min; 50 | this.max = max; 51 | this.value = new AtomicLong(min); 52 | } 53 | 54 | /** 55 | * 返回并递增下一个序列号 56 | * 57 | * @return 下一个序列号,如果返回-1表示序列号分配完毕 58 | */ 59 | public long getAndIncrement() { 60 | long currentValue = value.getAndIncrement(); 61 | if (currentValue > max) { 62 | over = true; 63 | return -1; 64 | } 65 | 66 | return currentValue; 67 | } 68 | 69 | public long getMin() { 70 | return min; 71 | } 72 | 73 | public long getMax() { 74 | return max; 75 | } 76 | 77 | public boolean isOver() { 78 | return over; 79 | } 80 | 81 | public void setOver(boolean over) { 82 | this.over = over; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "max: " + max + ", min: " + min + ", value: " + value; 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /magic-starter-locker/README.md: -------------------------------------------------------------------------------- 1 |

magic-starter-locker

2 |

3 | Travis-CI 4 | MAVEN 5 | Codacy 6 | author 7 | JDK 8 | Spring Boot 9 | LICENSE 10 |

11 | 12 | ## 简介 13 | 14 | `magic-starter-locker` 主要是提供了分布式锁的功能,提供 2 种实现方式,一种基于 Zookeeper,一种基于 Redis。 15 | 16 | -------------------------------------------------------------------------------- /magic-starter-locker/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 21 | 22 | Magic-Starter 23 | com.xkcoding.magic 24 | 0.0.1-SNAPSHOT 25 | 26 | 4.0.0 27 | 28 | magic-starter-locker 29 | ${project.artifactId} 30 | ${magic-starter.version} 31 | jar 32 | 33 | 34 | Magic Locker 分布式锁,Zookeeper/Redis 两种实现方式 35 | 36 | 37 | 38 | 39 | com.xkcoding.magic 40 | magic-core-auto 41 | ${magic-starter.version} 42 | true 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /magic-starter-log/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 21 | 22 | Magic-Starter 23 | com.xkcoding.magic 24 | 0.0.1-SNAPSHOT 25 | 26 | 4.0.0 27 | 28 | magic-starter-log 29 | ${project.artifactId} 30 | ${magic-starter.version} 31 | jar 32 | 33 | 34 | Magic Log 记录操作日志,通用日志配置文件,web请求日志 35 | 36 | 37 | 38 | 39 | com.xkcoding.magic 40 | magic-core-tool 41 | ${magic-starter.version} 42 | true 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-starter-aop 48 | 49 | 50 | 51 | com.xkcoding.magic 52 | magic-core-auto 53 | ${magic-starter.version} 54 | true 55 | 56 | 57 | 58 | 59 | 60 | 61 | src/main/java 62 | 63 | **/*.xml 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/annotation/OperateLog.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.annotation; 18 | 19 | import java.lang.annotation.*; 20 | 21 | /** 22 | *

23 | * 操作日志注解 24 | *

25 | * 26 | * @author yangkai.shen 27 | * @date Created in 2019/9/24 11:29 28 | */ 29 | @Target({ElementType.METHOD}) 30 | @Retention(RetentionPolicy.RUNTIME) 31 | @Inherited 32 | @Documented 33 | public @interface OperateLog { 34 | /** 35 | * 日志描述 36 | * 37 | * @return 日志描述 38 | */ 39 | String value() default "操作日志"; 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/aspect/OperateLogAspect.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.aspect; 18 | 19 | import com.xkcoding.magic.core.tool.constants.MagicConsts; 20 | import com.xkcoding.magic.log.annotation.OperateLog; 21 | import com.xkcoding.magic.log.support.publisher.LogEventPublisher; 22 | import lombok.RequiredArgsConstructor; 23 | import lombok.extern.slf4j.Slf4j; 24 | import org.aspectj.lang.ProceedingJoinPoint; 25 | import org.aspectj.lang.annotation.Around; 26 | import org.aspectj.lang.annotation.Aspect; 27 | import org.springframework.beans.factory.annotation.Autowired; 28 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 29 | import org.springframework.core.annotation.Order; 30 | import org.springframework.stereotype.Component; 31 | 32 | /** 33 | *

34 | * 操作日志切面 35 | *

36 | * 37 | * @author yangkai.shen 38 | * @date Created in 2019/9/24 13:57 39 | */ 40 | @Slf4j 41 | @Aspect 42 | @Component 43 | @Order(MagicConsts.AOP_ORDER_LOG) 44 | @RequiredArgsConstructor(onConstructor_ = @Autowired) 45 | @ConditionalOnProperty(value = "magic.log.enabled", havingValue = "true", matchIfMissing = true) 46 | public class OperateLogAspect { 47 | @Around("@annotation(operateLog)") 48 | public Object around(ProceedingJoinPoint point, OperateLog operateLog) throws Throwable { 49 | // 获取类名 50 | String className = point.getTarget().getClass().getName(); 51 | // 获取方法 52 | String methodName = point.getSignature().getName(); 53 | // 开始时间 54 | long beginTime = System.currentTimeMillis(); 55 | // 执行方法 56 | Object result = point.proceed(); 57 | // 执行时长(毫秒) 58 | long time = System.currentTimeMillis() - beginTime; 59 | // 日志描述 60 | String description = operateLog.value(); 61 | // 异步记录日志 62 | LogEventPublisher.publishOperateLogEvent(methodName, className, description, time); 63 | return result; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/autoconfigure/LogAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.autoconfigure; 18 | 19 | import com.xkcoding.magic.log.support.LogHandler; 20 | import com.xkcoding.magic.log.support.OperatorService; 21 | import com.xkcoding.magic.log.support.listener.LogEventListener; 22 | import com.xkcoding.magic.log.support.logger.MagicLogger; 23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 24 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 25 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 26 | import org.springframework.context.annotation.Bean; 27 | import org.springframework.context.annotation.Configuration; 28 | 29 | /** 30 | *

31 | * 自动装配类 32 | *

33 | * 34 | * @author yangkai.shen 35 | * @date Created in 2019/9/24 14:06 36 | */ 37 | @Configuration 38 | @EnableConfigurationProperties({LogProperties.class}) 39 | @ConditionalOnProperty(value = "magic.log.enabled", havingValue = "true", matchIfMissing = true) 40 | public class LogAutoConfiguration { 41 | @Bean 42 | @ConditionalOnMissingBean 43 | public LogHandler logService() { 44 | return new LogHandler() { 45 | }; 46 | } 47 | 48 | @Bean 49 | @ConditionalOnMissingBean 50 | public OperatorService operatorService() { 51 | return new OperatorService() { 52 | }; 53 | } 54 | 55 | @Bean 56 | public MagicLogger magicLogger() { 57 | return new MagicLogger(); 58 | } 59 | 60 | @Bean 61 | public LogEventListener logEventListener(LogHandler logHandler, OperatorService operatorService) { 62 | return new LogEventListener(logHandler, operatorService); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/autoconfigure/LogProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.autoconfigure; 18 | 19 | import lombok.Data; 20 | import org.springframework.boot.context.properties.ConfigurationProperties; 21 | 22 | /** 23 | *

24 | * 配置属性 25 | *

26 | * 27 | * @author yangkai.shen 28 | * @date Created in 2019/9/24 14:28 29 | */ 30 | @Data 31 | @ConfigurationProperties(prefix = "magic.log") 32 | public class LogProperties { 33 | /** 34 | * 是否启用 magic log,默认 true. 35 | */ 36 | private boolean enabled = true; 37 | 38 | /** 39 | * Web请求日志配置熟悉. 40 | */ 41 | private RequestLogProperties request = new RequestLogProperties(); 42 | 43 | /** 44 | * Web请求日志配置熟悉 45 | */ 46 | @Data 47 | public static class RequestLogProperties { 48 | /** 49 | * 是否启用 request log,默认 false. 50 | */ 51 | private boolean enabled = false; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/constants/LogConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.constants; 18 | 19 | /** 20 | *

21 | * 常量池 22 | *

23 | * 24 | * @author yangkai.shen 25 | * @date Created in 2019/9/24 16:03 26 | */ 27 | public interface LogConstants { 28 | /** 29 | * log 30 | */ 31 | String EVENT_LOG_KEY = "log"; 32 | /** 33 | * request 34 | */ 35 | String EVENT_REQUEST_KEY = "request"; 36 | /** 37 | * 默认操作人 38 | */ 39 | String OPERATOR = "admin"; 40 | } 41 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/enums/LogType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.enums; 18 | 19 | /** 20 | *

21 | * 日志类型枚举类 22 | *

23 | * 24 | * @author yangkai.shen 25 | * @date Created in 2019/9/24 14:39 26 | */ 27 | public enum LogType { 28 | /** 29 | * 操作日志 30 | */ 31 | OPERATE_LOG, 32 | /** 33 | * 错误日志 34 | */ 35 | ERROR_LOG, 36 | /** 37 | * 自定义日志 38 | */ 39 | CUSTOM; 40 | } 41 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/model/CustomLogModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.model; 18 | 19 | import com.xkcoding.magic.log.enums.LogType; 20 | import lombok.EqualsAndHashCode; 21 | import lombok.Getter; 22 | import lombok.Setter; 23 | import lombok.ToString; 24 | import org.springframework.boot.logging.LogLevel; 25 | 26 | /** 27 | *

28 | * 自定义日志实体类 29 | *

30 | * 31 | * @author yangkai.shen 32 | * @date Created in 2019/9/24 15:01 33 | */ 34 | @Getter 35 | @Setter 36 | @ToString(callSuper = true) 37 | @EqualsAndHashCode(callSuper = true) 38 | public class CustomLogModel extends AbstractLogModel { 39 | public CustomLogModel(LogLevel level) { 40 | super(LogType.CUSTOM, level); 41 | } 42 | 43 | /** 44 | * 日志前缀 45 | */ 46 | private String prefix; 47 | /** 48 | * 日志描述 49 | */ 50 | private String description; 51 | } 52 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/model/ErrorLogModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.model; 18 | 19 | import com.xkcoding.magic.log.enums.LogType; 20 | import lombok.EqualsAndHashCode; 21 | import lombok.Getter; 22 | import lombok.Setter; 23 | import lombok.ToString; 24 | import org.springframework.boot.logging.LogLevel; 25 | 26 | /** 27 | *

28 | * 错误日志实体类 29 | *

30 | * 31 | * @author yangkai.shen 32 | * @date Created in 2019/9/24 15:01 33 | */ 34 | @Getter 35 | @Setter 36 | @ToString(callSuper = true) 37 | @EqualsAndHashCode(callSuper = true) 38 | public class ErrorLogModel extends AbstractLogModel { 39 | public ErrorLogModel() { 40 | super(LogType.ERROR_LOG, LogLevel.ERROR); 41 | } 42 | 43 | /** 44 | * 堆栈信息 45 | */ 46 | private String stackTrace; 47 | /** 48 | * 异常名 49 | */ 50 | private String exceptionName; 51 | /** 52 | * 异常消息 53 | */ 54 | private String exceptionMessage; 55 | /** 56 | * 文件名 57 | */ 58 | private String fileName; 59 | /** 60 | * 代码行数 61 | */ 62 | private Integer lineNumber; 63 | } 64 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/model/OperateLogModel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.model; 18 | 19 | import com.xkcoding.magic.log.enums.LogType; 20 | import lombok.EqualsAndHashCode; 21 | import lombok.Getter; 22 | import lombok.Setter; 23 | import lombok.ToString; 24 | import org.springframework.boot.logging.LogLevel; 25 | 26 | /** 27 | *

28 | * 操作日志实体类 29 | *

30 | * 31 | * @author yangkai.shen 32 | * @date Created in 2019/9/24 14:46 33 | */ 34 | @Getter 35 | @Setter 36 | @ToString(callSuper = true) 37 | @EqualsAndHashCode(callSuper = true) 38 | public class OperateLogModel extends AbstractLogModel { 39 | public OperateLogModel() { 40 | super(LogType.OPERATE_LOG, LogLevel.INFO); 41 | } 42 | 43 | /** 44 | * 日志描述 45 | */ 46 | private String description; 47 | /** 48 | * 耗时 49 | */ 50 | private Long spendTime; 51 | } 52 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/support/LogHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.support; 18 | 19 | import com.xkcoding.magic.log.model.CustomLogModel; 20 | import com.xkcoding.magic.log.model.ErrorLogModel; 21 | import com.xkcoding.magic.log.model.OperateLogModel; 22 | 23 | /** 24 | *

25 | * 日志处理接口 26 | *

27 | * 28 | * @author yangkai.shen 29 | * @date Created in 2019/9/24 16:11 30 | */ 31 | public interface LogHandler { 32 | /** 33 | * 处理操作日志 34 | * 35 | * @param logModel 操作日志实体 36 | */ 37 | default void handleOperateLog(OperateLogModel logModel) { 38 | } 39 | 40 | /** 41 | * 处理自定义日志 42 | * 43 | * @param logModel 自定义日志实体 44 | */ 45 | default void handleCustomLog(CustomLogModel logModel) { 46 | } 47 | 48 | /** 49 | * 处理错误日志 50 | * 51 | * @param logModel 错误日志实体 52 | */ 53 | default void handleErrorLog(ErrorLogModel logModel) { 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/support/OperatorService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.support; 18 | 19 | import com.xkcoding.magic.log.constants.LogConstants; 20 | 21 | import javax.servlet.http.HttpServletRequest; 22 | 23 | /** 24 | *

25 | * 操作人获取接口 26 | *

27 | * 28 | * @author yangkai.shen 29 | * @date Created in 2019/9/24 16:13 30 | */ 31 | public interface OperatorService { 32 | /** 33 | * 获取操作人 34 | * 35 | * @param request 请求 36 | * @return 操作人 37 | */ 38 | default String getOperator(HttpServletRequest request) { 39 | return LogConstants.OPERATOR; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/support/event/CustomLogEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.support.event; 18 | 19 | import org.springframework.context.ApplicationEvent; 20 | 21 | import java.util.Map; 22 | 23 | /** 24 | *

25 | * 自定义日志事件 26 | *

27 | * 28 | * @author yangkai.shen 29 | * @date Created in 2019/9/24 16:05 30 | */ 31 | public class CustomLogEvent extends ApplicationEvent { 32 | 33 | public CustomLogEvent(Map source) { 34 | super(source); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/support/event/ErrorLogEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.support.event; 18 | 19 | import org.springframework.context.ApplicationEvent; 20 | 21 | import java.util.Map; 22 | 23 | /** 24 | *

25 | * 错误日志事件 26 | *

27 | * 28 | * @author yangkai.shen 29 | * @date Created in 2019/9/24 16:05 30 | */ 31 | public class ErrorLogEvent extends ApplicationEvent { 32 | 33 | public ErrorLogEvent(Map source) { 34 | super(source); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/support/event/OperateLogEvent.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.support.event; 18 | 19 | import org.springframework.context.ApplicationEvent; 20 | 21 | import java.util.Map; 22 | 23 | /** 24 | *

25 | * 操作日志事件 26 | *

27 | * 28 | * @author yangkai.shen 29 | * @date Created in 2019/9/24 16:05 30 | */ 31 | public class OperateLogEvent extends ApplicationEvent { 32 | 33 | public OperateLogEvent(Map source) { 34 | super(source); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /magic-starter-log/src/main/java/com/xkcoding/magic/log/support/logger/MagicLogger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.log.support.logger; 18 | 19 | import com.xkcoding.magic.log.support.publisher.LogEventPublisher; 20 | import lombok.extern.slf4j.Slf4j; 21 | import org.springframework.beans.factory.InitializingBean; 22 | import org.springframework.boot.logging.LogLevel; 23 | 24 | /** 25 | *

26 | * 自定义日志记录 27 | *

28 | * 29 | * @author yangkai.shen 30 | * @date Created in 2019/9/24 15:36 31 | */ 32 | @Slf4j 33 | public class MagicLogger implements InitializingBean { 34 | 35 | /** 36 | * WARN 级别日志 37 | * 38 | * @param prefix 日志前缀 39 | * @param description 日志描述 40 | */ 41 | public void warn(String prefix, String description) { 42 | LogEventPublisher.publishCustomLogEvent(LogLevel.WARN, prefix, description); 43 | } 44 | 45 | /** 46 | * DEBUG 级别日志 47 | * 48 | * @param prefix 日志前缀 49 | * @param description 日志描述 50 | */ 51 | public void debug(String prefix, String description) { 52 | LogEventPublisher.publishCustomLogEvent(LogLevel.DEBUG, prefix, description); 53 | } 54 | 55 | /** 56 | * INFO 级别日志 57 | * 58 | * @param prefix 日志前缀 59 | * @param description 日志描述 60 | */ 61 | public void info(String prefix, String description) { 62 | LogEventPublisher.publishCustomLogEvent(LogLevel.INFO, prefix, description); 63 | } 64 | 65 | /** 66 | * ERROR 级别日志 67 | * 68 | * @param prefix 日志前缀 69 | * @param description 日志描述 70 | */ 71 | public void error(String prefix, String description) { 72 | LogEventPublisher.publishCustomLogEvent(LogLevel.ERROR, prefix, description); 73 | } 74 | 75 | @Override 76 | public void afterPropertiesSet() throws Exception { 77 | log.info(">>>>>>>>>> MagicLogger 初始化成功!"); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/autoconfigure/EmailMessageTemplateAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.autoconfigure; 18 | 19 | import com.xkcoding.magic.core.tool.util.StrUtil; 20 | import com.xkcoding.magic.message.constants.MessageConstants; 21 | import lombok.RequiredArgsConstructor; 22 | import org.springframework.beans.factory.annotation.Qualifier; 23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 24 | import org.springframework.context.ApplicationContext; 25 | import org.springframework.context.annotation.Bean; 26 | import org.springframework.context.annotation.Configuration; 27 | import org.thymeleaf.TemplateEngine; 28 | import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect; 29 | import org.thymeleaf.spring5.SpringTemplateEngine; 30 | import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; 31 | import org.thymeleaf.templatemode.TemplateMode; 32 | 33 | /** 34 | *

35 | * 邮件模板 Thymeleaf 自动装配 36 | *

37 | * 38 | * @author yangkai.shen 39 | * @date Created in 2019/10/11 14:11 40 | */ 41 | @Configuration 42 | @RequiredArgsConstructor 43 | @ConditionalOnClass({TemplateEngine.class, SpringResourceTemplateResolver.class}) 44 | public class EmailMessageTemplateAutoConfiguration { 45 | private final MessageProperties messageProperties; 46 | 47 | @Bean(MessageConstants.EMAIL_TEMPLATE_ENGINE_BEAN) 48 | public TemplateEngine templateEngine(@Qualifier(MessageConstants.EMAIL_TEMPLATE_RESOLVER_BEAN) SpringResourceTemplateResolver templateResolver) { 49 | SpringTemplateEngine engine = new SpringTemplateEngine(); 50 | engine.addDialect(new Java8TimeDialect()); 51 | engine.addTemplateResolver(templateResolver); 52 | engine.getConfiguration(); 53 | 54 | return engine; 55 | } 56 | 57 | @Bean(MessageConstants.EMAIL_TEMPLATE_RESOLVER_BEAN) 58 | public SpringResourceTemplateResolver templateResolver(ApplicationContext context) { 59 | SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); 60 | templateResolver.setApplicationContext(context); 61 | templateResolver.setTemplateMode(TemplateMode.HTML); 62 | templateResolver.setCacheable(false); 63 | String prefix = messageProperties.getEmail().getPrefix(); 64 | templateResolver.setPrefix(StrUtil.appendIfMissing(prefix, StrUtil.SLASH)); 65 | templateResolver.setSuffix(messageProperties.getEmail().getSuffix()); 66 | return templateResolver; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/autoconfigure/MessageAutoConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.autoconfigure; 18 | 19 | import com.xkcoding.magic.message.constants.MessageConstants; 20 | import com.xkcoding.magic.message.support.dingtalk.DingTalkMessageSender; 21 | import com.xkcoding.magic.message.support.email.EmailMessageSender; 22 | import com.xkcoding.magic.message.support.sms.SmsMessageSender; 23 | import org.springframework.beans.factory.annotation.Qualifier; 24 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 25 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 26 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 27 | import org.springframework.context.annotation.Bean; 28 | import org.springframework.context.annotation.Configuration; 29 | import org.springframework.mail.javamail.JavaMailSender; 30 | import org.thymeleaf.TemplateEngine; 31 | 32 | /** 33 | *

34 | * 消息自动装配类 35 | *

36 | * 37 | * @author yangkai.shen 38 | * @date Created in 2019/10/11 14:14 39 | */ 40 | @Configuration 41 | @EnableConfigurationProperties({MessageProperties.class}) 42 | public class MessageAutoConfiguration { 43 | @Bean(MessageConstants.BEAN_NAME_SMS) 44 | @ConditionalOnMissingBean(name = MessageConstants.BEAN_NAME_SMS) 45 | @ConditionalOnProperty(value = "magic.message.sms.enabled", havingValue = "true") 46 | public SmsMessageSender smsMessageSender(MessageProperties properties) { 47 | return new SmsMessageSender(properties); 48 | } 49 | 50 | @Bean(MessageConstants.BEAN_NAME_EMAIL) 51 | @ConditionalOnMissingBean(name = MessageConstants.BEAN_NAME_EMAIL) 52 | @ConditionalOnProperty(value = "magic.message.email.enabled", havingValue = "true") 53 | public EmailMessageSender emailMessageSender(JavaMailSender mailSender, @Qualifier(MessageConstants.EMAIL_TEMPLATE_ENGINE_BEAN) TemplateEngine templateEngine) { 54 | return new EmailMessageSender(mailSender, templateEngine); 55 | } 56 | 57 | @Bean(MessageConstants.BEAN_NAME_DINGTALK) 58 | @ConditionalOnMissingBean(name = MessageConstants.BEAN_NAME_DINGTALK) 59 | @ConditionalOnProperty(value = "magic.message.dingtalk.enabled", havingValue = "true") 60 | public DingTalkMessageSender dingTalkMessageSender(MessageProperties properties) { 61 | return new DingTalkMessageSender(properties); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/autoconfigure/MessageProperties.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.autoconfigure; 18 | 19 | import lombok.Data; 20 | import org.springframework.boot.context.properties.ConfigurationProperties; 21 | 22 | import java.util.Map; 23 | 24 | /** 25 | *

26 | * 消息配置属性 27 | *

28 | * 29 | * @author yangkai.shen 30 | * @date Created in 2019/10/11 14:13 31 | */ 32 | @Data 33 | @ConfigurationProperties(prefix = "magic.message") 34 | public class MessageProperties { 35 | /** 36 | * 短信配置 37 | */ 38 | private SmsMessageProperties sms = new SmsMessageProperties(); 39 | /** 40 | * 邮箱配置 41 | */ 42 | private EmailMessageProperties email = new EmailMessageProperties(); 43 | /** 44 | * 钉钉配置 45 | */ 46 | private DingTalkMessageProperties dingtalk = new DingTalkMessageProperties(); 47 | 48 | /** 49 | * 短信配置 50 | */ 51 | @Data 52 | public static class SmsMessageProperties { 53 | /** 54 | * 是否启用 55 | */ 56 | private boolean enabled = false; 57 | /** 58 | * 应用ID 59 | */ 60 | private String accessKey; 61 | /** 62 | * 应用秘钥 63 | */ 64 | private String secretKey; 65 | /** 66 | * 短信模板配置,key是模版名称,value是模版CODE 67 | */ 68 | private Map channels; 69 | } 70 | 71 | /** 72 | * 邮箱配置 73 | */ 74 | @Data 75 | public static class EmailMessageProperties { 76 | /** 77 | * 是否启用 78 | */ 79 | private boolean enabled = false; 80 | /** 81 | * HTML模板文件的目录,默认位置为classpath:/email/ 82 | */ 83 | private String prefix = "classpath:/email/"; 84 | /** 85 | * 邮箱模板文件后缀,默认:.html 86 | */ 87 | private String suffix = ".html"; 88 | } 89 | 90 | /** 91 | * 钉钉配置 92 | */ 93 | @Data 94 | public static class DingTalkMessageProperties { 95 | /** 96 | * 是否启用 97 | */ 98 | private boolean enabled = false; 99 | /** 100 | * webhook 101 | */ 102 | private String webhook; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/constants/MessageConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.constants; 18 | 19 | /** 20 | *

21 | * 消息常量池 22 | *

23 | * 24 | * @author yangkai.shen 25 | * @date Created in 2019/10/11 14:10 26 | */ 27 | public interface MessageConstants { 28 | /** 29 | * 邮箱模板引擎 30 | */ 31 | String EMAIL_TEMPLATE_ENGINE_BEAN = "emailTemplateEngine"; 32 | /** 33 | * 邮箱模板解析器 34 | */ 35 | String EMAIL_TEMPLATE_RESOLVER_BEAN = "emailTemplateResolver"; 36 | /** 37 | * 短信产品 38 | */ 39 | String SMS_PRODUCT = "Dysmsapi"; 40 | /** 41 | * 短信域名 42 | */ 43 | String SMS_ENDPOINT = "dysmsapi.aliyuncs.com"; 44 | /** 45 | * 短信发送器 Bean 名称 46 | */ 47 | String BEAN_NAME_SMS = "smsMessageSender"; 48 | /** 49 | * 邮件发送器 Bean 名称 50 | */ 51 | String BEAN_NAME_EMAIL = "emailMessageSender"; 52 | /** 53 | * 钉钉发送器 Bean 名称 54 | */ 55 | String BEAN_NAME_DINGTALK = "dingTalkMessageSender"; 56 | /** 57 | * 短信成功状态码 58 | */ 59 | String SMS_SUCCESS_CODE = "OK"; 60 | /** 61 | * 钉钉消息默认超时时间 62 | */ 63 | int DINGTALK_DEFAULT_TIMEOUT = 5000; 64 | } 65 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/enums/DingTalkType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.enums; 18 | 19 | import lombok.AllArgsConstructor; 20 | import lombok.Getter; 21 | 22 | /** 23 | *

24 | * 钉钉消息类型枚举类 25 | *

26 | * 27 | * @author yangkai.shen 28 | * @date Created in 2019/10/11 13:46 29 | */ 30 | @Getter 31 | @AllArgsConstructor 32 | public enum DingTalkType { 33 | /** 34 | * text类型 35 | */ 36 | TEXT("text"), 37 | /** 38 | * link类型 39 | */ 40 | LINK("link"), 41 | /** 42 | * markdown类型 43 | */ 44 | MARKDOWN("markdown"), 45 | /** 46 | * 整体跳转ActionCard类型 47 | */ 48 | ACTIONCARD_WHOLE("actionCard"), 49 | /** 50 | * 独立跳转ActionCard类型 51 | */ 52 | ACTIONCARD_SINGLE("actionCard"), 53 | /** 54 | * FeedCard类型 55 | */ 56 | FEEDCARD("feedCard"); 57 | 58 | /** 59 | * 类型 60 | */ 61 | private String type; 62 | 63 | } 64 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/enums/EmailType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.enums; 18 | 19 | /** 20 | *

21 | * 邮件类型枚举类 22 | *

23 | * 24 | * @author yangkai.shen 25 | * @date Created in 2019/10/11 13:36 26 | */ 27 | public enum EmailType { 28 | /** 29 | * 简单文本邮件类型 30 | */ 31 | SIMPLE, 32 | /** 33 | * 复杂邮件类型 34 | */ 35 | MIME 36 | 37 | } 38 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/model/Message.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.model; 18 | 19 | /** 20 | *

21 | * 发送的消息接口 22 | *

23 | * 24 | * @author yangkai.shen 25 | * @date Created in 2019/10/11 13:28 26 | */ 27 | public interface Message { 28 | } 29 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/model/dingtalk/AbstractDingTalkMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.model.dingtalk; 18 | 19 | import com.xkcoding.magic.message.enums.DingTalkType; 20 | import com.xkcoding.magic.message.model.Message; 21 | import lombok.Data; 22 | 23 | /** 24 | *

25 | * 钉钉消息,参考文档:https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq 26 | *

27 | * 28 | * @author yangkai.shen 29 | * @date Created in 2019/10/11 13:48 30 | */ 31 | @Data 32 | public abstract class AbstractDingTalkMessage implements Message { 33 | /** 34 | * 钉钉消息类型 35 | */ 36 | private String msgtype; 37 | 38 | AbstractDingTalkMessage(DingTalkType dingTalkType) { 39 | this.msgtype = dingTalkType.getType(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/model/dingtalk/ActionCardSingleDingTalkMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.model.dingtalk; 18 | 19 | import com.xkcoding.magic.message.enums.DingTalkType; 20 | import com.xkcoding.magic.message.model.dingtalk.support.actioncard.ActionCardSingle; 21 | import lombok.Data; 22 | import lombok.EqualsAndHashCode; 23 | 24 | /** 25 | *

26 | * 钉钉独立跳转 ActionCard 类型,参考文档:https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq#a-namewhg6fha%E7%8B%AC%E7%AB%8B%E8%B7%B3%E8%BD%ACactioncard%E7%B1%BB%E5%9E%8B 27 | *

28 | * 29 | * @author yangkai.shen 30 | * @date Created in 2019/10/11 14:02 31 | */ 32 | @Data 33 | @EqualsAndHashCode(callSuper = true) 34 | public class ActionCardSingleDingTalkMessage extends AbstractDingTalkMessage { 35 | /** 36 | * 消息内容 37 | */ 38 | private ActionCardSingle actionCard; 39 | 40 | /** 41 | * 消息类型,此时固定为:actionCard 42 | */ 43 | public ActionCardSingleDingTalkMessage() { 44 | super(DingTalkType.ACTIONCARD_SINGLE); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/model/dingtalk/ActionCardWholeDingTalkMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.model.dingtalk; 18 | 19 | import com.xkcoding.magic.message.enums.DingTalkType; 20 | import com.xkcoding.magic.message.model.dingtalk.support.actioncard.ActionCardWhole; 21 | import lombok.Data; 22 | import lombok.EqualsAndHashCode; 23 | 24 | /** 25 | *

26 | * 钉钉整体跳转 ActionCard 类型,参考文档:https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq#a-nametefrxba%E6%95%B4%E4%BD%93%E8%B7%B3%E8%BD%ACactioncard%E7%B1%BB%E5%9E%8B 27 | *

28 | * 29 | * @author yangkai.shen 30 | * @date Created in 2019/10/11 14:04 31 | */ 32 | @Data 33 | @EqualsAndHashCode(callSuper = true) 34 | public class ActionCardWholeDingTalkMessage extends AbstractDingTalkMessage { 35 | /** 36 | * 消息内容 37 | */ 38 | private ActionCardWhole actionCard; 39 | 40 | /** 41 | * 消息类型,此时固定为:actionCard 42 | */ 43 | public ActionCardWholeDingTalkMessage() { 44 | super(DingTalkType.ACTIONCARD_WHOLE); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/model/dingtalk/FeedCardDingTalkMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.model.dingtalk; 18 | 19 | import com.xkcoding.magic.message.enums.DingTalkType; 20 | import com.xkcoding.magic.message.model.dingtalk.support.feedcard.FeedCard; 21 | import lombok.Data; 22 | import lombok.EqualsAndHashCode; 23 | 24 | /** 25 | *

26 | * 钉钉 FeedCard 类型,参考文档:https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq#a-nameidq3rhafeedcard%E7%B1%BB%E5%9E%8B 27 | *

28 | * 29 | * @author yangkai.shen 30 | * @date Created in 2019/10/11 14:06 31 | */ 32 | @Data 33 | @EqualsAndHashCode(callSuper = true) 34 | public class FeedCardDingTalkMessage extends AbstractDingTalkMessage { 35 | /** 36 | * 消息内容 37 | */ 38 | private FeedCard feedCard; 39 | 40 | /** 41 | * 消息类型,此时固定为:feedCard 42 | */ 43 | public FeedCardDingTalkMessage() { 44 | super(DingTalkType.FEEDCARD); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/model/dingtalk/LinkDingTalkMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.model.dingtalk; 18 | 19 | import com.xkcoding.magic.message.enums.DingTalkType; 20 | import com.xkcoding.magic.message.model.dingtalk.support.link.Link; 21 | import lombok.Data; 22 | import lombok.EqualsAndHashCode; 23 | 24 | /** 25 | *

26 | * 钉钉 link 类型,参考文档:https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq#a-namebfrzrdalink%E7%B1%BB%E5%9E%8B 27 | *

28 | * 29 | * @author yangkai.shen 30 | * @date Created in 2019/10/11 13:56 31 | */ 32 | @Data 33 | @EqualsAndHashCode(callSuper = true) 34 | public class LinkDingTalkMessage extends AbstractDingTalkMessage { 35 | /** 36 | * 消息内容 37 | */ 38 | private Link link; 39 | 40 | /** 41 | * 消息类型,此时固定为:link 42 | */ 43 | public LinkDingTalkMessage() { 44 | super(DingTalkType.LINK); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/model/dingtalk/MarkdownDingTalkMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.model.dingtalk; 18 | 19 | import com.xkcoding.magic.message.enums.DingTalkType; 20 | import com.xkcoding.magic.message.model.dingtalk.support.Attention; 21 | import com.xkcoding.magic.message.model.dingtalk.support.markdown.Markdown; 22 | import lombok.Data; 23 | import lombok.EqualsAndHashCode; 24 | 25 | /** 26 | *

27 | * 钉钉 markdown 类型,参考文档:https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq#a-nameytovydamarkdown%E7%B1%BB%E5%9E%8B 28 | *

29 | * 30 | * @author yangkai.shen 31 | * @date Created in 2019/10/11 13:58 32 | */ 33 | @Data 34 | @EqualsAndHashCode(callSuper = true) 35 | public class MarkdownDingTalkMessage extends AbstractDingTalkMessage { 36 | /** 37 | * 消息内容 38 | */ 39 | private Markdown markdown; 40 | /** 41 | * at的人 42 | */ 43 | private Attention at; 44 | 45 | /** 46 | * 消息类型,此时固定为:markdown 47 | */ 48 | public MarkdownDingTalkMessage() { 49 | super(DingTalkType.MARKDOWN); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/model/dingtalk/TextDingTalkMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.model.dingtalk; 18 | 19 | import com.xkcoding.magic.message.enums.DingTalkType; 20 | import com.xkcoding.magic.message.model.dingtalk.support.Attention; 21 | import com.xkcoding.magic.message.model.dingtalk.support.text.Text; 22 | import lombok.Data; 23 | import lombok.EqualsAndHashCode; 24 | 25 | /** 26 | *

27 | * 钉钉 text 类型,参考文档:https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq#a-nameg0hcpdatext%E7%B1%BB%E5%9E%8B 28 | *

29 | * 30 | * @author yangkai.shen 31 | * @date Created in 2019/10/11 13:49 32 | */ 33 | @Data 34 | @EqualsAndHashCode(callSuper = true) 35 | public class TextDingTalkMessage extends AbstractDingTalkMessage { 36 | /** 37 | * 消息内容 38 | */ 39 | private Text text; 40 | /** 41 | * at的人 42 | */ 43 | private Attention at; 44 | 45 | /** 46 | * 消息类型,此时固定为:text 47 | */ 48 | public TextDingTalkMessage() { 49 | super(DingTalkType.TEXT); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/model/dingtalk/support/Attention.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.model.dingtalk.support; 18 | 19 | import com.google.common.collect.Lists; 20 | import lombok.AllArgsConstructor; 21 | import lombok.Data; 22 | import lombok.NoArgsConstructor; 23 | 24 | import java.util.List; 25 | 26 | @Data 27 | @NoArgsConstructor 28 | @AllArgsConstructor 29 | public class Attention { 30 | /** 31 | * 被@人的手机号(在content里添加@人的手机号) 32 | */ 33 | private List atMobiles = Lists.newArrayList(); 34 | /** 35 | * 当@所有人时:true,否则为:false 36 | */ 37 | private boolean isAtAll; 38 | } 39 | -------------------------------------------------------------------------------- /magic-starter-message/src/main/java/com/xkcoding/magic/message/model/dingtalk/support/actioncard/ActionCardSingle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2019-2029, xkcoding & Yangkai.Shen & 沈扬凯 (237497819@qq.com & xkcoding.com). 3 | *

4 | * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; 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.gnu.org/licenses/lgpl.html 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 | package com.xkcoding.magic.message.model.dingtalk.support.actioncard; 18 | 19 | import com.google.common.collect.Lists; 20 | import lombok.AllArgsConstructor; 21 | import lombok.Data; 22 | import lombok.NoArgsConstructor; 23 | 24 | import java.util.List; 25 | 26 | /** 27 | *

28 | * 独立跳转 ActionCard 类型 29 | *

30 | * 31 | * @author yangkai.shen 32 | * @date Created in 2019/10/11 14:02 33 | */ 34 | @Data 35 | @NoArgsConstructor 36 | @AllArgsConstructor 37 | public class ActionCardSingle { 38 | /** 39 | * 首屏会话透出的展示内容 40 | */ 41 | private String title; 42 | /** 43 | * markdown格式的消息 44 | */ 45 | private String text; 46 | /** 47 | * 按钮的信息 48 | */ 49 | private List