版本修改 4-26 todo
This commit is contained in:
@@ -1,116 +0,0 @@
|
||||
package com.yxt.demo.common.core.constant;
|
||||
|
||||
/**
|
||||
* 通用常量信息
|
||||
*/
|
||||
public class Constants {
|
||||
/**
|
||||
* UTF-8 字符集
|
||||
*/
|
||||
public static final String UTF8 = "UTF-8";
|
||||
|
||||
/**
|
||||
* GBK 字符集
|
||||
*/
|
||||
public static final String GBK = "GBK";
|
||||
|
||||
/**
|
||||
* RMI 远程方法调用
|
||||
*/
|
||||
public static final String LOOKUP_RMI = "rmi://";
|
||||
|
||||
/**
|
||||
* LDAP 远程方法调用
|
||||
*/
|
||||
public static final String LOOKUP_LDAP = "ldap://";
|
||||
|
||||
/**
|
||||
* http请求
|
||||
*/
|
||||
public static final String HTTP = "http://";
|
||||
|
||||
/**
|
||||
* https请求
|
||||
*/
|
||||
public static final String HTTPS = "https://";
|
||||
|
||||
/**
|
||||
* 成功标记
|
||||
*/
|
||||
public static final Integer SUCCESS = 200;
|
||||
|
||||
/**
|
||||
* 失败标记
|
||||
*/
|
||||
public static final Integer FAIL = 500;
|
||||
|
||||
/**
|
||||
* 登录成功
|
||||
*/
|
||||
public static final String LOGIN_SUCCESS = "Success";
|
||||
|
||||
/**
|
||||
* 注销
|
||||
*/
|
||||
public static final String LOGOUT = "Logout";
|
||||
|
||||
/**
|
||||
* 注册
|
||||
*/
|
||||
public static final String REGISTER = "Register";
|
||||
|
||||
/**
|
||||
* 登录失败
|
||||
*/
|
||||
public static final String LOGIN_FAIL = "Error";
|
||||
|
||||
/**
|
||||
* 当前记录起始索引
|
||||
*/
|
||||
public static final String PAGE_NUM = "pageNum";
|
||||
|
||||
/**
|
||||
* 每页显示记录数
|
||||
*/
|
||||
public static final String PAGE_SIZE = "pageSize";
|
||||
|
||||
/**
|
||||
* 排序列
|
||||
*/
|
||||
public static final String ORDER_BY_COLUMN = "orderByColumn";
|
||||
|
||||
/**
|
||||
* 排序的方向 "desc" 或者 "asc".
|
||||
*/
|
||||
public static final String IS_ASC = "isAsc";
|
||||
|
||||
/**
|
||||
* 验证码 redis key
|
||||
*/
|
||||
public static final String CAPTCHA_CODE_KEY = "captcha_codes:";
|
||||
|
||||
/**
|
||||
* 验证码有效期(分钟)
|
||||
*/
|
||||
public static final long CAPTCHA_EXPIRATION = 2;
|
||||
|
||||
/**
|
||||
* 令牌有效期(分钟)
|
||||
*/
|
||||
public final static long TOKEN_EXPIRE = 720;
|
||||
|
||||
/**
|
||||
* 参数管理 cache key
|
||||
*/
|
||||
public static final String SYS_CONFIG_KEY = "sys_config:";
|
||||
|
||||
/**
|
||||
* 字典管理 cache key
|
||||
*/
|
||||
public static final String SYS_DICT_KEY = "sys_dict:";
|
||||
|
||||
/**
|
||||
* 资源映射路径 前缀
|
||||
*/
|
||||
public static final String RESOURCE_PREFIX = "/profile";
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package com.yxt.demo.common.core.constant;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/7/23 22:37
|
||||
* @Description
|
||||
*/
|
||||
public enum StatusEnum {
|
||||
/**
|
||||
* 状态分类
|
||||
*/
|
||||
SUCCESS(200, "成功"),
|
||||
FAIL(500, "失败"),
|
||||
OVERDUE(5000, "登录状态已过期");
|
||||
|
||||
private int code;
|
||||
private String msg;
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
StatusEnum(int code, String msg) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
package com.yxt.demo.common.core.domain;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2021/9/3 16:06
|
||||
* @description
|
||||
*/
|
||||
public class BaseEntity extends Entity {
|
||||
|
||||
@ApiModelProperty("字符型编号")
|
||||
private String sid = UUID.randomUUID().toString();
|
||||
|
||||
@ApiModelProperty("记录版本,锁")
|
||||
private Integer lockVersion = 0;
|
||||
|
||||
@ApiModelProperty("记录创建时间")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
private Date createTime = new Date();
|
||||
|
||||
@ApiModelProperty("记录最后修改时间")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
private Date modifyTime = new Date();
|
||||
|
||||
@ApiModelProperty("记录状态值")
|
||||
private Integer state = 0;
|
||||
|
||||
@ApiModelProperty("记录是否可用,0:可用(默认),1:不可用")
|
||||
private Integer isEnable = 0;
|
||||
|
||||
@ApiModelProperty("记录是否被删除,0:未删除(默认),1:已经删除")
|
||||
private Integer isDelete = 0;
|
||||
|
||||
@ApiModelProperty("备注信息")
|
||||
private String remarks;
|
||||
|
||||
@ApiModelProperty("创建者")
|
||||
private String createBySid;
|
||||
|
||||
@ApiModelProperty("更新者")
|
||||
private String updateBySid;
|
||||
|
||||
public String getSid() {
|
||||
return sid;
|
||||
}
|
||||
|
||||
public void setSid(String sid) {
|
||||
this.sid = sid;
|
||||
}
|
||||
|
||||
public Integer getLockVersion() {
|
||||
return lockVersion;
|
||||
}
|
||||
|
||||
public void setLockVersion(Integer lockVersion) {
|
||||
this.lockVersion = lockVersion;
|
||||
}
|
||||
|
||||
public Date getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public void setCreateTime(Date createTime) {
|
||||
this.createTime = createTime;
|
||||
}
|
||||
|
||||
public Date getModifyTime() {
|
||||
return modifyTime;
|
||||
}
|
||||
|
||||
public void setModifyTime(Date modifyTime) {
|
||||
this.modifyTime = modifyTime;
|
||||
}
|
||||
|
||||
public Integer getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(Integer state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public Integer getIsEnable() {
|
||||
return isEnable;
|
||||
}
|
||||
|
||||
public void setIsEnable(Integer isEnable) {
|
||||
this.isEnable = isEnable;
|
||||
}
|
||||
|
||||
public Integer getIsDelete() {
|
||||
return isDelete;
|
||||
}
|
||||
|
||||
public void setIsDelete(Integer isDelete) {
|
||||
this.isDelete = isDelete;
|
||||
}
|
||||
|
||||
public String getRemarks() {
|
||||
return remarks;
|
||||
}
|
||||
|
||||
public void setRemarks(String remarks) {
|
||||
this.remarks = remarks;
|
||||
}
|
||||
|
||||
public String getCreateBySid() {
|
||||
return createBySid;
|
||||
}
|
||||
|
||||
public void setCreateBySid(String createBySid) {
|
||||
this.createBySid = createBySid;
|
||||
}
|
||||
|
||||
public String getUpdateBySid() {
|
||||
return updateBySid;
|
||||
}
|
||||
|
||||
public void setUpdateBySid(String updateBySid) {
|
||||
this.updateBySid = updateBySid;
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
package com.yxt.demo.common.core.domain;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2021/9/3 16:06
|
||||
* @description
|
||||
*/
|
||||
public class Entity implements Serializable {
|
||||
|
||||
@ApiModelProperty("ID,唯一编号")
|
||||
private Integer id;
|
||||
|
||||
@ApiModelProperty("ID值的字符串形式")
|
||||
public String getIdStr() {
|
||||
if (null == id) {
|
||||
return "";
|
||||
}
|
||||
return "" + id;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ClassName:" + getClass().getName() + ";id:" + getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int id2 = Long.hashCode(getId());
|
||||
return super.hashCode() + id2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof Entity)) {
|
||||
return false;
|
||||
}
|
||||
Entity entity = (Entity) obj;
|
||||
return this.getId().equals(entity.getId());
|
||||
}
|
||||
}
|
||||
@@ -9,4 +9,5 @@ import com.yxt.demo.common.core.vo.Vo;
|
||||
*/
|
||||
|
||||
public interface Query extends Vo {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,141 +0,0 @@
|
||||
package com.yxt.demo.common.core.result;
|
||||
|
||||
import com.yxt.demo.common.core.constant.Constants;
|
||||
import com.yxt.demo.common.core.constant.StatusEnum;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2021/8/23 16:42
|
||||
* @Description 返回结果
|
||||
*/
|
||||
public class ResultBean<T> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 成功
|
||||
*/
|
||||
public static final int SUCCESS = Constants.SUCCESS;
|
||||
|
||||
/**
|
||||
* 失败
|
||||
*/
|
||||
public static final int FAIL = Constants.FAIL;
|
||||
|
||||
private int code;
|
||||
|
||||
private String msg;
|
||||
|
||||
private T data;
|
||||
private Boolean success;
|
||||
|
||||
public ResultBean() {
|
||||
}
|
||||
|
||||
public ResultBean(boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public ResultBean(boolean success, String msg) {
|
||||
this.success = success;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public ResultBean(boolean success, String msg, int code) {
|
||||
this.success = success;
|
||||
this.msg = msg;
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public ResultBean(T data) {
|
||||
this.success = true;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public ResultBean(int code, T data) {
|
||||
this.success = true;
|
||||
this.code = code;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public ResultBean(int code, String msg, T data) {
|
||||
this.success = true;
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public boolean getSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public ResultBean<T> setSuccess(boolean success) {
|
||||
this.success = success;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public ResultBean<T> setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public ResultBean<T> setCode(int code) {
|
||||
this.code = code;
|
||||
return this;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public ResultBean<T> setData(T data) {
|
||||
this.data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1.类实例化的非静态方法调用 2.类不实例化的静态方法调用
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public ResultBean<T> success() {
|
||||
this.setSuccess(true);
|
||||
this.setCode(StatusEnum.SUCCESS.getCode());
|
||||
this.setMsg("操作成功!");
|
||||
return this;
|
||||
}
|
||||
|
||||
public ResultBean<T> fail() {
|
||||
this.setSuccess(false);
|
||||
this.setCode(StatusEnum.FAIL.getCode());
|
||||
this.setMsg("操作失败!");
|
||||
return this;
|
||||
}
|
||||
|
||||
public static <T> ResultBean<T> fireSuccess() {
|
||||
ResultBean<T> rb = new ResultBean<T>();
|
||||
rb.setSuccess(true);
|
||||
rb.setCode(StatusEnum.SUCCESS.getCode());
|
||||
rb.setMsg("操作成功!");
|
||||
return rb;
|
||||
}
|
||||
|
||||
public static <T> ResultBean<T> fireFail() {
|
||||
ResultBean<T> rb = new ResultBean<T>();
|
||||
rb.setSuccess(false);
|
||||
rb.setCode(StatusEnum.FAIL.getCode());
|
||||
rb.setMsg("操作失败!");
|
||||
return rb;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.yxt.demo.common.core.vo;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.yxt.demo.common.core.domain.Entity;
|
||||
|
||||
import javax.swing.text.html.parser.Entity;
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.yxt.demo.common.jdbc.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2021/9/4 0:40
|
||||
* @description
|
||||
*/
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
@Bean
|
||||
public MybatisPlusInterceptor paginationInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
|
||||
return interceptor;
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.yxt.demo.common.jdbc.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MybatisBaseService<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> {
|
||||
|
||||
|
||||
public T fetchBySid(String sid) {
|
||||
QueryWrapper<T> qw = new QueryWrapper<>();
|
||||
qw.eq("sid", sid);
|
||||
return baseMapper.selectOne(qw);
|
||||
}
|
||||
|
||||
public int deleteBySid(String sid) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("sid", sid);
|
||||
return baseMapper.deleteByMap(map);
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package com.yxt.demo.common.jdbc.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.yxt.demo.common.core.query.PagerQuery;
|
||||
import com.yxt.demo.common.core.vo.PagerVo;
|
||||
import com.yxt.demo.common.core.vo.Vo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Project: jbsc-commons <br/>
|
||||
* File: PagerUtil.java <br/>
|
||||
* Class: org.jbase.jbsc.commons.base.utils.PagerUtil <br/>
|
||||
* Description: <描述类的功能>. <br/>
|
||||
* Copyright: Copyright (c) 2011 <br/>
|
||||
* Company: https://gitee.com/liuzp315 <br/>
|
||||
* Makedate: 2020/9/22 上午9:35 <br/>
|
||||
*
|
||||
* @author popo
|
||||
* @version 1.0
|
||||
* @since 1.0
|
||||
*/
|
||||
public abstract class PagerUtil {
|
||||
|
||||
public static <T> IPage<T> queryToPage(PagerQuery pq) {
|
||||
IPage<T> page = new Page<>();
|
||||
page.setSize(pq.getSize()).setCurrent(pq.getCurrent());
|
||||
return page;
|
||||
}
|
||||
|
||||
public static <V, T> PagerVo<V> pageToVo(IPage pr, PagerVo<V> pv) {
|
||||
if (pv == null) {
|
||||
pv = new PagerVo<V>();
|
||||
}
|
||||
pv.setCurrent(pr.getCurrent()).setSize(pr.getSize())
|
||||
.setTotal(pr.getTotal()).setPages(pr.getPages())
|
||||
.setRecords(pr.getRecords());
|
||||
return pv;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换pagerVo
|
||||
*
|
||||
* @param soure
|
||||
* @return
|
||||
*/
|
||||
public static <S extends Vo, T extends Vo> PagerVo<T> switchPagerVo(
|
||||
PagerVo<S> soure) {
|
||||
if (soure == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
PagerVo<T> target = new PagerVo<>();
|
||||
target.setCurrent(soure.getCurrent()).setPages(soure.getPages())
|
||||
.setSize(soure.getSize()).setTotal(soure.getTotal());
|
||||
target.setRecords(new ArrayList<T>());
|
||||
|
||||
return target;
|
||||
}
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
package com.yxt.demo.common.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/2/9 21:52
|
||||
* @Description 工具类
|
||||
*/
|
||||
public class BeanUtils {
|
||||
|
||||
|
||||
/**
|
||||
* 获取String 类型
|
||||
*
|
||||
* @param map
|
||||
* @param keyName 键值对中键名称
|
||||
* @param defaultValue 默认值
|
||||
* @return
|
||||
*/
|
||||
public static String getString(Map<String, Object> map, String keyName, String defaultValue) {
|
||||
if (map == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (map.containsKey(keyName)) {
|
||||
Object o = map.get(keyName);
|
||||
if (o instanceof String) {
|
||||
return (String) o;
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取Integer
|
||||
public static Integer getInteger(Map<String, Object> map, String keyName) {
|
||||
if (map == null) {
|
||||
return -1;
|
||||
}
|
||||
if (map.containsKey(keyName)) {
|
||||
Object o = map.get(keyName);
|
||||
if (o instanceof Integer) {
|
||||
return (int) o;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//获取map类型
|
||||
public static Map<String, Object> getMap(Map<String, Object> map, String keyName) {
|
||||
if (map == null) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
if (map.containsKey(keyName)) {
|
||||
Object o = map.get(keyName);
|
||||
if (o instanceof Map) {
|
||||
return (Map) o;
|
||||
} else {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
} else {
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
// 获取List类型
|
||||
public static List<Map<String, Object>> getListData(Map<String, Object> map, String keyName) {
|
||||
List<Map<String, Object>> list = new ArrayList<>();
|
||||
if (map == null) {
|
||||
return list;
|
||||
}
|
||||
if (map.containsKey(keyName)) {
|
||||
Object o = map.get(keyName);
|
||||
if (o instanceof List) {
|
||||
for (int i = 0; i < ((List) o).size(); i++) {
|
||||
Object o1 = ((List) o).get(i);
|
||||
if (o1 instanceof Map) {
|
||||
list.add((Map) o1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package com.yxt.demo.common.utils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/6/21 0:03
|
||||
* @Description 校验参数工具
|
||||
*/
|
||||
public class ValidationUtil {
|
||||
|
||||
public final static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
|
||||
|
||||
/**
|
||||
* 验证单个实体
|
||||
*
|
||||
* @param t 参数
|
||||
* @param <T> 类型
|
||||
* @return 验证结果
|
||||
*/
|
||||
public static <T> String validateOne(T t) {
|
||||
Set<ConstraintViolation<T>> validateResult = validator.validate(t);
|
||||
if (validateResult != null && validateResult.size() != 0) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (ConstraintViolation<T> valid : validateResult) {
|
||||
sb.append(valid.getPropertyPath().toString()).append(StringUtils.SPACE).append(valid.getMessage())
|
||||
.append(",");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证多个实体
|
||||
*
|
||||
* @param ts 参数
|
||||
* @param <T> 类型
|
||||
* @return 验证结果
|
||||
*/
|
||||
public static <T> String validateMutil(List<T> ts) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int index = 0; index < ts.size(); ++index) {
|
||||
String validateOneResult = validateOne(ts.get(index));
|
||||
if (!StringUtils.isBlank(validateOneResult)) {
|
||||
sb.append("index[" + index + "]:").append(validateOneResult).append(";");
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,189 +0,0 @@
|
||||
package com.yxt.demo.common.utils.allutils;
|
||||
|
||||
import org.apache.commons.codec.DecoderException;
|
||||
import org.apache.commons.codec.binary.Hex;
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
import org.apache.tomcat.util.codec.binary.Base64;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
* 封装各种格式的编码解码工具类. 1.Commons-Codec的 hex/base64 编码 2.自制的base62 编码
|
||||
* 3.Commons-Lang的xml/html escape 4.JDK提供的URLEncoder
|
||||
*
|
||||
* @author dimengzhe
|
||||
* @date 2020/9/11 13:38
|
||||
* @description 编码解码工具类
|
||||
*/
|
||||
|
||||
public class Encodes {
|
||||
|
||||
private static final String DEFAULT_URL_ENCODING = "UTF-8";
|
||||
private static final char[] BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
|
||||
private static final String SALT = "jlzx@yxt?";
|
||||
|
||||
/**
|
||||
* Hex编码.
|
||||
*/
|
||||
public static String encodeHex(byte[] input) {
|
||||
return new String(Hex.encodeHex(input));
|
||||
}
|
||||
|
||||
/**
|
||||
* Hex解码.
|
||||
*/
|
||||
public static byte[] decodeHex(String input) {
|
||||
try {
|
||||
return Hex.decodeHex(input.toCharArray());
|
||||
} catch (DecoderException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64编码.
|
||||
*/
|
||||
public static String encodeBase64(byte[] input) {
|
||||
return new String(Base64.encodeBase64(input));
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64编码.
|
||||
*/
|
||||
public static String encodeBase64(String input) {
|
||||
try {
|
||||
return new String(Base64.encodeBase64(input.getBytes(DEFAULT_URL_ENCODING)));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64解码.
|
||||
*/
|
||||
public static byte[] decodeBase64(String input) {
|
||||
return Base64.decodeBase64(input.getBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64解码.
|
||||
*/
|
||||
public static String decodeBase64String(String input) {
|
||||
try {
|
||||
return new String(Base64.decodeBase64(input.getBytes()), DEFAULT_URL_ENCODING);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base62编码。
|
||||
*/
|
||||
public static String encodeBase62(byte[] input) {
|
||||
char[] chars = new char[input.length];
|
||||
for (int i = 0; i < input.length; i++) {
|
||||
chars[i] = BASE62[((input[i] & 0xFF) % BASE62.length)];
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
/**
|
||||
* Html 转码.
|
||||
*/
|
||||
public static String escapeHtml(String html) {
|
||||
return StringEscapeUtils.escapeHtml4(html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Html 解码.
|
||||
*/
|
||||
public static String unescapeHtml(String htmlEscaped) {
|
||||
return StringEscapeUtils.unescapeHtml4(htmlEscaped);
|
||||
}
|
||||
|
||||
/**
|
||||
* Xml 转码.
|
||||
*/
|
||||
public static String escapeXml(String xml) {
|
||||
return StringEscapeUtils.escapeXml10(xml);
|
||||
}
|
||||
|
||||
/**
|
||||
* Xml 解码.
|
||||
*/
|
||||
public static String unescapeXml(String xmlEscaped) {
|
||||
return StringEscapeUtils.unescapeXml(xmlEscaped);
|
||||
}
|
||||
|
||||
/**
|
||||
* URL 编码, Encode默认为UTF-8.
|
||||
*/
|
||||
public static String urlEncode(String part) {
|
||||
try {
|
||||
return URLEncoder.encode(part, DEFAULT_URL_ENCODING);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* URL 解码, Encode默认为UTF-8.
|
||||
*/
|
||||
public static String urlDecode(String part) {
|
||||
|
||||
try {
|
||||
return URLDecoder.decode(part, DEFAULT_URL_ENCODING);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String md5(String str) {
|
||||
return digest("MD5", str + SALT);
|
||||
}
|
||||
|
||||
public static String sha1(CharSequence cs) {
|
||||
return digest("SHA1", cs);
|
||||
}
|
||||
|
||||
public static String digest(String algorithm, CharSequence cs) {
|
||||
return digest(algorithm, StringUtils.getBytesUTF8(null == cs ? "" : cs), null, 1);
|
||||
}
|
||||
|
||||
public static String digest(String algorithm, byte[] bytes, byte[] salt, int iterations) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance(algorithm);
|
||||
if (salt != null) {
|
||||
md.update(salt);
|
||||
}
|
||||
byte[] hashBytes = md.digest(bytes);
|
||||
for (int i = 1; i < iterations; i++) {
|
||||
md.reset();
|
||||
hashBytes = md.digest(hashBytes);
|
||||
}
|
||||
return fixedHexString(hashBytes);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String fixedHexString(byte[] hashBytes) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (int i = 0; i < hashBytes.length; i++) {
|
||||
sb.append(Integer.toString((hashBytes[i] & 0xFF) + 256, 16).substring(1));
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String md5(String str, boolean isShort) {
|
||||
if (isShort) {
|
||||
return md5(str).substring(8, 24);
|
||||
}
|
||||
return md5(str);
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
package com.yxt.demo.common.utils.allutils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
/**
|
||||
* 关于异常的工具类.
|
||||
*
|
||||
* @author dimengzhe
|
||||
* @date 2020/9/11 13:44
|
||||
* @description
|
||||
*/
|
||||
|
||||
public class Exceptions {
|
||||
|
||||
/**
|
||||
* 将CheckedException转换为UncheckedException.
|
||||
*/
|
||||
public static RuntimeException unchecked(Throwable e) {
|
||||
if ((e instanceof RuntimeException)) {
|
||||
return (RuntimeException) e;
|
||||
}
|
||||
if ((e instanceof InvocationTargetException)) {
|
||||
return unchecked(((InvocationTargetException) e).getTargetException());
|
||||
}
|
||||
return new RuntimeException(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将ErrorStack转化为String.
|
||||
*/
|
||||
public static String getStackTraceAsString(Throwable e) {
|
||||
if (e == null) {
|
||||
return "";
|
||||
}
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(stringWriter));
|
||||
return stringWriter.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断异常是否由某些底层的异常引起.
|
||||
*/
|
||||
public static boolean isCausedBy(Exception ex, Class<? extends Exception>... causeExceptionClasses) {
|
||||
Throwable cause = ex.getCause();
|
||||
while (cause != null) {
|
||||
for (Class<? extends Exception> causeClass : causeExceptionClasses) {
|
||||
if (causeClass.isInstance(cause)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
cause = cause.getCause();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在request中获取异常类
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public static Throwable getThrowable(HttpServletRequest request) {
|
||||
Throwable ex = null;
|
||||
if (request.getAttribute("exception") != null) {
|
||||
ex = (Throwable) request.getAttribute("exception");
|
||||
} else if (request.getAttribute("javax.servlet.error.exception") != null) {
|
||||
ex = (Throwable) request.getAttribute("javax.servlet.error.exception");
|
||||
}
|
||||
return ex;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.yxt.demo.common.utils.allutils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/7/23 23:59
|
||||
* @Description
|
||||
*/
|
||||
@Slf4j
|
||||
public class RegexpUtils {
|
||||
}
|
||||
@@ -1,395 +0,0 @@
|
||||
package com.yxt.demo.common.utils.allutils;
|
||||
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author dimengzhe
|
||||
* @date 2020/9/18 9:35
|
||||
* @description
|
||||
*/
|
||||
|
||||
public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
||||
|
||||
private static final char SEPARATOR = '_';
|
||||
private static final String CHARSET_NAME = "UTF-8";
|
||||
|
||||
public static boolean isNull(Object obj) {
|
||||
return obj == null;
|
||||
}
|
||||
|
||||
public static boolean isNotNull(Object obj) {
|
||||
return !isNull(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为字节数组
|
||||
*
|
||||
* @param str 字符串
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getBytes(String str) {
|
||||
if (str != null) {
|
||||
try {
|
||||
return str.getBytes(CHARSET_NAME);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为字节数组
|
||||
*
|
||||
* @param bytes
|
||||
* @return
|
||||
*/
|
||||
public static String toString(byte[] bytes) {
|
||||
try {
|
||||
return new String(bytes, CHARSET_NAME);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否包含字符串
|
||||
*
|
||||
* @param str 验证字符串
|
||||
* @param strs 字符串组
|
||||
* @return 包含返回true
|
||||
*/
|
||||
public static boolean inString(String str, String... strs) {
|
||||
if (str != null) {
|
||||
for (String s : strs) {
|
||||
if (str.equals(trim(s))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换掉HTML标签方法
|
||||
*/
|
||||
public static String replaceHtml(String html) {
|
||||
if (isBlank(html)) {
|
||||
return "";
|
||||
}
|
||||
String regEx = "<.+?>";
|
||||
Pattern p = Pattern.compile(regEx);
|
||||
Matcher m = p.matcher(html);
|
||||
String s = m.replaceAll("");
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换为手机识别的HTML,去掉样式及属性,保留回车。
|
||||
*
|
||||
* @param html
|
||||
* @return
|
||||
*/
|
||||
public static String replaceMobileHtml(String html) {
|
||||
if (html == null) {
|
||||
return "";
|
||||
}
|
||||
return html.replaceAll("<([a-z]+?)\\s+?.*?>", "<$1>");
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换为手机识别的HTML,去掉样式及属性,保留回车。
|
||||
*
|
||||
* @param txt
|
||||
* @return
|
||||
*/
|
||||
public static String toHtml(String txt) {
|
||||
if (txt == null) {
|
||||
return "";
|
||||
}
|
||||
return replace(replace(Encodes.escapeHtml(txt), "\n", "<br/>"), "\t", " ");
|
||||
}
|
||||
|
||||
/**
|
||||
* 缩略字符串(不区分中英文字符)
|
||||
*
|
||||
* @param str 目标字符串
|
||||
* @param length 截取长度
|
||||
* @return
|
||||
*/
|
||||
public static String abbr(String str, int length) {
|
||||
if (str == null) {
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int currentLength = 0;
|
||||
for (char c : replaceHtml(StringEscapeUtils.unescapeHtml4(str)).toCharArray()) {
|
||||
currentLength += String.valueOf(c).getBytes("GBK").length;
|
||||
if (currentLength <= length - 3) {
|
||||
sb.append(c);
|
||||
} else {
|
||||
sb.append("...");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String abbr2(String param, int length) {
|
||||
if (param == null) {
|
||||
return "";
|
||||
}
|
||||
StringBuffer result = new StringBuffer();
|
||||
int n = 0;
|
||||
char temp;
|
||||
boolean isCode = false; // 是不是HTML代码
|
||||
boolean isHTML = false; // 是不是HTML特殊字符,如
|
||||
for (int i = 0; i < param.length(); i++) {
|
||||
temp = param.charAt(i);
|
||||
if (temp == '<') {
|
||||
isCode = true;
|
||||
} else if (temp == '&') {
|
||||
isHTML = true;
|
||||
} else if (temp == '>' && isCode) {
|
||||
n = n - 1;
|
||||
isCode = false;
|
||||
} else if (temp == ';' && isHTML) {
|
||||
isHTML = false;
|
||||
}
|
||||
try {
|
||||
if (!isCode && !isHTML) {
|
||||
n += String.valueOf(temp).getBytes("GBK").length;
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (n <= length - 3) {
|
||||
result.append(temp);
|
||||
} else {
|
||||
result.append("...");
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 取出截取字符串中的HTML标记
|
||||
String temp_result = result.toString().replaceAll("(>)[^<>]*(<?)", "$1$2");
|
||||
// 去掉不需要结素标记的HTML标记
|
||||
temp_result = temp_result.replaceAll(
|
||||
"</?(AREA|BASE|BASEFONT|BODY|BR|COL|COLGROUP|DD|DT|FRAME|HEAD|HR|HTML|IMG|INPUT|ISINDEX|LI|LINK|META|OPTION|P|PARAM|TBODY|TD|TFOOT|TH|THEAD|TR|area|base|basefont|body|br|col|colgroup|dd|dt|frame|head|hr|html|img|input|isindex|li|link|meta|option|p|param|tbody|td|tfoot|th|thead|tr)[^<>]*/?>",
|
||||
"");
|
||||
// 去掉成对的HTML标记
|
||||
temp_result = temp_result.replaceAll("<([a-zA-Z]+)[^<>]*>(.*?)</\\1>", "$2");
|
||||
// 用正则表达式取出标记
|
||||
Pattern p = Pattern.compile("<([a-zA-Z]+)[^<>]*>");
|
||||
Matcher m = p.matcher(temp_result);
|
||||
List<String> endHTML = new ArrayList<String>();
|
||||
while (m.find()) {
|
||||
endHTML.add(m.group(1));
|
||||
}
|
||||
// 补全不成对的HTML标记
|
||||
for (int i = endHTML.size() - 1; i >= 0; i--) {
|
||||
result.append("</");
|
||||
result.append(endHTML.get(i));
|
||||
result.append(">");
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为Double类型
|
||||
*/
|
||||
public static Double toDouble(Object val) {
|
||||
if (val == null) {
|
||||
return 0D;
|
||||
}
|
||||
try {
|
||||
return Double.valueOf(trim(val.toString()));
|
||||
} catch (Exception e) {
|
||||
return 0D;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为Float类型
|
||||
*/
|
||||
public static Float toFloat(Object val) {
|
||||
return toDouble(val).floatValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为Long类型
|
||||
*/
|
||||
public static Long toLong(Object val) {
|
||||
return toDouble(val).longValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为Integer类型
|
||||
*/
|
||||
public static Integer toInteger(Object val) {
|
||||
return toLong(val).intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为BigDecimal类型
|
||||
*/
|
||||
public static BigDecimal toBigDecimal(String val) {
|
||||
if (StringUtils.isBlank(val)) {
|
||||
return null;
|
||||
}
|
||||
BigDecimal bd = new BigDecimal(val);
|
||||
return bd;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得用户远程地址
|
||||
*/
|
||||
public static String getRemoteAddr(HttpServletRequest request) {
|
||||
String remoteAddr = request.getHeader("X-Real-IP");
|
||||
if (isNotBlank(remoteAddr)) {
|
||||
remoteAddr = request.getHeader("X-Forwarded-For");
|
||||
} else if (isNotBlank(remoteAddr)) {
|
||||
remoteAddr = request.getHeader("Proxy-Client-IP");
|
||||
} else if (isNotBlank(remoteAddr)) {
|
||||
remoteAddr = request.getHeader("WL-Proxy-Client-IP");
|
||||
}
|
||||
return remoteAddr != null ? remoteAddr : request.getRemoteAddr();
|
||||
}
|
||||
|
||||
/**
|
||||
* 驼峰命名法工具
|
||||
*
|
||||
* @return toCamelCase(" hello_world ") == "helloWorld"
|
||||
* toCapitalizeCamelCase("hello_world") == "HelloWorld"
|
||||
* toUnderScoreCase("helloWorld") = "hello_world"
|
||||
*/
|
||||
public static String toCamelCase(String s) {
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
s = s.toLowerCase();
|
||||
|
||||
StringBuilder sb = new StringBuilder(s.length());
|
||||
boolean upperCase = false;
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (c == SEPARATOR) {
|
||||
upperCase = true;
|
||||
} else if (upperCase) {
|
||||
sb.append(Character.toUpperCase(c));
|
||||
upperCase = false;
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 驼峰命名法工具
|
||||
*
|
||||
* @return toCamelCase(" hello_world ") == "helloWorld"
|
||||
* toCapitalizeCamelCase("hello_world") == "HelloWorld"
|
||||
* toUnderScoreCase("helloWorld") = "hello_world"
|
||||
*/
|
||||
public static String toCapitalizeCamelCase(String s) {
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
s = toCamelCase(s);
|
||||
return s.substring(0, 1).toUpperCase() + s.substring(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 驼峰命名法工具
|
||||
*
|
||||
* @return toCamelCase(" hello_world ") == "helloWorld"
|
||||
* toCapitalizeCamelCase("hello_world") == "HelloWorld"
|
||||
* toUnderScoreCase("helloWorld") = "hello_world"
|
||||
*/
|
||||
public static String toUnderScoreCase(String s) {
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
boolean upperCase = false;
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
|
||||
boolean nextUpperCase = true;
|
||||
|
||||
if (i < (s.length() - 1)) {
|
||||
nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
|
||||
}
|
||||
|
||||
if ((i > 0) && Character.isUpperCase(c)) {
|
||||
if (!upperCase || !nextUpperCase) {
|
||||
sb.append(SEPARATOR);
|
||||
}
|
||||
upperCase = true;
|
||||
} else {
|
||||
upperCase = false;
|
||||
}
|
||||
|
||||
sb.append(Character.toLowerCase(c));
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果不为空,则设置值
|
||||
*
|
||||
* @param target
|
||||
* @param source
|
||||
*/
|
||||
public static void setValueIfNotBlank(String target, String source) {
|
||||
if (isNotBlank(source)) {
|
||||
target = source;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为JS获取对象值,生成三目运算返回结果
|
||||
*
|
||||
* @param objectString 对象串 例如:row.user.id
|
||||
* 返回:!row?'':!row.user?'':!row.user.id?'':row.user.id
|
||||
*/
|
||||
public static String jsGetVal(String objectString) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
StringBuilder val = new StringBuilder();
|
||||
String[] vals = split(objectString, ".");
|
||||
for (int i = 0; i < vals.length; i++) {
|
||||
val.append("." + vals[i]);
|
||||
result.append("!" + (val.substring(1)) + "?'':");
|
||||
}
|
||||
result.append(val.substring(1));
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static byte[] getBytesUTF8(CharSequence cs) {
|
||||
try {
|
||||
return cs.toString().getBytes("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw Exceptions.unchecked(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.yxt.demo.common.utils.captcha;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2021/11/6 21:58
|
||||
* @Description
|
||||
*/
|
||||
public class ImageUtils {
|
||||
|
||||
|
||||
}
|
||||
@@ -1,197 +0,0 @@
|
||||
package com.yxt.demo.common.utils.convert;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2022/7/23 23:38
|
||||
* @Description
|
||||
*/
|
||||
public class StringUtil extends StringUtils {
|
||||
|
||||
/**
|
||||
* 空字符串
|
||||
*/
|
||||
private static final String NULLSTR = "";
|
||||
|
||||
/**
|
||||
* 下划线
|
||||
*/
|
||||
private static final char SEPARATOR = '_';
|
||||
|
||||
/**
|
||||
* 星号
|
||||
*/
|
||||
private static final String START = "*";
|
||||
|
||||
/**
|
||||
* * 判断一个Collection是否为空, 包含List,Set,Queue
|
||||
*
|
||||
* @param coll 要判断的Collection
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
public static boolean isEmpty(Collection<?> coll) {
|
||||
return isNull(coll) || coll.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* * 判断一个对象是否为空
|
||||
*
|
||||
* @param object Object
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
public static boolean isNull(Object object) {
|
||||
return object == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
|
||||
*
|
||||
* @param str 指定字符串
|
||||
* @param strs 需要检查的字符串数组
|
||||
* @return 是否匹配
|
||||
*/
|
||||
public static boolean matchesTwo(String str, List<String> strs) {
|
||||
if (isEmpty(str) || isEmpty(strs)) {
|
||||
return false;
|
||||
}
|
||||
for (String testStr : strs) {
|
||||
if (matchesTwo(str, testStr)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean matches(String str, List<String> strs) {
|
||||
if (isEmpty(str) || isEmpty(strs)) {
|
||||
return false;
|
||||
}
|
||||
for (String testStr : strs) {
|
||||
if (matches(str, testStr)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean matches(String str, String pattern) {
|
||||
if (isEmpty(pattern) || isEmpty(str)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
pattern = pattern.replaceAll("\\s*", ""); // 替换空格
|
||||
int beginOffset = 0; // pattern截取开始位置
|
||||
int formerStarOffset = -1; // 前星号的偏移位置
|
||||
int latterStarOffset = -1; // 后星号的偏移位置
|
||||
|
||||
String remainingURI = str;
|
||||
String prefixPattern = "";
|
||||
String suffixPattern = "";
|
||||
|
||||
boolean result = false;
|
||||
do {
|
||||
formerStarOffset = indexOf(pattern, START, beginOffset);
|
||||
prefixPattern = substring(pattern, beginOffset, formerStarOffset > -1 ? formerStarOffset : pattern.length());
|
||||
|
||||
// 匹配前缀Pattern
|
||||
result = remainingURI.equals(prefixPattern);
|
||||
// 已经没有星号,直接返回
|
||||
if (formerStarOffset == -1) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 匹配失败,直接返回
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
if (!isEmpty(prefixPattern)) {
|
||||
remainingURI = substringAfter(str, prefixPattern);
|
||||
}
|
||||
|
||||
// 匹配后缀Pattern
|
||||
latterStarOffset = indexOf(pattern, START, formerStarOffset + 1);
|
||||
suffixPattern = substring(pattern, formerStarOffset + 1, latterStarOffset > -1 ? latterStarOffset : pattern.length());
|
||||
|
||||
result = remainingURI.equals(suffixPattern);
|
||||
// 匹配失败,直接返回
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
if (!isEmpty(suffixPattern)) {
|
||||
remainingURI = substringAfter(str, suffixPattern);
|
||||
}
|
||||
|
||||
// 移动指针
|
||||
beginOffset = latterStarOffset + 1;
|
||||
|
||||
}
|
||||
while (!isEmpty(suffixPattern) && !isEmpty(remainingURI));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找指定字符串是否匹配
|
||||
*
|
||||
* @param str 指定字符串
|
||||
* @param pattern 需要检查的字符串
|
||||
* @return 是否匹配
|
||||
*/
|
||||
public static boolean matchesTwo(String str, String pattern) {
|
||||
if (isEmpty(pattern) || isEmpty(str)) {
|
||||
return false;
|
||||
}
|
||||
pattern = pattern.replaceAll("\\s*", ""); // 替换空格
|
||||
int beginOffset = 0; // pattern截取开始位置
|
||||
int formerStarOffset = -1; // 前星号的偏移位置
|
||||
int latterStarOffset = -1; // 后星号的偏移位置
|
||||
|
||||
String remainingURI = str;
|
||||
String prefixPattern = "";
|
||||
String suffixPattern = "";
|
||||
|
||||
boolean result = false;
|
||||
do {
|
||||
formerStarOffset = indexOf(pattern, START, beginOffset);
|
||||
prefixPattern = substring(pattern, beginOffset, formerStarOffset > -1 ? formerStarOffset : pattern.length());
|
||||
|
||||
// 匹配前缀Pattern
|
||||
result = remainingURI.contains(prefixPattern);
|
||||
// 已经没有星号,直接返回
|
||||
if (formerStarOffset == -1) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// 匹配失败,直接返回
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
if (!isEmpty(prefixPattern)) {
|
||||
remainingURI = substringAfter(str, prefixPattern);
|
||||
}
|
||||
|
||||
// 匹配后缀Pattern
|
||||
latterStarOffset = indexOf(pattern, START, formerStarOffset + 1);
|
||||
suffixPattern = substring(pattern, formerStarOffset + 1, latterStarOffset > -1 ? latterStarOffset : pattern.length());
|
||||
|
||||
result = remainingURI.contains(suffixPattern);
|
||||
// 匹配失败,直接返回
|
||||
if (!result) {
|
||||
return false;
|
||||
}
|
||||
if (!isEmpty(suffixPattern)) {
|
||||
remainingURI = substringAfter(str, suffixPattern);
|
||||
}
|
||||
|
||||
// 移动指针
|
||||
beginOffset = latterStarOffset + 1;
|
||||
}
|
||||
while (!isEmpty(suffixPattern) && !isEmpty(remainingURI));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package com.yxt.demo.common.utils.date;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2021/11/7 11:58
|
||||
* @Description 关于日期处理工具类
|
||||
*/
|
||||
public class DateUtils {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
//时间格式字符串转换成Date类型
|
||||
Date date = dateStrConvertDateCst("2021-11-07 12:28:20", "yyyy-MM-dd HH:mm:ss");
|
||||
//获取两个时间间隔的x天x小时x分钟
|
||||
System.out.println(getDatePoor(date, new Date()));
|
||||
//将Date类型转换成字符串类型
|
||||
System.out.println(dateConvertStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取两个时间间隔的x天x小时x分钟
|
||||
*
|
||||
* @param endDate 结尾时间
|
||||
* @param nowDate 当前时间
|
||||
* @return
|
||||
*/
|
||||
public static String getDatePoor(Date endDate, Date nowDate) {
|
||||
long nd = 1000 * 24 * 60 * 60;
|
||||
long nh = 1000 * 60 * 60;
|
||||
long nm = 1000 * 60;
|
||||
long ns = 1000;
|
||||
// 获得两个时间的毫秒时间差异
|
||||
long diff = endDate.getTime() - nowDate.getTime();
|
||||
// 计算差多少天
|
||||
long day = diff / nd;
|
||||
// 计算差多少小时
|
||||
long hour = diff % nd / nh;
|
||||
// 计算差多少分钟
|
||||
long min = diff % nd % nh / nm;
|
||||
// 计算差多少秒
|
||||
long s = diff % nd % nh % nm / ns;
|
||||
// 输出结果
|
||||
return day + "天" + hour + "小时" + min + "分钟" + s + "秒";
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间格式字符串转换成Date类型
|
||||
*
|
||||
* @param date 时间字符串
|
||||
* @param format 如:yyyy-MM-dd HH:mm:ss
|
||||
* @return
|
||||
*/
|
||||
public static Date dateStrConvertDateCst(String date, String format) {
|
||||
try {
|
||||
SimpleDateFormat simpledateformat = new SimpleDateFormat(format,
|
||||
Locale.US);
|
||||
Date newDate = simpledateformat.parse(date);
|
||||
return newDate;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 将Date类型转换成字符串类型
|
||||
*
|
||||
* @param date 时间
|
||||
* @param format 格式
|
||||
* @return
|
||||
*/
|
||||
public static String dateConvertStr(Date date, String format) {
|
||||
return (date == null) ? null : new SimpleDateFormat(format)
|
||||
.format(date);
|
||||
}
|
||||
|
||||
public static final String parseDateToStr(final String format, final Date date) {
|
||||
return new SimpleDateFormat(format).format(date);
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
package com.yxt.demo.common.utils.date;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2021/11/6 22:52
|
||||
* @Description java 8 日期工具类再封装
|
||||
*/
|
||||
public class LocalDateUtils {
|
||||
|
||||
/**
|
||||
* 比较第一个日期是否小于第二个日期
|
||||
*
|
||||
* @param firstDate 第一个日期
|
||||
* @param secondDate 第二个日期
|
||||
* @return true-小于;false-大于
|
||||
*/
|
||||
public static boolean localDateIsBefore(LocalDate firstDate, LocalDate secondDate) {
|
||||
return firstDate.isBefore(secondDate);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 比较第一个日期是否大于第二个日期
|
||||
*
|
||||
* @param firstDate 第一个日期
|
||||
* @param secondDate 第二个日期
|
||||
* @return true-大于;false-不大于
|
||||
*/
|
||||
public static boolean localDateIsAfter(LocalDate firstDate, LocalDate secondDate) {
|
||||
return firstDate.isAfter(secondDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 比较两个日期是否相等
|
||||
*
|
||||
* @param firstDate 第一个日期
|
||||
* @param secondDate 第二个日期
|
||||
* @return true-相等;false-不相等
|
||||
*/
|
||||
public static boolean localDateIsEqual(LocalDate firstDate, LocalDate secondDate) {
|
||||
return firstDate.isEqual(secondDate);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 字符串转换datetime
|
||||
*
|
||||
* @param dateTime
|
||||
* @return yyyy-MM-dd HH:mm:ss
|
||||
*/
|
||||
public static Date stringCoverDateTime(String dateTime) {
|
||||
LocalDateTime startDateTime =
|
||||
LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
||||
Date LocalDateTimeToDate = Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
||||
return LocalDateTimeToDate;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 字符串转换date
|
||||
*
|
||||
* @param dateTime
|
||||
* @return yyyy-MM-dd
|
||||
*/
|
||||
public static Date stringCoverDate(String dateTime) {
|
||||
LocalDateTime startDateTime =
|
||||
LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||
Date LocalDateTimeToDate = Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
||||
return LocalDateTimeToDate;
|
||||
}
|
||||
}
|
||||
@@ -1,345 +0,0 @@
|
||||
package com.yxt.demo.common.utils.http;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import okhttp3.*;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2021/11/5 20:57
|
||||
* @Description 封装OkHttp3工具类
|
||||
*/
|
||||
public class OkHttpUtils {
|
||||
|
||||
private static volatile OkHttpClient okHttpClient = null;
|
||||
private static volatile Semaphore semaphore = null;
|
||||
private Map<String, String> headerMap;
|
||||
private Map<String, String> paramMap;
|
||||
private String url;
|
||||
private Request.Builder request;
|
||||
|
||||
/**
|
||||
* 初始化okHttpClient,并且允许https访问
|
||||
*/
|
||||
private OkHttpUtils() {
|
||||
if (okHttpClient == null) {
|
||||
synchronized (OkHttpUtils.class) {
|
||||
if (okHttpClient == null) {
|
||||
TrustManager[] trustManagers = buildTrustManagers();
|
||||
okHttpClient = new OkHttpClient.Builder()
|
||||
.connectTimeout(15, TimeUnit.SECONDS)
|
||||
.writeTimeout(20, TimeUnit.SECONDS)
|
||||
.readTimeout(20, TimeUnit.SECONDS)
|
||||
.sslSocketFactory(createSSLSocketFactory(trustManagers), (X509TrustManager) trustManagers[0])
|
||||
.hostnameVerifier((hostName, session) -> true)
|
||||
.retryOnConnectionFailure(true)
|
||||
.build();
|
||||
addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于异步请求时,控制访问线程数,返回结果
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static Semaphore getSemaphoreInstance() {
|
||||
//只能1个线程同时访问
|
||||
synchronized (OkHttpUtils.class) {
|
||||
if (semaphore == null) {
|
||||
semaphore = new Semaphore(0);
|
||||
}
|
||||
}
|
||||
return semaphore;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建OkHttpUtils
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static OkHttpUtils builder() {
|
||||
return new OkHttpUtils();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加url
|
||||
*
|
||||
* @param url
|
||||
* @return
|
||||
*/
|
||||
public OkHttpUtils url(String url) {
|
||||
this.url = url;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加参数
|
||||
*
|
||||
* @param key 参数名
|
||||
* @param value 参数值
|
||||
* @return
|
||||
*/
|
||||
public OkHttpUtils addParam(String key, String value) {
|
||||
if (paramMap == null) {
|
||||
paramMap = new LinkedHashMap<>(16);
|
||||
}
|
||||
paramMap.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加请求头
|
||||
*
|
||||
* @param key 参数名
|
||||
* @param value 参数值
|
||||
* @return
|
||||
*/
|
||||
public OkHttpUtils addHeader(String key, String value) {
|
||||
if (headerMap == null) {
|
||||
headerMap = new LinkedHashMap<>(16);
|
||||
}
|
||||
headerMap.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化get方法
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public OkHttpUtils get() {
|
||||
request = new Request.Builder().get();
|
||||
StringBuilder urlBuilder = new StringBuilder(url);
|
||||
if (paramMap != null) {
|
||||
urlBuilder.append("?");
|
||||
try {
|
||||
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
|
||||
urlBuilder.append(URLEncoder.encode(entry.getKey(), "utf-8")).
|
||||
append("=").
|
||||
append(URLEncoder.encode(entry.getValue(), "utf-8")).
|
||||
append("&");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
urlBuilder.deleteCharAt(urlBuilder.length() - 1);
|
||||
}
|
||||
request.url(urlBuilder.toString());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化post方法
|
||||
*
|
||||
* @param isJsonPost true等于json的方式提交数据,类似postman里post方法的raw
|
||||
* false等于普通的表单提交
|
||||
* @return
|
||||
*/
|
||||
public OkHttpUtils post(boolean isJsonPost) {
|
||||
RequestBody requestBody;
|
||||
if (isJsonPost) {
|
||||
String json = "";
|
||||
if (paramMap != null) {
|
||||
json = JSON.toJSONString(paramMap);
|
||||
}
|
||||
requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
|
||||
} else {
|
||||
FormBody.Builder formBody = new FormBody.Builder();
|
||||
if (paramMap != null) {
|
||||
paramMap.forEach(formBody::add);
|
||||
}
|
||||
requestBody = formBody.build();
|
||||
}
|
||||
request = new Request.Builder().post(requestBody).url(url);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步请求
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String sync() {
|
||||
setHeader(request);
|
||||
try {
|
||||
Response response = okHttpClient.newCall(request.build()).execute();
|
||||
assert response.body() != null;
|
||||
return response.body().string();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return "请求失败:" + e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步请求,有返回值
|
||||
*/
|
||||
public String async() {
|
||||
StringBuilder buffer = new StringBuilder("");
|
||||
setHeader(request);
|
||||
okHttpClient.newCall(request.build()).enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(Call call, IOException e) {
|
||||
buffer.append("请求出错:").append(e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Call call, Response response) throws IOException {
|
||||
assert response.body() != null;
|
||||
buffer.append(response.body().string());
|
||||
getSemaphoreInstance().release();
|
||||
}
|
||||
});
|
||||
try {
|
||||
getSemaphoreInstance().acquire();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步请求,带有接口回调
|
||||
*
|
||||
* @param callBack
|
||||
*/
|
||||
public void async(ICallBack callBack) {
|
||||
setHeader(request);
|
||||
okHttpClient.newCall(request.build()).enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(Call call, IOException e) {
|
||||
callBack.onFailure(call, e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(Call call, Response response) throws IOException {
|
||||
assert response.body() != null;
|
||||
callBack.onSuccessful(call, response.body().string());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 为request添加请求头
|
||||
*
|
||||
* @param request
|
||||
*/
|
||||
private void setHeader(Request.Builder request) {
|
||||
if (headerMap != null) {
|
||||
try {
|
||||
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
|
||||
request.addHeader(entry.getKey(), entry.getValue());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 生成安全套接字工厂,用于https请求的证书跳过
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static SSLSocketFactory createSSLSocketFactory(TrustManager[] trustAllCerts) {
|
||||
SSLSocketFactory ssfFactory = null;
|
||||
try {
|
||||
SSLContext sc = SSLContext.getInstance("SSL");
|
||||
sc.init(null, trustAllCerts, new SecureRandom());
|
||||
ssfFactory = sc.getSocketFactory();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ssfFactory;
|
||||
}
|
||||
|
||||
private static TrustManager[] buildTrustManagers() {
|
||||
return new TrustManager[]{
|
||||
new X509TrustManager() {
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return new X509Certificate[]{};
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义一个接口回调
|
||||
*/
|
||||
public interface ICallBack {
|
||||
void onSuccessful(Call call, String data);
|
||||
|
||||
void onFailure(Call call, String errorMsg);
|
||||
}
|
||||
|
||||
/*****************************使用教程************************************************************/
|
||||
|
||||
public static void main(String[] args) {
|
||||
// get请求,方法顺序按照这种方式,切记选择post/get一定要放在倒数第二,同步或者异步倒数第一,才会正确执行
|
||||
OkHttpUtils.builder().url("请求地址,http/https都可以")
|
||||
// 有参数的话添加参数,可多个
|
||||
.addParam("参数名", "参数值")
|
||||
.addParam("参数名", "参数值")
|
||||
// 也可以添加多个
|
||||
.addHeader("Content-Type", "application/json; charset=utf-8")
|
||||
.get()
|
||||
// 可选择是同步请求还是异步请求
|
||||
//.async();
|
||||
.sync();
|
||||
|
||||
// post请求,分为两种,一种是普通表单提交,一种是json提交
|
||||
OkHttpUtils.builder().url("请求地址,http/https都可以")
|
||||
// 有参数的话添加参数,可多个
|
||||
.addParam("参数名", "参数值")
|
||||
.addParam("参数名", "参数值")
|
||||
// 也可以添加多个
|
||||
.addHeader("Content-Type", "application/json; charset=utf-8")
|
||||
// 如果是true的话,会类似于postman中post提交方式的raw,用json的方式提交,不是表单
|
||||
// 如果是false的话传统的表单提交
|
||||
.post(true)
|
||||
.sync();
|
||||
|
||||
// 选择异步有两个方法,一个是带回调接口,一个是直接返回结果
|
||||
OkHttpUtils.builder().url("")
|
||||
.post(false)
|
||||
.async();
|
||||
|
||||
OkHttpUtils.builder().url("").post(false).async(new ICallBack() {
|
||||
@Override
|
||||
public void onSuccessful(Call call, String data) {
|
||||
// 请求成功后的处理
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call call, String errorMsg) {
|
||||
// 请求失败后的处理
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package com.yxt.demo.common.utils.jwt;
|
||||
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@Slf4j
|
||||
public class JWTUtil {
|
||||
|
||||
private static final String TOKEN_SECRET = "yXtJLzxh2bGciO5iJIUzI1NiJ9";
|
||||
private static final String ISS = "WBK";
|
||||
private static final String USERNO = "userNo";
|
||||
private static final Long TIME = 24 * 3600 * 1000L; // 1天
|
||||
|
||||
//创建Token
|
||||
public static String create(String userNo) {
|
||||
try {
|
||||
return JWT
|
||||
.create()
|
||||
.withIssuer(ISS)
|
||||
.withClaim(USERNO, userNo)
|
||||
.withExpiresAt(new Date(System.currentTimeMillis() + TIME))
|
||||
.sign(Algorithm.HMAC256(TOKEN_SECRET));
|
||||
} catch (Exception e) {
|
||||
log.error("JWT生成失败", e);
|
||||
return e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
//解析Token
|
||||
public static DecodedJWT verify(String token) throws Exception {
|
||||
return JWT
|
||||
.require(Algorithm.HMAC256(TOKEN_SECRET))
|
||||
.withIssuer(ISS)
|
||||
.build()
|
||||
.verify(token);
|
||||
}
|
||||
|
||||
//根据解析生成的Token返回userNo
|
||||
public static Long getUserNo(DecodedJWT decodedJWT) {
|
||||
return Long.parseLong(decodedJWT.getClaim(USERNO).asString());
|
||||
}
|
||||
|
||||
public static String getUserSid(DecodedJWT decodedJWT) {
|
||||
return decodedJWT.getClaim(USERNO).asString();
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package com.yxt.demo.common.utils.jwt;
|
||||
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author dimengzhe
|
||||
* @Date 2021/11/6 22:55
|
||||
* @Description
|
||||
*/
|
||||
public class JwtUtils {
|
||||
|
||||
/**
|
||||
* 签名密钥(高度保密)
|
||||
*/
|
||||
private static final String SECRET = "qYYjXt7s1C*nEC%9RCwQGFA$YwPr$Jrj";
|
||||
/**
|
||||
* 签名算法
|
||||
*/
|
||||
private static final SignatureAlgorithm SIGNATURE_ALGORITHM = SignatureAlgorithm.HS512;
|
||||
/**
|
||||
* token前缀
|
||||
*/
|
||||
private static final String TOKEN_PREFIX = "Bearer ";
|
||||
/**
|
||||
* 有效期:1天
|
||||
*/
|
||||
private static final Long TIME = 24 * 3600 * 1000L;
|
||||
|
||||
/**
|
||||
* 生成JWT token
|
||||
*
|
||||
* @param map 传入数据
|
||||
* @return
|
||||
*/
|
||||
public static String sign(Map<String, Object> map) {
|
||||
Date now = new Date(System.currentTimeMillis());
|
||||
String jwt = Jwts.builder()
|
||||
// 设置自定义数据
|
||||
.setClaims(map)
|
||||
// 设置签发时间
|
||||
.setIssuedAt(now)
|
||||
// 设置过期时间
|
||||
.setExpiration(new Date(now.getTime() + TIME))
|
||||
.signWith(SIGNATURE_ALGORITHM, SECRET)
|
||||
.compact();
|
||||
return TOKEN_PREFIX + jwt;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证JWT token并返回数据。当验证失败时,抛出异常
|
||||
*
|
||||
* @param token token
|
||||
* @return
|
||||
*/
|
||||
public static Map unSign(String token) {
|
||||
try {
|
||||
return Jwts.parser()
|
||||
.setSigningKey(SECRET)
|
||||
.parseClaimsJws(token.replace(TOKEN_PREFIX, ""))
|
||||
.getBody();
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("Token验证失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Map<String, Object> map = new HashMap<>();
|
||||
map.put("userName", "admin");
|
||||
map.put("userId", "001");
|
||||
String token = JwtUtils.sign(map, 3600_000);
|
||||
System.out.println(JwtUtils.unSign(token));*/
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("userSid", "123456788");
|
||||
map.put("userName", "你好");
|
||||
// sign(map);
|
||||
// System.out.println(sign(map));
|
||||
Map<String,Object> map1 = unSign("Bearer eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyU2lkIjoiMTIzNDU2Nzg4IiwidXNlck5hbWUiOiLkvaDlpb0iLCJleHAiOjE2MzYyOTk0NTgsImlhdCI6MTYzNjIxMzA1OH0.iuyZznSCm0QYneqfck_zc3fpg57YsAdG8k2aLrDY_4NZJwJdVxE7supqLtfEvTC5O0EhG590ShhRsVoU-rbSrA");
|
||||
System.out.println(map1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user