Commit 3ab6e756 authored by shengnan hu's avatar shengnan hu
Browse files

init

parents
Pipeline #294 passed with stage
in 2 minutes and 13 seconds
package com.mall4j.cloud.common.serializer;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.mall4j.cloud.common.util.PrincipalUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import java.io.IOException;
/**
* @author FrozenWatermelon
* @date 2020/09/03
*/
@Component
@RefreshScope
public class ImgJsonSerializer extends JsonSerializer<String> {
@Value("${biz.oss.resources-url}")
private String imgDomain;
@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (StrUtil.isBlank(value)) {
gen.writeString(StrUtil.EMPTY);
return;
} else if (StrUtil.isBlank(imgDomain)) {
gen.writeString(value);
return;
}
String[] imgs = value.split(StrUtil.COMMA);
StringBuilder sb = new StringBuilder();
for (String img : imgs) {
// 图片为http协议开头,直接返回
if (PrincipalUtil.isHttpProtocol(img)) {
sb.append(img).append(StrUtil.COMMA);
}
else {
sb.append(imgDomain).append(img).append(StrUtil.COMMA);
}
}
sb.deleteCharAt(sb.length() - 1);
gen.writeString(sb.toString());
}
}
/*
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
*
* https://www.mall4j.com/
*
* 未经允许,不可做商业用途!
*
* 版权所有,侵权必究!
*/
package com.mall4j.cloud.common.util;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import java.util.List;
/**
* @author lanhai
*/
public class BeanUtil {
public static <S, D> List<D> mapAsList(final Iterable<S> sourceObject, Class<D> clazz) {
return JSONArray.parseArray(JSONArray.toJSONString(sourceObject), clazz);
}
public static <S, D> D map(final S sourceObject, Class<D> clazz) {
return JSONObject.parseObject(JSONObject.toJSONString(sourceObject), clazz);
}
}
package com.mall4j.cloud.common.util;
import java.util.Objects;
/**
* @author FrozenWatermelon
* @date 2020/9/2
*/
public class BooleanUtil {
/**
* 判断一个数字是否为true(等于1就是true)
* @param num 输入的数字
* @return 是否为true
*/
public static boolean isTrue(Integer num) {
if (num == null) {
return false;
}
return Objects.equals(num, 1);
}
public static boolean isFalse(Integer num) {
return !isTrue(num);
}
}
package com.mall4j.cloud.common.util;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import jakarta.servlet.http.HttpServletRequest;
/**
* IP帮助工具
* @author FrozenWatermelon
*/
public class IpHelper {
private static final String UNKNOWN = "unknown";
/**
* 得到用户的真实地址,如果有多个就取第一个
*
* @return
*/
public static String getIpAddr() {
if (RequestContextHolder.getRequestAttributes() == null) {
return "";
}
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
String[] ips = ip.split(",");
return ips[0].trim();
}
}
package com.mall4j.cloud.common.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* @author FrozenWatermelon
* @date 2020/7/11
*/
public class Json {
private static final Logger logger = LoggerFactory.getLogger(Json.class);
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
static {
// 如果为空则不输出
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
// 对于空的对象转json的时候不抛出错误
OBJECT_MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
// 禁用序列化日期为timestamps
OBJECT_MAPPER.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// 禁用遇到未知属性抛出异常
OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
/**
* 对象转json
* @param object 对象
* @return json
*/
public static String toJsonString(Object object) {
try {
return OBJECT_MAPPER.writeValueAsString(object);
}
catch (JsonProcessingException e) {
logger.error("toJsonString() error: {}", e.getMessage());
}
return "";
}
/**
* json转换换成对象
* @param json json
* @param clazz clazz
* @return Class
*/
public static <T> T parseObject(String json, Class<T> clazz) {
if (json == null) {
return null;
}
T result = null;
try {
result = OBJECT_MAPPER.readValue(json, clazz);
}
catch (Exception e) {
logger.error("parseObject() error: {}", e.getMessage());
}
return result;
}
/**
* json转换换成对象
* @param src src
* @param clazz clazz
* @return Class
*/
public static <T> T parseObject(byte[] src, Class<T> clazz) {
T result = null;
try {
result = OBJECT_MAPPER.readValue(src, clazz);
}
catch (Exception e) {
logger.error("parseObject() error: {}", e.getMessage());
}
return result;
}
public static ObjectMapper getObjectMapper() {
return OBJECT_MAPPER;
}
/**
* *
* https://stackoverflow.com/questions/6349421/how-to-use-jackson-to-deserialise-an-array-of-objects
* * List<MyClass> myObjects = Arrays.asList(mapper.readValue(json, MyClass[].class))
* * works up to 10 time faster than TypeRefence.
* @return List数组
*/
public static <T> List<T> parseArray(String json, Class<T[]> clazz) {
if (json == null) {
return null;
}
T[] result = null;
try {
result = OBJECT_MAPPER.readValue(json, clazz);
}
catch (Exception e) {
logger.error("parseArray() error: {}", e.getMessage());
}
if (result == null) {
return Collections.emptyList();
}
return Arrays.asList(result);
}
public static <T> List<T> parseArray(byte[] src, Class<T[]> clazz) {
T[] result = null;
try {
result = OBJECT_MAPPER.readValue(src, clazz);
}
catch (Exception e) {
logger.error("parseArray() error: {}", e.getMessage());
}
if (result == null) {
return Collections.emptyList();
}
return Arrays.asList(result);
}
/**
* 转换成json节点,即map
* @param jsonStr jsonStr
* @return JsonNode
*/
public static JsonNode parseJson(String jsonStr) {
if (jsonStr == null) {
return null;
}
JsonNode jsonNode = null;
try {
jsonNode = OBJECT_MAPPER.readTree(jsonStr);
}
catch (Exception e) {
logger.error("parseJson() error: {}", e.getMessage());
}
return jsonNode;
}
}
package com.mall4j.cloud.common.util;
import cn.hutool.core.util.StrUtil;
import java.util.regex.Pattern;
/**
* 正则表达式工具
*
* @author FrozenWatermelon
*/
public class PrincipalUtil {
/**
* 以1开头,后面跟10位数
*/
public static final String MOBILE_REGEXP = "1[0-9]{10}";
/**
* 1. 用户名不能为纯数字 2. 由数字字母下划线 4-16位组成
*/
public static final String USER_NAME_REGEXP = "(?!\\d+$)([a-zA-Z0-9_]{4,16})";
/**
* 字段名,数字字母下划线
*/
public static final String FIELD_REGEXP = "([a-zA-Z0-9_]+)";
/**
* 由简单的字母数字拼接而成的字符串 不含有下划线,大写字母
*/
public static final String SIMPLE_CHAR_REGEXP = "([a-z0-9]+)";
/**
* 邮箱正则
*/
public static final String EMAIL_REGEXP = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
/**
* http协议正则
*/
public static final String HTTP_PROTOCOL_REGEXP = "^((http[s]{0,1})://)";
/**
* 是否是手机号
* @param value 输入值
* @return 匹配结果
*/
public static boolean isMobile(String value) {
return isMatching(MOBILE_REGEXP, value);
}
/**
* 是否是用户名
* @param value 输入值
* @return 匹配结果
*/
public static boolean isUserName(String value) {
return isMatching(USER_NAME_REGEXP, value);
}
/**
* 是否符合字段规则
* @param value 输入值
* @return 匹配结果
*/
public static boolean isField(String value) {
return !isMatching(FIELD_REGEXP, value);
}
/**
* 是否是邮箱
* @param value 输入值
* @return 匹配结果
*/
public static boolean isEmail(String value) {
return isMatching(EMAIL_REGEXP, value);
}
/**
* 是否是由简单的字母数字拼接而成的字符串
* @param value 输入值
* @return 匹配结果
*/
public static boolean isSimpleChar(String value) {
return isMatching(SIMPLE_CHAR_REGEXP, value);
}
/**
* 是否是HTTP协议
* @param value 输入值
* @return 匹配结果
*/
public static boolean isHttpProtocol(String value) {
return isFind(HTTP_PROTOCOL_REGEXP, value);
}
public static boolean isMatching(String regexp, String value) {
if (StrUtil.isBlank(value)) {
return false;
}
return Pattern.matches(regexp, value);
}
public static boolean isFind(String regexp, String value) {
if (StrUtil.isBlank(value)) {
return false;
}
Pattern pattern= Pattern.compile(regexp);
return pattern.matcher(value).find();
}
}
package com.mall4j.cloud.common.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* Spring Context 工具类
*
* @author FrozenWatermelon
* @date 2020/7/11
*/
@Component
public class SpringContextUtils implements ApplicationContextAware {
public static ApplicationContext applicationContext;
@SuppressWarnings("NullableProblems")
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.applicationContext = applicationContext;
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> requiredType) {
return applicationContext.getBean(requiredType);
}
public static <T> T getBean(String name, Class<T> requiredType) {
return applicationContext.getBean(name, requiredType);
}
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
public static boolean isSingleton(String name) {
return applicationContext.isSingleton(name);
}
public static Class<?> getType(String name) {
return applicationContext.getType(name);
}
}
package com.mall4j.cloud.common.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.Date;
/**
* 阿里java开发手册: 【强制】表必备三字段:id, create_time, update_time。 说明:其中 id 必为主键,类型为 bigint
* unsigned、单表时自增、步长为 1。create_time, update_time 的类型均为 datetime
* 类型,前者现在时表示主动式创建,后者过去分词表示被动式更新。
*
* @author FrozenWatermelon
*/
public class BaseVO {
/**
* 创建时间
*/
@Schema(description = "创建时间" )
protected Date createTime;
/**
* 更新时间
*/
@Schema(description = "更新时间" )
protected Date updateTime;
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
@Override
public String toString() {
return "BaseDTO{" + "createTime=" + createTime + ", updateTime=" + updateTime + '}';
}
}
{
"groups": [
{
"name": "feign.inside",
"type": "com.mall4j.cloud.common.feign.FeignInsideAuthConfig",
"sourceType": "com.mall4j.cloud.common.feign.FeignInsideAuthConfig"
}
],
"properties": [
{
"name": "feign.inside.ips",
"type": "java.util.List<java.lang.String>",
"sourceType": "com.mall4j.cloud.common.feign.FeignInsideAuthConfig"
},
{
"name": "feign.inside.key",
"type": "java.lang.String",
"sourceType": "com.mall4j.cloud.common.feign.FeignInsideAuthConfig"
},
{
"name": "feign.inside.secret",
"type": "java.lang.String",
"sourceType": "com.mall4j.cloud.common.feign.FeignInsideAuthConfig"
}
],
"hints": []
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment