🎨 优化重构部分代码
This commit is contained in:
@@ -4,7 +4,6 @@ import com.google.common.collect.Lists;
|
|||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import jodd.util.MathUtil;
|
|
||||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -173,7 +172,7 @@ public class GsonHelper {
|
|||||||
* @param keyOrValue 包含key或value的数组
|
* @param keyOrValue 包含key或value的数组
|
||||||
*/
|
*/
|
||||||
public static void put(JsonObject jsonObject, Object... keyOrValue) {
|
public static void put(JsonObject jsonObject, Object... keyOrValue) {
|
||||||
if (MathUtil.isOdd(keyOrValue.length)) {
|
if (keyOrValue.length % 2 == 1) {
|
||||||
throw new WxRuntimeException("参数个数必须为偶数");
|
throw new WxRuntimeException("参数个数必须为偶数");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.google.gson.annotations.SerializedName;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.experimental.Accessors;
|
import lombok.experimental.Accessors;
|
||||||
|
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.security.PrivateKey;
|
import java.security.PrivateKey;
|
||||||
@@ -72,6 +73,8 @@ public class WxPayUnifiedOrderV3Result implements Serializable {
|
|||||||
@Data
|
@Data
|
||||||
@Accessors(chain = true)
|
@Accessors(chain = true)
|
||||||
public static class JsapiResult implements Serializable {
|
public static class JsapiResult implements Serializable {
|
||||||
|
private static final long serialVersionUID = 4465376277943307271L;
|
||||||
|
|
||||||
private String appId;
|
private String appId;
|
||||||
private String timeStamp;
|
private String timeStamp;
|
||||||
private String nonceStr;
|
private String nonceStr;
|
||||||
@@ -80,16 +83,18 @@ public class WxPayUnifiedOrderV3Result implements Serializable {
|
|||||||
private String paySign;
|
private String paySign;
|
||||||
|
|
||||||
private String getSignStr() {
|
private String getSignStr() {
|
||||||
return String.format("%s\n%s\n%s\n%s\n", appId, timeStamp, nonceStr, packageValue);
|
return String.format("%s%n%s%n%s%n%s%n", appId, timeStamp, nonceStr, packageValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Accessors(chain = true)
|
@Accessors(chain = true)
|
||||||
public static class AppResult implements Serializable {
|
public static class AppResult implements Serializable {
|
||||||
|
private static final long serialVersionUID = 5465773025172875110L;
|
||||||
|
|
||||||
private String appid;
|
private String appid;
|
||||||
private String partnerid;
|
private String partnerId;
|
||||||
private String prepayid;
|
private String prepayId;
|
||||||
private String packageValue;
|
private String packageValue;
|
||||||
private String noncestr;
|
private String noncestr;
|
||||||
private String timestamp;
|
private String timestamp;
|
||||||
@@ -111,14 +116,15 @@ public class WxPayUnifiedOrderV3Result implements Serializable {
|
|||||||
return (T) this.h5Url;
|
return (T) this.h5Url;
|
||||||
case APP:
|
case APP:
|
||||||
AppResult appResult = new AppResult();
|
AppResult appResult = new AppResult();
|
||||||
appResult.setAppid(appId).setPrepayid(this.prepayId).setPartnerid(mchId)
|
appResult.setAppid(appId).setPrepayId(this.prepayId).setPartnerId(mchId)
|
||||||
.setNoncestr(nonceStr).setTimestamp(timestamp)
|
.setNoncestr(nonceStr).setTimestamp(timestamp)
|
||||||
//暂填写固定值Sign=WXPay
|
//暂填写固定值Sign=WXPay
|
||||||
.setPackageValue("Sign=WXPay");
|
.setPackageValue("Sign=WXPay");
|
||||||
return (T) appResult;
|
return (T) appResult;
|
||||||
case NATIVE:
|
case NATIVE:
|
||||||
return (T) this.codeUrl;
|
return (T) this.codeUrl;
|
||||||
}
|
default:
|
||||||
return null;
|
throw new WxRuntimeException("不支持的支付类型");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +1,18 @@
|
|||||||
package com.github.binarywang.wxpay.util;
|
package com.github.binarywang.wxpay.util;
|
||||||
|
|
||||||
import jodd.io.IOUtil;
|
|
||||||
import jodd.util.ClassUtil;
|
import jodd.util.ClassUtil;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 基于jodd.util.ResourcesUtil改造实现
|
* 基于jodd.util.ResourcesUtil改造实现
|
||||||
|
*
|
||||||
* @author jodd
|
* @author jodd
|
||||||
*/
|
*/
|
||||||
public class ResourcesUtils {
|
public class ResourcesUtils {
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves given resource as URL.
|
|
||||||
* @see #getResourceUrl(String, ClassLoader)
|
|
||||||
*/
|
|
||||||
public static URL getResourceUrl(final String resourceName) {
|
|
||||||
return getResourceUrl(resourceName, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves given resource as URL. Resource is always absolute and may
|
* Retrieves given resource as URL. Resource is always absolute and may
|
||||||
* starts with a slash character.
|
* starts with a slash character.
|
||||||
@@ -71,19 +61,9 @@ public class ResourcesUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getResourceAsString(final String resourceName) throws IOException {
|
|
||||||
final InputStream inputStream = getResourceAsStream(resourceName);
|
|
||||||
try {
|
|
||||||
final char[] data = IOUtil.readChars(inputStream);
|
|
||||||
return new String(data);
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
IOUtil.close(inputStream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens a resource of the specified name for reading.
|
* Opens a resource of the specified name for reading.
|
||||||
|
*
|
||||||
* @see #getResourceAsStream(String, ClassLoader)
|
* @see #getResourceAsStream(String, ClassLoader)
|
||||||
*/
|
*/
|
||||||
public static InputStream getResourceAsStream(final String resourceName) throws IOException {
|
public static InputStream getResourceAsStream(final String resourceName) throws IOException {
|
||||||
@@ -92,6 +72,7 @@ public class ResourcesUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens a resource of the specified name for reading.
|
* Opens a resource of the specified name for reading.
|
||||||
|
*
|
||||||
* @see #getResourceUrl(String, ClassLoader)
|
* @see #getResourceUrl(String, ClassLoader)
|
||||||
*/
|
*/
|
||||||
public static InputStream getResourceAsStream(final String resourceName, final ClassLoader callingClass) throws IOException {
|
public static InputStream getResourceAsStream(final String resourceName, final ClassLoader callingClass) throws IOException {
|
||||||
@@ -102,19 +83,4 @@ public class ResourcesUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Opens a resource of the specified name for reading. Controls caching,
|
|
||||||
* that is important when the same jar is reloaded using custom classloader.
|
|
||||||
*/
|
|
||||||
public static InputStream getResourceAsStream(final String resourceName, final ClassLoader callingClass,
|
|
||||||
final boolean useCache) throws IOException {
|
|
||||||
final URL url = getResourceUrl(resourceName, callingClass);
|
|
||||||
if (url != null) {
|
|
||||||
final URLConnection urlConnection = url.openConnection();
|
|
||||||
urlConnection.setUseCaches(useCache);
|
|
||||||
return urlConnection.getInputStream();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ import java.security.*;
|
|||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author cloudx
|
||||||
|
*/
|
||||||
public class SignUtils {
|
public class SignUtils {
|
||||||
|
|
||||||
public static String sign(String string, PrivateKey privateKey) {
|
public static String sign(String string, PrivateKey privateKey) {
|
||||||
try {
|
try {
|
||||||
Signature sign = Signature.getInstance("SHA256withRSA");
|
Signature sign = Signature.getInstance("SHA256withRSA");
|
||||||
@@ -35,7 +37,7 @@ public class SignUtils {
|
|||||||
* 生成随机字符串
|
* 生成随机字符串
|
||||||
*
|
*
|
||||||
* @param length 字符串长度
|
* @param length 字符串长度
|
||||||
* @return
|
* @return 随机字符串
|
||||||
*/
|
*/
|
||||||
public static String genRandomStr(int length) {
|
public static String genRandomStr(int length) {
|
||||||
String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||||
|
|||||||
Reference in New Issue
Block a user