Explorar o código

新增获取手机号码解密

anderx %!s(int64=4) %!d(string=hai) anos
pai
achega
9a2895537b

+ 6 - 27
src/main/java/com/kede/admin/controller/AdminLoginController.java

@@ -1,6 +1,5 @@
 package com.kede.admin.controller;
 
-import java.io.IOException;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.UUID;
@@ -9,12 +8,6 @@ import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 
 import org.apache.commons.lang3.StringUtils;
-import org.apache.http.HttpResponse;
-import org.apache.http.HttpStatus;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.HttpClientBuilder;
-import org.apache.http.util.EntityUtils;
 import org.apache.shiro.web.util.WebUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -24,7 +17,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.ResponseBody;
 
-import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.kede.common.bo.Result;
 import com.kede.common.constant.ErrorConstants;
@@ -33,9 +25,9 @@ import com.kede.common.dao.AdminMapper;
 import com.kede.common.dao.UserMapper;
 import com.kede.common.model.Admin;
 import com.kede.common.model.User;
-import com.kede.common.utils.LoggerUtils;
 import com.kede.common.utils.PasswordUtil;
 import com.kede.core.shiro.token.TokenManager;
+import com.kede.wxsdk.service.WxService;
 
 
 @Controller
@@ -45,6 +37,8 @@ public class AdminLoginController extends BaseController {
 	private AdminMapper		adminMapper;
 	@Resource
 	private UserMapper	userMapper; 
+	@Resource
+	private WxService wxService;
 	@Autowired
 	private PasswordUtil	passwordUtil;
 	
@@ -115,6 +109,7 @@ public class AdminLoginController extends BaseController {
 		JSONObject jsonObject=getUserOpenId(request, code);
 		String sessionKey =jsonObject.getString("session_key");
 		String openId=jsonObject.getString("openid");
+		System.out.println("=-================="+jsonObject.getString("unionid"));
 		if (StringUtils.isBlank(openId)) {
 			res.getError().add(buildError(ErrorConstants.PARAM_ERROR,"openId","openId"));
 			return res;
@@ -141,26 +136,10 @@ public class AdminLoginController extends BaseController {
 	private JSONObject getUserOpenId(HttpServletRequest request,String code){
 		String url = "https://api.weixin.qq.com/sns/jscode2session?";
 		url=url+"appid="+appId+"&secret="+appSecret+"&js_code="+code+"&grant_type=authorization_code";
-		return this.httpGet(url);
+		return wxService.httpGet(url);
 	}
 	
-	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;
-	}
+	
 
 
 	/**

+ 2 - 19
src/main/java/com/kede/common/controller/PublicApiController.java

@@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
+import com.alibaba.fastjson.JSONObject;
 import com.kede.activity.bo.InputActivity;
 import com.kede.activity.service.ActivityService;
 import com.kede.banners.service.BannersService;
@@ -154,29 +155,11 @@ public class PublicApiController extends BaseController {
 	@RequestMapping(value = "/payCallBack", method = RequestMethod.POST)
 	public String wxPayCallBack(HttpServletRequest req, HttpServletResponse response) {
 		LoggerUtils.debug(getClass(), "===============微信支付回调==========================");
-//		CallBackBo cb = JSON.parseObject(data, CallBackBo.class);
-//		LoggerUtils.debug(getClass(), "返回参数 request=%s",cb);
-//		Map<String, String> map=new HashedMap<>();
-//		if (cb.getSummary().equals("支付成功")) {
-//			wxService.wxPayCallBack(cb);
-//			map.put("code", "SUCCESS");
-//			map.put("message", "成功");
-//		}else {
-//			map.put("code", "FAIL");
-//			map.put("message", "失败");
-//		}
-		
-		// 1、读取微信通知 --- xml 格式字符串
 		wxService.wxPayCallBack(req);
-		
-		return NOTIFY_FAIL;
+		return NOTIFY_SUCCESS;
 	}
 
 	
 	
 	
-
-	
-	
-	
 }

+ 5 - 0
src/main/java/com/kede/core/mybatis/page/Pagination.java

@@ -23,6 +23,11 @@ public class Pagination<T> extends SimplePage implements java.io.Serializable, P
 		this.list = list;
 	}
 
+	public Pagination(Integer pageNo, Integer pageSize, Object object, Object object2) {
+		super(pageNo, pageSize, (Integer)object);
+		this.list = (List<?>) object2;
+	}
+
 	public int getFirstResult() {
 		return (pageNo - 1) * pageSize;
 	}

+ 18 - 0
src/main/java/com/kede/user/controller/UserApiController.java

@@ -8,12 +8,14 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
+import com.alibaba.fastjson.JSONObject;
 import com.kede.common.bo.Result;
 import com.kede.common.constant.ErrorConstants;
 import com.kede.common.controller.BaseController;
 import com.kede.common.model.Order;
 import com.kede.common.model.Project;
 import com.kede.common.utils.StringUtils;
+import com.kede.core.shiro.token.TokenManager;
 import com.kede.order.service.impl.OrderService;
 import com.kede.project.service.ProjectService;
 import com.kede.user.service.UserService;
@@ -91,4 +93,20 @@ public class UserApiController extends BaseController{
 		
 	}
 	
+	
+	@RequestMapping(value = "/getliveinfo" ,method = RequestMethod.GET)
+	public Result getliveinfo(Integer pageNo,Integer pageSize){
+		Result res =new Result();
+		res.data(wxService.getliveinfo(pageNo, pageSize));
+		return res;
+		
+	}
+	
+	
+	@RequestMapping(value = "/decryptData", method = RequestMethod.POST)
+	public String decryptData(String encryptedData, String iv) {
+		String sessionKey=(String) TokenManager.getVal2Session("sessionKey");
+		JSONObject js=wxService.pushDecryptData(sessionKey, encryptedData, iv);
+		return js.toJSONString();
+	}
 }

+ 119 - 23
src/main/java/com/kede/wxsdk/service/WxService.java

@@ -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;
+		}
+