//package com.blueland.core.util;
//
//import java.security.interfaces.RSAKey;
//import java.time.Duration;
//import java.time.Instant;
//import java.util.Map;
//
//import org.apache.http.HttpHeaders;
//import org.apache.http.client.methods.HttpGet;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.message.BasicHeader;
//import org.apache.http.protocol.HTTP;
//import com.alibaba.fastjson.JSONArray;
//import gateway.api.*;
//
//import lombok.extern.slf4j.Slf4j;
//
//@Slf4j
//// @Service
//public class Apigateway {
//
//	// KEY
//	private static final String TestAppToken = "5Zaex1VJp1LiXHV54bvoQtam3SAcKbbL";
//
//	// 应用RSA私钥（PEM格式）
//	private final String TestPrivatePem = "-----BEGIN RSA PRIVATE KEY-----\n"
//			+ "MIICXAIBAAKBgQDYjFY9ceLar5FJqtkey3YqWnp5jyjulqRj1L4wM8RFqJ68LdqQ\n"
//			+ "q3bkjg/EROJIrqFH+QIcql2X4+XDxHjxElJm/rbr4aVJVWP96gd2dlQE42rijAyh\n"
//			+ "Sfe+1pOaWoHtO98qbs75wt7WqhBhqfApMLJHK5gtx2y5R/Klfcau2X/z1wIDAQAB\n"
//			+ "AoGAfSP0foO+9mO5UVojKPGpcuZgmrkJjX+OtoZt9kskJKFCQjs5cJs2FPSn1eN8\n"
//			+ "5UXNvFsBRnGJrsorsIts3NuatAw1TgmR2l4nYaQAQBzik2l+bPX9cn39DZwD8xRt\n"
//			+ "mB0kgwj0S4Pf2tC2hKRYEDGNUkRBTsNgkDDQyTOfP4298UkCQQD0K+vcrMv3idVa\n"
//			+ "TDO1Ihd7xbUi3Fob7OHxLWudaDZ7T/jnaMcsbJooJHD1WKLOEfCd63A0GviepbvH\n"
//			+ "lZGa+6Z1AkEA4wnYjwar4o4Yjy2OJEbi6DDBsJQ2h0bbBfSfmunXBEjd/GVgZSya\n"
//			+ "T6LOu8QBUJ2fPY7e1zxlTFu/8UCzRtIfmwJAZHD42lMeqFpDsJxuHNHEVT70Dz4T\n"
//			+ "ty8PlpV3kk2/M6SnIBJC9GxMUb0D1nk6p4hLsbqa8mQx55kHkMdfGSDxzQJBAJPJ\n"
//			+ "QPI5AYIDxPDOUoxCqElEE5CBqcz8zm1eAtawCIp9dCJrSwgQtJHKBJAs0j5Lm/BS\n"
//			+ "I7QvWDEQOC0uVIRxGqkCQC5kGzQqjdw0F8xsJcqdzTt9X29VdNrwJWl0IdBK8gXt\n"
//			+ "5HYkBZa3PSaeDz+YpVtYUt5HO25VbBM0PTZEpkuezLU=\n" + "-----END RSA PRIVATE KEY-----";
//
//	private volatile static Apigateway singleton = new Apigateway();
//
//	private Apigateway() {
//		loadJwtContext();
//	}
//
//	public static Apigateway getInstance() {
//		return singleton;
//	}
//
//	private JwtContext jwtContext;
//	private Instant createTime = Instant.now();
//
//	private void loadJwtContext() {
//		jwtContext = JwtContext.create(TestAppToken, (RSAKey) RSAUtils.parsePrivateKeyFromPEM(TestPrivatePem), 1200);
//	}
//
//	private JwtToken createJwtToken() {
//		// 生成的jwt令牌可多次使用，为安全起见请设置令牌过期时间。
//		Instant currDate = Instant.now();
//		if (Duration.between(currDate, createTime).getSeconds() >= 1100) {
//			loadJwtContext();
//		}
//		JwtToken jwt_token = jwtContext.createJwtToken();
////		log.error("token:"+jwt_token.toAuthorizationString());
//		return jwt_token;
//	}
//
//	private final static String APPLICATION_JSON = "application/json";
//	public static final String CHARSET = "UTF-8";
//
//	/**
//	 * HTTP Get 获取内容
//	 *
//	 * @param url请求的url地址
//	 *            ?之前的地址
//	 * @param params请求的参数
//	 * @param charset编码格式
//	 * @return 页面内容
//	 */
//	public String sendGet(String url, Map<String, Object> params) {
//		String apiUrl = url;
//		if (params != null && params.size() > 0) {
//			StringBuffer param = new StringBuffer();
//			int i = 0;
//			for (String key : params.keySet()) {
//				if (i == 0)
//					param.append("?");
//				else
//					param.append("&");
//				param.append(key).append("=").append(params.get(key));
//				i++;
//			}
//			apiUrl += param;
//		}
//		HttpGet httpget = null;
//
//		String result = null;
//		try {
//
//			httpget = new HttpGet(apiUrl);
//			httpget.addHeader(HttpHeaders.AUTHORIZATION, createJwtToken().toAuthorizationString());
//			if (ViaGatewayUtlis.isAppRequestViaGateway()) {
//				ViaGatewayUtlis.copyRequestChainForHttpClient(httpget);
//			} else {
//				ViaGatewayUtlis.firstRequestChainForHttpClient(httpget);
//			}
//			result = HttpClientHelper.requestExecute(httpget, HttpClientHelper.RESPONSE_STRING_EXTRACTOR);
//
//		} catch (Exception e) {
//			log.error("apigateway->sendget->请求出错: " + e.getMessage(), e);
//		}
//		return result;
//	}
//
//	/**
//	 * HTTP Post 获取内容
//	 *
//	 * @param url请求的url地址
//	 *            ?之前的地址
//	 * @param params请求的参数
//	 * @param charset编码格式
//	 * @return 页面内容
//	 */
//	public String sendPost(String url, Map<String, Object> params) {
//		String param = JSONArray.toJSON(params).toString();
//
//		return post(url, param);
//	}
//
//	public String post(String url, String postData) {
//		String result = "";
//		HttpPost httppost = null;
//
//		try {
//
//			httppost = new HttpPost(url);
//			httppost.addHeader(HttpHeaders.AUTHORIZATION, createJwtToken().toAuthorizationString());
//			if (ViaGatewayUtlis.isAppRequestViaGateway()) {
//				ViaGatewayUtlis.copyRequestChainForHttpClient(httppost);
//			} else {
//				ViaGatewayUtlis.firstRequestChainForHttpClient(httppost);
//			}
//			httppost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);
//			StringEntity entity = new StringEntity(postData, "UTF-8");
//			entity.setContentType(APPLICATION_JSON);
//			entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));
//			httppost.setEntity(entity);
//			result = HttpClientHelper.requestExecute(httppost, HttpClientHelper.RESPONSE_STRING_EXTRACTOR);
//		} catch (Exception ex) {
//			log.error("apigateway->sendget->请求出错: " + ex.getMessage(), ex);
//		}
//		return result;
//	}
//
//	public static void main(String[] args) {
//		String url="https://crm.vazyme.com/blueland-plateform-render/staff/substaffs?staffType=VAZYME&orgId=40280c8164f445ff0164f47266dd005b&staffStatus=QY&queryStaff=&pageNum=1&pageSize=10";
//		String result="";
//
//		try {
//		result=Apigateway.getInstance().sendGet(url, null);
//		log.error("result:"+result);
//	} catch (Exception e) {
//		log.error("请求出错:" + e.getMessage(), e);
//	}
////		try {
////			log.info(HttpClientHelper.requestExecute(new HttpGet("http://www.baidu.com"),
////					HttpClientHelper.RESPONSE_STRING_EXTRACTOR));
////		} catch (Exception e) {
////			log.error("请求出错: " + e.getMessage(), e);
////		} finally {
////			HttpClientHelper.destorySingletonHttpClientObjects();
////		}
//	}
//
//}
