
文章目录
- 4.1后端统一设计思想
- 4.1.1后端统一返回格式对象
- 4.1.2后端统一响应状态码
- 4.1.3后端统一异常处理类
- 4.1.4StringUtils类
- 4.1.5 RedisUtils类
4.1后端统一设计思想
4.1.1后端统一返回格式对象
- com.easypan.entity.vo.ResponseVO
@Data
public class ResponseVO<T> {private String status;private Integer code; private String info; private T data;
}
4.1.2后端统一响应状态码
- com.easypan.entity.enums.ResponseCodeEnum
public enum ResponseCodeEnum {CODE_200(200, "请求成功"),CODE_404(404, "请求地址不存在"),CODE_600(600, "请求参数错误"),CODE_601(601, "信息已经存在"),CODE_500(500, "服务器返回错误,请联系管理员"),CODE_901(901, "登录超时,请重新登录"),CODE_902(902, "分享连接不存在,或者已失效"),CODE_903(903, "分享验证失效,请重新验证"),CODE_904(904, "网盘空间不足,请扩容");private Integer code; private String msg; ResponseCodeEnum(Integer code, String msg) {this.code = code;this.msg = msg;}public Integer getCode() {return code;}public String getMsg() {return msg;}
}
4.1.3后端统一异常处理类
- com.easypan.exception.BusinessException
@Data
@AllArgsConstructor
public class BusinessException extends RuntimeException {private ResponseCodeEnum codeEnum; private Integer code; private String message; public BusinessException(String message, Throwable e) {super(message, e);this.message = message;}public BusinessException(String message) {super(message);this.message = message;}public BusinessException(Throwable e) {super(e);}public BusinessException(ResponseCodeEnum codeEnum) {super(codeEnum.getMsg());this.codeEnum = codeEnum;this.code = codeEnum.getCode();this.message = codeEnum.getMsg();}public BusinessException(Integer code, String message) {super(message);this.code = code;this.message = message;}@Overridepublic Throwable fillInStackTrace() {return this;}}
4.1.4StringUtils类
- com.easypan.utils.StringUtils
public class StringUtils {public static String encodeByMD5(String originString) {return StringUtils.isEmpty(originString) ? null : DigestUtils.md5Hex(originString);}public static boolean isEmpty(String str) {if (null == str || "".equals(str) || "null".equals(str) || "\u0000".equals(str)) {return true;} else if ("".equals(str.trim())) {return true;}return false;}public static String getFileSuffix(String fileName) {Integer index = fileName.lastIndexOf(".");if (index == -1) {return "";}String suffix = fileName.substring(index);return suffix;}public static String getFileNameNoSuffix(String fileName) {Integer index = fileName.lastIndexOf(".");if (index == -1) {return fileName;}fileName = fileName.substring(0, index);return fileName;}public static String rename(String fileName) {String fileNameReal = getFileNameNoSuffix(fileName);String suffix = getFileSuffix(fileName);return fileNameReal + "_" + getRandomString(Constants.LENGTH_5) + suffix;}public static final String getRandomString(Integer count) {return RandomStringUtils.random(count, true, true);}public static final String getRandomNumber(Integer count) {return RandomStringUtils.random(count, false, true);}public static String escapeTitle(String content) {if (isEmpty(content)) {return content;}content = content.replace("<", "<");return content;}public static String escapeHtml(String content) {if (isEmpty(content)) {return content;}content = content.replace("<", "<");content = content.replace(" ", " ");content = content.replace("\n", "<br>");return content;}public static boolean pathIsOk(String path) {if (StringUtils.isEmpty(path)) {return true;}if (path.contains("../") || path.contains("..\\")) {return false;}return true;}
}
4.1.5 RedisUtils类
- com.easypan.utils.RedisUtils
@Slf4j
@Component("redisUtils")
public class RedisUtils<V> {@Resourceprivate RedisTemplate<String, V> redisTemplate;public boolean set(String key, V value) {try {redisTemplate.opsForValue().set(key, value);return true;} catch (Exception e) {log.error("设置redisKey:{},value:{}失败", key, value);return false;}}public boolean setex(String key, V value, long time) {try {if (time > 0) {redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);} else {set(key, value);}return true;} catch (Exception e) {log.error("设置redisKey:{},value:{}失败", key, value);return false;}}public V get(String key) {return key == null ? null : redisTemplate.opsForValue().get(key);}public void delete(String... key) {if (key != null && key.length > 0) {if (key.length == 1) {redisTemplate.delete(key[0]);} else {redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));}}}
}
