提交 95b4db38 authored 作者: 李秋林's avatar 李秋林

添加所有工具类,用于其他项目的commont子模块

上级 3ea762a4
......@@ -3,30 +3,23 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
<groupId>com.wangxiaolu</groupId>
<artifactId>wangxiaolu-promotion-parent</artifactId>
<version>0.0.1</version>
</parent>
<groupId>com.promotion</groupId>
<groupId>com.wangxiaolu</groupId>
<artifactId>wangxiaolu-promotion-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>wangxiaolu-promotion-common</name>
<description>wangxiaolu-promotion-common</description>
<properties>
<java.version>22</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
......@@ -34,6 +27,17 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>none</mainClass>
<classifier>execute</classifier>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
......
package com.promotion.common;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WangxiaoluPromotionCommonApplication {
public static void main(String[] args) {
SpringApplication.run(WangxiaoluPromotionCommonApplication.class, args);
}
}
package com.wangxiaolu.promotion.common.redis;
import lombok.AllArgsConstructor;
import lombok.Getter;
public interface RedisKeys {
@AllArgsConstructor
@Getter
enum UserKeys {
/**
* 用户接收手机验证码
*/
PHONE_VER_CODE("user:phone_code:phone_"),
/**
* 用户登录信息:token
*/
TEMPORARY_TOKEN("user:login_token:temporary:"),
;
String key;
}
}
package com.wangxiaolu.promotion.common.redis.service;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
public class RedisCache {
@Autowired
RedisTemplate<String, String> redisTemplate;
/**
* 保存一个值
*/
public void add(String key, String val) {
ValueOperations<String, String> vo = redisTemplate.opsForValue();
vo.set(key, val);
}
/**
* 保存一个值,设置过期时间(分钟)
*/
public void addToMinute(String key, String val, long time) {
ValueOperations<String, String> vo = redisTemplate.opsForValue();
vo.set(key, val, time, TimeUnit.MINUTES);
}
/**
* 保存一个值,并将val json化
*/
public void addToJson(String key, Object val) {
ValueOperations<String, String> vo = redisTemplate.opsForValue();
vo.set(key, valToJson(val));
}
/**
* 保存一个值,并将val json化,设置过期时间(分钟)
*/
public void addToJsonToMinute(String key, Object val, long time) {
ValueOperations<String, String> vo = redisTemplate.opsForValue();
vo.set(key, valToJson(val), time, TimeUnit.MINUTES);
}
/**
* 保存一个值,并将val json化,设置过期时间(天)
*/
public void addToJsonToDays(String key, Object val, long days) {
ValueOperations<String, String> vo = redisTemplate.opsForValue();
vo.set(key, valToJson(val), days, TimeUnit.DAYS);
}
/**
* 获取一个值
*/
public String get(String key) {
ValueOperations<String, String> vo = redisTemplate.opsForValue();
return vo.get(key);
}
/**
* 获取一个值,并将val json化
*/
public JSONObject getToJson(String key) {
ValueOperations<String, String> vo = redisTemplate.opsForValue();
String val = vo.get(key);
return JSONObject.parseObject(val);
}
/**
* 获取一个值,并将val json化
*/
// public JSONObject getUserJsonInfo(String authorization) {
// JSONObject userDetail = getToJson(RedisKeys.UserKeys.TOKEN.getKey() + authorization);
// return userDetail;
// }
public void removeKey(String key) {
redisTemplate.delete(key);
}
private String valToJson(Object o) {
return JSONObject.toJSONString(o);
}
}
package com.wangxiaolu.promotion.exception;
import com.wangxiaolu.promotion.result.basedata.RCode;
import com.wangxiaolu.promotion.result.basedata.StatusCode;
import lombok.Getter;
/**
* @author : liqiulin
* @date : 2024-03-28 17
* @describe : response包装R失败
*/
@Getter
public class APIException extends RuntimeException {
private int code;
private String msg;
public APIException(StatusCode statusCode, String message) {
super(message);
this.code = statusCode.getCode();
this.msg = statusCode.getMsg();
}
/**
* 默认异常编码
*/
public APIException(String message) {
super(message);
this.code = RCode.API_ERROR.getCode();
this.msg = RCode.API_ERROR.getMsg();
}
}
package com.wangxiaolu.promotion.exception;
import com.wangxiaolu.promotion.result.basedata.RCode;
import com.wangxiaolu.promotion.result.basedata.StatusCode;
import lombok.Getter;
/**
* @author : liqiulin
* @date : 2024-03-28 17
* @describe : response包装R失败
*/
@Getter
public class DataException extends RuntimeException {
private int code;
private String msg;
public DataException(StatusCode statusCode, String message) {
super(message);
this.code = statusCode.getCode();
this.msg = statusCode.getMsg();
}
/**
* 默认异常编码
*/
public DataException(String message) {
super(message);
this.code = RCode.API_ERROR.getCode();
this.msg = RCode.API_ERROR.getMsg();
}
}
package com.wangxiaolu.promotion.exception;
import com.wangxiaolu.promotion.result.basedata.RCode;
import com.wangxiaolu.promotion.result.basedata.StatusCode;
import lombok.Getter;
/**
* @author : liqiulin
* @date : 2024-03-28 19
* @describe : 业务流程异常
*/
@Getter
public class FlowException extends RuntimeException{
private int code;
private String msg;
public FlowException(StatusCode statusCode, String message) {
super(message);
this.code = statusCode.getCode();
this.msg = statusCode.getMsg();
}
/**
* 默认异常编码
*/
public FlowException(String message) {
super(message);
this.code = RCode.LOGIN_PARAM_ERROR.getCode();
this.msg = RCode.LOGIN_PARAM_ERROR.getMsg();
}
}
package com.wangxiaolu.promotion.exception;
import com.wangxiaolu.promotion.result.basedata.RCode;
import com.wangxiaolu.promotion.result.basedata.StatusCode;
import lombok.Getter;
/**
* @author : liqiulin
* @date : 2024-03-28 19
* @describe : 参数异常
*/
@Getter
public class ParamException extends RuntimeException{
private int code;
private String msg;
public ParamException(StatusCode statusCode, String message) {
super(message);
this.code = statusCode.getCode();
this.msg = statusCode.getMsg();
}
/**
* 默认异常编码
*/
public ParamException(String message) {
super(message);
this.code = RCode.LOGIN_PARAM_ERROR.getCode();
this.msg = RCode.LOGIN_PARAM_ERROR.getMsg();
}
}
package com.wangxiaolu.promotion.result.advice;
import com.wangxiaolu.promotion.exception.APIException;
import com.wangxiaolu.promotion.exception.FlowException;
import com.wangxiaolu.promotion.exception.ParamException;
import com.wangxiaolu.promotion.result.basedata.RCode;
import com.wangxiaolu.promotion.result.basedata.R;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.sql.SQLIntegrityConstraintViolationException;
/**
* @author : liqiulin
* @date : 2024-03-28 19
* @describe : 统一异常处理
*/
@RestControllerAdvice
public class ControllerExceptionAdvice {
@ExceptionHandler({ParamException.class})
public R paramExceptionHandler(ParamException e) {
return new R(e.getCode(), e.getMsg(), e.getMessage());
}
@ExceptionHandler({FlowException.class})
public R flowExceptionHandler(FlowException e) {
return new R(e.getCode(), e.getMsg(), e.getMessage());
}
@ExceptionHandler({APIException.class})
public R apiExceptionHandler(APIException e) {
return new R(e.getCode(), e.getMsg(), e.getMessage());
}
@ExceptionHandler({SQLIntegrityConstraintViolationException.class})
public R dbConstraintViolationExceptionHandler(SQLIntegrityConstraintViolationException e) {
RCode errorCode = RCode.USER_PHONE_IDENNUMBER_WXOPENID_UNIQUE_ERROR;
String msg = errorCode.getMsg();
String message = e.getMessage();
if (message.contains("phone_unique")) {
msg = "手机号已注册";
} else if (message.contains("iden_num_unique")) {
msg = "身份证已注册";
} else if (message.contains("open_id_unique")) {
msg = "微信号已注册";
}
return new R(errorCode.getCode(), msg, message);
}
@ExceptionHandler({MethodArgumentNotValidException.class})
public R methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
String msg = e.getBindingResult().getFieldError().getDefaultMessage();
return new R(RCode.PARAM_ERROR.getCode(), msg);
}
@ExceptionHandler({DataIntegrityViolationException.class})
public R dataIntegrityViolationExceptionHandler(DataIntegrityViolationException e) {
String msg = e.getMessage();
if (msg.contains("java.sql.SQLIntegrityConstraintViolationException")){
return dbConstraintViolationExceptionHandler(new SQLIntegrityConstraintViolationException(msg));
}
return new R(RCode.API_DATA_ERROR.getCode(), RCode.API_DATA_ERROR.getMsg(), msg);
}
@ExceptionHandler({TooManyResultsException.class})
public R tooManyResultsExceptionHandler(TooManyResultsException e) {
String msg = e.getMessage();
return new R(RCode.DATA_TOO_MANY_ERROR.getCode(), msg);
}
}
package com.wangxiaolu.promotion.result.advice;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wangxiaolu.promotion.exception.APIException;
import com.wangxiaolu.promotion.result.basedata.RCode;
import com.wangxiaolu.promotion.result.basedata.R;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
* @author : liqiulin
* @date : 2024-03-28 17
* @describe : todo 自动封装 R
*/
@RestControllerAdvice(basePackages = {"com.wangxiaolu.promotion.controller"})
public class ControllerResponseAdvice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
// response是R类型或者注释了NotControllerResponseAdvice都不进行包装
return !methodParameter.getParameterType().isAssignableFrom(R.class);
}
@Override
public Object beforeBodyWrite(Object data, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
// String类型不能直接包装
if (methodParameter.getGenericParameterType().equals(String.class)) {
ObjectMapper objectMapper = new ObjectMapper();
try {
// 将数据包装在ResultVo里后转换为json串进行返回
return objectMapper.writeValueAsString(new R(data));
} catch (JsonProcessingException e) {
throw new APIException(RCode.RESPONSE_PACK_ERROR, e.getMessage());
}
}
// 包装成R返回
return new R(data);
}
}
package com.wangxiaolu.promotion.result.basedata;
import lombok.Data;
/**
* @author : liqiulin
* @date : 2024-03-28 17
* @describe : 统一响应VO
*/
@Data
public class R {
/**
* 响应码
* 非0失败
*/
private int code;
/**
* 提示信息
*/
private String msg;
/**
* 返回对象
*/
private Object data;
/**
* 默认成功
*/
public R(Object data) {
this.code = RCode.SUCCESS.getCode();
this.msg = RCode.SUCCESS.getMsg();
this.data = data;
}
/**
* 无数据返回
*/
public R(StatusCode statusCode) {
this.code = statusCode.getCode();
this.msg = statusCode.getMsg();
this.data = null;
}
/**
* 手动配置code、msg
*/
public R(Integer code, String msg) {
this.code = code;
this.msg = msg;
this.data = null;
}
/**
* 指定状态
*/
public R(StatusCode statusCode, Object data) {
this.code = statusCode.getCode();
this.msg = statusCode.getMsg();
this.data = data;
}
/**
* 手动设置
*/
public R(int code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
}
package com.wangxiaolu.promotion.result.basedata;
import lombok.Getter;
/**
* @author : liqiulin
* @date : 2024-03-28 17
* @describe : 返回状态编码
*/
@Getter
public enum RCode implements StatusCode {
/**
* 程序统一编码(不分模块)
* 1000+
*/
SUCCESS(0, "请求成功"),
FAILED(1001, "请求失败"),
PARAM_ERROR(1002, "参数错误"),
RESPONSE_PACK_ERROR(1003, "包装R失败"),
/**
* 业务统一编码(不分模块)
* 2000+
*/
API_ERROR(2000, "业务异常"),
API_DATA_ERROR(2001, "业务数据异常"),
DATA_HAVE_ERROR(2002, "数据已存在,不可新增"),
STATUS_UPDATE_ERROR(2003, "当前状态不可修改"),
DATA_NOT_HAVE_ERROR(2004, "数据不存在"),
DATA_TOO_MANY_ERROR(2005, "唯一数据存在多条"),
/**
* user
* 3000+
*/
LOGIN_PARAM_ERROR(3000, "登录信息错误"),
ENROLL_PARAM_ERROR(3001, "注册信息错误"),
PHONE_PARAM_ERROR(3002, "手机号异常"),
USER_PHONE_IDENNUMBER_WXOPENID_UNIQUE_ERROR(3003, "个人信息已注册"),
LOGIN_USER_IS_NULL_ERROR(3004, "账号未注册"),
CLOCK_DETAIL_ERROR(3005, "打卡信息错误"),
CLOCK_DETAIL_TIME_ERROR(3006, "不在打卡时间范围内"),
CHARGER_ID_ERROR(3007, "未找到负责人"),
/**
* 腾讯云
* 3500+
*/
TENCENT_SMS_ERROR(3500, "短信发送失败"),
TENCENT_SMS_REPETITION(3501, "短信已发送"),
TENCENT_SMS_PHONE_CODE_ERROR(3502, "验证码错误"),
;
private int code;
private String msg;
RCode(int code, String msg) {
this.code = code;
this.msg = msg;
}
}
package com.wangxiaolu.promotion.result.basedata;
/**
* @author : liqiulin
* @date : 2024-03-28 17
* @describe :返回状态编码
*/
public interface StatusCode {
int getCode();
String getMsg();
}
spring.application.name=wangxiaolu-promotion-common
package com.promotion.common;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class WangxiaoluPromotionCommonApplicationTests {
@Test
void contextLoads() {
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论