build: 工具
parent
c1aa8aaeb1
commit
dee5eb0518
|
@ -63,7 +63,11 @@
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.qiniu</groupId>
|
||||||
|
<artifactId>qiniu-java-sdk</artifactId>
|
||||||
|
<version>7.4.0</version>
|
||||||
|
</dependency>
|
||||||
<!-- JSON工具类 -->
|
<!-- JSON工具类 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.mcwl.common.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 七牛云配置
|
||||||
|
*
|
||||||
|
* @date 2024/5/5 下午5:01
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "oss.qiniu")
|
||||||
|
@Data
|
||||||
|
public class QiNiuConfig {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AccessKey
|
||||||
|
*/
|
||||||
|
private String accessKey;
|
||||||
|
/**
|
||||||
|
* SecretKey
|
||||||
|
*/
|
||||||
|
private String secretKey;
|
||||||
|
/**
|
||||||
|
* 图片存储空间名
|
||||||
|
*/
|
||||||
|
private String bucketPictureName;
|
||||||
|
/**
|
||||||
|
* 图片外链
|
||||||
|
*/
|
||||||
|
private String domainPicture;
|
||||||
|
/**
|
||||||
|
* 文件存储空间名
|
||||||
|
*/
|
||||||
|
private String bucketFileName;
|
||||||
|
/**
|
||||||
|
* 文件外链
|
||||||
|
*/
|
||||||
|
private String domainFile;
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
|
||||||
|
package com.mcwl.common.interfaces;
|
||||||
|
/**
|
||||||
|
* @author 苏三,该项目是知识星球:java突击队 的内部项目
|
||||||
|
* @date 2024/6/11 下午4:12
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import com.mcwl.common.valid.MaxMoneyConstraintValidator;
|
||||||
|
|
||||||
|
import javax.validation.Constraint;
|
||||||
|
import javax.validation.Payload;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 最大值约束.
|
||||||
|
*
|
||||||
|
* @author 苏三,该项目是知识星球:java突击队 的内部项目
|
||||||
|
* @date 2024/6/11 下午4:13
|
||||||
|
*/
|
||||||
|
@Target({ElementType.METHOD, ElementType.FIELD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Constraint(validatedBy = MaxMoneyConstraintValidator.class)
|
||||||
|
public @interface MaxMoney {
|
||||||
|
/**
|
||||||
|
* message.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String message() default "{minMoney.message.error}";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* max value.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
double value() default 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* group.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* payload.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
package com.mcwl.common.interfaces;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @date 2024/6/11 下午4:12
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
import com.mcwl.common.valid.MinMoneyConstraintValidator;
|
||||||
|
|
||||||
|
import javax.validation.Constraint;
|
||||||
|
import javax.validation.Payload;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 最小值约束.
|
||||||
|
*
|
||||||
|
|
||||||
|
* @date 2024/6/11 下午4:13
|
||||||
|
*/
|
||||||
|
@Target({ElementType.METHOD, ElementType.FIELD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Constraint(validatedBy = MinMoneyConstraintValidator.class)
|
||||||
|
public @interface MinMoney {
|
||||||
|
/**
|
||||||
|
* message.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String message() default "{minMoney.message.error}";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* min value.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
double value() default 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* group.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* payload.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.mcwl.common.interfaces;
|
||||||
|
|
||||||
|
import com.mcwl.common.valid.PhoneValidator;
|
||||||
|
|
||||||
|
import javax.validation.Constraint;
|
||||||
|
import javax.validation.Payload;
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 苏三
|
||||||
|
* @date 2024/9/24 下午3:09
|
||||||
|
*/
|
||||||
|
@Documented
|
||||||
|
@Constraint(validatedBy = {PhoneValidator.class})
|
||||||
|
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface ValidPhone {
|
||||||
|
/**
|
||||||
|
* 返回信息
|
||||||
|
*/
|
||||||
|
String message() default "手机号码格式不正确";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分组
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
package com.mcwl.common.utils;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单编号生成工具
|
||||||
|
*
|
||||||
|
* @date 2024/5/30 下午6:02
|
||||||
|
*/
|
||||||
|
public class OrderCodeUtil {
|
||||||
|
|
||||||
|
public static final int ORDER_CODE_LENGTH = 24;
|
||||||
|
private static final String ORDER_CODE_DATA_FORMAT = "yyyyMMddHHmmss";
|
||||||
|
private static final int ORDER_CODE_RANDOM_NUMBER = 10;
|
||||||
|
private static final int ORDER_CODE_RANDOM_LENGTH = 6;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成订单编号
|
||||||
|
*
|
||||||
|
* @return 订单编号
|
||||||
|
*/
|
||||||
|
public static String generateOrderCode() {
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat(ORDER_CODE_DATA_FORMAT);
|
||||||
|
String random = getRandom(ORDER_CODE_RANDOM_LENGTH);
|
||||||
|
Date date = new Date();
|
||||||
|
String time = dateFormat.format(date);
|
||||||
|
String code = "XS" + time + random;
|
||||||
|
while (code.length() < ORDER_CODE_LENGTH) {
|
||||||
|
code = code + 0;
|
||||||
|
}
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getRandom(int len) {
|
||||||
|
Random r = new Random();
|
||||||
|
StringBuilder rs = new StringBuilder();
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
rs.append(r.nextInt(ORDER_CODE_RANDOM_NUMBER));
|
||||||
|
}
|
||||||
|
return rs.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
package com.mcwl.common.utils;
|
||||||
|
|
||||||
|
import com.qiniu.http.Response;
|
||||||
|
import com.qiniu.storage.Configuration;
|
||||||
|
import com.qiniu.storage.Region;
|
||||||
|
import com.qiniu.storage.UploadManager;
|
||||||
|
import com.qiniu.storage.model.DefaultPutRet;
|
||||||
|
import com.qiniu.util.Auth;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.mcwl.common.config.QiNiuConfig;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 七牛云上传工具
|
||||||
|
*
|
||||||
|
* @date 2024/5/5 下午5:02
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class QiNiuUtil {
|
||||||
|
public static final String IMAGE = "image";
|
||||||
|
public static final String FILE = "file";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private QiNiuConfig qiNiuConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将图片上传到七牛云
|
||||||
|
*/
|
||||||
|
public String upload(InputStream file, String fileType, String fileContextType) throws Exception {
|
||||||
|
Configuration cfg = new Configuration(Region.region2());
|
||||||
|
UploadManager uploadManager = new UploadManager(cfg);
|
||||||
|
Auth auth = Auth.create(qiNiuConfig.getAccessKey(), qiNiuConfig.getSecretKey());
|
||||||
|
String upToken = null;
|
||||||
|
String path = null;
|
||||||
|
if (fileType.equals(IMAGE)) {
|
||||||
|
upToken = auth.uploadToken(qiNiuConfig.getBucketPictureName());
|
||||||
|
path = qiNiuConfig.getDomainFile();
|
||||||
|
} else if (fileType.equals(FILE)) {
|
||||||
|
upToken = auth.uploadToken(qiNiuConfig.getBucketFileName());
|
||||||
|
path = qiNiuConfig.getDomainFile();
|
||||||
|
}
|
||||||
|
Response response = uploadManager.put(file, null, upToken, null, fileContextType);
|
||||||
|
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
|
||||||
|
return path + putRet.key;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.mcwl.common.valid;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import com.mcwl.common.interfaces.MaxMoney;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintValidator;
|
||||||
|
import javax.validation.ConstraintValidatorContext;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 最大金额校验
|
||||||
|
* @date 2024/6/11 下午4:13
|
||||||
|
*/
|
||||||
|
public class MaxMoneyConstraintValidator implements ConstraintValidator<MaxMoney, BigDecimal> {
|
||||||
|
|
||||||
|
private MaxMoney constraint;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize(MaxMoney constraint) {
|
||||||
|
this.constraint = constraint;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(BigDecimal value, ConstraintValidatorContext context) {
|
||||||
|
return value != null && value.doubleValue() < constraint.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.mcwl.common.valid;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import com.mcwl.common.interfaces.MinMoney;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintValidator;
|
||||||
|
import javax.validation.ConstraintValidatorContext;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 最小金额校验
|
||||||
|
*
|
||||||
|
* @date 2024/6/11 下午4:13
|
||||||
|
*/
|
||||||
|
public class MinMoneyConstraintValidator implements ConstraintValidator<MinMoney, BigDecimal> {
|
||||||
|
|
||||||
|
private MinMoney constraint;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initialize(MinMoney constraint) {
|
||||||
|
this.constraint = constraint;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(BigDecimal value, ConstraintValidatorContext context) {
|
||||||
|
return value != null && value.doubleValue() >= constraint.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.mcwl.common.valid;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import com.mcwl.common.interfaces.ValidPhone;
|
||||||
|
|
||||||
|
import javax.validation.ConstraintValidator;
|
||||||
|
import javax.validation.ConstraintValidatorContext;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 手机号校验
|
||||||
|
*
|
||||||
|
* @author 苏三
|
||||||
|
* @date 2024/9/24 下午3:12
|
||||||
|
*/
|
||||||
|
public class PhoneValidator implements ConstraintValidator<ValidPhone, String> {
|
||||||
|
private static final String PHONE_REGEX = "^1([38][0-9]|4[5-9]|5[0-3,5-9]|6[6]|7[0-8]|9[89])[0-9]{8}$";
|
||||||
|
private final Pattern pattern = Pattern.compile(PHONE_REGEX);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||||
|
if (value == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return pattern.matcher(value).matches();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue