苍穹外卖初始化代码
This commit is contained in:
68
sky-common/src/main/java/com/sky/utils/AliOssUtil.java
Normal file
68
sky-common/src/main/java/com/sky/utils/AliOssUtil.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package com.sky.utils;
|
||||
|
||||
import com.aliyun.oss.ClientException;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.OSSException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@Slf4j
|
||||
public class AliOssUtil {
|
||||
|
||||
private String endpoint;
|
||||
private String accessKeyId;
|
||||
private String accessKeySecret;
|
||||
private String bucketName;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param bytes
|
||||
* @param objectName
|
||||
* @return
|
||||
*/
|
||||
public String upload(byte[] bytes, String objectName) {
|
||||
|
||||
// 创建OSSClient实例。
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
|
||||
try {
|
||||
// 创建PutObject请求。
|
||||
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
|
||||
} catch (OSSException oe) {
|
||||
System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
||||
+ "but was rejected with an error response for some reason.");
|
||||
System.out.println("Error Message:" + oe.getErrorMessage());
|
||||
System.out.println("Error Code:" + oe.getErrorCode());
|
||||
System.out.println("Request ID:" + oe.getRequestId());
|
||||
System.out.println("Host ID:" + oe.getHostId());
|
||||
} catch (ClientException ce) {
|
||||
System.out.println("Caught an ClientException, which means the client encountered "
|
||||
+ "a serious internal problem while trying to communicate with OSS, "
|
||||
+ "such as not being able to access the network.");
|
||||
System.out.println("Error Message:" + ce.getMessage());
|
||||
} finally {
|
||||
if (ossClient != null) {
|
||||
ossClient.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
//文件访问路径规则 https://BucketName.Endpoint/ObjectName
|
||||
StringBuilder stringBuilder = new StringBuilder("https://");
|
||||
stringBuilder
|
||||
.append(bucketName)
|
||||
.append(".")
|
||||
.append(endpoint)
|
||||
.append("/")
|
||||
.append(objectName);
|
||||
|
||||
log.info("文件上传到:{}", stringBuilder.toString());
|
||||
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
}
|
||||
179
sky-common/src/main/java/com/sky/utils/HttpClientUtil.java
Normal file
179
sky-common/src/main/java/com/sky/utils/HttpClientUtil.java
Normal file
@@ -0,0 +1,179 @@
|
||||
package com.sky.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
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.client.utils.URIBuilder;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Http工具类
|
||||
*/
|
||||
public class HttpClientUtil {
|
||||
|
||||
static final int TIMEOUT_MSEC = 5 * 1000;
|
||||
|
||||
/**
|
||||
* 发送GET方式请求
|
||||
* @param url
|
||||
* @param paramMap
|
||||
* @return
|
||||
*/
|
||||
public static String doGet(String url,Map<String,String> paramMap){
|
||||
// 创建Httpclient对象
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
|
||||
String result = "";
|
||||
CloseableHttpResponse response = null;
|
||||
|
||||
try{
|
||||
URIBuilder builder = new URIBuilder(url);
|
||||
if(paramMap != null){
|
||||
for (String key : paramMap.keySet()) {
|
||||
builder.addParameter(key,paramMap.get(key));
|
||||
}
|
||||
}
|
||||
URI uri = builder.build();
|
||||
|
||||
//创建GET请求
|
||||
HttpGet httpGet = new HttpGet(uri);
|
||||
|
||||
//发送请求
|
||||
response = httpClient.execute(httpGet);
|
||||
|
||||
//判断响应状态
|
||||
if(response.getStatusLine().getStatusCode() == 200){
|
||||
result = EntityUtils.toString(response.getEntity(),"UTF-8");
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
try {
|
||||
response.close();
|
||||
httpClient.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送POST方式请求
|
||||
* @param url
|
||||
* @param paramMap
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String doPost(String url, Map<String, String> paramMap) throws IOException {
|
||||
// 创建Httpclient对象
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
CloseableHttpResponse response = null;
|
||||
String resultString = "";
|
||||
|
||||
try {
|
||||
// 创建Http Post请求
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
|
||||
// 创建参数列表
|
||||
if (paramMap != null) {
|
||||
List<NameValuePair> paramList = new ArrayList();
|
||||
for (Map.Entry<String, String> param : paramMap.entrySet()) {
|
||||
paramList.add(new BasicNameValuePair(param.getKey(), param.getValue()));
|
||||
}
|
||||
// 模拟表单
|
||||
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
|
||||
httpPost.setConfig(builderRequestConfig());
|
||||
|
||||
// 执行http请求
|
||||
response = httpClient.execute(httpPost);
|
||||
|
||||
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
} finally {
|
||||
try {
|
||||
response.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return resultString;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送POST方式请求
|
||||
* @param url
|
||||
* @param paramMap
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String doPost4Json(String url, Map<String, String> paramMap) throws IOException {
|
||||
// 创建Httpclient对象
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
CloseableHttpResponse response = null;
|
||||
String resultString = "";
|
||||
|
||||
try {
|
||||
// 创建Http Post请求
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
|
||||
if (paramMap != null) {
|
||||
//构造json格式数据
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
for (Map.Entry<String, String> param : paramMap.entrySet()) {
|
||||
jsonObject.put(param.getKey(),param.getValue());
|
||||
}
|
||||
StringEntity entity = new StringEntity(jsonObject.toString(),"utf-8");
|
||||
//设置请求编码
|
||||
entity.setContentEncoding("utf-8");
|
||||
//设置数据类型
|
||||
entity.setContentType("application/json");
|
||||
httpPost.setEntity(entity);
|
||||
}
|
||||
|
||||
httpPost.setConfig(builderRequestConfig());
|
||||
|
||||
// 执行http请求
|
||||
response = httpClient.execute(httpPost);
|
||||
|
||||
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
} finally {
|
||||
try {
|
||||
response.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return resultString;
|
||||
}
|
||||
private static RequestConfig builderRequestConfig() {
|
||||
return RequestConfig.custom()
|
||||
.setConnectTimeout(TIMEOUT_MSEC)
|
||||
.setConnectionRequestTimeout(TIMEOUT_MSEC)
|
||||
.setSocketTimeout(TIMEOUT_MSEC).build();
|
||||
}
|
||||
|
||||
}
|
||||
58
sky-common/src/main/java/com/sky/utils/JwtUtil.java
Normal file
58
sky-common/src/main/java/com/sky/utils/JwtUtil.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package com.sky.utils;
|
||||
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.JwtBuilder;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
public class JwtUtil {
|
||||
/**
|
||||
* 生成jwt
|
||||
* 使用Hs256算法, 私匙使用固定秘钥
|
||||
*
|
||||
* @param secretKey jwt秘钥
|
||||
* @param ttlMillis jwt过期时间(毫秒)
|
||||
* @param claims 设置的信息
|
||||
* @return
|
||||
*/
|
||||
public static String createJWT(String secretKey, long ttlMillis, Map<String, Object> claims) {
|
||||
// 指定签名的时候使用的签名算法,也就是header那部分
|
||||
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
|
||||
|
||||
// 生成JWT的时间
|
||||
long expMillis = System.currentTimeMillis() + ttlMillis;
|
||||
Date exp = new Date(expMillis);
|
||||
|
||||
// 设置jwt的body
|
||||
JwtBuilder builder = Jwts.builder()
|
||||
// 如果有私有声明,一定要先设置这个自己创建的私有的声明,这个是给builder的claim赋值,一旦写在标准的声明赋值之后,就是覆盖了那些标准的声明的
|
||||
.setClaims(claims)
|
||||
// 设置签名使用的签名算法和签名使用的秘钥
|
||||
.signWith(signatureAlgorithm, secretKey.getBytes(StandardCharsets.UTF_8))
|
||||
// 设置过期时间
|
||||
.setExpiration(exp);
|
||||
|
||||
return builder.compact();
|
||||
}
|
||||
|
||||
/**
|
||||
* Token解密
|
||||
*
|
||||
* @param secretKey jwt秘钥 此秘钥一定要保留好在服务端, 不能暴露出去, 否则sign就可以被伪造, 如果对接多个客户端建议改造成多个
|
||||
* @param token 加密后的token
|
||||
* @return
|
||||
*/
|
||||
public static Claims parseJWT(String secretKey, String token) {
|
||||
// 得到DefaultJwtParser
|
||||
Claims claims = Jwts.parser()
|
||||
// 设置签名的秘钥
|
||||
.setSigningKey(secretKey.getBytes(StandardCharsets.UTF_8))
|
||||
// 设置需要解析的jwt
|
||||
.parseClaimsJws(token).getBody();
|
||||
return claims;
|
||||
}
|
||||
|
||||
}
|
||||
235
sky-common/src/main/java/com/sky/utils/WeChatPayUtil.java
Normal file
235
sky-common/src/main/java/com/sky/utils/WeChatPayUtil.java
Normal file
@@ -0,0 +1,235 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* 获取调用微信接口的客户端工具对象
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private CloseableHttpClient getClient() {
|
||||
PrivateKey merchantPrivateKey = null;
|
||||
try {
|
||||
//merchantPrivateKey商户API私钥,如何加载商户API私钥请看常见问题
|
||||
merchantPrivateKey = PemUtil.loadPrivateKey(new FileInputStream(new File(weChatProperties.getPrivateKeyFilePath())));
|
||||
//加载平台证书文件
|
||||
X509Certificate x509Certificate = PemUtil.loadCertificate(new FileInputStream(new File(weChatProperties.getWeChatPayCertFilePath())));
|
||||
//wechatPayCertificates微信支付平台证书列表。你也可以使用后面章节提到的“定时更新平台证书功能”,而不需要关心平台证书的来龙去脉
|
||||
List<X509Certificate> wechatPayCertificates = Arrays.asList(x509Certificate);
|
||||
|
||||
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
|
||||
.withMerchant(weChatProperties.getMchid(), weChatProperties.getMchSerialNo(), merchantPrivateKey)
|
||||
.withWechatPay(wechatPayCertificates);
|
||||
|
||||
// 通过WechatPayHttpClientBuilder构造的HttpClient,会自动的处理签名和验签
|
||||
CloseableHttpClient httpClient = builder.build();
|
||||
return httpClient;
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送post方式请求
|
||||
*
|
||||
* @param url
|
||||
* @param body
|
||||
* @return
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送get方式请求
|
||||
*
|
||||
* @param url
|
||||
* @return
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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);
|
||||
System.out.println(jsonObject);
|
||||
|
||||
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
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user