From 0b3d3960943fe040dac0f620ba80a7d0b8531807 Mon Sep 17 00:00:00 2001 From: suchu Date: Wed, 10 Jul 2019 19:25:28 +0800 Subject: [PATCH] First commit --- .gitignore | 11 +++++++++++ README.md | 0 fw-rp-common/pom.xml | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-common/src/main/java/cn/fw/rp/common/constant/Constant.java | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-common/src/main/java/cn/fw/rp/common/enums/FileTypeEnums.java | 46 ++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-common/src/main/java/cn/fw/rp/common/util/AesUtil.java | 208 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-common/src/main/java/cn/fw/rp/common/util/DateUtils.java | 725 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-common/src/main/java/cn/fw/rp/common/util/PublicUtil.java | 591 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-common/src/main/java/cn/fw/rp/common/util/QRCodeKit.java | 290 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-common/src/main/java/cn/fw/rp/common/util/StringUtils.java | 488 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-dao/pom.xml | 37 +++++++++++++++++++++++++++++++++++++ fw-rp-dao/src/main/java/cn/fw/rp/dao/FileListDao.java | 15 +++++++++++++++ fw-rp-dao/src/main/java/cn/fw/rp/dao/ProblemFileDao.java | 14 ++++++++++++++ fw-rp-dao/src/main/java/cn/fw/rp/dao/ProblemListDao.java | 21 +++++++++++++++++++++ fw-rp-dao/src/main/resources/mapper/FileList.xml | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-dao/src/main/resources/mapper/ProblemFile.xml | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-dao/src/main/resources/mapper/ProblemList.xml | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-dao/src/main/resources/mapper/core.xml | 359 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-domain/pom.xml | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-domain/src/main/java/cn/fw/rp/annotation/EnumValid.java | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-domain/src/main/java/cn/fw/rp/db/FileList.java | 33 +++++++++++++++++++++++++++++++++ fw-rp-domain/src/main/java/cn/fw/rp/db/ProblemFile.java | 17 +++++++++++++++++ fw-rp-domain/src/main/java/cn/fw/rp/db/ProblemList.java | 42 ++++++++++++++++++++++++++++++++++++++++++ fw-rp-domain/src/main/java/cn/fw/rp/man/ProblemDto.java | 39 +++++++++++++++++++++++++++++++++++++++ fw-rp-domain/src/main/java/cn/fw/rp/man/ProblemQueryParam.java | 19 +++++++++++++++++++ fw-rp-man/pom.xml | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-man/src/main/java/cn/fw/rp/web/RpManServer.java | 25 +++++++++++++++++++++++++ fw-rp-man/src/main/java/cn/fw/rp/web/controller/FileController.java | 287 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-man/src/main/java/cn/fw/rp/web/controller/LoginController.java | 19 +++++++++++++++++++ fw-rp-man/src/main/java/cn/fw/rp/web/controller/ProblemController.java | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-man/src/main/resources/application-dev.yml | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-man/src/main/resources/application-local.yml | 36 ++++++++++++++++++++++++++++++++++++ fw-rp-man/src/main/resources/application-prd.yml | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-man/src/main/resources/application-test.yml | 44 ++++++++++++++++++++++++++++++++++++++++++++ fw-rp-man/src/main/resources/application.yml | 3 +++ fw-rp-man/src/main/resources/banner.txt | 6 ++++++ fw-rp-man/src/main/resources/logback-spring.xml | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-man/src/test/java/cn/fw/rp/web/MockMvcRequestBuilderUtils.java | 231 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-sdk/pom.xml | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fw-rp-service/pom.xml | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pom.xml | 218 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 41 files changed, 5092 insertions(+), 0 deletions(-) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 fw-rp-common/pom.xml create mode 100644 fw-rp-common/src/main/java/cn/fw/rp/common/constant/Constant.java create mode 100644 fw-rp-common/src/main/java/cn/fw/rp/common/enums/FileTypeEnums.java create mode 100644 fw-rp-common/src/main/java/cn/fw/rp/common/util/AesUtil.java create mode 100644 fw-rp-common/src/main/java/cn/fw/rp/common/util/DateUtils.java create mode 100644 fw-rp-common/src/main/java/cn/fw/rp/common/util/PublicUtil.java create mode 100644 fw-rp-common/src/main/java/cn/fw/rp/common/util/QRCodeKit.java create mode 100644 fw-rp-common/src/main/java/cn/fw/rp/common/util/StringUtils.java create mode 100644 fw-rp-dao/pom.xml create mode 100644 fw-rp-dao/src/main/java/cn/fw/rp/dao/FileListDao.java create mode 100644 fw-rp-dao/src/main/java/cn/fw/rp/dao/ProblemFileDao.java create mode 100644 fw-rp-dao/src/main/java/cn/fw/rp/dao/ProblemListDao.java create mode 100644 fw-rp-dao/src/main/resources/mapper/FileList.xml create mode 100644 fw-rp-dao/src/main/resources/mapper/ProblemFile.xml create mode 100644 fw-rp-dao/src/main/resources/mapper/ProblemList.xml create mode 100644 fw-rp-dao/src/main/resources/mapper/core.xml create mode 100644 fw-rp-domain/pom.xml create mode 100644 fw-rp-domain/src/main/java/cn/fw/rp/annotation/EnumValid.java create mode 100644 fw-rp-domain/src/main/java/cn/fw/rp/db/FileList.java create mode 100644 fw-rp-domain/src/main/java/cn/fw/rp/db/ProblemFile.java create mode 100644 fw-rp-domain/src/main/java/cn/fw/rp/db/ProblemList.java create mode 100644 fw-rp-domain/src/main/java/cn/fw/rp/man/ProblemDto.java create mode 100644 fw-rp-domain/src/main/java/cn/fw/rp/man/ProblemQueryParam.java create mode 100644 fw-rp-man/pom.xml create mode 100644 fw-rp-man/src/main/java/cn/fw/rp/web/RpManServer.java create mode 100644 fw-rp-man/src/main/java/cn/fw/rp/web/controller/FileController.java create mode 100644 fw-rp-man/src/main/java/cn/fw/rp/web/controller/LoginController.java create mode 100644 fw-rp-man/src/main/java/cn/fw/rp/web/controller/ProblemController.java create mode 100644 fw-rp-man/src/main/resources/application-dev.yml create mode 100644 fw-rp-man/src/main/resources/application-local.yml create mode 100644 fw-rp-man/src/main/resources/application-prd.yml create mode 100644 fw-rp-man/src/main/resources/application-test.yml create mode 100644 fw-rp-man/src/main/resources/application.yml create mode 100644 fw-rp-man/src/main/resources/banner.txt create mode 100644 fw-rp-man/src/main/resources/logback-spring.xml create mode 100644 fw-rp-man/src/test/java/cn/fw/rp/web/MockMvcRequestBuilderUtils.java create mode 100644 fw-rp-sdk/pom.xml create mode 100644 fw-rp-service/pom.xml create mode 100644 pom.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f9de1b9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +# Created by .ignore support plugin (hsz.mobi) +### Example user template template +### Example user template + +# IntelliJ project files +.idea +*.iml +out +gen +*.log +/*/target/* \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/README.md diff --git a/fw-rp-common/pom.xml b/fw-rp-common/pom.xml new file mode 100644 index 0000000..753282f --- /dev/null +++ b/fw-rp-common/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + + + cn.fw + fw-rp + 1.0 + + + fw-rp-common + jar + fw-rp-common + + + + + org.slf4j + slf4j-api + 1.6.4 + + + + + com.google.zxing + core + 2.3.0 + + + + + net.sf.json-lib + json-lib + jdk15 + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.3.0 + + + net.sf.json-lib + json-lib + jdk15 + 2.3 + + + com.fasterxml.jackson.core + jackson-databind + + + commons-codec + commons-codec + + + \ No newline at end of file diff --git a/fw-rp-common/src/main/java/cn/fw/rp/common/constant/Constant.java b/fw-rp-common/src/main/java/cn/fw/rp/common/constant/Constant.java new file mode 100644 index 0000000..648fecf --- /dev/null +++ b/fw-rp-common/src/main/java/cn/fw/rp/common/constant/Constant.java @@ -0,0 +1,67 @@ +package cn.fw.rp.common.constant; + +/** + * @author 张志伟 + * @date 2018-01-27 14:19 + * @description + */ +public class Constant { + + /** + * 产品名称:云通信隐私保护产品,开发者无需替换 + */ + public static final String PRODUCT = "Dyplsapi"; + /** + * 营销活动消息通知 topic + */ + public static final String MKT_PUBLISH_TITLE = "MKT_WS_MESSAGE"; + /** + * 产品域名,开发者无需替换 + */ + public static final String DOMAIN = "dyplsapi.aliyuncs.com"; + /** + * 访问ID + */ + public static final String ACCESS_KEY_ID = "LTAIU865lo41xCTI"; + /** + * 访问ID对应的KEY + */ + public static final String ACCESS_KEY_SECRET = "C5ilXwesfPV9KtF1V74IQsaXMgR4wC"; + /** + * 更新类型 + */ + public static final String A = "updateNoA"; + public static final String B = "updateNoB"; + public static final String T = "updateExpire"; + /** + * 此处替换成您所需的消息类型名称 + */ + public static final String MESSAGE_TYPE = "SecretReport"; + /** + * 在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName + */ + public static final String QUEUE_NAME = "Alicom-Queue-1294201731075657-SecretReport"; + /** + * 成功的返回码 + */ + public static final String SUCCESS = "OK"; + + /** + * websocket config + */ + public static class WebSocketConfig { + /** + * ws endpoint + */ + public static final String WS_ENDPOINT = "marketing"; + /** + * app destination prefix + */ + public static final String APP_DESTINATION_PREFIX = "/app"; + /** + * simple broker destination prefix + */ + public static final String BROKER_DESTINATION_PREFIX = "/activity"; + + } +} diff --git a/fw-rp-common/src/main/java/cn/fw/rp/common/enums/FileTypeEnums.java b/fw-rp-common/src/main/java/cn/fw/rp/common/enums/FileTypeEnums.java new file mode 100644 index 0000000..fd19aec --- /dev/null +++ b/fw-rp-common/src/main/java/cn/fw/rp/common/enums/FileTypeEnums.java @@ -0,0 +1,46 @@ +package cn.fw.rp.common.enums; + +/** + * @author suchu + * @since 2018/5/9 13:24 + */ +public enum FileTypeEnums { + /** + * 图片 + */ + IMAGE(1, "图片"), + + /** + * 视频 + */ + VIDEO(2, "视频"), + + /** + * 文档 + */ + FILE(3, "附件"); + + private int type; + private String desc; + + FileTypeEnums(int type, String desc) { + this.type = type; + this.desc = desc; + } + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } +} diff --git a/fw-rp-common/src/main/java/cn/fw/rp/common/util/AesUtil.java b/fw-rp-common/src/main/java/cn/fw/rp/common/util/AesUtil.java new file mode 100644 index 0000000..3e634db --- /dev/null +++ b/fw-rp-common/src/main/java/cn/fw/rp/common/util/AesUtil.java @@ -0,0 +1,208 @@ +package cn.fw.rp.common.util; + +import sun.misc.BASE64Decoder; +import sun.misc.BASE64Encoder; + +import javax.crypto.*; +import javax.crypto.spec.SecretKeySpec; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + +/** + * Created with IntelliJ IDEA. + * Created By Devin + * Date: 2018/4/10 + * Time: 17:14 + * Description: + */ +public class AesUtil { + + private final static String KEY = ")O[xfrs]6,YF}+efcaj{+oESb9d8>Zhy"; + private final static String encoding = "UTF-8"; + + public static String aesDecrypt(String content) { + return decrypt(content, KEY); + } + + public static String aesEncrypt(String content) { + return encryptAES(content, KEY); + } + + /** + * AES加密 + * + * @param content + * @param password + * @return + */ + public static String encryptAES(String content, String password) { + byte[] encryptResult = encrypt(content, password); + String encryptResultStr = parseByte2HexStr(encryptResult); + // BASE64位加密 + encryptResultStr = ebotongEncrypto(encryptResultStr); + return encryptResultStr; + } + + /** + * AES解密 + * + * @param encryptResultStr + * @param password + * @return + */ + public static String decrypt(String encryptResultStr, String password) { + // BASE64位解密 + String decrpt = ebotongDecrypto(encryptResultStr); + byte[] decryptFrom = parseHexStr2Byte(decrpt); + byte[] decryptResult = decrypt(decryptFrom, password); + return new String(decryptResult); + } + + /** + * 加密字符串 + */ + public static String ebotongEncrypto(String str) { + BASE64Encoder base64encoder = new BASE64Encoder(); + String result = str; + if (str != null && str.length() > 0) { + try { + byte[] encodeByte = str.getBytes(encoding); + result = base64encoder.encode(encodeByte); + } catch (Exception e) { + e.printStackTrace(); + } + } + //base64加密超过一定长度会自动换行 需要去除换行符 + return result.replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", ""); + } + + /** + * 解密字符串 + */ + public static String ebotongDecrypto(String str) { + BASE64Decoder base64decoder = new BASE64Decoder(); + try { + byte[] encodeByte = base64decoder.decodeBuffer(str); + return new String(encodeByte); + } catch (IOException e) { + e.printStackTrace(); + return str; + } + } + + /** + * 加密 + * + * @param content 需要加密的内容 + * @param password 加密密码 + * @return + */ + private static byte[] encrypt(String content, String password) { + try { + KeyGenerator kgen = KeyGenerator.getInstance("AES"); + //防止linux下 随机生成key + SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); + secureRandom.setSeed(password.getBytes()); + kgen.init(128, secureRandom); + //kgen.init(128, new SecureRandom(password.getBytes())); + SecretKey secretKey = kgen.generateKey(); + byte[] enCodeFormat = secretKey.getEncoded(); + SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); + Cipher cipher = Cipher.getInstance("AES");// 创建密码器 + byte[] byteContent = content.getBytes("utf-8"); + cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 + byte[] result = cipher.doFinal(byteContent); + return result; // 加密 + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (NoSuchPaddingException e) { + e.printStackTrace(); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } catch (IllegalBlockSizeException e) { + e.printStackTrace(); + } catch (BadPaddingException e) { + e.printStackTrace(); + } + return null; + } + + + /** + * 解密 + * + * @param content 待解密内容 + * @param password 解密密钥 + * @return + */ + private static byte[] decrypt(byte[] content, String password) { + try { + KeyGenerator kgen = KeyGenerator.getInstance("AES"); + //防止linux下 随机生成key + SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); + secureRandom.setSeed(password.getBytes()); + kgen.init(128, secureRandom); + //kgen.init(128, new SecureRandom(password.getBytes())); + SecretKey secretKey = kgen.generateKey(); + byte[] enCodeFormat = secretKey.getEncoded(); + SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); + Cipher cipher = Cipher.getInstance("AES");// 创建密码器 + cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 + byte[] result = cipher.doFinal(content); + return result; // 加密 + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (NoSuchPaddingException e) { + e.printStackTrace(); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } catch (IllegalBlockSizeException e) { + e.printStackTrace(); + } catch (BadPaddingException e) { + e.printStackTrace(); + } + return null; + } + + /** + * 将二进制转换成16进制 + * + * @param buf + * @return + */ + public static String parseByte2HexStr(byte buf[]) { + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < buf.length; i++) { + String hex = Integer.toHexString(buf[i] & 0xFF); + if (hex.length() == 1) { + hex = '0' + hex; + } + sb.append(hex.toUpperCase()); + } + return sb.toString(); + } + + + /** + * 将16进制转换为二进制 + * + * @param hexStr + * @return + */ + public static byte[] parseHexStr2Byte(String hexStr) { + if (hexStr.length() < 1) + return null; + byte[] result = new byte[hexStr.length() / 2]; + for (int i = 0; i < hexStr.length() / 2; i++) { + int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); + int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); + result[i] = (byte) (high * 16 + low); + } + return result; + } +} diff --git a/fw-rp-common/src/main/java/cn/fw/rp/common/util/DateUtils.java b/fw-rp-common/src/main/java/cn/fw/rp/common/util/DateUtils.java new file mode 100644 index 0000000..6f8a098 --- /dev/null +++ b/fw-rp-common/src/main/java/cn/fw/rp/common/util/DateUtils.java @@ -0,0 +1,725 @@ +package cn.fw.rp.common.util; + +import java.sql.Timestamp; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.ParsePosition; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * 日期工具类 + * + * @author luox + * @version 1.0 + * @since 2018-04-16 + */ +public final class DateUtils { + + public static final long SECOND = 1000; + + public static final long MINUTE = SECOND * 60; + + public static final long HOUR = MINUTE * 60; + + public static final long DAY = HOUR * 24; + + public static final long WEEK = DAY * 7; + + public static final long YEAR = DAY * 365; + + public static final String FOMTER_TIMES = "yyyy-MM-dd HH:mm:ss"; + + private static final Map WEEK_DAY = new HashMap(); + private static final Map DATE_FIELD = new HashMap(); + + static { + WEEK_DAY.put(7, "星期六"); + WEEK_DAY.put(1, "星期天"); + WEEK_DAY.put(2, "星期一"); + WEEK_DAY.put(3, "星期二"); + WEEK_DAY.put(4, "星期三"); + WEEK_DAY.put(5, "星期四"); + WEEK_DAY.put(6, "星期五"); + } + + static { + DATE_FIELD.put("y", Calendar.YEAR); + DATE_FIELD.put("M", Calendar.MONTH); + DATE_FIELD.put("d", Calendar.DATE); + DATE_FIELD.put("H", Calendar.HOUR); + DATE_FIELD.put("m", Calendar.MINUTE); + DATE_FIELD.put("s", Calendar.SECOND); + DATE_FIELD.put("w", Calendar.WEDNESDAY); + } + + /** + * 解析日期 + * + * @param date 日期字符串 + * @param pattern 日期格式 + * @return + */ + public static Date parse(String date, String pattern) { + Date resultDate = null; + try { + resultDate = new SimpleDateFormat(pattern).parse(date); + } catch (ParseException e) { + e.printStackTrace(); + } + return resultDate; + } + + /** + * 解析日期 yyyy-MM-dd + * + * @param date 日期字符串 + * @return + */ + public static Timestamp parseSimple(String date) { + Date result = null; + try { + DateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd"); + result = yyyyMMdd.parse(date); + } catch (ParseException e) { + e.printStackTrace(); + } + return result != null ? new Timestamp(result.getTime()) : null; + } + + /** + * 解析日期字符串 + * + * @param date + * @return + */ + public static Timestamp parseFull(String date) { + Date result = null; + try { + DateFormat yyyyMMddHHmmss = new SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss"); + result = yyyyMMddHHmmss.parse(date); + } catch (ParseException e) { + e.printStackTrace(); + } + return result != null ? new Timestamp(result.getTime()) : null; + } + + /** + * 解析日期 yyyy-MM-dd + * + * @return + */ + public static Timestamp parse(Object object) { + if (object instanceof Date) { + return new Timestamp(((Date) object).getTime()); + } + if (StringUtils.isEmpty(object)) + return null; + String date = StringUtils.asString(object); + try { + if (date.length() == 10) { + return parseSimple(date); + } else if (date.length() == 8) { + DateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd"); + Date d = yyyyMMdd.parse(date); + return new Timestamp(d.getTime()); + } else if (date.length() == 9) { + if (date.matches("\\d{4}-\\d-\\d{2}")) {//yyyy-M-dd + DateFormat yyyyMdd = new SimpleDateFormat("yyyy-M-dd"); + Date d = yyyyMdd.parse(date); + return new Timestamp(d.getTime()); + } else if (date.matches("\\d{4}-\\d{2}-\\d")) {//yyyy-MM-d + DateFormat yyyyMdd = new SimpleDateFormat("yyyy-MM-d"); + Date d = yyyyMdd.parse(date); + return new Timestamp(d.getTime()); + } + } else if (date.length() == 16) {//yyyy-MM-dd HH:mm + DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + Date d = format.parse(date); + return new Timestamp(d.getTime()); + } else if (date.length() == 18) {//yyyy-MM-ddHH:mm:ss 医保返回日期可能出现 + DateFormat format = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss"); + Date d = format.parse(date); + return new Timestamp(d.getTime()); + } else if (date.length() == 19) {//yyyy-MM-dd HH:mm:ss + return parseFull(date); + } else if (date.length() >= 20 && date.length() <= 23) {//yyyy-MM-dd HH:mm:ss.SSS + if (date.matches("\\d{4}-\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\\.\\d{1,3}")) { + DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + Date d = format.parse(date); + return new Timestamp(d.getTime()); + } + } else { + return Timestamp.valueOf(object.toString()); + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** + * 格式化日期字符串 + * + * @param date 日期 + * @param pattern 日期格式 + * @return + */ + public static String format(Date date, String pattern) { + if (date == null) { + return null; + } + DateFormat format = new SimpleDateFormat(pattern); + return format.format(date); + } + + /** + * 格式化日期 + * + * @param date + * @return yyyy年MM月dd日 + */ + public static String formatCHS(Date date) { + if (date == null) { + return null; + } + DateFormat format = new SimpleDateFormat("yyyy年MM月dd日"); + return format.format(date); + } + + /** + * 格式化日期字符串 + * + * @param date 日期 + * @return + */ + public static String format(Date date) { + if (date == null) { + return null; + } + DateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd"); + return YYYY_MM_DD.format(date); + } + + /** + * 格式化日期 + * + * @param date + * @return + */ + public static String formatFull(Date date) { + DateFormat YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss"); + return YYYY_MM_DD_HH_MM_SS.format(date); + } + + /** + * 取得当前日期 + * + * @return + */ + public static Timestamp getNow() { + return new Timestamp(System.currentTimeMillis()); + } + + /** + * 取得当前日期 + * + * @return + */ + public static Integer getNowYear() { + return getYear(getNow()); + } + + /** + * 取得当前月份 + * + * @return + */ + public static Integer getNowMonth() { + return getMonth(getNow()); + } + + /** + * 取得年度 + * + * @param value + * @return + */ + public static Integer getYear(Object value) { + Calendar c = Calendar.getInstance(); + Date date = getDate(value); + c.setTime(date); + return c.get(Calendar.YEAR); + } + + /** + * 取得月份 + * + * @param value + * @return + */ + public static Integer getMonth(Object value) { + Calendar c = Calendar.getInstance(); + Date date = getDate(value); + c.setTime(date); + return c.get(Calendar.MONTH) + 1; + } + + /** + * 取得日 + * + * @param value + * @return + */ + public static Integer getDay(Object value) { + Calendar c = Calendar.getInstance(); + Date date = getDate(value); + c.setTime(date); + return c.get(Calendar.DAY_OF_MONTH); + } + + /** + * 取得日期对象 + * + * @param value + * @return + */ + private static Date getDate(Object value) { + Date date = null; + if (value instanceof Date) { + date = (Date) value; + } else { + date = parse((String) value); + } + if (date == null) { + throw new RuntimeException("日期格式解析错误!date=" + value); + } + return date; + } + + /** + * 取得年龄 + * + * @param value + * @return + */ + @SuppressWarnings("all") + public static Integer getAge(Object value) { + Timestamp date = DateUtils.parse(value); + if (date == null) { + return null; + } + Timestamp now = DateUtils.getNow(); + int year1 = date.getYear(), year2 = now.getYear(); + int month1 = date.getMonth(), month2 = now.getMonth(); + int date1 = date.getDate(), date2 = now.getDate(); + int months = (year2 - year1) * 12 + month2 - month1; + if (date1 > date2) { + months = months - 1; + } + int age = months / 12; + return age; + } + + /** + * @param offsetYear + * @return 当前时间 + offsetYear + */ + public static Timestamp getNowExpiredYear(int offsetYear) { + Calendar now = Calendar.getInstance(); + now.add(Calendar.YEAR, offsetYear); + return new Timestamp(now.getTime().getTime()); + } + + /** + * @param offset + * @return 当前时间 + offsetMonth + */ + public static Timestamp getNowExpiredMonth(int offset) { + Calendar now = Calendar.getInstance(); + now.add(Calendar.MONTH, offset); + return new Timestamp(now.getTime().getTime()); + } + + /** + * @param offset + * @return 当前时间 + offsetDay + */ + public static Timestamp getNowExpiredDay(int offset) { + Calendar now = Calendar.getInstance(); + now.add(Calendar.DATE, offset); + return new Timestamp(now.getTime().getTime()); + } + + /** + * @param offset + * @return 当前时间 + offsetDay + */ + public static Timestamp getNowExpiredHour(int offset) { + Calendar now = Calendar.getInstance(); + now.add(Calendar.HOUR, offset); + return new Timestamp(now.getTime().getTime()); + } + + /** + * @param offsetSecond + * @return 当前时间 + offsetSecond + */ + public static Timestamp getNowExpiredSecond(int offsetSecond) { + Calendar now = Calendar.getInstance(); + now.add(Calendar.SECOND, offsetSecond); + return new Timestamp(now.getTime().getTime()); + } + + /** + * @param offset + * @return 当前时间 + offset + */ + public static Timestamp getNowExpiredMinute(int offset) { + Calendar now = Calendar.getInstance(); + now.add(Calendar.MINUTE, offset); + return new Timestamp(now.getTime().getTime()); + } + + /** + * @param offset + * @return 指定时间 + offsetDay + */ + public static Timestamp getExpiredDay(Date givenDate, int offset) { + Calendar date = Calendar.getInstance(); + date.setTime(givenDate); + date.add(Calendar.DATE, offset); + return new Timestamp(date.getTime().getTime()); + } + + /** + * 实现ORACLE中ADD_MONTHS函数功能 + * + * @param offset + * @return 指定时间 + offsetMonth + */ + public static Timestamp getExpiredMonth(Date givenDate, int offset) { + Calendar date = Calendar.getInstance(); + date.setTime(givenDate); + date.add(Calendar.MONTH, offset); + return new Timestamp(date.getTime().getTime()); + } + + /** + * @param offsetSecond + * @return 指定时间 + offsetSecond + */ + public static Timestamp getExpiredYear(Date givenDate, int offsetHour) { + Calendar date = Calendar.getInstance(); + date.setTime(givenDate); + date.add(Calendar.YEAR, offsetHour); + return new Timestamp(date.getTime().getTime()); + } + + /** + * @param second + * @return 指定时间 + offsetSecond + */ + public static Timestamp getExpiredSecond(Date givenDate, int second) { + Calendar date = Calendar.getInstance(); + date.setTime(givenDate); + date.add(Calendar.SECOND, second); + return new Timestamp(date.getTime().getTime()); + } + + /** + * 计算时间差 + * + * @param givenDate 日期 + * @param offset 2 + * @param type 日期字段类型 + * @return + */ + public static Timestamp getExpired(Date givenDate, int offset, Integer type) { + Calendar date = Calendar.getInstance(); + date.setTime(givenDate); + date.add(type, offset); + return new Timestamp(date.getTime().getTime()); + } + + /** + * 根据日期取得日历 + * + * @param date + * @return + */ + public static Calendar getCalendar(Date date) { + Calendar c = Calendar.getInstance(); + c.setTime(date); + return c; + } + + /** + * @param offsetSecond + * @return 指定时间 + offsetSecond + */ + public static Timestamp getExpiredHour(Date givenDate, int offsetHour) { + Calendar date = Calendar.getInstance(); + date.setTime(givenDate); + date.add(Calendar.HOUR, offsetHour); + return new Timestamp(date.getTime().getTime()); + } + + /** + * @return 给出指定日期的月份的第一天 + */ + public static Date getMonthFirstDay(Date givenDate) { + Date date = DateUtils.parse(DateUtils.format(givenDate, "yyyy-MM"), + "yyyy-MM"); + return date; + } + + /** + * 取得当前是周几? + * + * @param givenDate + * @return + */ + public static int getDayOfWeek(Date givenDate) { + Calendar c = Calendar.getInstance(); + c.setTime(givenDate); + int day = c.get(Calendar.DAY_OF_WEEK); + return day; + } + + /** + * 取得中文星期? + * + * @param dayOfWeek + * @return + */ + public static String getChineseDayOfWeek(int dayOfWeek) { + return WEEK_DAY.get(dayOfWeek); + } + + /** + * 给定日期是否在范围内 + * + * @param date 给定日期 + * @param begin 开始日期 + * @param end 结束日期 + * @return true 在指定范围内 + */ + public static Boolean between(Date date, Date begin, Date end) { + if (date == null || begin == null || end == null) { + return true; + } + return date.after(begin) && date.before(end); + } + + /** + * 当前日期是否在范围内 + * + * @param begin 开始日期 + * @param end 结束日期 + * @return true 在指定范围内 + */ + public static Boolean between(Date begin, Date end) { + Date now = getNow(); + return between(now, begin, end); + } + + /** + * 取得今天零点日期 + * + * @return + */ + public static Calendar getTodayZero() { + Calendar c = Calendar.getInstance(); + c.set(Calendar.HOUR_OF_DAY, 0); + c.set(Calendar.MINUTE, 0); + c.set(Calendar.SECOND, 0); + c.set(Calendar.MILLISECOND, 0); + return c; + } + + /** + * 是否是日期格式 + * + * @param value + * @return + */ + public static boolean isDate(String value) { + return parse(value) != null; + } + + /** + *

+ * Parses a string representing a date by trying a variety of different + * parsers. + *

+ *

+ *

+ * The parse will try each parse pattern in turn. A parse is only deemed + * sucessful if it parses the whole of the input string. If no parse + * patterns match, a ParseException is thrown. + *

+ * + * @param str the date to parse, not null + * @param parsePatterns the date format patterns to use, see SimpleDateFormat, not + * null + * @return the parsed date + * @throws IllegalArgumentException if the date string or pattern array is null + * @throws ParseException if none of the date patterns were suitable + */ + public static Date parseDate(String str, String[] parsePatterns) + throws ParseException { + if (str == null || parsePatterns == null) { + throw new IllegalArgumentException( + "Date and Patterns must not be null"); + } + SimpleDateFormat parser = null; + ParsePosition pos = new ParsePosition(0); + for (int i = 0; i < parsePatterns.length; i++) { + if (i == 0) { + parser = new SimpleDateFormat(parsePatterns[0]); + } else { + parser.applyPattern(parsePatterns[i]); + } + pos.setIndex(0); + Date date = parser.parse(str, pos); + if (date != null && pos.getIndex() == str.length()) { + return date; + } + } + throw new ParseException("Unable to parse the date: " + str, -1); + } + + /** + * 取得java.sql.Date + * + * @param value + * @return + */ + public static java.sql.Date getSqlDate(Object value) { + if (value == null) { + return null; + } + if (value instanceof Date) { + return new java.sql.Date(((Date) value).getTime()); + } else if (value instanceof String) { + Timestamp date = parse((String) value); + return date != null ? new java.sql.Date(date.getTime()) : null; + } + return null; + } + + + /** + * 计算日期表达式 + * + * @return + */ + public static Date getExpired(Object givenDate, String off) { + Date date = null; + if (givenDate instanceof Date) { + date = (Date) givenDate; + } else { + date = parse(StringUtils.asString(givenDate)); + } + String op = String.valueOf(off.charAt(0)); + int offset = Integer.parseInt(String.valueOf(off.charAt(1))); + String type = String.valueOf(off.charAt(2)); + Date result = null; + int dateField = DATE_FIELD.get(type); + if (op.equals("+")) { + result = DateUtils.getExpired(date, +offset, dateField); + } else if (op.equals("-")) { + result = DateUtils.getExpired(date, -offset, dateField); + } + return result; + } + + /** + * 计算日期表达式 + * + * @param expr today + 1d 或 2014-06-29 + 1d + * @return + */ + public static Date eval(String expr) { + expr = expr.trim(); + if (expr.equals("today") || expr.equals("now")) { + return getNow(); + } + String EXPR = "(.+)\\s*(\\+|\\-)\\s*(\\w+)"; + String p1 = expr.replaceAll(EXPR, "$1").trim(); + String p2 = expr.replaceAll(EXPR, "$2").trim(); + String p3 = expr.replaceAll(EXPR, "$3").trim(); + Date date = null; + if (p1.equals("today")) { + date = getNow(); + } else { + p1 = p1.replace("'", "").trim(); + date = parse(p1); + } + Date result = null; + result = getExpired(date, p2 + p3); + return result; + } + + /** + * 计算日期差 + * + * @param a 日期A + * @param b 日期B + * @param type 类型 + * @return 计算结果 + */ + public static Integer sub(Object a, Object b, String type) { + Date d1 = parse(a);//开始日期 + Date d2 = parse(b);//截止日期 + if (d1 == null || d2 == null) { + return null; + } + Long result = null; + long DAY = 24 * 60 * 60 * 1000; + long sub = d1.getTime() - d2.getTime(); + long daysub = (sub / DAY); + long y1 = d1.getYear(), y2 = d2.getYear(); + long m1 = d1.getMonth(), m2 = d2.getMonth(); + long monthsub = (y1 - y2) * 12 + (m1 - m2); + if (type.equals("m")) {//月 + result = monthsub; + } else if (type == "y") {//年 + result = monthsub / 12; + } else if (type.equals("d")) {//天 + result = daysub; + } + return Integer.valueOf(result.intValue()); + } + + public static void main(String[] args) { + System.out.println(getYear(new Date())); + System.out.println(getMonth(new Date())); + System.out.println(eval("today + 1d")); + System.out.println(eval("today - 1d")); + System.out.println(eval("today + 1M")); + System.out.println(eval("today - 1M")); + System.out.println(eval("today + 1y")); + System.out.println(eval("today - 1y")); + + System.out.println(eval("2014-06-29 + 1d")); + System.out.println(eval("2014-06-29 - 1d")); + System.out.println(eval("2014-06-29 + 1M")); + System.out.println(eval("2014-06-29 - 1M")); + System.out.println(eval("2014-06-29 + 1y")); + System.out.println(eval("2014-06-29 - 1y")); + System.out.println(eval("2014-06-29 - 1w")); + System.out.println(getExpired(new Date(), "-1M")); + String now = DateUtils.getNow().toString(); + System.out.println(now + "=" + now + " parse=" + parse(now) + ""); + System.out.println(sub("2014-07-30", "2013-06-29", "d")); + System.out.println(getAge("1982-09-29")); + System.out.println(parse("2015-03-2519:35:21")); + } +} \ No newline at end of file diff --git a/fw-rp-common/src/main/java/cn/fw/rp/common/util/PublicUtil.java b/fw-rp-common/src/main/java/cn/fw/rp/common/util/PublicUtil.java new file mode 100644 index 0000000..274e6c8 --- /dev/null +++ b/fw-rp-common/src/main/java/cn/fw/rp/common/util/PublicUtil.java @@ -0,0 +1,591 @@ +package cn.fw.rp.common.util; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Array; +import java.math.BigDecimal; +import java.net.URLEncoder; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Created with IntelliJ IDEA + * Created By Devin + * Date: 2018/4/3 + * Time: 17:22 + * Description: 通用工具类 + */ +public class PublicUtil { + public static final String DATA_FORMAT = "yyyy-MM-dd"; + public static final String DATA_FORMAT_CLEAR = "yyyyMMdd"; + public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; + public static final String TIME_FORMAT_S = "yyyy-MM-dd HH:mm:ss.sss"; + protected static Logger logger = LoggerFactory.getLogger(PublicUtil.class); + + public PublicUtil() { + } + + public static String toShowHtml(String str) { + if (str == null) { + return null; + } else { + String html = new String(str); + html = replace(html, "&", "&"); + html = replace(html, "<", "<"); + html = replace(html, ">", ">"); + html = replace(html, """, "\""); + html = replace(html, "
", "\n"); + return html; + } + } + + public static Boolean containsStr(String source, String str) { + return Boolean.valueOf(isNotEmpty(source) && source.contains(str)); + } + + public static int getLength(String text) { + int textLength = text.length(); + int length = textLength; + + for (int i = 0; i < textLength; ++i) { + if (String.valueOf(text.charAt(i)).getBytes().length > 1) { + ++length; + } + } + + return length % 2 == 0 ? length / 2 : length / 2 + 1; + } + + public static String subMaxString(String str, Integer length) { + if (isNotEmpty(str) && str.length() > length.intValue()) { + Integer realLength = Integer.valueOf(0); + Integer len = Integer.valueOf(str.length()); + + for (int i = 0; i < len.intValue(); ++i) { + if (String.valueOf(str.charAt(i)).getBytes().length > 1) { + realLength = Integer.valueOf(realLength.intValue() + 2); + } else { + realLength = Integer.valueOf(realLength.intValue() + 1); + } + + if (realLength.intValue() > length.intValue()) { + str = toAppendStr(new Object[]{replaceBlank(str).substring(0, i), "..."}); + break; + } + } + } + + return str; + } + + public static String replaceBlank(String str) { + String dest = ""; + if (isNotEmpty(str)) { + Pattern p = Pattern.compile("\\s*|\t|\r|\n"); + Matcher m = p.matcher(str); + dest = m.replaceAll(""); + } + + return dest; + } + + public static String replace(String source, String oldString, String newString) { + StringBuffer output = new StringBuffer(); + if (!source.equals("") && source != null) { + int lengthOfSource = source.length(); + int lengthOfOld = oldString.length(); + + int posStart; + int pos; + for (posStart = 0; (pos = source.indexOf(oldString, posStart)) >= 0; posStart = pos + lengthOfOld) { + output.append(source.substring(posStart, pos)); + output.append(newString); + } + + if (posStart < lengthOfSource) { + output.append(source.substring(posStart)); + } + + return output.toString(); + } else { + return ""; + } + } + + public static String getAuthorizeURLBase(String url, String appId, String componentAppId) { +// url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId +// + "&redirect_uri=" + url +// + "&response_type=code" +// + "&scope=snsapi_userinfo" +// + "&component_appid=" + componentAppId +// + "&state=123#wechat_redirect"; + url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + + "&redirect_uri=" + url + + "&response_type=code" + + "&scope=snsapi_userinfo" + + "&state=123" + + "&component_appid=" + componentAppId + + "&connect_redirect=1#wechat_redirect"; + return url; + } + + public static String getUrlEncode(String url) { + try { + url = URLEncoder.encode(url, "utf-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return url; + } + + public static String replaceAll(String str, String oldStr, String newStr) { + int length = str.indexOf(oldStr); + int j = 0; + + for (String temp = ""; length != -1; length = str.indexOf(oldStr)) { + ++j; + if (j == 10) { + break; + } + + temp = str.substring(length + 1); + str = str.substring(0, length); + str = str + newStr + temp; + } + + return str; + } + + public static int parseInt(Object value, Integer defaultValue) { + try { + return Integer.parseInt(String.valueOf(value)); + } catch (Exception var3) { + return defaultValue.intValue(); + } + } + + public static long parseLong(Object value, Long defaultValue) { + try { + return Long.parseLong(String.valueOf(value)); + } catch (Exception var3) { + return defaultValue.longValue(); + } + } + + public static Date parseDate(Object date) { + return parseDate(String.valueOf(date)); + } + + public static Date parseDate(String date) { + try { + SimpleDateFormat ex = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + return ex.parse(date); + } catch (Exception var2) { + return null; + } + } + + public static Date parseDate(String date, String format) { + try { + SimpleDateFormat ex = new SimpleDateFormat(format); + return ex.parse(date); + } catch (Exception var3) { + return null; + } + } + + public static String parseString(Object value, String defaultValue) { + return null != value && !"null".equals(String.valueOf(value).toLowerCase()) ? String.valueOf(value) : defaultValue; + } + + public static String getCurrentTime() { + return getCurrentTime("yyyy-MM-dd HH:mm:ss"); + } + + public static Date getCurrentDate() { + return new Date(); + } + + public static String getCurrentTime(String format) { + Date nowTime = new Date(); + SimpleDateFormat fmt = new SimpleDateFormat(format); + return fmt.format(nowTime); + } + + public static String getMySQLDateTimeFormat() { + return "yyyy-MM-dd HH:mm:ss"; + } + + public static String fmtDate(Date date, String fmt) { + if (null == date) { + return ""; + } else { + SimpleDateFormat f = new SimpleDateFormat(fmt); + return f.format(date); + } + } + + public static String fmtDate(Date date) { + if (null == date) { + return ""; + } else { + SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + return f.format(date); + } + } + + /** + * 转换为时间(天,时:分:秒.毫秒) + * + * @param timeMillis + * @return + */ + public static String formatDateTime(long timeMillis) { + long day = timeMillis / (24 * 60 * 60 * 1000); + long hour = (timeMillis / (60 * 60 * 1000) - day * 24); + long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60); + long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); + long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000); + return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss; + } + + public static String nextId() { + return UUID.randomUUID().toString().replaceAll("-", ""); + } + + public static String getUUID() { + String uid = "0"; + SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); + String tempId = sf.format(new Date()); + if (Long.parseLong(uid) >= Long.parseLong(tempId)) { + uid = Long.parseLong(uid) + 1L + ""; + } else { + uid = tempId; + } + + return uid + getRandomString(10); + } + + public static String getRandomString(int size) { + char[] c = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'}; + Random random = new Random(); + StringBuffer sb = new StringBuffer(); + + for (int i = 0; i < size; ++i) { + sb.append(c[Math.abs(random.nextInt()) % c.length]); + } + + return sb.toString(); + } + + public static String getRandomNumber(int size) { + char[] c = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; + Random random = new Random(); + StringBuffer sb = new StringBuffer(); + + for (int i = 0; i < size; ++i) { + sb.append(c[Math.abs(random.nextInt()) % c.length]); + } + + return sb.toString(); + } + + public static boolean isNotEmpty(Object obj) { + return !isEmpty(obj); + } + + public static boolean isEmpty(Object obj) { + if (obj == null) { + return true; + } else if (obj instanceof String) { + return obj.equals(""); + } else if (obj instanceof Collection) { + Collection map1 = (Collection) obj; + return map1.size() == 0; + } else if (obj instanceof Map) { + Map map = (Map) obj; + return map.size() == 0; + } else { + return obj.getClass().isArray() ? Array.getLength(obj) == 0 : false; + } + } + + // 获取随机字符串 + public static String getNonceStr() { + String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + String res = ""; + for (int i = 0; i < 16; i++) { + Random rd = new Random(); + res += chars.charAt(rd.nextInt(chars.length() - 1)); + } + return res; + } + + public static boolean isDigitalString(String str) { + if (isEmpty(str)) { + return false; + } else { + try { + Double.parseDouble(str); + return true; + } catch (Exception var2) { + return false; + } + } + } + + public static boolean equalsFlag(Object str) { + try { + if (str != null && "true".equals(str)) { + return true; + } + } catch (Exception var2) { + var2.printStackTrace(); + } + + return false; + } + + public static boolean isNumeric(String str) { + try { + return str != null && str.matches("\\d*.?\\d*"); + } catch (Exception var2) { + var2.printStackTrace(); + return false; + } + } + + public static int arrayIndexOf(Object array, Object value) { + if (array != null && value != null) { + int len = Array.getLength(array); + + for (int i = 0; i < len; ++i) { + if (value.equals(Array.get(array, i))) { + return i; + } + } + + return -1; + } else { + return -1; + } + } + + public static String toUtf8String(String s) { + StringBuffer sb = new StringBuffer(); + + for (int i = 0; i < s.length(); ++i) { + char c = s.charAt(i); + if (c >= 0 && c <= 255) { + sb.append(c); + } else { + byte[] b; + try { + b = Character.toString(c).getBytes("utf-8"); + } catch (Exception var7) { + var7.printStackTrace(); + b = new byte[0]; + } + + for (int j = 0; j < b.length; ++j) { + int k = b[j]; + if (k < 0) { + k += 256; + } + + sb.append("%" + Integer.toHexString(k).toUpperCase()); + } + } + } + + return sb.toString(); + } + + public static String sicenToComm(Object value) { + return value == null ? null : (value instanceof Double ? sicenToCommDouble(((Double) value).doubleValue()) : String.valueOf(value)); + } + + public static String sicenToCommDouble(double value) { + String retValue = null; + DecimalFormat df = new DecimalFormat(); + df.setMinimumFractionDigits(0); + df.setMaximumFractionDigits(5); + retValue = df.format(value); + retValue = retValue.replaceAll(",", ""); + return retValue; + } + + public static String roundStr(Double value) { + DecimalFormat df = new DecimalFormat("#.00"); + return df.format(value); + } + + public static Double round(Double value, int count) { + try { + BigDecimal e = new BigDecimal(value.doubleValue()); + return Double.valueOf(e.setScale(count, 4).doubleValue()); + } catch (Exception var3) { + logger.warn("round double" + value); + return Double.valueOf(0.0D); + } + } + + public static Double round(Object value, int count) { + return round(parseDouble(value), count); + } + + public static Double parseDouble(Object value) { + return value == null ? Double.valueOf(0.0D) : parseDouble(String.valueOf(value)); + } + + public static Double parseDouble(String value) { + return parseDouble(value, Double.valueOf(0.0D)); + } + + public static Double parseDouble(String value, Double dafualtVal) { + Double r = dafualtVal; + + try { + if (value != null) { + value = value.trim(); + if (value.endsWith("%")) { + value = value.substring(0, value.length() - 1); + } + + r = Double.valueOf(Double.parseDouble(value)); + } + } catch (Exception var4) { + logger.warn("转换失败:" + var4.getMessage()); + } + + return r; + } + + public static String toLowerCaseFirstOne(String s) { + return Character.isLowerCase(s.charAt(0)) ? s : Character.toLowerCase(s.charAt(0)) + s.substring(1); + } + + public static String toUpperCaseFirstOne(String s) { + return Character.isUpperCase(s.charAt(0)) ? s : Character.toUpperCase(s.charAt(0)) + s.substring(1); + } + + public static String toAppendStr(Object... strs) { + StringBuffer sb = new StringBuffer(); + Object[] var2 = strs; + int var3 = strs.length; + + for (int var4 = 0; var4 < var3; ++var4) { + Object str = var2[var4]; + if (isNotEmpty(str)) { + sb.append(str); + } + } + + return sb.toString(); + } + + public static boolean match(String pattern, String str) { + Pattern p = Pattern.compile(pattern); + Matcher m = p.matcher(str); + return m.find(); + } + + public static String toStrString(Object obj) { + return isNotEmpty(obj) ? String.valueOf(obj) : ""; + } + + public static String toStrStringNull(Object obj) { + return isNotEmpty(obj) ? String.valueOf(obj) : null; + } + + public static Object fmtDate(ZonedDateTime val, String format) { + return val == null ? null : val.format(DateTimeFormatter.ofPattern(format)); + } + + public static Object fmtDate(ZonedDateTime val) { + return fmtDate(val, "yyyy-MM-dd HH:mm:ss"); + } + + public static String getRandomBsString(int length) { + String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + Random random = new Random(); + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < length; i++) { + int number = random.nextInt(62); + sb.append(str.charAt(number)); + } + return sb.toString(); + } + + /** + * 将字符串进行sha1加密 + * + * @param str 需要加密的字符串 + * @return 加密后的内容 + */ + public static String sha1(String str) { + try { + MessageDigest digest = MessageDigest.getInstance("SHA-1"); + digest.update(str.getBytes()); + byte messageDigest[] = digest.digest(); + // Create Hex String + StringBuffer hexString = new StringBuffer(); + // 字节数组转换为 十六进制 数 + for (int i = 0; i < messageDigest.length; i++) { + String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); + if (shaHex.length() < 2) { + hexString.append(0); + } + hexString.append(shaHex); + } + return hexString.toString(); + + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + return ""; + } + + public static boolean isBlank(CharSequence cs) { + int strLen; + if (cs != null && (strLen = cs.length()) != 0) { + for (int i = 0; i < strLen; ++i) { + if (!Character.isWhitespace(cs.charAt(i))) { + return false; + } + } + + return true; + } else { + return true; + } + } + + public static boolean isNotBlank(CharSequence cs) { + return !isBlank(cs); + } + + /** + * 替换掉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; + } +} + diff --git a/fw-rp-common/src/main/java/cn/fw/rp/common/util/QRCodeKit.java b/fw-rp-common/src/main/java/cn/fw/rp/common/util/QRCodeKit.java new file mode 100644 index 0000000..979cdec --- /dev/null +++ b/fw-rp-common/src/main/java/cn/fw/rp/common/util/QRCodeKit.java @@ -0,0 +1,290 @@ +package cn.fw.rp.common.util; + +import com.google.zxing.*; +import com.google.zxing.common.BitMatrix; +import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.binary.Base64OutputStream; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.*; +import java.util.HashMap; +import java.util.Map; + +/** + * QrCodeKit + * + * @author https://www.jianshu.com/p/39c5bca32e3e + */ +public class QRCodeKit { + + public static final String QRCODE_DEFAULT_CHARSET = "UTF-8"; + + public static final int QRCODE_DEFAULT_HEIGHT = 150; + + public static final int QRCODE_DEFAULT_WIDTH = 150; + + private static final int BLACK = 0xFF000000; + private static final int WHITE = 0xFFFFFFFF; + + public static void main(String[] args) throws IOException, NotFoundException { + String data = "当然是测试的二维码啊"; + File logoFile = new File("C:\\Users\\suchu\\Pictures\\erha_hd.jpg"); + BufferedImage image = QRCodeKit.createQRCodeWithLogo(data, logoFile); + String base64Str = QRCodeKit.getImageBase64String(image); + System.out.println(base64Str); + ImageIO.write(image, "png", new File("result_with_logo.png")); + System.out.println("done"); + } + + /** + * Create qrcode with default settings + * + * @param data + * @return + * @author stefli + */ + public static BufferedImage createQRCode(String data) { + return createQRCode(data, QRCODE_DEFAULT_WIDTH, QRCODE_DEFAULT_HEIGHT); + } + + /** + * Create qrcode with default charset + * + * @param data + * @param width + * @param height + * @return + * @author stefli + */ + public static BufferedImage createQRCode(String data, int width, int height) { + return createQRCode(data, QRCODE_DEFAULT_CHARSET, width, height); + } + + /** + * Create qrcode with specified charset + * + * @param data + * @param charset + * @param width + * @param height + * @return + * @author stefli + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + public static BufferedImage createQRCode(String data, String charset, int width, int height) { + Map hint = new HashMap(); + hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); + hint.put(EncodeHintType.CHARACTER_SET, charset); + + return createQRCode(data, charset, hint, width, height); + } + + /** + * Create qrcode with specified hint + * + * @param data + * @param charset + * @param hint + * @param width + * @param height + * @return + * @author stefli + */ + public static BufferedImage createQRCode(String data, String charset, Map hint, int width, + int height) { + BitMatrix matrix; + try { + matrix = new MultiFormatWriter().encode(new String(data.getBytes(charset), charset), BarcodeFormat.QR_CODE, + width, height, hint); + return toBufferedImage(matrix); + } catch (WriterException e) { + throw new RuntimeException(e.getMessage(), e); + } catch (Exception e) { + throw new RuntimeException(e.getMessage(), e); + } + } + + + public static BufferedImage toBufferedImage(BitMatrix matrix) { + int width = matrix.getWidth(); + int height = matrix.getHeight(); + BufferedImage image = new BufferedImage(width, height, + BufferedImage.TYPE_INT_RGB); + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); + } + } + return image; + } + + /** + * Create qrcode with default settings and logo + * + * @param data + * @param logoFile + * @return + * @author stefli + */ + public static BufferedImage createQRCodeWithLogo(String data, File logoFile) { + return createQRCodeWithLogo(data, QRCODE_DEFAULT_WIDTH, QRCODE_DEFAULT_HEIGHT, logoFile); + } + + /** + * Create qrcode with default charset and logo + * + * @param data + * @param width + * @param height + * @param logoFile + * @return + * @author stefli + */ + public static BufferedImage createQRCodeWithLogo(String data, int width, int height, File logoFile) { + return createQRCodeWithLogo(data, QRCODE_DEFAULT_CHARSET, width, height, logoFile); + } + + /** + * Create qrcode with specified charset and logo + * + * @param data + * @param charset + * @param width + * @param height + * @param logoFile + * @return + * @author stefli + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + public static BufferedImage createQRCodeWithLogo(String data, String charset, int width, int height, File logoFile) { + Map hint = new HashMap(); + hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); + hint.put(EncodeHintType.CHARACTER_SET, charset); + + return createQRCodeWithLogo(data, charset, hint, width, height, logoFile); + } + + /** + * Create qrcode with specified hint and logo + * + * @param data + * @param charset + * @param hint + * @param width + * @param height + * @param logoFile + * @return + * @author stefli + */ + public static BufferedImage createQRCodeWithLogo(String data, String charset, Map hint, + int width, int height, File logoFile) { + try { + BufferedImage qrcode = createQRCode(data, charset, hint, width, height); + + Graphics2D g = qrcode.createGraphics(); + BufferedImage logo = ImageIO.read(logoFile); + //logo最大宽度为二维码宽度的20% + int widthLogo = logo.getWidth(null) > qrcode.getWidth() * 2 / 10 ? + (qrcode.getWidth() * 2 / 10) : logo.getWidth(null); + int heightLogo = logo.getHeight(null) > qrcode.getHeight() * 2 / 10 ? + (qrcode.getHeight() * 2 / 10) : logo.getHeight(null); + int x = (qrcode.getWidth() - widthLogo) / 2; + int y = (qrcode.getHeight() - heightLogo) / 2; + + // 开始绘制图片 + g.drawImage(logo, x, y, widthLogo, heightLogo, null); + g.drawRoundRect(x, y, widthLogo, heightLogo, 15, 15); + //边框宽度 + g.setStroke(new BasicStroke(2)); + //边框颜色 + g.setColor(Color.WHITE); + g.drawRect(x, y, widthLogo, heightLogo); + g.dispose(); + logo.flush(); + qrcode.flush(); + return qrcode; + /* BufferedImage logo = ImageIO.read(logoFile); + int deltaHeight = height - logo.getHeight(); + int deltaWidth = width - logo.getWidth(); + BufferedImage combined = new BufferedImage(height, width, BufferedImage.TYPE_INT_ARGB); + Graphics2D g = (Graphics2D) combined.getGraphics(); + g.drawImage(qrcode, 0, 0, null); + g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f)); + g.drawImage(logo, (int) Math.round(deltaWidth / 2), (int) Math.round(deltaHeight / 2), null); + + return combined;*/ + } catch (IOException e) { + throw new RuntimeException(e.getMessage(), e); + } catch (Exception e) { + throw new RuntimeException(e.getMessage(), e); + } + } + + /** + * Return base64 for image + * + * @param image + * @return + * @author stefli + */ + public static String getImageBase64String(BufferedImage image) { + String result = null; + try { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + OutputStream b64 = new Base64OutputStream(os); + ImageIO.write(image, "png", b64); + result = "data:image/png;base64," + os.toString("UTF-8"); + result = result.replaceAll("\r\n", ""); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e.getMessage(), e); + } catch (IOException e) { + throw new RuntimeException(e.getMessage(), e); + } + return result; + } + + + public static String getPureImageBase64String(BufferedImage image) { + String result = null; + try { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + OutputStream b64 = new Base64OutputStream(os); + ImageIO.write(image, "jpg", b64); + result = os.toString("UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e.getMessage(), e); + } catch (IOException e) { + throw new RuntimeException(e.getMessage(), e); + } + return result; + } + + /** + * Decode the base64Image data to image + * + * @param base64ImageString + * @param file + * @author stefli + */ + public static void convertBase64StringToImage(String base64ImageString, File file) { + FileOutputStream os; + try { + Base64 d = new Base64(); + byte[] bs = d.decode(base64ImageString); + os = new FileOutputStream(file.getAbsolutePath()); + os.write(bs); + os.close(); + } catch (FileNotFoundException e) { + throw new RuntimeException(e.getMessage(), e); + } catch (IOException e) { + throw new RuntimeException(e.getMessage(), e); + } catch (Exception e) { + throw new RuntimeException(e.getMessage(), e); + } + } + + +} diff --git a/fw-rp-common/src/main/java/cn/fw/rp/common/util/StringUtils.java b/fw-rp-common/src/main/java/cn/fw/rp/common/util/StringUtils.java new file mode 100644 index 0000000..d930d1b --- /dev/null +++ b/fw-rp-common/src/main/java/cn/fw/rp/common/util/StringUtils.java @@ -0,0 +1,488 @@ +/* + * 创建日期 2011-3-16 + * + * 成都天和软件公司 + * 电话:028-85425861 + * 传真:028-85425861-8008 + * 邮编:610041 + * 地址:成都市武侯区航空路6号丰德万瑞中心B座1001 + * 版权所有 + */ +package cn.fw.rp.common.util; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * 字符串工具 + * + * @author: luox + * @version: 1.0 + * @since 2018-01-15 + */ +public final class StringUtils { + + /** + * 空字符串 + */ + public static final String EMPTY = ""; + + private StringUtils() { + } + + /** + * 首字母小写 + * + * @param s String + * @return String + */ + public static String firstCharLowerCase(String s) { + if (isValid(s)) { + return s.substring(0, 1).toLowerCase() + s.substring(1); + } + return s; + } + + /** + * 删除前缀 + * + * @param s + * @param prefix + * @return + */ + public static String removePrefix(String s, String prefix) { + int index = s.indexOf(prefix); + return index == 0 ? s.substring(prefix.length()) : s; + } + + /** + * 删除后缀 + * + * @param s + * @param suffix + * @return + */ + public static String removeSuffix(String s, String suffix) { + return s.endsWith(suffix) ? s.substring(0, s.length() - suffix.length()) : s; + } + + /** + * 首字母大写 + * + * @param s String + * @return String + */ + public static String firstCharUpperCase(String s) { + if (isValid(s)) { + return s.substring(0, 1).toUpperCase() + s.substring(1); + } + return s; + } + + /** + * 检查对象是否有效 obj != null && obj.toString().length() > 0 + * + * @param obj + * @return boolean + */ + public static boolean isValid(Object obj) { + return obj != null && obj.toString().length() > 0; + } + + /** + * 是否是空的 + * + * @param obj + * @return + */ + public static boolean isEmpty(Object obj) { + return obj == null || obj.toString().length() == 0; + } + + /** + * 转化为String对象 + * + * @param obj + * @return boolean + */ + public static String asString(Object obj) { + return obj != null ? obj.toString() : ""; + } + + /** + * 返回其中一个有效的对象 value != null && value.toString().length() > 0 + * + * @param values + */ + public static String tryThese(Object... values) { + for (int i = 0; i < values.length; i++) { + String value = StringUtils.asString(values[i]); + if (!value.isEmpty()) { + return value; + } + } + return ""; + } + + /** + * EL表达式提供的定义方法 + * + * @param v1 + * @param v2 + * @return + */ + public static String tryThese(String v1, String v2) { + return tryThese(new Object[]{v1, v2}); + } + + /** + * 连接字符串 + * + * @param list + * @param split + * @return 字符串 + */ + public static String join(T[] list, String split) { + return join(list, split, ""); + } + + /** + * 连接字符串 + * + * @param list + * @param split + * @return 字符串 + */ + public static String join(T[] list, String split, String wrap) { + if (list == null) + return null; + StringBuilder s = new StringBuilder(128); + for (int i = 0; i < list.length; i++) { + if (i > 0) { + s.append(split); + } + s.append(wrap + list[i] + wrap); + } + return s.toString(); + } + + /** + * 连接 + * + * @param list + * @param split + * @param wrap + * @return + */ + public static String join(List list, String split, String wrap) { + return join(list.toArray(), split, wrap); + } + + /** + * 连接字符串 + * + * @param list + * @param split + * @return 字符串 + */ + public static String join(List list, String split) { + return join(list.toArray(), split); + } + + /** + * 包裹字符串 id:12, {, } 输出 {id:12} + * + * @param input 输入串 + * @param begin { + * @param end } + * @return String + */ + public static String wrap(String begin, String input, String end) { + if (!input.startsWith(begin)) { + input = begin + input; + } + if (!input.endsWith(end)) { + input = input + end; + } + return input; + } + + /** + * 取得匹配的字符串 + * + * @param input + * @param regex + * @return + */ + public static List matchs(String input, String regex) { + return matchs(input, regex, 0); + } + + /** + * 取得匹配的字符串 + * + * @param input + * @param regex + * @return + */ + public static List matchs(String input, String regex, int group) { + Pattern pattern = Pattern.compile(regex); + Matcher match = pattern.matcher(input); + List matches = new ArrayList(); + while (match.find()) { + matches.add(match.group(group)); + } + return matches; + } + + /** + * 找到匹配的第一个字符串 + * + * @param input + * @param regex + * @param group + * @return + */ + public static String matchFirst(String input, String regex, int group) { + List matches = matchs(input, regex, group); + return matches.isEmpty() ? null : matches.get(0); + } + + /** + * 截取指定长度字符串 + * + * @return + */ + public static String getShorterString(String str, int maxLength) { + return getShorterString(str, "...", maxLength); + } + + /** + * 截取指定长度字符串 + * + * @param input + * @param tail + * @param length + * @return + */ + public static String getShorterString(String input, String tail, int length) { + tail = isValid(tail) ? tail : ""; + StringBuffer buffer = new StringBuffer(512); + try { + int len = input.getBytes("GBK").length; + if (len > length) { + int ln = 0; + for (int i = 0; ln < length; i++) { + String temp = input.substring(i, i + 1); + if (temp.getBytes("GBK").length == 2) + ln += 2; + else + ln++; + + if (ln <= length) + buffer.append(temp); + } + } else { + return input; + } + buffer.append(tail); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return buffer.toString(); + } + + /** + * 取得GBK编码 + * + * @return + */ + public static String getBytesString(String input, String code) { + try { + byte[] b = input.getBytes(code); + return Arrays.toString(b); + } catch (UnsupportedEncodingException e) { + return String.valueOf(code.hashCode()); + } + } + + /** + * 转换格式 CUST_INFO_ID - > custInfoId + * + * @param input + * @return + */ + public static String getFieldString(String input) { + if (input == null) { + return null; + } + String field = input.toLowerCase(); + String[] values = field.split("\\_"); + StringBuffer b = new StringBuffer(input.length()); + for (int i = 0; i < values.length; i++) { + if (i == 0) + b.append(values[i]); + else + b.append(firstCharUpperCase(values[i])); + } + return b.toString(); + } + + /** + * 转换格式 CUST_INFO_ID - > custInfoId + * + * @param columnName + * @return + */ + public static String toFieldName(String columnName) { + return getFieldString(columnName); + } + + /** + * 转换格式 custInfoId - > CUST_INFO_ID + * + * @param field + * @return + */ + public static String toColumnName(String field) { + if (field == null) { + return null; + } + StringBuffer b = new StringBuffer(field.length() + 3); + for (int i = 0; i < field.length(); i++) { + Character char1 = field.charAt(i); + if (Character.isUpperCase(char1) && i != 0) { + b.append("_"); + } + b.append(char1); + } + return b.toString(); + } + + /** + * 转化为JSON值 + * + * @param value + * @return + * @throws IOException + */ + public static String toJsonValue(Object value) throws IOException { + if (value instanceof Number) { + return value.toString(); + } else { + return "'" + value.toString() + "'"; + } + } + + /** + * 字符串转化为UUID + * + * @param value + * @return + */ + public static String toUUID(String value) { + if (value == null) + throw new RuntimeException("value is null!"); + return UUID.nameUUIDFromBytes(value.getBytes()).toString(); + } + + /** + * 获取Style样式中样式的值 + * + * @param styleString + * @param styleName + * @return 相应的值 + */ + public static String getStyleValue(String styleString, String styleName) { + String[] styles = styleString.split(";"); + for (int i = 0; i < styles.length; i++) { + String tempValue = styles[i].trim(); + if (tempValue.startsWith(styleName)) { + String[] style = tempValue.split(":"); + return style[1]; + } + } + return ""; + } + + /** + * 生成重复次字符 + * + * @param charactor + * @param repeat + * @return + */ + public static String getRepeat(String charactor, int repeat) { + return repeat(charactor, repeat, ""); + } + + /** + * 生成重复次字符 + * + * @param charactor + * @param repeat + * @return + */ + public static String repeat(String charactor, int repeat, String split) { + StringBuilder s = new StringBuilder(charactor.length() * repeat); + for (int i = 0; i < repeat; i++) { + if (i != 0) { + s.append(split != null ? split : ""); + } + s.append(charactor); + } + return s.toString(); + } + + /** + * 取得长度 + * + * @param text + * @return + */ + public static int length(String text) { + int len = text.length(); + try { + len = text.getBytes("GBK").length;//SQLServer数据库用的GBK编码 + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return len; + } + + /** + * 字符串替换函数 + * + * @param data 字符串 + * @param data from 旧值 + * @param to from 新值 + */ + public static String replaceString(String data, String from, String to) { + StringBuffer buf = new StringBuffer(data.length()); + int pos = -1; + int i = 0; + while ((pos = data.indexOf(from, i)) != -1) { + buf.append(data.substring(i, pos)).append(to); + i = pos + from.length(); + } + buf.append(data.substring(i)); + return buf.toString(); + } + + + public static void main(String[] args) { + System.out.println(toUUID("1")); + System.out.println(removePrefix("abcd123", "ab")); + System.out.println(removeSuffix("abcd123", "123")); + System.out.println(toColumnName("usernameid")); + System.out.println(getFieldString(toColumnName("userNameId"))); + System.out.println(repeat("?", 10, ",")); + length("AAA中国()111222bb"); + } +} \ No newline at end of file diff --git a/fw-rp-dao/pom.xml b/fw-rp-dao/pom.xml new file mode 100644 index 0000000..90b6193 --- /dev/null +++ b/fw-rp-dao/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + cn.fw + fw-rp + 1.0 + + + fw-rp-dao + jar + fw-rp-dao + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + + + cn.fw + fw-rp-domain + ${project.parent.version} + + + cn.fw + fw-rp-common + ${project.parent.version} + + + cn.fw + fw-data-base + + + + diff --git a/fw-rp-dao/src/main/java/cn/fw/rp/dao/FileListDao.java b/fw-rp-dao/src/main/java/cn/fw/rp/dao/FileListDao.java new file mode 100644 index 0000000..d60fca0 --- /dev/null +++ b/fw-rp-dao/src/main/java/cn/fw/rp/dao/FileListDao.java @@ -0,0 +1,15 @@ +package cn.fw.rp.dao; + +import cn.fw.data.base.db.dao.BaseDao; +import cn.fw.rp.db.FileList; +import org.apache.ibatis.annotations.Mapper; + +/** + * 2018年5月8日 15点26分 + * + * @author suchu + */ +@Mapper +public interface FileListDao extends BaseDao { + FileList selectByFileId(String fileId); +} diff --git a/fw-rp-dao/src/main/java/cn/fw/rp/dao/ProblemFileDao.java b/fw-rp-dao/src/main/java/cn/fw/rp/dao/ProblemFileDao.java new file mode 100644 index 0000000..ce114f0 --- /dev/null +++ b/fw-rp-dao/src/main/java/cn/fw/rp/dao/ProblemFileDao.java @@ -0,0 +1,14 @@ +package cn.fw.rp.dao; + +import cn.fw.data.base.db.dao.BaseDao; +import cn.fw.rp.db.ProblemFile; +import org.apache.ibatis.annotations.Mapper; + +/** + * 2018年5月8日 15点26分 + * + * @author suchu + */ +@Mapper +public interface ProblemFileDao extends BaseDao { +} diff --git a/fw-rp-dao/src/main/java/cn/fw/rp/dao/ProblemListDao.java b/fw-rp-dao/src/main/java/cn/fw/rp/dao/ProblemListDao.java new file mode 100644 index 0000000..25f7d26 --- /dev/null +++ b/fw-rp-dao/src/main/java/cn/fw/rp/dao/ProblemListDao.java @@ -0,0 +1,21 @@ +package cn.fw.rp.dao; + +import cn.fw.data.base.db.dao.BaseDao; +import cn.fw.rp.db.ProblemFile; +import cn.fw.rp.db.ProblemList; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; +import java.util.Map; + +/** + * @author suchu + * @since 2018/5/8 15:23 + */ +@Mapper +public interface ProblemListDao extends BaseDao { + List getFiles(@Param("flag") Integer flag, @Param("problemId") Integer problemId); + + List fileCount(Integer problemId); +} diff --git a/fw-rp-dao/src/main/resources/mapper/FileList.xml b/fw-rp-dao/src/main/resources/mapper/FileList.xml new file mode 100644 index 0000000..27f4c82 --- /dev/null +++ b/fw-rp-dao/src/main/resources/mapper/FileList.xml @@ -0,0 +1,100 @@ + + + + + + + + + + + + + + + + + + update file_list + + where id = #{t.id} + + + + + update marketing_comment + + + + + + + insert into file_list + (file_id, + file_name, + content_type, + upload_time, + upload_user, + file_path, + flag, + file_length) + values (#{fileId}, + #{fileName}, + #{contentType}, + now(), + #{uploadUser}, + #{filePath}, + #{flag}, + #{fileLength}) + + + + + delete + from file_list + where id = #{id} + + + + + delete from file_list + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/fw-rp-dao/src/main/resources/mapper/ProblemFile.xml b/fw-rp-dao/src/main/resources/mapper/ProblemFile.xml new file mode 100644 index 0000000..6961860 --- /dev/null +++ b/fw-rp-dao/src/main/resources/mapper/ProblemFile.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + update problem_file + + where id = #{t.id} + + + + + update marketing_comment + + + + + + + insert into problem_file + (problem_id, + file_id) + values (#{problemId}, + #{fileId}) + + + + + delete + from problem_file + where id = #{id} + + + + + delete from problem_file + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/fw-rp-dao/src/main/resources/mapper/ProblemList.xml b/fw-rp-dao/src/main/resources/mapper/ProblemList.xml new file mode 100644 index 0000000..b821eeb --- /dev/null +++ b/fw-rp-dao/src/main/resources/mapper/ProblemList.xml @@ -0,0 +1,161 @@ + + + + + + + + + + + + + + + + + + + + + update problem_list + + where id = #{t.id} + + + + + update marketing_comment + + + + + + + insert into problem_list + (system_ref, + module_ref, + title, + content, + purpose, + question, + scenarios, + logic, + status, + create_time, + update_time, + create_user, + update_user) + values (#{systemRef}, + #{moduleRef}, + #{title}, + #{content}, + #{purpose}, + #{question}, + #{scenarios}, + #{logic}, + #{status}, + now(), + #{updateTime}, + #{createUser}, + #{updateUser}) + + + + + delete + from problem_list + where id = #{id} + + + + + delete from problem_list + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/fw-rp-dao/src/main/resources/mapper/core.xml b/fw-rp-dao/src/main/resources/mapper/core.xml new file mode 100644 index 0000000..3048d15 --- /dev/null +++ b/fw-rp-dao/src/main/resources/mapper/core.xml @@ -0,0 +1,359 @@ + + + + + + + + + + + + + + + + + + + + 1=1 + + + #{val}]]> + + + + + = #{val}]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #{val} + + + + + + + + #{val} + + + + + + + + + + + + + + + + + + + #{val}]]> + + + + + = #{val}]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #{val} + + + + + + + + #{val} + + + + + + + + + + + #{val}]]> + + + + + = #{val}]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #{val} + + + + + + + + #{val} + + + + + + + + + + + + + + + + + + + #{val}]]> + + + + + = #{val}]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #{val} + + + + + + + + #{val} + + + + + + \ No newline at end of file diff --git a/fw-rp-domain/pom.xml b/fw-rp-domain/pom.xml new file mode 100644 index 0000000..afa6023 --- /dev/null +++ b/fw-rp-domain/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + cn.fw + fw-rp + 1.0 + + fw-rp-domain + fw-rp-domain + jar + + + + cn.fw + fw-rp-common + ${project.parent.version} + + + org.springframework.boot + spring-boot-starter-validation + + + cn.fw + fw-data-base + + + io.springfox + springfox-swagger2 + 2.8.0 + + + com.google.guava + guava + + + + + io.springfox + springfox-swagger-ui + 2.6.1 + + + com.google.guava + guava + + + + com.alibaba + fastjson + + + + org.projectlombok + lombok + + + cn.fw + fw-rp-sdk + ${project.parent.version} + + + + diff --git a/fw-rp-domain/src/main/java/cn/fw/rp/annotation/EnumValid.java b/fw-rp-domain/src/main/java/cn/fw/rp/annotation/EnumValid.java new file mode 100644 index 0000000..8fd3eeb --- /dev/null +++ b/fw-rp-domain/src/main/java/cn/fw/rp/annotation/EnumValid.java @@ -0,0 +1,72 @@ +package cn.fw.rp.annotation; + +import javax.validation.Constraint; +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import javax.validation.Payload; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +/** + * @author Devin + * @date: 2018/4/18 20:44 + */ +@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Constraint(validatedBy = EnumValid.Validator.class) +public @interface EnumValid { + + String message() default "枚举类型不符合"; + + Class[] groups() default {}; + + Class[] payload() default {}; + + Class> enumClass(); + + String enumMethod(); + + class Validator implements ConstraintValidator { + + private Class> enumClass; + private String enumMethod; + + @Override + public void initialize(EnumValid enumValid) { + enumMethod = enumValid.enumMethod(); + enumClass = enumValid.enumClass(); + } + + @Override + public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) { + if (value == null) { + return Boolean.FALSE; + } + if (enumClass == null || enumMethod == null) { + return Boolean.FALSE; + } + Class valueClass = value.getClass(); + try { + Method method = enumClass.getMethod(enumMethod, valueClass); + if (!Boolean.TYPE.equals(method.getReturnType()) && !Boolean.class.equals(method.getReturnType())) { + throw new RuntimeException(String.format("%s method return is not boolean type in the %s class", enumMethod, enumClass)); + } + if (!Modifier.isStatic(method.getModifiers())) { + throw new RuntimeException(String.format("%s method is not static method in the %s class", enumMethod, enumClass)); + } + Boolean result = (Boolean) method.invoke(null, value); + return result == null ? false : result; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException | SecurityException e) { + throw new RuntimeException(String.format("This %s(%s) method does not exist in the %s", enumMethod, valueClass, enumClass), e); + } + } + + } +} diff --git a/fw-rp-domain/src/main/java/cn/fw/rp/db/FileList.java b/fw-rp-domain/src/main/java/cn/fw/rp/db/FileList.java new file mode 100644 index 0000000..70d837e --- /dev/null +++ b/fw-rp-domain/src/main/java/cn/fw/rp/db/FileList.java @@ -0,0 +1,33 @@ +package cn.fw.rp.db; + +import cn.fw.data.base.db.annotation.DbTable; +import lombok.Data; + +import java.util.Date; + +/** + * @author suchu + * @since 2018/5/8 15:22 + */ +@Data +@DbTable("file_list") +public class FileList { + + Integer id; + + String fileName; + + String contentType; + + Date uploadTime; + + String uploadUser; + + String filePath; + + Integer flag; + + String fileId; + + Long fileLength; +} diff --git a/fw-rp-domain/src/main/java/cn/fw/rp/db/ProblemFile.java b/fw-rp-domain/src/main/java/cn/fw/rp/db/ProblemFile.java new file mode 100644 index 0000000..78d00d3 --- /dev/null +++ b/fw-rp-domain/src/main/java/cn/fw/rp/db/ProblemFile.java @@ -0,0 +1,17 @@ +package cn.fw.rp.db; + +import lombok.Data; + +/** + * @author suchu + * @since 2018/5/8 15:21 + */ +@Data +public class ProblemFile { + private Integer id; + + private String problemId; + + private String fileId; + +} diff --git a/fw-rp-domain/src/main/java/cn/fw/rp/db/ProblemList.java b/fw-rp-domain/src/main/java/cn/fw/rp/db/ProblemList.java new file mode 100644 index 0000000..fc15466 --- /dev/null +++ b/fw-rp-domain/src/main/java/cn/fw/rp/db/ProblemList.java @@ -0,0 +1,42 @@ +package cn.fw.rp.db; + +import cn.fw.data.base.db.annotation.DbTable; +import lombok.Data; + +import java.util.Date; + +/** + * @author suchu + * @since 2018/5/8 15:18 + */ +@DbTable("problem_list") +@Data +public class ProblemList { + + private Integer id; + + private String systemRef; + + private String moduleRef; + + private String title; + private String content; + + private String purpose; + + private String question; + + private String scenarios; + + private String logic; + + private Integer status; + + private Date createTime; + + private Date updateTime; + + private String createUser; + + private String updateUser; +} diff --git a/fw-rp-domain/src/main/java/cn/fw/rp/man/ProblemDto.java b/fw-rp-domain/src/main/java/cn/fw/rp/man/ProblemDto.java new file mode 100644 index 0000000..35ddb7c --- /dev/null +++ b/fw-rp-domain/src/main/java/cn/fw/rp/man/ProblemDto.java @@ -0,0 +1,39 @@ +package cn.fw.rp.man; + +import lombok.Data; + +import java.util.Date; + +/** + * @author suchu + * @since 2018/5/8 16:08 + */ +@Data +public class ProblemDto { + private Integer id; + + private String systemRef; + + private String moduleRef; + + private String title; + private String content; + + private String purpose; + + private String question; + + private String scenarios; + + private String logic; + + private Integer status; + + private Date createTime; + + private Date updateTime; + + private String createUser; + + private String updateUser; +} diff --git a/fw-rp-domain/src/main/java/cn/fw/rp/man/ProblemQueryParam.java b/fw-rp-domain/src/main/java/cn/fw/rp/man/ProblemQueryParam.java new file mode 100644 index 0000000..b74902e --- /dev/null +++ b/fw-rp-domain/src/main/java/cn/fw/rp/man/ProblemQueryParam.java @@ -0,0 +1,19 @@ +package cn.fw.rp.man; + +import lombok.Data; + +/** + * @author suchu + * @since 2018/5/9 19:10 + */ +@Data +public class ProblemQueryParam { + + private String title; + private String systemRef; + private String moduleRef; + private String createUser; + private String beginDate; + private String endDate; + private Integer status; +} diff --git a/fw-rp-man/pom.xml b/fw-rp-man/pom.xml new file mode 100644 index 0000000..5a309b8 --- /dev/null +++ b/fw-rp-man/pom.xml @@ -0,0 +1,171 @@ + + + 4.0.0 + + cn.fw + fw-rp + 1.0 + + + fw-rp-man + jar + fw-rp-man + + + + org.springframework.boot + spring-boot-devtools + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-starter-actuator + + + com.alibaba + druid-spring-boot-starter + 1.1.7 + + + + junit + junit + compile + + + org.springframework + spring-test + + + org.springframework.boot + spring-boot-starter-test + test + + + + + cn.fw + fw-rp-common + ${project.parent.version} + + + cn.fw + fw-rp-domain + ${project.parent.version} + + + cn.fw + fw-rp-service + ${project.parent.version} + + + cn.fw + fw-base-config-sdk + + + cn.fw + fw-data-base + + + cn.fw + fw-personnel-sdk + + + cn.fw + fw-potential-sdk + + + cn.fw.third + fw-wechat-sdk + + + mysql + mysql-connector-java + + + cn.fw + fw-auth-client + + + cn.fw + fw-rp-sdk + ${project.parent.version} + + + org.apache.commons + commons-lang3 + + + org.springframework.cloud + spring-cloud-starter-eureka + + + net.coobird + thumbnailator + 0.4.8 + + + + + fw-rp-man + + + src/main/resources + true + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + true + + + + + + + + dev + + dev + + + true + + + + test + + test + + + + local + + local + + + + prd + + prd + + + + diff --git a/fw-rp-man/src/main/java/cn/fw/rp/web/RpManServer.java b/fw-rp-man/src/main/java/cn/fw/rp/web/RpManServer.java new file mode 100644 index 0000000..e83de82 --- /dev/null +++ b/fw-rp-man/src/main/java/cn/fw/rp/web/RpManServer.java @@ -0,0 +1,25 @@ +package cn.fw.rp.web; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +/** + * man端服务启动类 + * + * @author luoxin + * @create 2018-02-24 + */ +@SpringBootApplication +@EnableTransactionManagement +@EnableAutoConfiguration +@MapperScan(basePackages = "cn.fw.rp.dao") +@ComponentScan({"cn.fw.rp.service", "cn.fw.rp.web"}) +public class RpManServer { + public static void main(String[] args) { + new SpringApplicationBuilder(RpManServer.class).web(true).run(args); + } +} diff --git a/fw-rp-man/src/main/java/cn/fw/rp/web/controller/FileController.java b/fw-rp-man/src/main/java/cn/fw/rp/web/controller/FileController.java new file mode 100644 index 0000000..99d9e1d --- /dev/null +++ b/fw-rp-man/src/main/java/cn/fw/rp/web/controller/FileController.java @@ -0,0 +1,287 @@ +package cn.fw.rp.web.controller; + +import cn.fw.data.base.domain.common.Message; +import cn.fw.rp.common.enums.FileTypeEnums; +import cn.fw.rp.common.util.DateUtils; +import cn.fw.rp.dao.FileListDao; +import cn.fw.rp.dao.ProblemFileDao; +import cn.fw.rp.db.FileList; +import cn.fw.rp.db.ProblemFile; +import net.coobird.thumbnailator.Thumbnails; +import org.apache.catalina.connector.ClientAbortException; +import org.apache.tomcat.util.http.fileupload.IOUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Controller; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.Assert; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.text.SimpleDateFormat; +import java.util.UUID; + +/** + * @author suchu + * @since 2018/5/9 10:59 + */ +@Controller +@RequestMapping("/file") +public class FileController { + + @Value("${upload.path}") + String uploadPath; + + @Autowired + FileListDao dao; + + @Autowired + ProblemFileDao problemFileDao; + + public static void main(String[] args) throws IOException { + Thumbnails.of("F:\\RP_UPLOAD\\2018\\05\\09\\1db414949be24c3d9de479d2cbfa4c81.jpg") + .size(100, 100).toFile(new File("F:\\\\RP_UPLOAD\\test1.jpg")); + } + + public String datePath() { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); + return sdf.format(DateUtils.getNow()); + } + + @RequestMapping("/video/{fileId}") + public void videoView(@PathVariable String fileId, HttpServletResponse response, HttpServletRequest request, @RequestHeader(value = "range", required = false) String range) { + FileList fileList = dao.selectByFileId(fileId); + File file = null; + if (null != fileList) { + file = new File(fileList.getFilePath()); + if (!file.exists()) { + response.setStatus(204);//NO content + return; + } + } + + //文件目录 +// File music = new File("E:\\CloudMusic\\林子祥 - 街头霸王榜.mp3"); + + + //开始下载位置 + long startByte = 0; + //结束下载位置 + long endByte = file.length() - 1; + + //有range的话 + if (range != null && range.contains("bytes=") && range.contains("-")) { + range = range.substring(range.lastIndexOf("=") + 1).trim(); + String ranges[] = range.split("-"); + try { + //判断range的类型 + if (ranges.length == 1) { + //类型一:bytes=-2343 + if (range.startsWith("-")) { + endByte = Long.parseLong(ranges[0]); + } + //类型二:bytes=2343- + else if (range.endsWith("-")) { + startByte = Long.parseLong(ranges[0]); + } + } + //类型三:bytes=22-2343 + else if (ranges.length == 2) { + startByte = Long.parseLong(ranges[0]); + endByte = Long.parseLong(ranges[1]); + } + + } catch (NumberFormatException e) { + startByte = 0; + endByte = file.length() - 1; + } + } + + //要下载的长度(为啥要加一问小学数学老师去) + long contentLength = endByte - startByte + 1; + //文件名 + String fileName = file.getName(); + //文件类型 + String contentType = request.getServletContext().getMimeType(fileName); + + + //各种响应头设置 + //参考资料:https://www.ibm.com/developerworks/cn/java/joy-down/index.html + //坑爹地方一:看代码 + response.setHeader("Accept-Ranges", "bytes"); + //坑爹地方二:http状态码要为206 + response.setStatus(response.SC_PARTIAL_CONTENT); + response.setContentType(contentType); + response.setHeader("Content-Type", contentType); + //这里文件名换你想要的,inline表示浏览器直接实用(我方便测试用的) + //参考资料:http://hw1287789687.iteye.com/blog/2188500 + response.setHeader("Content-Disposition", "inline;filename=test.mp3"); + response.setHeader("Content-Length", String.valueOf(contentLength)); + //坑爹地方三:Content-Range,格式为 + // [要下载的开始位置]-[结束位置]/[文件总大小] + response.setHeader("Content-Range", "bytes " + startByte + "-" + endByte + "/" + file.length()); + + + BufferedOutputStream outputStream = null; + RandomAccessFile randomAccessFile = null; + //已传送数据大小 + long transmitted = 0; + try { + randomAccessFile = new RandomAccessFile(file, "r"); + outputStream = new BufferedOutputStream(response.getOutputStream()); + byte[] buff = new byte[4096]; + int len = 0; + randomAccessFile.seek(startByte); + //坑爹地方四:判断是否到了最后不足4096(buff的length)个byte这个逻辑((transmitted + len) <= contentLength)要放前面!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + //不然会会先读取randomAccessFile,造成后面读取位置出错,找了一天才发现问题所在 + while ((transmitted + len) <= contentLength && (len = randomAccessFile.read(buff)) != -1) { + outputStream.write(buff, 0, len); + transmitted += len; + //停一下,方便测试,用的时候删了就行了 + // Thread.sleep(10); + } + //处理不足buff.length部分 + if (transmitted < contentLength) { + len = randomAccessFile.read(buff, 0, (int) (contentLength - transmitted)); + outputStream.write(buff, 0, len); + transmitted += len; + } + + outputStream.flush(); + response.flushBuffer(); + randomAccessFile.close(); + System.out.println("下载完毕:" + startByte + "-" + endByte + ":" + transmitted); + + } catch (ClientAbortException e) { + System.out.println("用户停止下载:" + startByte + "-" + endByte + ":" + transmitted); + //捕获此异常表示拥护停止下载 + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (randomAccessFile != null) { + randomAccessFile.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + + @RequestMapping("/view/{fileId}") + public void view(@PathVariable String fileId, HttpServletResponse response, HttpServletRequest request) throws IOException { + + FileList fileList = dao.selectByFileId(fileId); + if (null != fileList) { + File file = new File(fileList.getFilePath()); + if (!file.exists()) { + response.setStatus(404); + return; + } + FileInputStream fileInputStream = new FileInputStream(file); + response.setContentType(fileList.getContentType()); + response.setContentLength(fileInputStream.available()); + byte buffer[] = new byte[4096]; + ServletOutputStream outputStream = response.getOutputStream(); + int length = fileInputStream.read(buffer); + while (length != -1) { + outputStream.write(buffer, 0, length); + length = fileInputStream.read(buffer); + } + outputStream.close(); + } else { + response.setStatus(404); + System.out.println("无法下载..."); + } + } + + public int getFlag(String contentType) { + if (contentType.contains("image")) { + return FileTypeEnums.IMAGE.getType(); + } else if (contentType.contains("video")) + return FileTypeEnums.VIDEO.getType(); + else return FileTypeEnums.FILE.getType(); + } + + @RequestMapping("/image/thumb") + public void thumbnails(HttpServletResponse response, @RequestParam String fileId, @RequestParam(defaultValue = "200") Integer width, @RequestParam(defaultValue = "200") Integer height) throws IOException { + FileList fileList = dao.selectByFileId(fileId); + if (width > 2000) + width = 150; + if (height > 2000) { + height = 150; + } + if (null != fileList) { + File file = new File(fileList.getFilePath()); + if (!file.exists()) { + response.setStatus(404); + } else { + response.setContentType(fileList.getContentType()); + ServletOutputStream outputStream = response.getOutputStream(); + Thumbnails.of(file).size(width, height).toOutputStream(outputStream); + outputStream.close(); + } + } else { + response.setStatus(404); + } + } + + @Transactional(rollbackFor = Exception.class) + @RequestMapping("/relate") + @ResponseBody + public Message relate(String fileId, String problemId) { + Assert.notNull(fileId, "file id must not be null"); + Assert.notNull(problemId, "problem id must not be null"); + ProblemFile problemFile = new ProblemFile(); + problemFile.setFileId(fileId); + problemFile.setProblemId(problemId); + problemFileDao.insert(problemFile); + return Message.success("关联成功"); + } + + @Transactional(rollbackFor = Exception.class) + @PostMapping("/upload") + @ResponseBody + public Message upload(@RequestParam("file") MultipartFile file, @RequestParam(name = "flag", defaultValue = "3") Integer flag, HttpServletRequest request) throws IOException { + String contentType = file.getContentType(); + String uuid = UUID.randomUUID().toString().replaceAll("-", ""); + Long fileLength = file.getSize(); + //int flag = getFlag(contentType); + String fileName = file.getOriginalFilename(); + String fileSuffix = fileName.substring(fileName.lastIndexOf(".")); + FileList fileList = new FileList(); + fileList.setContentType(contentType); + fileList.setFileId(uuid); + fileList.setUploadTime(DateUtils.getNow()); + fileList.setFileName(fileName); + fileList.setFileLength(fileLength); + fileList.setFlag(flag); + // IOUtils. + File rootPath = new File(uploadPath); + if (!rootPath.exists()) { + rootPath.mkdir(); + } + String datePath = datePath(); + + File outPath = new File(rootPath, datePath); + if (!outPath.exists()) { + outPath.mkdirs(); + } + File outFile = new File(outPath, uuid + fileSuffix); + fileList.setFilePath(outFile.getAbsolutePath()); + FileOutputStream fos = new FileOutputStream(outFile); + IOUtils.copy(file.getInputStream(), fos); + fos.flush(); + fos.close(); + dao.insert(fileList); + Message message = Message.success(); + message.setData(fileList); + return message; + + } +} diff --git a/fw-rp-man/src/main/java/cn/fw/rp/web/controller/LoginController.java b/fw-rp-man/src/main/java/cn/fw/rp/web/controller/LoginController.java new file mode 100644 index 0000000..608c51a --- /dev/null +++ b/fw-rp-man/src/main/java/cn/fw/rp/web/controller/LoginController.java @@ -0,0 +1,19 @@ +package cn.fw.rp.web.controller; + +import cn.fw.data.base.domain.common.Message; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author suchu + * @since 2018/5/8 15:16 + */ +@RestController +public class LoginController { + + @RequestMapping("") + Message login(String username, String password) { + + return Message.success(); + } +} diff --git a/fw-rp-man/src/main/java/cn/fw/rp/web/controller/ProblemController.java b/fw-rp-man/src/main/java/cn/fw/rp/web/controller/ProblemController.java new file mode 100644 index 0000000..774997a --- /dev/null +++ b/fw-rp-man/src/main/java/cn/fw/rp/web/controller/ProblemController.java @@ -0,0 +1,136 @@ +package cn.fw.rp.web.controller; + +import cn.fw.data.base.db.query.Condition; +import cn.fw.data.base.db.query.QueryCriterion; +import cn.fw.data.base.domain.common.Message; +import cn.fw.rp.common.util.DateUtils; +import cn.fw.rp.common.util.StringUtils; +import cn.fw.rp.dao.ProblemListDao; +import cn.fw.rp.db.ProblemList; +import cn.fw.rp.man.ProblemDto; +import cn.fw.rp.man.ProblemQueryParam; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.Assert; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * 需求controller + * + * @author suchu + * @since 2018/5/8 16:05 + */ +@RestController() +@RequestMapping("/problem") +public class ProblemController { + @Autowired + ProblemListDao problemListDao; + + /** + * 查询problem 列表 + * + * @param queryParam + * @return + */ + @RequestMapping("/list") + public Message query(@RequestBody ProblemQueryParam queryParam) { + Message message = Message.success(); + QueryCriterion condition = Condition.open(ProblemList.class); + if (StringUtils.isValid(queryParam.getBeginDate())) { + condition.gte("createTime", queryParam.getBeginDate()); + } + if (StringUtils.isValid(queryParam.getEndDate())) { + condition.lte("createTime", queryParam.getEndDate()); + } + if (StringUtils.isValid(queryParam.getStatus())) { + condition.eq("status", queryParam.getStatus()); + } + if (StringUtils.isValid(queryParam.getSystemRef())) { + condition.eq("systemRef", queryParam.getSystemRef()); + } + if (StringUtils.isValid(queryParam.getModuleRef())) { + condition.eq("moduleRef", queryParam.getModuleRef()); + } + + if (StringUtils.isValid(queryParam.getTitle())) { + condition.like("title", queryParam.getTitle()); + } + condition.orderbyDesc("createTime"); + List list = problemListDao.selectList(condition); + message.setData(list); + return message; + } + + @RequestMapping("/getFiles") + public Message getFiles(Integer flag, Integer problemId) { + Assert.notNull(problemId, "problemId must not be null"); + List result = problemListDao.getFiles(flag, problemId); + Message message = Message.success(); + message.setData(result); + return message; + } + + /** + * 查询problem 通过id + * + * @param problemId + * @return + */ + @GetMapping("/get/{problemId}") + public Message get(@PathVariable Integer problemId) { + Assert.notNull(problemId, "problemId must not be null"); + ProblemList problemList = problemListDao.selectById(problemId); + if (problemList == null) { + return Message.failure("没查询到相关数据"); + } + Message message = Message.success(); + message.setData(problemList); + return message; + } + + /** + * 统计problem上传的文件量 + * + * @param problemId + * @return + */ + @RequestMapping("fileCount") + public Message fileCount(Integer problemId) { + Assert.notNull(problemId, "problemId must not be null"); + List data = problemListDao.fileCount(problemId); + Message message = Message.success(); + message.setData(data); + return message; + } + + + /** + * 保存接口 + * + * @param problemDto + * @return + */ + @Transactional(rollbackFor = Exception.class) + @RequestMapping("/save") + public Message save(@RequestBody ProblemDto problemDto) { + if (problemDto.getId() != null) { + System.out.println(problemDto.toString()); + ProblemList problemList = new ProblemList(); + BeanUtils.copyProperties(problemDto, problemList); + problemList.setUpdateTime(DateUtils.getNow()); + int i = problemListDao.updateById(problemList); + return Message.success(); + } else { + Message message = Message.success(); + ProblemList problemList = new ProblemList(); + BeanUtils.copyProperties(problemDto, problemList); + problemListDao.insert(problemList); + message.setCode(10); + message.setData(problemList.getId()); + return message; + } + } +} diff --git a/fw-rp-man/src/main/resources/application-dev.yml b/fw-rp-man/src/main/resources/application-dev.yml new file mode 100644 index 0000000..2388ddf --- /dev/null +++ b/fw-rp-man/src/main/resources/application-dev.yml @@ -0,0 +1,88 @@ +server: + port: 8030 + tomcat.max-threads: 50 + tomcat.uri-encoding: UTF-8 + +eureka: + instance: + hostname: fw-marketing-man # 设置当前实例的主机名称 + prefer-ip-address: true #访问路径变更为IP + lease-renewal-interval-in-seconds: 5 #心跳间隔 + lease-expiration-duration-in-seconds: 5 #如果现在超过了5秒的间隔 + client: + serviceUrl: + defaultZone: http://feewee:1234qwer@deveureka.feewee.cn/eureka/ + fetch-registry: true + +spring: + application: + name: fw-marketing-man + datasource: + url: jdbc:mysql://192.168.0.8:3306/fw_rp?characterEncoding=UTF-8 + # 只有下面三个是必填项(使用内嵌数据库的话这三个也可以不用填,会使用默认配置),其他配置不是必须的 + username: root + password: mysql@pwd123 + driver-class-name: com.mysql.jdbc.Driver + initial-size: 2 + max-active: 30 + min-idle: 2 + max-wait: 1234 + pool-prepared-statements: true + max-pool-prepared-statement-per-connection-size: 5 + validation-query: SELECT 1 FROM DUAL + validation-query-timeout: 1 + test-on-borrow: true + test-on-return: true + test-while-idle: true + time-between-eviction-runs-millis: 10000 + min-evictable-idle-time-millis: 30001 + async-close-connection-enable: true + aop-patterns: cn.fw.third.pstn.dao.* + # 自定义StatFilter 配置 其他 Filter 不再演示 + filter: + stat: + db-type: mysql + log-slow-sql: true + slow-sql-millis: 2000 + redis: + host: 192.168.0.8 + http: + multipart: + max-file-size: 100Mb + max-request-size: 100Mb + + +mybatis: + type-aliases-package: cn.fw.rp.db cn.fw.data.base.db.query + mapper-locations: classpath:mapper/*.xml + configuration: + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl + +jedis: + pool: + host: 192.168.0.8 + # host: 127.0.0.1 + port: 6379 + config: + maxActive: 50 + maxIdle: 20 + minIdle: 3 + maxWaitMillis: 1000 + testOnBorrow: false + testOnReturn: false + testWhileIdle: true + +auth: + aes: + key: bN7DVjFMJPcoWDcR + ignore: + start-with: /user/*,/role/*,/user/login,/v2/api-docs,/swagger-resources/** + +logging: + # path: ${dynamic.log.path} + level: + root: info + + +upload: + path: F:/RP_UPLOAD \ No newline at end of file diff --git a/fw-rp-man/src/main/resources/application-local.yml b/fw-rp-man/src/main/resources/application-local.yml new file mode 100644 index 0000000..67b08cb --- /dev/null +++ b/fw-rp-man/src/main/resources/application-local.yml @@ -0,0 +1,36 @@ +spring: + datasource: + url: jdbc:mysql://192.168.1.47:3306/fw_erp?characterEncoding=UTF-8 + # 只有下面三个是必填项(使用内嵌数据库的话这三个也可以不用填,会使用默认配置),其他配置不是必须的 + username: root + password: '07141105' + driver-class-name: com.mysql.jdbc.Driver + initial-size: 2 + max-active: 30 + min-idle: 2 + max-wait: 1234 + pool-prepared-statements: true + max-pool-prepared-statement-per-connection-size: 5 + validation-query: SELECT 1 FROM DUAL + validation-query-timeout: 1 + test-on-borrow: true + test-on-return: true + test-while-idle: true + time-between-eviction-runs-millis: 10000 + min-evictable-idle-time-millis: 30001 + async-close-connection-enable: true + aop-patterns: cn.fw.erp.dao.* + + # 自定义StatFilter 配置 其他 Filter 不再演示 + filter: + stat: + db-type: mysql + log-slow-sql: true + slow-sql-millis: 2000 + +mybatis: + type-aliases-package: cn.fw.erp.domain cn.fw.data.base.db.query + mapper-locations: classpath:mapper/*.xml +security: + aes: + key: bN7DVjFMJPcoWDcR \ No newline at end of file diff --git a/fw-rp-man/src/main/resources/application-prd.yml b/fw-rp-man/src/main/resources/application-prd.yml new file mode 100644 index 0000000..eafcbab --- /dev/null +++ b/fw-rp-man/src/main/resources/application-prd.yml @@ -0,0 +1,83 @@ +server: + port: 8082 + tomcat.max-threads: 50 + tomcat.uri-encoding: UTF-8 + +eureka: + instance: + hostname: fw-marketing-man # 设置当前实例的主机名称 + prefer-ip-address: true #访问路径变更为IP + lease-renewal-interval-in-seconds: 5 #心跳间隔 + lease-expiration-duration-in-seconds: 5 #如果现在超过了5秒的间隔 + client: + serviceUrl: + defaultZone: http://feewee:1234qwer@eureka.feewee.cn/eureka/ + fetch-registry: true + +spring: + application: + name: fw-marketing-man + datasource: + url: jdbc:mysql://localhost:3306/fw_rp?characterEncoding=UTF-8 + # 只有下面三个是必填项(使用内嵌数据库的话这三个也可以不用填,会使用默认配置),其他配置不是必须的 + username: root + password: fw123456 + driver-class-name: com.mysql.jdbc.Driver + initial-size: 2 + max-active: 30 + min-idle: 2 + max-wait: 1234 + pool-prepared-statements: true + max-pool-prepared-statement-per-connection-size: 5 + validation-query: SELECT 1 FROM DUAL + validation-query-timeout: 1 + test-on-borrow: true + test-on-return: true + test-while-idle: true + time-between-eviction-runs-millis: 10000 + min-evictable-idle-time-millis: 30001 + async-close-connection-enable: true + aop-patterns: cn.fw.third.pstn.dao.* + # 自定义StatFilter 配置 其他 Filter 不再演示 + filter: + stat: + db-type: mysql + log-slow-sql: true + slow-sql-millis: 2000 + redis: + host: 172.26.154.159 + http: + multipart: + max-file-size: 100Mb + max-request-size: 100Mb +mybatis: + type-aliases-package: cn.fw.rp.db cn.fw.data.base.db.query + mapper-locations: classpath:mapper/*.xml + +jedis: + pool: + host: 172.26.154.159 + # host: 127.0.0.1 + port: 6379 + config: + maxActive: 50 + maxIdle: 20 + minIdle: 3 + maxWaitMillis: 1000 + testOnBorrow: false + testOnReturn: false + testWhileIdle: true + +auth: + aes: + key: bN7DVjFMJPcoWDcR + ignore: + start-with: /user/*,/role/*,/user/login,/v2/api-docs,/swagger-resources/** + +logging: + # path: ${dynamic.log.path} + level: + root: info + +upload: + path: E:/RP_UPLOAD \ No newline at end of file diff --git a/fw-rp-man/src/main/resources/application-test.yml b/fw-rp-man/src/main/resources/application-test.yml new file mode 100644 index 0000000..7b5ff16 --- /dev/null +++ b/fw-rp-man/src/main/resources/application-test.yml @@ -0,0 +1,44 @@ +spring: + datasource: + url: jdbc:mysql://192.168.1.47:3306/fw_erp?characterEncoding=UTF-8 + # 只有下面三个是必填项(使用内嵌数据库的话这三个也可以不用填,会使用默认配置),其他配置不是必须的 + username: root + password: '07141105' + driver-class-name: com.mysql.jdbc.Driver + initial-size: 2 + max-active: 30 + min-idle: 2 + max-wait: 1234 + pool-prepared-statements: true + max-pool-prepared-statement-per-connection-size: 5 + validation-query: SELECT 1 FROM DUAL + validation-query-timeout: 1 + test-on-borrow: true + test-on-return: true + test-while-idle: true + time-between-eviction-runs-millis: 10000 + min-evictable-idle-time-millis: 30001 + async-close-connection-enable: true + aop-patterns: cn.fw.erp.dao.* + + # 自定义StatFilter 配置 其他 Filter 不再演示 + filter: + stat: + db-type: mysql + log-slow-sql: true + slow-sql-millis: 2000 + +mybatis: + type-aliases-package: cn.fw.erp.domain cn.fw.data.base.db.query + mapper-locations: classpath:mapper/*.xml + +auth: + aes: + key: bN7DVjFMJPcoWDcR + ignore: + start-with: /user/*,/role/*,/user/login,/v2/api-docs,/swagger-resources/** + +logging: + path: ${dynamic.log.path} + level: + root: info \ No newline at end of file diff --git a/fw-rp-man/src/main/resources/application.yml b/fw-rp-man/src/main/resources/application.yml new file mode 100644 index 0000000..f50d812 --- /dev/null +++ b/fw-rp-man/src/main/resources/application.yml @@ -0,0 +1,3 @@ +spring: + profiles: + active: @activatedProperties@ \ No newline at end of file diff --git a/fw-rp-man/src/main/resources/banner.txt b/fw-rp-man/src/main/resources/banner.txt new file mode 100644 index 0000000..9e918ee --- /dev/null +++ b/fw-rp-man/src/main/resources/banner.txt @@ -0,0 +1,6 @@ + _____ ___________ _____ _ _ _____ _ _______ _____ +|_ _| _ | ___ \ ___| \ | |_ _| | | | ___ \ ___| + | | | | | | |_/ / |__ | \| | | | | | | | |_/ / |__ + | | | | | | ___ \ __|| . ` | | | | | | | ___ \ __| + | | \ \_/ / |_/ / |___| |\ |_| |_| |_| | |_/ / |___ + \_/ \___/\____/\____/\_| \_/\___/ \___/\____/\____/ \ No newline at end of file diff --git a/fw-rp-man/src/main/resources/logback-spring.xml b/fw-rp-man/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..31e56de --- /dev/null +++ b/fw-rp-man/src/main/resources/logback-spring.xml @@ -0,0 +1,50 @@ + + + + + + + + [%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%c] - %m%n + ${charSet} + + + + + + ${dynamic.log.path}/fw_mkt_man_all.log + + ${dynamic.log.path}/fw_mkt_man_all.log.%d{yyyy-MM-dd} + 7 + 1GB + + + [%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%c] - %m%n + ${charSet} + + + + + + ERROR + + ${dynamic.log.path}/fw_mkt_man_error.log + + ${dynamic.log.path}/fw_mkt_man_error.log.%d{yyyy-MM-dd} + 7 + 1GB + + + [%-5p] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%c] - %m%n + ${charSet} + + + + + + + + + \ No newline at end of file diff --git a/fw-rp-man/src/test/java/cn/fw/rp/web/MockMvcRequestBuilderUtils.java b/fw-rp-man/src/test/java/cn/fw/rp/web/MockMvcRequestBuilderUtils.java new file mode 100644 index 0000000..8ae266f --- /dev/null +++ b/fw-rp-man/src/test/java/cn/fw/rp/web/MockMvcRequestBuilderUtils.java @@ -0,0 +1,231 @@ +package cn.fw.rp.web; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.reflect.FieldUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.MediaType; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.util.ClassUtils; +import org.springframework.util.CollectionUtils; + +import java.beans.PropertyEditor; +import java.lang.reflect.Field; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.time.temporal.Temporal; +import java.util.*; + +/** + * Custom MockMvcRequestBuilder to post an entire form to a given url. + * Useful to test Spring MVC form validation. + * + * @author Florian Lopes + * @see org.springframework.test.web.servlet.request.MockMvcRequestBuilders + */ +public class MockMvcRequestBuilderUtils { + + private static final Map propertyEditors = new HashMap<>(); + + private static final Logger LOGGER = LoggerFactory.getLogger(MockMvcRequestBuilderUtils.class); + + private MockMvcRequestBuilderUtils() { + } + + /** + * Register custom property editor for a given type. + * + * @param type type of the property + * @param propertyEditor {@link PropertyEditor} to register + */ + public static void registerPropertyEditor(Class type, PropertyEditor propertyEditor) { + propertyEditors.put(type, propertyEditor); + } + + /** + * Unregister custom property editor for a given type + * + * @param type type of the property + */ + public static void unregisterPropertyEditor(Class type) { + propertyEditors.remove(type); + } + + /** + * Unregister all previously registered property editors + */ + public static void unregisterPropertyEditors() { + propertyEditors.clear(); + } + + /** + * Post a form to the given url. + * All non-null form fields will be added as HTTP request parameters using POST method + * + * @param url the URL to post the form to + * @param form form object to send using POST method + * @return mockHttpServletRequestBuilder wrapped mockHttpServletRequestBuilder + */ + public static MockHttpServletRequestBuilder postForm(String url, Object form) { + final MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.post(url) + .contentType(MediaType.APPLICATION_FORM_URLENCODED); + final Map formFields = getFormFields(form, new TreeMap<>(), StringUtils.EMPTY); + formFields.forEach((path, value) -> { + LOGGER.debug(String.format("Adding form field (%s=%s) to HTTP request parameters", path, value)); + mockHttpServletRequestBuilder.param(path, value); + }); + + return mockHttpServletRequestBuilder; + } + + public static MockHttpServletRequestBuilder postForm(String url, JSONObject object) { + final MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.post(url) + .contentType(MediaType.APPLICATION_FORM_URLENCODED); + object.forEach((path, value) -> { + LOGGER.debug(String.format("Adding form field (%s=%s) to HTTP request parameters", path, value)); + + if (object.get(path) instanceof String) { + LOGGER.info("{}:{}", path, object.get(path)); + mockHttpServletRequestBuilder.param(path, (String) object.get(path)); + } else { + LOGGER.info("{}:{}", path, JSON.toJSONString(object.get(path))); + mockHttpServletRequestBuilder.param(path, JSON.toJSONString(object.get(path))); + } + + }); + + return mockHttpServletRequestBuilder; + } + + private static Map getFormFields(Object form, Map formFields, String path) { + final List fields = form != null ? Arrays.asList(FieldUtils.getAllFields(form.getClass())) : new ArrayList<>(); + for (Field field : fields) { + final Class fieldType = field.getType(); + final Object fieldValue = getFieldValue(form, field); + if (isIterable(fieldType)) { + final Iterable iterableObject = getIterable(fieldValue, fieldType); + if (iterableObject != null) { + if (isComplexField(field)) { + int i = 0; + for (Object object : iterableObject) { + final String nestedPath = getPositionedField(path, field, i) + "."; + formFields.putAll(getFormFields(object, formFields, nestedPath)); + i++; + } + } else { + formFields.putAll(getCollectionFields(iterableObject, path, field)); + } + } + } else if (isMap(fieldType)) { + final Map map = getMap(fieldValue, fieldType); + if (map != null) { + map.forEach((key, value) -> formFields.put(getPositionedField(path, field, getStringValue(key)), getStringValue(value))); + } + } else { + if (isComplexField(field)) { + final String nestedPath = getNestedPath(field); + formFields.putAll(getFormFields(ReflectionTestUtils.getField(form, field.getName()), formFields, nestedPath)); + } else { + formFields.put(path + field.getName(), getFieldStringValue(form, field)); + } + } + } + return formFields; + } + + private static Map getMap(Object fieldValue, Class type) { + return Map.class.isAssignableFrom(type) ? (Map) fieldValue : null; + } + + private static Iterable getIterable(Object fieldValue, Class type) { + return Iterable.class.isAssignableFrom(type) ? (Iterable) fieldValue : CollectionUtils.arrayToList(fieldValue); + } + + private static Map getCollectionFields(Iterable iterable, String path, Field field) { + final Map fields = new TreeMap<>(); + int i = 0; + for (Object object : iterable) { + fields.put(getPositionedField(path, field, i), getStringValue(object)); + i++; + } + return fields; + } + + private static String getPositionedField(String path, Field field, int position) { + return getPositionedField(path, field, String.valueOf(position)); + } + + private static String getPositionedField(String path, Field field, String positionOrKey) { + return String.format("%s%s[%s]", path, field.getName(), positionOrKey); + } + + private static String getNestedPath(Field field) { + return field.getName() + "."; + } + + private static Object getFieldValue(Object form, Field field) { + return ReflectionTestUtils.getField(form, field.getName()); + } + + private static String getFieldStringValue(Object object, Field field) { + return getStringValue(getFieldValue(object, field)); + } + + private static String getStringValue(Object object) { + if (object != null) { +// final PropertyEditor propertyEditor = getPropertyEditorFor(object); +// if (propertyEditor != null) { +// propertyEditor.setValue(object); +// return propertyEditor.getAsText(); +// } + // Default strategy + return JSONObject.toJSONString(object); + } + return StringUtils.EMPTY; + } + + private static PropertyEditor getPropertyEditorFor(Object object) { + final Optional> propertyEditorEntry = propertyEditors.entrySet().stream() + .filter(entry -> entry.getKey().equals(object.getClass())) + .findFirst(); + return propertyEditorEntry.map(Map.Entry::getValue).orElse(null); + } + + private static boolean isComplexField(Field field) { + if (isGeneric(field)) { + final ParameterizedType genericType = (ParameterizedType) field.getGenericType(); + final Type[] actualTypeArguments = genericType.getActualTypeArguments(); + return isComplexType((Class) actualTypeArguments[0]); + } else { + return isComplexType(field.getType()); + } + } + + private static boolean isGeneric(Field field) { + return !(field.getGenericType() instanceof Class); + } + + private static boolean isComplexType(Class type) { + if (type.getComponentType() != null) { + return isComplexType(type.getComponentType()); + } + return !ClassUtils.isPrimitiveOrWrapper(type) + && !String.class.isAssignableFrom(type) + && !Date.class.isAssignableFrom(type) + && !Temporal.class.isAssignableFrom(type) + && type.getSuperclass() != null + && !Enum.class.isAssignableFrom(type.getSuperclass()); + } + + private static boolean isIterable(Class fieldClass) { + return Iterable.class.isAssignableFrom(fieldClass) || Object[].class.isAssignableFrom(fieldClass); + } + + private static boolean isMap(Class type) { + return Map.class.isAssignableFrom(type); + } +} \ No newline at end of file diff --git a/fw-rp-sdk/pom.xml b/fw-rp-sdk/pom.xml new file mode 100644 index 0000000..2ed9694 --- /dev/null +++ b/fw-rp-sdk/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + cn.fw + fw-rp-sdk + jar + 1.0 + fw-rp-sdk + + + 1.8 + UTF-8 + UTF-8 + + + + + + org.springframework.cloud + spring-cloud-dependencies + Dalston.SR1 + pom + import + + + + + + org.springframework.cloud + spring-cloud-starter-feign + + + + + + releases + Nexus Release Repository + http://nexus.feewee.cn/nexus/content/repositories/releases/ + + + snapshots + Nexus Snapshot Repository + http://nexus.feewee.cn/nexus/content/repositories/snapshots/ + true + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + 1.8 + 1.8 + + + + + + \ No newline at end of file diff --git a/fw-rp-service/pom.xml b/fw-rp-service/pom.xml new file mode 100644 index 0000000..f3f0585 --- /dev/null +++ b/fw-rp-service/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + cn.fw + fw-rp + 1.0 + + + fw-rp-service + jar + fw-rp-service + + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.3.0 + + + + com.alibaba + fastjson + + + cn.fw + fw-rp-common + ${project.parent.version} + + + cn.fw + fw-rp-domain + ${project.parent.version} + + + cn.fw + fw-rp-dao + ${project.parent.version} + + + net.sf.json-lib + json-lib + jdk15 + + + org.slf4j + slf4j-api + + + ch.qos.logback + logback-core + + + cn.fw + fw-auth-client + + + com.alibaba + fastjson + 1.1.31 + + + cn.fw + redis-spring-boot-starter + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..283e5c4 --- /dev/null +++ b/pom.xml @@ -0,0 +1,218 @@ + + + 4.0.0 + cn.fw + fw-rp + 1.0 + pom + fw-rp + + + fw-rp-common + fw-rp-domain + fw-rp-dao + fw-rp-service + fw-rp-man + fw-rp-sdk + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.10.RELEASE + + + + + 1.8 + UTF-8 + UTF-8 + Dalston.SR1 + 1.3.0 + 1.8.8 + 1.7.16 + 2.6.1 + 5.1.25 + 14.0.1 + 1.1.31 + 3.2.1 + 2.3 + 2.6.1 + 2.6.1 + 4.1.6.RELEASE + 1.0.2 + 1.0.3 + 1.0 + 1.16.16 + 2.6.1 + 4.12 + 4.3.8.RELEASE + 3.7 + 1.0 + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring.cloud.version} + pom + import + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + ${mybatis.spring.starter.version} + + + org.aspectj + aspectjweaver + ${aspectjweaver.version} + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.codehaus.janino + janino + ${janino.version} + + + mysql + mysql-connector-java + ${mysql.connector.version} + + + + + + com.google.guava + guava + ${guava.version} + + + + com.alibaba + fastjson + ${fastjson.version} + + + + commons-collections + commons-collections + ${commons.collections.version} + + + net.sf.json-lib + json-lib + jdk15 + ${json.lib.version} + + + + com.fasterxml.jackson.core + jackson-core + ${jackson.core.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.databind.version} + + + org.springframework + spring-tx + ${spring.tx.version} + + + + + junit + junit + ${junit.version} + test + + + org.springframework + spring-test + ${spring.test.version} + + + + + cn.fw + fw-data-base + ${fw.data.base.version} + + + cn.fw + fw-auth-client + ${fw.auth.client.version} + + + + cn.fw + fw-personnel-sdk + 1.0.1 + + + + cn.fw + fw-potential-sdk + 1.0.1 + + + + cn.fw.third + fw-wechat-sdk + 1.0 + + + + cn.fw + fw-base-config-sdk + 1.0.1 + + + cn.fw + redis-spring-boot-starter + ${redis.spring.starter.version} + + + org.projectlombok + lombok + ${lombok.version} + + + io.springfox + springfox-swagger-ui + ${swagger.version} + + + org.apache.commons + commons-lang3 + ${commons.lang3.version} + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + -- libgit2 0.22.2