update
This commit is contained in:
@@ -587,6 +587,44 @@ public class CrmebUtil {
|
||||
// setScaler(1,BigDecimal.ROUND_HALF_DOWN);//四舍五入,2.35变成2.3,如果是5则向下舍
|
||||
}
|
||||
|
||||
/**
|
||||
* 同比率计算 //同比增长率= ((当前周期 - 上一个周期) ÷ 上一个周期 ) *100%
|
||||
* @param i1 当前周期
|
||||
* @param i2 上一个周期
|
||||
* @author Mr.Zhang
|
||||
* @since 2020-05-06
|
||||
* @
|
||||
*/
|
||||
public static BigDecimal getRateBig(Integer i1, Integer i2){
|
||||
BigDecimal b1 = new BigDecimal(i1);
|
||||
BigDecimal b2 = new BigDecimal(i2);
|
||||
return getRateBig(b1, b2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 同比率计算 //同比增长率= ((当前周期 - 上一个周期) ÷ 上一个周期 ) *100%
|
||||
* @param b1 当前周期
|
||||
* @param b2 上一个周期
|
||||
* @author Mr.Zhang
|
||||
* @since 2020-05-06
|
||||
* @
|
||||
*/
|
||||
public static BigDecimal getRateBig(BigDecimal b1, BigDecimal b2){
|
||||
//计算差值
|
||||
|
||||
if(b2.equals(b1)){
|
||||
//数值一样,说明没有增长
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
if(b2.equals(BigDecimal.ZERO)){
|
||||
//b2是0
|
||||
return BigDecimal.TEN.multiply(BigDecimal.TEN).setScale(2, BigDecimal.ROUND_UP);
|
||||
}
|
||||
|
||||
return (b1.subtract(b2)).multiply(BigDecimal.TEN).multiply(BigDecimal.TEN).divide(b2, BigDecimal.ROUND_UP);
|
||||
}
|
||||
|
||||
/**
|
||||
* hash 转换
|
||||
* @param hash final byte[] hash参数
|
||||
|
||||
@@ -507,10 +507,10 @@ public final class DateUtil {
|
||||
startTime = list.get(0);
|
||||
endTime = list.get(1);
|
||||
|
||||
if (startTime.equals(endTime)) {
|
||||
// if (startTime.equals(endTime)) {
|
||||
startTime = DateUtil.appointedDayStrToFormatStr(startTime, Constants.DATE_FORMAT_DATE, Constants.DATE_FORMAT_START);
|
||||
endTime = DateUtil.appointedDayStrToFormatStr(endTime, Constants.DATE_FORMAT_DATE, Constants.DATE_FORMAT_END);
|
||||
}
|
||||
// }
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
39
crmeb/src/main/java/com/utils/WXPayXmlUtil.java
Normal file
39
crmeb/src/main/java/com/utils/WXPayXmlUtil.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package com.utils;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import javax.xml.XMLConstants;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
/**
|
||||
* 微信支付xml工具类
|
||||
* +----------------------------------------------------------------------
|
||||
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
* +----------------------------------------------------------------------
|
||||
* | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
|
||||
* +----------------------------------------------------------------------
|
||||
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
* +----------------------------------------------------------------------
|
||||
* | Author: CRMEB Team <admin@crmeb.com>
|
||||
* +----------------------------------------------------------------------
|
||||
*/
|
||||
public final class WXPayXmlUtil {
|
||||
public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
||||
documentBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
||||
documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
||||
documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
||||
documentBuilderFactory.setXIncludeAware(false);
|
||||
documentBuilderFactory.setExpandEntityReferences(false);
|
||||
|
||||
return documentBuilderFactory.newDocumentBuilder();
|
||||
}
|
||||
|
||||
public static Document newDocument() throws ParserConfigurationException {
|
||||
return newDocumentBuilder().newDocument();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,23 @@
|
||||
package com.utils;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.constants.Constants;
|
||||
import com.constants.PayConstants;
|
||||
import com.exception.CrmebException;
|
||||
import com.zbkj.crmeb.payment.vo.wechat.CreateOrderRequestVo;
|
||||
import com.zbkj.crmeb.payment.vo.wechat.WxRefundVo;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import java.util.HashMap;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 微信支付工具类
|
||||
@@ -19,10 +33,6 @@ import java.util.HashMap;
|
||||
*/
|
||||
public class WxPayUtil {
|
||||
|
||||
/**
|
||||
* TODO 后期微信签名生成、校验等在这里开发
|
||||
*/
|
||||
|
||||
/**
|
||||
* 处理 HTTPS API返回数据,转换成Map对象。return_code为SUCCESS时,验证签名。
|
||||
*
|
||||
@@ -48,4 +58,141 @@ public class WxPayUtil {
|
||||
throw new CrmebException(String.format("return_code value %s is invalid in XML: %s", return_code, xmlStr));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取随机字符串,长度要求在32位以内。
|
||||
*/
|
||||
public static String getNonceStr() {
|
||||
return DigestUtils.md5Hex(CrmebUtil.getUuid() + CrmebUtil.randomCount(111111, 666666));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取sign
|
||||
* @param vo 微信公共下单对象
|
||||
* @param signKey 微信签名key
|
||||
* @return String
|
||||
*/
|
||||
public static String getSign(CreateOrderRequestVo vo, String signKey) {
|
||||
// 对象转map
|
||||
Map<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(vo), Map.class);
|
||||
// map排序
|
||||
Set<String> keySet = map.keySet();
|
||||
String[] keyArray = keySet.toArray(new String[keySet.size()]);
|
||||
Arrays.sort(keyArray);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String k : keyArray) {
|
||||
if (k.equals(PayConstants.FIELD_SIGN)) {
|
||||
continue;
|
||||
}
|
||||
if (ObjectUtil.isNotNull(map.get(k))) // 参数值为空,则不参与签名
|
||||
sb.append(k).append("=").append(map.get(k)).append("&");
|
||||
}
|
||||
sb.append("key=").append(signKey);
|
||||
String sign = SecureUtil.md5(sb.toString()).toUpperCase();
|
||||
System.out.println("sign ========== " + sign);
|
||||
return sign;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取sign
|
||||
* @param wxRefundVo 微信退款对象
|
||||
* @param signKey 微信签名key
|
||||
* @return String
|
||||
*/
|
||||
public static String getSign(WxRefundVo wxRefundVo, String signKey) {
|
||||
// 对象转map
|
||||
Map<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(wxRefundVo), Map.class);
|
||||
// map排序
|
||||
Set<String> keySet = map.keySet();
|
||||
String[] keyArray = keySet.toArray(new String[keySet.size()]);
|
||||
Arrays.sort(keyArray);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String k : keyArray) {
|
||||
if (k.equals(PayConstants.FIELD_SIGN)) {
|
||||
continue;
|
||||
}
|
||||
if (ObjectUtil.isNotNull(map.get(k))) // 参数值为空,则不参与签名
|
||||
sb.append(k).append("=").append(map.get(k)).append("&");
|
||||
}
|
||||
sb.append("key=").append(signKey);
|
||||
String sign = SecureUtil.md5(sb.toString()).toUpperCase();
|
||||
System.out.println("sign ========== " + sign);
|
||||
return sign;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取sign
|
||||
* @param map 待签名数据
|
||||
* @param signKey 微信签名key
|
||||
* @return String
|
||||
*/
|
||||
public static String getSign(Map<String, String> map, String signKey) {
|
||||
// map排序
|
||||
Set<String> keySet = map.keySet();
|
||||
String[] keyArray = keySet.toArray(new String[keySet.size()]);
|
||||
Arrays.sort(keyArray);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String k : keyArray) {
|
||||
if (k.equals(PayConstants.FIELD_SIGN)) {
|
||||
continue;
|
||||
}
|
||||
if (StrUtil.isNotBlank(map.get(k)) && map.get(k).trim().length() > 0) // 参数值为空,则不参与签名
|
||||
sb.append(k).append("=").append(map.get(k).trim()).append("&");
|
||||
}
|
||||
sb.append("key=").append(signKey);
|
||||
String sign = SecureUtil.md5(sb.toString()).toUpperCase();
|
||||
System.out.println("sign ========== " + sign);
|
||||
return sign;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间戳,单位秒
|
||||
* @return Long
|
||||
*/
|
||||
public static Long getCurrentTimestamp() {
|
||||
return System.currentTimeMillis()/1000;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间戳,单位毫秒
|
||||
* @return Long
|
||||
*/
|
||||
public static Long getCurrentTimestampMs() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* XML格式字符串转换为Map
|
||||
*
|
||||
* @param strXML XML字符串
|
||||
* @return XML数据转换后的Map
|
||||
* @throws Exception
|
||||
*/
|
||||
public static Map<String, String> xmlToMap(String strXML) throws Exception {
|
||||
try {
|
||||
Map<String, String> data = new HashMap<String, String>();
|
||||
DocumentBuilder documentBuilder = WXPayXmlUtil.newDocumentBuilder();
|
||||
InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
|
||||
org.w3c.dom.Document doc = documentBuilder.parse(stream);
|
||||
doc.getDocumentElement().normalize();
|
||||
NodeList nodeList = doc.getDocumentElement().getChildNodes();
|
||||
for (int idx = 0; idx < nodeList.getLength(); ++idx) {
|
||||
Node node = nodeList.item(idx);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
org.w3c.dom.Element element = (org.w3c.dom.Element) node;
|
||||
data.put(element.getNodeName(), element.getTextContent());
|
||||
}
|
||||
}
|
||||
try {
|
||||
stream.close();
|
||||
} catch (Exception ex) {
|
||||
// do nothing
|
||||
}
|
||||
return data;
|
||||
} catch (Exception ex) {
|
||||
System.out.println(StrUtil.format("Invalid XML, can not convert to map. Error message: {}. XML content: {}", ex.getMessage(), strXML));
|
||||
throw ex;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,30 +10,35 @@ import org.dom4j.Element;
|
||||
import org.dom4j.io.SAXReader;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* XML 工具类
|
||||
* +----------------------------------------------------------------------
|
||||
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
* +----------------------------------------------------------------------
|
||||
* | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
|
||||
* +----------------------------------------------------------------------
|
||||
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
* +----------------------------------------------------------------------
|
||||
* | Author: CRMEB Team <admin@crmeb.com>
|
||||
* +----------------------------------------------------------------------
|
||||
* XML 工具类
|
||||
* +----------------------------------------------------------------------
|
||||
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||||
* +----------------------------------------------------------------------
|
||||
* | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
|
||||
* +----------------------------------------------------------------------
|
||||
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||||
* +----------------------------------------------------------------------
|
||||
* | Author: CRMEB Team <admin@crmeb.com>
|
||||
* +----------------------------------------------------------------------
|
||||
*/
|
||||
public class XmlUtil {
|
||||
public static Map<String,String> xmlToMap(HttpServletRequest request)
|
||||
{
|
||||
Map<String,String> map = new HashMap<>();
|
||||
public static Map<String, String> xmlToMap(HttpServletRequest request) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
SAXReader reader = new SAXReader();
|
||||
|
||||
InputStream in = null;
|
||||
@@ -47,7 +52,7 @@ public class XmlUtil {
|
||||
}
|
||||
} catch (IOException | DocumentException e) {
|
||||
e.printStackTrace();
|
||||
} finally{
|
||||
} finally {
|
||||
try {
|
||||
assert in != null;
|
||||
in.close();
|
||||
@@ -61,14 +66,14 @@ public class XmlUtil {
|
||||
/**
|
||||
* 将发送消息封装成对应的xml格式
|
||||
*/
|
||||
public static HashMap<String,Object> xmlToMap(String strxml)throws Exception{
|
||||
public static HashMap<String, Object> xmlToMap(String strxml) throws Exception {
|
||||
strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");
|
||||
|
||||
HashMap<String,Object> map = new HashMap<>();
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
SAXReader reader = new SAXReader();
|
||||
InputStream inputStream = new ByteArrayInputStream(strxml.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
if(StringUtils.isBlank(strxml)) {
|
||||
if (StringUtils.isBlank(strxml)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -76,8 +81,8 @@ public class XmlUtil {
|
||||
Element root = document.getRootElement();
|
||||
List<Element> list = root.elements();
|
||||
|
||||
for (Element e : list){
|
||||
map.put(e.getName(),e.getText());
|
||||
for (Element e : list) {
|
||||
map.put(e.getName(), e.getText());
|
||||
}
|
||||
inputStream.close();
|
||||
|
||||
@@ -88,10 +93,42 @@ public class XmlUtil {
|
||||
* 将发送消息封装成对应的xml格式
|
||||
*/
|
||||
public static String objectToXml(Object object) {
|
||||
XStream xstream = new XStream(new Xpp3Driver(new NoNameCoder())); //不需要转义
|
||||
XStream xstream = new XStream(new Xpp3Driver(new NoNameCoder())); //不需要转义
|
||||
xstream.alias("xml", object.getClass());
|
||||
return xstream.toXML(object);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将Map转换为XML格式的字符串
|
||||
*
|
||||
* @param data Map类型数据
|
||||
* @return XML格式的字符串
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String mapToXml(Map<String, String> data) throws Exception {
|
||||
org.w3c.dom.Document document = WXPayXmlUtil.newDocument();
|
||||
org.w3c.dom.Element root = document.createElement("xml");
|
||||
document.appendChild(root);
|
||||
for (String key : data.keySet()) {
|
||||
String value = data.get(key);
|
||||
if (value == null) {
|
||||
value = "";
|
||||
}
|
||||
value = value.trim();
|
||||
org.w3c.dom.Element filed = document.createElement(key);
|
||||
filed.appendChild(document.createTextNode(value));
|
||||
root.appendChild(filed);
|
||||
}
|
||||
TransformerFactory tf = TransformerFactory.newInstance();
|
||||
Transformer transformer = tf.newTransformer();
|
||||
DOMSource source = new DOMSource(document);
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
transformer.transform(source, result);
|
||||
String output = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
|
||||
writer.close();
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user