| 
					
				 | 
			
			
				@@ -0,0 +1,118 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+package com.tzld.longarticle.recommend.server.remote; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.alibaba.fastjson.JSONObject; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.aliyun.odps.utils.StringUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValue; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.google.common.cache.CacheBuilder; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.google.common.cache.CacheLoader; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.google.common.cache.LoadingCache; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.google.common.reflect.TypeToken; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.common.HttpPoolFactory; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.model.Content; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.model.WxUserGetResponse; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.util.JSONUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import lombok.extern.slf4j.Slf4j; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.apache.http.HttpEntity; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.apache.http.StatusLine; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+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.util.EntityUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.stereotype.Service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.nio.charset.StandardCharsets; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.concurrent.TimeUnit; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+/** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * @author dyp 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@Service 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@Slf4j 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+public class WxAccessTokenRemoteService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private final CloseableHttpClient client = HttpPoolFactory.defaultPool(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private LoadingCache<String, String> cache = CacheBuilder.newBuilder() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            .expireAfterWrite(280, TimeUnit.SECONDS) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            .build(new CacheLoader<String, String>() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                public String load(String s) throws Exception { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    return getAccessToken(s); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @ApolloJsonValue("${gzhConfig:{}}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private Map<String, Map<String, String>> gzhConfig; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public String getAccessToken(String gzhId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return cache.getUnchecked(gzhId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private String loadAccessToken(String gzhId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Map<String, Object> param = new HashMap<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            param.put("grant_type", "client_credential"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            param.put("appid", gzhConfig.get(gzhId).get("appid")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            param.put("secret", gzhConfig.get(gzhId).get("secret")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            param.put("force_refresh", false); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/stable_token"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            StringEntity stringEntity = new StringEntity(JSONUtils.toJson(param), StandardCharsets.UTF_8); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            httpPost.setEntity(stringEntity); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            CloseableHttpResponse response = client.execute(httpPost); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            StatusLine statusLine = response.getStatusLine(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (statusLine.getStatusCode() == 200) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                HttpEntity responseEntity = response.getEntity(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (Objects.nonNull(responseEntity)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    String responseBody = EntityUtils.toString(responseEntity, "UTF-8"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    Map<String, String> result = JSONUtils.fromJson(responseBody, new TypeToken<Map<String, String>>() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    }, Collections.emptyMap()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    return result.get("access_token"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            log.error("get user error gzhId {} ", gzhId, e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public WxUserGetResponse getAllUserLimit10000(String gzhId, String nextOpenId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String accessToken = accessTokenMap.get(gzhId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (StringUtils.isBlank(accessToken)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                log.error("gzh {} access_token not config", gzhId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            URIBuilder uriBuilder = new URIBuilder("https://api.weixin.qq.com/cgi-bin/user/get"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            uriBuilder.setParameter("gzhId", gzhId); // 假设需要传递gzhId参数 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            uriBuilder.setParameter("access_token", accessToken); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            uriBuilder.setParameter("next_openid", nextOpenId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            HttpGet httpGet = new HttpGet(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            httpGet.setURI(uriBuilder.build()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            CloseableHttpResponse response = client.execute(httpGet); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            StatusLine statusLine = response.getStatusLine(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (statusLine.getStatusCode() == 200) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                HttpEntity responseEntity = response.getEntity(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (Objects.nonNull(responseEntity)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    String responseBody = EntityUtils.toString(responseEntity, "UTF-8"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    return JSONUtils.fromJson(responseBody, new TypeToken<WxUserGetResponse>() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    }, null); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            log.error("get user error gzhId={} nextOpenId={} ", gzhId, nextOpenId, e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |