|
@@ -7,6 +7,7 @@ import com.idiot.operationbackend.entity.Account;
|
|
|
import com.idiot.operationbackend.service.facade.AccountService;
|
|
|
import com.idiot.operationbackend.service.facade.WeChatService;
|
|
|
import com.idiot.operationbackend.support.Constants;
|
|
|
+import com.idiot.operationbackend.support.CustomException;
|
|
|
import com.idiot.operationbackend.support.wechat.AesException;
|
|
|
import com.idiot.operationbackend.support.wechat.WXBizMsgCrypt;
|
|
|
import org.slf4j.Logger;
|
|
@@ -21,6 +22,7 @@ import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
@@ -67,7 +69,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
String componentAppId = param.get("AppId");
|
|
|
String ticket = param.get("ComponentVerifyTicket");
|
|
|
logger.info("接受微信验证票据数据,infoType:{},第三方平台appId:{},ticket:{}",infoType,componentAppId,ticket);
|
|
|
- cache.put(String.format("component_verify_ticket_%s",componentAppId),ticket);
|
|
|
+ cache.put(String.format(Constants.COMPONENT_VERIFY_TICKET,componentAppId),ticket);
|
|
|
return Constants.SUCCESS;
|
|
|
}
|
|
|
|
|
@@ -97,7 +99,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
|
|
|
@Override
|
|
|
public void cacheAuthorizerAccessToken(String accountId, String authorizerAccessToken) {
|
|
|
- cache.put(String.format("authorizer_access_token_%s",accountId),authorizerAccessToken);
|
|
|
+ cache.put(String.format(Constants.AUTHORIZER_ACCESS_TOKEN,accountId),authorizerAccessToken);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -109,7 +111,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
public String getComponentAccessToken() {
|
|
|
logger.info("获取第三方平台令牌 component_access_token start");
|
|
|
String componentAppId = properties.getAppId();
|
|
|
- String componentAccessToken = cache.getIfPresent(String.format("component_access_token_%s",componentAppId));
|
|
|
+ String componentAccessToken = cache.getIfPresent(String.format(Constants.COMPONENT_ACCESS_TOKEN,componentAppId));
|
|
|
if ( !StringUtils.isEmpty(componentAccessToken)) {
|
|
|
return componentAccessToken;
|
|
|
}
|
|
@@ -117,7 +119,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
MultiValueMap<String,String> params = new LinkedMultiValueMap<>(3);
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
- String ticket = cache.getIfPresent(String.format("component_verify_ticket_%s",componentAppId));
|
|
|
+ String ticket = cache.getIfPresent(String.format(Constants.COMPONENT_VERIFY_TICKET,componentAppId));
|
|
|
params.add("component_appid",componentAppId);
|
|
|
params.add("component_appsecret",properties.getAppSecret());
|
|
|
params.add("component_verify_ticket",ticket);
|
|
@@ -127,7 +129,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
logger.info("请求微信获取令牌component_access_token 第三方平台appId:{},ticket:{},微信返回结果:{}",componentAppId,ticket,jsonStr);
|
|
|
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
|
|
|
componentAccessToken = jsonObject.getString("component_access_token");
|
|
|
- cache.put(String.format("component_access_token_%s",componentAppId),componentAccessToken);
|
|
|
+ cache.put(String.format(Constants.COMPONENT_ACCESS_TOKEN,componentAppId),componentAccessToken);
|
|
|
return componentAccessToken;
|
|
|
}
|
|
|
|
|
@@ -135,7 +137,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
public String getAuthorizerAccessToken(String accountId) {
|
|
|
|
|
|
logger.info("获取授权方令牌 authorizer_access_token start");
|
|
|
- String authorizerAccessToken = cache.getIfPresent(String.format("authorizer_access_token_%s",accountId));
|
|
|
+ String authorizerAccessToken = cache.getIfPresent(String.format(Constants.AUTHORIZER_ACCESS_TOKEN,accountId));
|
|
|
if ( !StringUtils.isEmpty(authorizerAccessToken)) {
|
|
|
return authorizerAccessToken;
|
|
|
}
|
|
@@ -165,7 +167,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
String authorizerRefreshToken = jsonObject.getString("authorizer_refresh_token");
|
|
|
account.setAuthorizerAccessToken(authorizerAccessToken);
|
|
|
account.setAuthorizerRefreshToken(authorizerRefreshToken);
|
|
|
- cache.put(String.format("authorizer_access_token_%s",accountId),authorizerAccessToken);
|
|
|
+ cache.put(String.format(Constants.AUTHORIZER_ACCESS_TOKEN,accountId),authorizerAccessToken);
|
|
|
accountService.saveOrUpdate(account);
|
|
|
return authorizerAccessToken;
|
|
|
}
|
|
@@ -247,4 +249,43 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
logger.info("请求微信获取授权方信息 第三方平台appId:{},授权方appId:{},微信返回结果:{}",componentAppId,authorizerAppId,jsonStr);
|
|
|
return jsonStr;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int syncAccountUser(String accountId) {
|
|
|
+
|
|
|
+ String lockKey = String.format(Constants.LOCK_SYNC_USER,accountId);
|
|
|
+ String lockValue = cache.getIfPresent(lockKey);
|
|
|
+ if (StringUtils.isEmpty(lockValue)) {
|
|
|
+ throw new CustomException(500,"当前公众号正在后台同步粉丝数据,请您稍等一会!");
|
|
|
+ }
|
|
|
+ cache.put(lockKey,lockValue);
|
|
|
+ logger.info("公众号:{}同步粉丝数据,时间:{}",accountId, LocalDateTime.now().toString());
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s";
|
|
|
+ String nextOpenId = "";
|
|
|
+ String accessToken = getAuthorizerAccessToken(accountId);
|
|
|
+ int count = 0;
|
|
|
+ int total = 0;
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<> (headers);
|
|
|
+ String jsonStr = null;
|
|
|
+ JSONObject respJsonBody = null;
|
|
|
+ do {
|
|
|
+ if (StringUtils.isEmpty(nextOpenId)) {
|
|
|
+ nextOpenId = "";
|
|
|
+ }
|
|
|
+ requestUrl = String.format(requestUrl,accessToken,nextOpenId);
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.exchange(requestUrl,HttpMethod.GET,entity,String.class);
|
|
|
+ jsonStr = responseEntity.getBody();
|
|
|
+ respJsonBody = JSONObject.parseObject(jsonStr);
|
|
|
+ total = respJsonBody.getInteger("total");
|
|
|
+ count += respJsonBody.getInteger("count");
|
|
|
+ nextOpenId = respJsonBody.getString("next_openid");
|
|
|
+ }while (count < total);
|
|
|
+ cache.invalidate(lockKey);
|
|
|
+ return count;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|