|  | @ -1,6 +1,5 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.care.service.hz;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.BaseHzInterfaceDictDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.util.DSLUtils;
 | 
	
	
		
			
				|  | @ -9,13 +8,6 @@ import com.yihu.jw.care.util.MD5Utils;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.util.baseHzInterfaceDictEntity;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.date.DateUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.http.HttpClientUtil;
 | 
	
		
			
				|  |  | import org.apache.commons.httpclient.Header;
 | 
	
		
			
				|  |  | import org.apache.commons.httpclient.HttpClient;
 | 
	
		
			
				|  |  | import org.apache.commons.httpclient.methods.PostMethod;
 | 
	
		
			
				|  |  | import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
 | 
	
		
			
				|  |  | import org.apache.commons.httpclient.methods.multipart.Part;
 | 
	
		
			
				|  |  | import org.apache.commons.httpclient.params.HttpMethodParams;
 | 
	
		
			
				|  |  | import org.apache.commons.io.IOUtils;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
	
		
			
				|  | @ -27,7 +19,11 @@ import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.net.URLEncoder;
 | 
	
		
			
				|  |  | import java.io.BufferedReader;
 | 
	
		
			
				|  |  | import java.io.DataOutputStream;
 | 
	
		
			
				|  |  | import java.io.InputStreamReader;
 | 
	
		
			
				|  |  | import java.net.HttpURLConnection;
 | 
	
		
			
				|  |  | import java.net.URL;
 | 
	
		
			
				|  |  | import java.util.Date;
 | 
	
		
			
				|  |  | import java.util.HashMap;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
	
		
			
				|  | @ -51,31 +47,24 @@ public class HzInterfaceService {
 | 
	
		
			
				|  |  |     private HttpClientUtil httpClientUtil;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public JSONObject electricityTable(String name,String date) throws Exception {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //
 | 
	
		
			
				|  |  |         String cityApi = getAreaApi(date);
 | 
	
		
			
				|  |  |         JSONObject jsonObject = JSONObject.parseObject(cityApi);
 | 
	
		
			
				|  |  |         JSONArray dataArr = jsonObject.getJSONArray("data");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         for (Object o : dataArr) {
 | 
	
		
			
				|  |  |         JSONObject jsonObject = getAreaApi(date);
 | 
	
		
			
				|  |  |         System.out.println(jsonObject.toJSONString());
 | 
	
		
			
				|  |  |         /*for (Object o : dataArr) {
 | 
	
		
			
				|  |  |             //JSONObject.parseObject();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         }*/
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private String getAreaApi(String date) throws Exception{
 | 
	
		
			
				|  |  |         baseHzInterfaceDictEntity dbkey = DBKEY();
 | 
	
		
			
				|  |  |     private JSONObject getAreaApi(String date) throws Exception{
 | 
	
		
			
				|  |  |         baseHzInterfaceDictEntity dbkey = requestDBKEY();
 | 
	
		
			
				|  |  |         if(!redisTemplate.hasKey(dbkey.getCode())){
 | 
	
		
			
				|  |  |             PopBasicInfo();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         String privateKey = redisTemplate.opsForValue().get(dbkey.getCode());
 | 
	
		
			
				|  |  |         //String privateKey = redisTemplate.opsForValue().get(dbkey.getCode());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //请求接口地址(政务网环境根据接口文档地址改动)
 | 
	
		
			
				|  |  |         String electricityTableSql = "SELECT url FROM base_hz_interface_dict WHERE `status` = 1 AND `code` = 'zfjddjlrdbjk'";
 | 
	
	
		
			
				|  | @ -87,82 +76,60 @@ public class HzInterfaceService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         JSONObject json = new JSONObject();
 | 
	
		
			
				|  |  |         json.put("queryDate",date);
 | 
	
		
			
				|  |  |         /*json.put("sign",MD5Utils.encoderByMd5(dbkey.getAppKey()+ dbkey.getRequestSecret() + requestTime));
 | 
	
		
			
				|  |  |         json.put("requestTime",date);
 | 
	
		
			
				|  |  |         json.put("appKey",privateKey);*/
 | 
	
		
			
				|  |  |         url +="?appKey="+privateKey+"&sign="+sign+"&requestTime="+date;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         url +="?appKey="+dbkey.getAppKey()+"&sign="+sign+"&requestTime="+requestTime+"&queryDate="+date;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String s = httpURLConnectionPOST(url, json);
 | 
	
		
			
				|  |  |         logger.info("自己封装的请求方式:" + s);
 | 
	
		
			
				|  |  |         HttpEntity<JSONObject> response = httpClientUtil.assesTokenPostHttp(url, json, HttpMethod.POST);
 | 
	
		
			
				|  |  |         logger.info("电表接口调用结果----> "+response);
 | 
	
		
			
				|  |  |         JSONObject responseBody = response.getBody();
 | 
	
		
			
				|  |  |         logger.info("电表接口调用结果Body----> "+responseBody);
 | 
	
		
			
				|  |  |         /*String request = RSAUtils.encryptByPrivateKey(json.toJSONString(),privateKey);
 | 
	
		
			
				|  |  |         String sign = RSAUtils.sign(request,privateKey);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         Part[] parts = {
 | 
	
		
			
				|  |  |                 new StringPart("request", request,"UTF-8"),
 | 
	
		
			
				|  |  |                 new StringPart("sign", sign,"UTF-8")
 | 
	
		
			
				|  |  |         };
 | 
	
		
			
				|  |  |         String result = post(url,parts);*/
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |         return JSONObject.parseObject(responseBody.get("data").toString());
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     //post方式
 | 
	
		
			
				|  |  |     private static String post(String url, Part[] parts){
 | 
	
		
			
				|  |  |         /**导入SSL证书 [解决PKIX path building failed错误问题]**/
 | 
	
		
			
				|  |  |         initCert(url);
 | 
	
		
			
				|  |  |         HttpClient client = new HttpClient();
 | 
	
		
			
				|  |  |         PostMethod post = new PostMethod(url);
 | 
	
		
			
				|  |  |         post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
 | 
	
		
			
				|  |  |         if(parts != null) post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
 | 
	
		
			
				|  |  |         String response="";
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             //按需加载请求头参数
 | 
	
		
			
				|  |  |             post.setRequestHeader(new Header("systemDepartmentCode", URLEncoder.encode("请求部门","utf-8")));
 | 
	
		
			
				|  |  |             post.setRequestHeader(new Header("areaCode", URLEncoder.encode("所在行政区域","utf-8")));
 | 
	
		
			
				|  |  |             client.executeMethod(post);
 | 
	
		
			
				|  |  |             response = IOUtils.toString(post.getResponseBodyAsStream(),"UTF-8");
 | 
	
		
			
				|  |  |         } catch (Exception e){
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }finally {
 | 
	
		
			
				|  |  |             post.releaseConnection();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return response;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private static void initCert(final String url){
 | 
	
		
			
				|  |  |         /**
 | 
	
		
			
				|  |  |          * 导入SSL证书 [解决PKIX path building failed错误问题]
 | 
	
		
			
				|  |  |          *    --使用[javax.net.ssl.trustStore]不能解决多域名证书问题
 | 
	
		
			
				|  |  |          *    --建议将生成的证书文件 copy 到$JAVA_HOME/jre/lib/security目录下
 | 
	
		
			
				|  |  |          */
 | 
	
		
			
				|  |  |         String[] c = url.replaceFirst("(http|https)://","").split("/")[0].split(":");
 | 
	
		
			
				|  |  |         String host = c[0];
 | 
	
		
			
				|  |  |         Integer port = (c.length == 1) ? 443 : Integer.parseInt(c[1]);
 | 
	
		
			
				|  |  |         if(System.getProperty(host + "ssl_cert_path") != null) return;
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             String sslCertPath = InstallCert.createCert(host,port);
 | 
	
		
			
				|  |  |             System.out.println("证书文件生成路径--->" + sslCertPath);
 | 
	
		
			
				|  |  |             System.setProperty(host + "ssl_cert_path", sslCertPath);
 | 
	
		
			
				|  |  |             System.setProperty("javax.net.ssl.trustStore", sslCertPath);
 | 
	
		
			
				|  |  |         }catch (Exception e){
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         /****证书导入成功****/
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     // 刷新密钥还存在时可以调用这个方法
 | 
	
		
			
				|  |  |     private String refreshKey(baseHzInterfaceDictEntity refreshdbkey,baseHzInterfaceDictEntity requestdbkey){
 | 
	
		
			
				|  |  |         Map<String, String> params = new HashMap<String, String>();
 | 
	
		
			
				|  |  |         String appkey = refreshdbkey.getAppKey();   //appKey
 | 
	
		
			
				|  |  |         String requstSecret = refreshdbkey.getRefreshSecret();//刷新密钥
 | 
	
		
			
				|  |  |         String requestTime = DSLUtils.dateToLong(new Date()) + "";//时间戳
 | 
	
		
			
				|  |  |         String url = refreshdbkey.getUrl();  //请求url
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String sign = MD5Utils.encoderByMd5(appkey + requstSecret + requestTime);
 | 
	
		
			
				|  |  |         params.put("appKey", appkey);
 | 
	
		
			
				|  |  |         params.put("sign", sign);
 | 
	
		
			
				|  |  |         params.put("requestTime", requestTime);
 | 
	
		
			
				|  |  |         JSONObject resultObj = HttpToolUtil.httpURLConnection_sendPost(url, params);
 | 
	
		
			
				|  |  |         logger.info("接口返回值:"+resultObj.toString());
 | 
	
		
			
				|  |  |         System.out.println("接口返回值:"+resultObj.toString());
 | 
	
		
			
				|  |  |         JSONObject datasObj = JSONObject.parseObject(resultObj.get("datas").toString());
 | 
	
		
			
				|  |  |         requestdbkey.setRequestSecret(datasObj.get("requestSecret").toString());
 | 
	
		
			
				|  |  |         requestdbkey.setRequestSecretEndTime(DateUtil.strToDateLong(datasObj.get("requestSecretEndTime").toString()));
 | 
	
		
			
				|  |  |         baseHzInterfaceDictDao.save(requestdbkey);
 | 
	
		
			
				|  |  |         redisTemplate.opsForValue().set(requestdbkey.getCode(),resultObj.get("requestSecret").toString(),14, TimeUnit.MINUTES);
 | 
	
		
			
				|  |  |         return datasObj.toString();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  |     public String PopBasicInfo() {
 | 
	
		
			
				|  |  |         Map<String, String> params = new HashMap<String, String>();
 | 
	
		
			
				|  |  |         baseHzInterfaceDictEntity dbkey = DBKEY();
 | 
	
		
			
				|  |  |         String appkey = dbkey.getAppKey();   //appKey
 | 
	
		
			
				|  |  |         String requstSecret = dbkey.getAppPwd();//app密钥
 | 
	
		
			
				|  |  |         baseHzInterfaceDictEntity requestdbkey = requestDBKEY();
 | 
	
		
			
				|  |  |         baseHzInterfaceDictEntity refreshdbkey = refreshDBKEY();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         /*if(redisTemplate.hasKey(refreshdbkey.getCode())){
 | 
	
		
			
				|  |  |             return refreshKey(refreshdbkey,requestdbkey);
 | 
	
		
			
				|  |  |         }*/
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String appkey = requestdbkey.getAppKey();   //appKey
 | 
	
		
			
				|  |  |         String requstSecret = requestdbkey.getAppPwd();//app密钥
 | 
	
		
			
				|  |  |         String requestTime = DSLUtils.dateToLong(new Date()) + "";//时间戳
 | 
	
		
			
				|  |  |         String url = dbkey.getUrl();  //请求url
 | 
	
		
			
				|  |  |         String url = requestdbkey.getUrl();  //请求url
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //sign 格式:APP_KEY + APP秘钥+时间戳,经过MD5加密生成的串(字母小写)
 | 
	
		
			
				|  |  |         String sign = MD5Utils.encoderByMd5(appkey + requstSecret + requestTime);
 | 
	
	
		
			
				|  | @ -174,20 +141,77 @@ public class HzInterfaceService {
 | 
	
		
			
				|  |  |         logger.info("接口返回值:"+resultObj.toString());
 | 
	
		
			
				|  |  |         System.out.println("接口返回值:"+resultObj.toString());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         //请求密钥
 | 
	
		
			
				|  |  |         JSONObject datasObj = JSONObject.parseObject(resultObj.get("datas").toString());
 | 
	
		
			
				|  |  |         dbkey.setRefreshSecret(datasObj.get("refreshSecret").toString());
 | 
	
		
			
				|  |  |         dbkey.setRefreshSecretEndTime(DateUtil.strToDateLong(datasObj.get("refreshSecretEndTime").toString()));
 | 
	
		
			
				|  |  |         dbkey.setRequestSecret(datasObj.get("requestSecret").toString());
 | 
	
		
			
				|  |  |         dbkey.setRequestSecretEndTime(DateUtil.strToDateLong(datasObj.get("requestSecretEndTime").toString()));
 | 
	
		
			
				|  |  |         baseHzInterfaceDictDao.save(dbkey);
 | 
	
		
			
				|  |  |         requestdbkey.setRequestSecret(datasObj.get("requestSecret").toString());
 | 
	
		
			
				|  |  |         requestdbkey.setRequestSecretEndTime(DateUtil.strToDateLong(datasObj.get("requestSecretEndTime").toString()));
 | 
	
		
			
				|  |  |         //刷新密钥
 | 
	
		
			
				|  |  |         refreshdbkey.setRefreshSecret(datasObj.get("refreshSecret").toString());
 | 
	
		
			
				|  |  |         baseHzInterfaceDictDao.save(requestdbkey);
 | 
	
		
			
				|  |  |         baseHzInterfaceDictDao.save(refreshdbkey);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         redisTemplate.opsForValue().set(dbkey.getCode(),datasObj.get("requestSecret").toString(),14, TimeUnit.MINUTES);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         redisTemplate.opsForValue().set(requestdbkey.getCode(),datasObj.get("requestSecret").toString(),14, TimeUnit.MINUTES);
 | 
	
		
			
				|  |  |         redisTemplate.opsForValue().set(refreshdbkey.getCode(),datasObj.get("refreshSecret").toString(),47, TimeUnit.HOURS);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return resultObj.toString();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public baseHzInterfaceDictEntity DBKEY(){
 | 
	
		
			
				|  |  |     // 请求密钥 15分钟过期
 | 
	
		
			
				|  |  |     public baseHzInterfaceDictEntity requestDBKEY(){
 | 
	
		
			
				|  |  |         String sql = "SELECT * FROM base_hz_interface_dict WHERE `status` = 1 AND `code` = 'dbmyhq'";
 | 
	
		
			
				|  |  |         return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(baseHzInterfaceDictEntity.class));
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     // 刷新密钥 48小时过期
 | 
	
		
			
				|  |  |     public baseHzInterfaceDictEntity refreshDBKEY(){
 | 
	
		
			
				|  |  |         String sql = "SELECT * FROM base_hz_interface_dict WHERE `status` = 1 AND `code` = 'dbmysxhq'";
 | 
	
		
			
				|  |  |         return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(baseHzInterfaceDictEntity.class));
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private static String httpURLConnectionPOST(String url, JSONObject data) {
 | 
	
		
			
				|  |  |         StringBuffer strBf = new StringBuffer();
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             URL realUrl = new URL(url);
 | 
	
		
			
				|  |  |             //将realUrl以 open方法返回的urlConnection  连接强转为HttpURLConnection连接  (标识一个url所引用的远程对象连接)
 | 
	
		
			
				|  |  |             //此时cnnection只是为一个连接对象,待连接中
 | 
	
		
			
				|  |  |             HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
 | 
	
		
			
				|  |  |             //设置连接输出流为true,默认false (post请求是以流的方式隐式的传递参数)
 | 
	
		
			
				|  |  |             connection.setDoOutput(true);
 | 
	
		
			
				|  |  |             //设置连接输入流为true
 | 
	
		
			
				|  |  |             connection.setDoInput(true);
 | 
	
		
			
				|  |  |             //设置请求方式为post
 | 
	
		
			
				|  |  |             connection.setRequestMethod("POST");
 | 
	
		
			
				|  |  |             //post请求缓存设为false
 | 
	
		
			
				|  |  |             connection.setUseCaches(false);
 | 
	
		
			
				|  |  |             //设置该HttpURLConnection实例是否自动执行重定向
 | 
	
		
			
				|  |  |             connection.setInstanceFollowRedirects(true);
 | 
	
		
			
				|  |  |             //设置请求头里面的各个属性 (以下为设置内容的类型,设置为经过urlEncoded编码过的from参数)
 | 
	
		
			
				|  |  |             connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
 | 
	
		
			
				|  |  |             //建立连接 (请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行)
 | 
	
		
			
				|  |  |             connection.connect();
 | 
	
		
			
				|  |  |             //创建输入输出流,用于往连接里面输出携带的参数
 | 
	
		
			
				|  |  |             DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
 | 
	
		
			
				|  |  |             String query = data.toString();
 | 
	
		
			
				|  |  |             //将参数输出到连接
 | 
	
		
			
				|  |  |             dataout.write(query.getBytes("UTF-8"));
 | 
	
		
			
				|  |  |             // 输出完成后刷新并关闭流
 | 
	
		
			
				|  |  |             dataout.flush();
 | 
	
		
			
				|  |  |             dataout.close(); // 关闭流
 | 
	
		
			
				|  |  |             BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
 | 
	
		
			
				|  |  |             String lines;
 | 
	
		
			
				|  |  |             while ((lines = reader.readLine()) != null) {
 | 
	
		
			
				|  |  |                 lines = new String(lines.getBytes(), "utf-8");
 | 
	
		
			
				|  |  |                 strBf.append(lines);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             reader.close();
 | 
	
		
			
				|  |  |             connection.disconnect();
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return strBf.toString();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |