w_sky_common

branch_wudengtao
wdt 7 months ago
parent 4880bc2947
commit e002d6f715

@ -0,0 +1,43 @@
package com.sky.context;
/**
* BaseContext ThreadLocal 线
* 线
*/
public class BaseContext {
/**
* threadLocal ThreadLocal 线
* 线线
*/
private static ThreadLocal<Long> threadLocal = new ThreadLocal<>();
/**
* 线ID
* IDIDID
*
* @param id ID
*/
public static void setCurrentId(Long id) {
threadLocal.set(id);
}
/**
* 线ID
* 线ID null
*
* @return 线ID
*/
public static Long getCurrentId() {
return threadLocal.get();
}
/**
* 线ID
* 线
*/
public static void removeCurrentId() {
threadLocal.remove();
}
}

@ -0,0 +1,19 @@
package com.sky.exception;
/**
* AddressBookBusinessException 簿
* BaseException使簿
*/
public class AddressBookBusinessException extends BaseException {
/**
* AddressBookBusinessException
*
* @param msg
* 便
*/
public AddressBookBusinessException(String msg) {
super(msg); // 调用父类 BaseException 的构造函数,传递错误信息。
}
}

@ -0,0 +1,19 @@
package com.sky.exception;
/**
* OrderBusinessException
* BaseException使
*/
public class OrderBusinessException extends BaseException {
/**
* OrderBusinessException
*
* @param msg
* 便
*/
public OrderBusinessException(String msg) {
super(msg); // 调用父类 BaseException 的构造函数,传递错误信息。
}
}

@ -0,0 +1,19 @@
package com.sky.exception;
/**
* ShoppingCartBusinessException
* BaseException使
*/
public class ShoppingCartBusinessException extends BaseException {
/**
* ShoppingCartBusinessException
*
* @param msg
*
*/
public ShoppingCartBusinessException(String msg) {
super(msg); // 调用父类 BaseException 的构造函数,并传递错误消息。
}
}

@ -0,0 +1,52 @@
package com.sky.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* JwtProperties JWTJSON Web Tokens
* Spring Boot @ConfigurationProperties application.properties application.yml
*/
@Component
@ConfigurationProperties(prefix = "sky.jwt") // 指定配置文件中属性的前缀
@Data
public class JwtProperties {
/**
* adminSecretKey JWT
* JWT
*/
private String adminSecretKey;
/**
* adminTtl JWT
*
*/
private long adminTtl;
/**
* adminTokenName JWT
* HTTP JWT
*/
private String adminTokenName;
/**
* userSecretKey JWT
* JWT
*/
private String userSecretKey;
/**
* userTtl JWT
*
*/
private long userTtl;
/**
* userTokenName JWT
* HTTP JWT
*/
private String userTokenName;
}

@ -0,0 +1,76 @@
package com.sky.result;
import lombok.Data;
import java.io.Serializable;
/**
* Result
* Serializable 便
*
* @param <T>
*/
@Data
public class Result<T> implements Serializable {
private static final long serialVersionUID = 1L; // 序列化版本号,用于确保反序列化时的兼容性
/**
* 1 0
*
*/
private Integer code;
/**
*
* 便
*/
private String msg;
/**
*
*
*/
private T data;
/**
* Result
*
* @param <T>
* @return Result 1
*/
public static <T> Result<T> success() {
Result<T> result = new Result<T>();
result.code = 1; // 设置状态码为成功
return result;
}
/**
* Result
*
* @param object
* @param <T>
* @return Result 1
*/
public static <T> Result<T> success(T object) {
Result<T> result = new Result<T>();
result.data = object; // 设置返回的数据
result.code = 1; // 设置状态码为成功
return result;
}
/**
* Result
*
* @param msg
* @param <T>
* @return Result 0
*/
public static <T> Result<T> error(String msg) {
Result<T> result = new Result<T>();
result.msg = msg; // 设置错误信息
result.code = 0; // 设置状态码为失败
return result;
}
}

@ -0,0 +1,233 @@
package com.sky.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.properties.WeChatProperties;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
/**
* 退
*/
@Component
public class WeChatPayUtil {
/**
*
*/
public static final String JSAPI = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";
/**
* 退
*/
public static final String REFUNDS = "https://api.mch.weixin.qq.com/v3/refund/domestic/refunds";
@Autowired
private WeChatProperties weChatProperties;
/**
*
* 使API
*
* @return CloseableHttpClient
*/
private CloseableHttpClient getClient() {
PrivateKey merchantPrivateKey = null;
try {
// 加载商户API私钥
merchantPrivateKey = PemUtil.loadPrivateKey(new FileInputStream(new File(weChatProperties.getPrivateKeyFilePath())));
// 加载微信支付平台证书
X509Certificate x509Certificate = PemUtil.loadCertificate(new FileInputStream(new File(weChatProperties.getWeChatPayCertFilePath())));
List<X509Certificate> wechatPayCertificates = Arrays.asList(x509Certificate);
// 构建HttpClient自动处理签名和验签
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
.withMerchant(weChatProperties.getMchid(), weChatProperties.getMchSerialNo(), merchantPrivateKey)
.withWechatPay(wechatPayCertificates);
CloseableHttpClient httpClient = builder.build();
return httpClient;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* POSTURL
*
* @param url URL
* @param body JSON
* @return
* @throws Exception
*/
private String post(String url, String body) throws Exception {
CloseableHttpClient httpClient = getClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.toString());
httpPost.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
httpPost.addHeader("Wechatpay-Serial", weChatProperties.getMchSerialNo());
httpPost.setEntity(new StringEntity(body, "UTF-8"));
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
String bodyAsString = EntityUtils.toString(response.getEntity());
return bodyAsString;
} finally {
httpClient.close();
response.close();
}
}
/**
* GETURL
*
* @param url URL
* @return
* @throws Exception
*/
private String get(String url) throws Exception {
CloseableHttpClient httpClient = getClient();
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.toString());
httpGet.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
httpGet.addHeader("Wechatpay-Serial", weChatProperties.getMchSerialNo());
CloseableHttpResponse response = httpClient.execute(httpGet);
try {
String bodyAsString = EntityUtils.toString(response.getEntity());
return bodyAsString;
} finally {
httpClient.close();
response.close();
}
}
/**
* 使JSAPI
*
* @param orderNum
* @param total
* @param description
* @param openid openid
* @return
* @throws Exception
*/
private String jsapi(String orderNum, BigDecimal total, String description, String openid) throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put("appid", weChatProperties.getAppid());
jsonObject.put("mchid", weChatProperties.getMchid());
jsonObject.put("description", description);
jsonObject.put("out_trade_no", orderNum);
jsonObject.put("notify_url", weChatProperties.getNotifyUrl());
JSONObject amount = new JSONObject();
amount.put("total", total.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).intValue());
amount.put("currency", "CNY");
jsonObject.put("amount", amount);
JSONObject payer = new JSONObject();
payer.put("openid", openid);
jsonObject.put("payer", payer);
String body = jsonObject.toJSONString();
return post(JSAPI, body);
}
/**
*
*
* @param orderNum
* @param total
* @param description
* @param openid openid
* @return JSONObject
* @throws Exception
*/
public JSONObject pay(String orderNum, BigDecimal total, String description, String openid) throws Exception {
String bodyAsString = jsapi(orderNum, total, description, openid);
JSONObject jsonObject = JSON.parseObject(bodyAsString);
String prepayId = jsonObject.getString("prepay_id");
if (prepayId != null) {
String timeStamp = String.valueOf(System.currentTimeMillis() / 1000);
String nonceStr = RandomStringUtils.randomNumeric(32);
ArrayList<Object> list = new ArrayList<>();
list.add(weChatProperties.getAppid());
list.add(timeStamp);
list.add(nonceStr);
list.add("prepay_id=" + prepayId);
StringBuilder stringBuilder = new StringBuilder();
for (Object o : list) {
stringBuilder.append(o).append("\n");
}
String signMessage = stringBuilder.toString();
byte[] message = signMessage.getBytes();
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(PemUtil.loadPrivateKey(new FileInputStream(new File(weChatProperties.getPrivateKeyFilePath()))));
signature.update(message);
String packageSign = Base64.getEncoder().encodeToString(signature.sign());
JSONObject jo = new JSONObject();
jo.put("timeStamp", timeStamp);
jo.put("nonceStr", nonceStr);
jo.put("package", "prepay_id=" + prepayId);
jo.put("signType", "RSA");
jo.put("paySign", packageSign);
return jo;
}
return jsonObject;
}
/**
* 退
*
* @param outTradeNo
* @param outRefundNo 退
* @param refund 退
* @param total
* @return
* @throws Exception
*/
public String refund(String outTradeNo, String outRefundNo, BigDecimal refund, BigDecimal total) throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put("out_trade_no", outTradeNo);
jsonObject.put("out_refund_no", outRefundNo);
JSONObject amount = new JSONObject();
amount.put("refund", refund.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).intValue());
amount.put("total", total.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).intValue());
amount.put("currency", "CNY");
jsonObject.put("amount", amount);
jsonObject.put("notify_url", weChatProperties.getRefundNotifyUrl());
String body = jsonObject.toJSONString();
return post(REFUNDS, body);
}
}
Loading…
Cancel
Save