|
|
@@ -2,9 +2,11 @@ package com.kede.wxsdk.service;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
+import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.net.InetAddress;
|
|
|
+import java.nio.charset.Charset;
|
|
|
import java.security.AlgorithmParameters;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Date;
|
|
|
@@ -20,20 +22,34 @@ import javax.xml.parsers.DocumentBuilder;
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
+import org.apache.commons.collections4.map.HashedMap;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.HttpStatus;
|
|
|
+import org.apache.http.client.ClientProtocolException;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.w3c.dom.Node;
|
|
|
import org.w3c.dom.NodeList;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.kede.common.dao.OrderMapper;
|
|
|
+import com.kede.common.dao.UserMapper;
|
|
|
import com.kede.common.error.BusinessException;
|
|
|
import com.kede.common.model.Order;
|
|
|
import com.kede.common.model.Project;
|
|
|
+import com.kede.common.model.User;
|
|
|
import com.kede.common.utils.LoggerUtils;
|
|
|
import com.kede.common.utils.StringUtils;
|
|
|
import com.kede.core.mybatis.JDBCIdGenerator;
|
|
|
+import com.kede.core.mybatis.page.Pagination;
|
|
|
import com.kede.core.shiro.token.TokenManager;
|
|
|
import com.kede.order.service.impl.OrderService;
|
|
|
import com.kede.wxsdk.WXPayConstants;
|
|
|
@@ -61,6 +77,9 @@ public class WxService {
|
|
|
private JDBCIdGenerator idGenerator;
|
|
|
@Autowired
|
|
|
private OrderService orderService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserMapper userMapper;
|
|
|
@Autowired
|
|
|
private OrderMapper orderMapper;
|
|
|
|
|
|
@@ -216,24 +235,7 @@ public class WxService {
|
|
|
}
|
|
|
|
|
|
|
|
|
-// public void wxPayCallBack(CallBackBo cb) {
|
|
|
-// //解密回调信息
|
|
|
-// byte[] bytes = StringUtils.strToByte(apiv3);
|
|
|
-// byte[] associatedData=StringUtils.strToByte(cb.getResource().getAssociatedData());
|
|
|
-// byte[] nonce=StringUtils.strToByte(cb.getResource().getNonce());
|
|
|
-// WxAPIV3AesUtil aesUtil = new WxAPIV3AesUtil(bytes);
|
|
|
-// String decryptToString = "";
|
|
|
-// try {
|
|
|
-// decryptToString = aesUtil.decryptToString(associatedData,nonce,cb.getResource().getCiphertext());
|
|
|
-// } catch (GeneralSecurityException | IOException e) {
|
|
|
-// e.printStackTrace();
|
|
|
-// }
|
|
|
-// JSONObject json=JSON.parseObject(decryptToString);
|
|
|
-// String transaction_id=(String) json.get("transaction_id");
|
|
|
-// String out_trade_no=(String) json.get("out_trade_no");
|
|
|
-// LoggerUtils.debug(getClass(), "transaction_id="+transaction_id+",out_trade_no="+out_trade_no);
|
|
|
-// orderMapper.updateByOrderNo(out_trade_no,transaction_id);
|
|
|
-// }
|
|
|
+
|
|
|
|
|
|
public void wxPayCallBack(HttpServletRequest req) {
|
|
|
BufferedReader reader;
|
|
|
@@ -324,7 +326,15 @@ public class WxService {
|
|
|
return xmlString;
|
|
|
}
|
|
|
|
|
|
- public JSONObject decryptData(String sessionKey,String encryptedData, String iv) throws Exception{
|
|
|
+ /**
|
|
|
+ * 参数解密
|
|
|
+ * @param sessionKey
|
|
|
+ * @param encryptedData
|
|
|
+ * @param iv
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public JSONObject pushDecryptData(String sessionKey,String encryptedData, String iv) {
|
|
|
byte[] dataByte = Base64.decodeBase64(encryptedData);
|
|
|
// 加密秘钥
|
|
|
byte[] keyByte = Base64.decodeBase64(sessionKey);
|
|
|
@@ -341,8 +351,7 @@ public class WxService {
|
|
|
keyByte = temp;
|
|
|
}
|
|
|
// 初始化
|
|
|
-// Security.addProvider(new BouncyCastleProvider());
|
|
|
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding","BC");
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
|
|
|
AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
|
|
|
parameters.init(new IvParameterSpec(ivByte));
|
|
|
@@ -350,7 +359,13 @@ public class WxService {
|
|
|
byte[] resultByte = cipher.doFinal(dataByte);
|
|
|
if (null != resultByte && resultByte.length > 0) {
|
|
|
String result = new String(resultByte, "UTF-8");
|
|
|
- return JSONObject.parseObject(result);
|
|
|
+ JSONObject js=JSONObject.parseObject(result);
|
|
|
+ String mobile=(String) js.get("purePhoneNumber");
|
|
|
+ User u=new User();
|
|
|
+ u.setId(userMapper.selectByOpenId((String)TokenManager.getVal2Session("openId")).getId());
|
|
|
+ u.setMobile(mobile);
|
|
|
+ userMapper.updateByPrimaryKeySelective(u);
|
|
|
+ return js;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
@@ -358,10 +373,91 @@ public class WxService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public Object getliveinfo(Integer pageNo,Integer pageSize){
|
|
|
+ String url = String.format("https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=%s", getAccessToken());
|
|
|
+ Map<String, Object>map=new HashedMap<String, Object>();
|
|
|
+ map.put("start", (pageNo - 1) * pageSize);
|
|
|
+ map.put("limit", pageSize);
|
|
|
+ map=this.getPostMethod(url, map);
|
|
|
+ Pagination<Object> p=new Pagination<Object>(pageNo, pageSize, map.get("total"), map.get("room_info"));
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getAccessToken() {
|
|
|
+ String access_token=(String) TokenManager.getVal2Session("access_token");
|
|
|
+ String expires_in= (String) TokenManager.getVal2Session("expires_in");
|
|
|
+ if (access_token!=null&&expires_in!=null) {
|
|
|
+ expires_in=expires_in+7200;
|
|
|
+ if (new Date().getTime()<Long.valueOf(expires_in)) {
|
|
|
+ return access_token;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String url = String.format(
|
|
|
+ "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appId,
|
|
|
+ appSecret);
|
|
|
+ JSONObject js = this.httpGet(url);
|
|
|
+ LoggerUtils.debug(getClass(), String.format("返回参数={%s}", js.toJSONString()));
|
|
|
+ String errcode =js.getString("errcode");
|
|
|
+ if (errcode!=null) {
|
|
|
+ throw new BusinessException("获取AccessToekn失败!");
|
|
|
+ }
|
|
|
+ access_token = js.getString("access_token");
|
|
|
+ expires_in = js.getString("expires_in");
|
|
|
+ TokenManager.setVal2Session("access_token", access_token);
|
|
|
+ TokenManager.setVal2Session("expires_in", expires_in);
|
|
|
+ return access_token;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+ public JSONObject httpGet(String url) {
|
|
|
+ JSONObject jsonResult = null;
|
|
|
+ try {
|
|
|
+ HttpClient client = HttpClientBuilder.create().build();//获取DefaultHttpClient请求
|
|
|
+ HttpGet request = new HttpGet(url);
|
|
|
+ HttpResponse response = client.execute(request);
|
|
|
+ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
|
|
+ String strResult = EntityUtils.toString(response.getEntity(),"UTF-8");//此处设定编码格式
|
|
|
+ jsonResult = JSON.parseObject(strResult);
|
|
|
+ } else {
|
|
|
+ LoggerUtils.error(getClass(), "HttpStatus异常");
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return jsonResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject getPostMethod(String url,Map<String, Object>map){
|
|
|
+ HttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.addHeader("Content-type", "application/json; charset=utf-8");
|
|
|
+ httpPost.setHeader("Accept", "application/json");
|
|
|
+ httpPost.setEntity(new StringEntity(JSON.toJSONString(map), Charset.forName("UTF-8")));
|
|
|
+ HttpResponse response;
|
|
|
+ try {
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ if (null == response || response.getStatusLine() == null) {
|
|
|
+ throw new Exception("Post Request For Url[{}] is not ok. Response is null");
|
|
|
+ } else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
|
|
|
+ throw new Exception("Post Request For Url[{}] is not ok. Response Status Code is {}");
|
|
|
+ }
|
|
|
+ String resultString = EntityUtils.toString(response.getEntity());
|
|
|
+ JSONObject jsonObj = JSONObject.parseObject(resultString);
|
|
|
+ LoggerUtils.debug(getClass(), jsonObj.toJSONString());
|
|
|
+ return jsonObj;
|
|
|
+ } catch (ClientProtocolException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
|