|
@@ -3,6 +3,8 @@ package com.idiot.operationbackend.service.impl;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.google.common.cache.*;
|
|
|
import com.idiot.operationbackend.config.PlatformProperties;
|
|
|
+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.wechat.AesException;
|
|
@@ -52,6 +54,9 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
@Autowired
|
|
|
private PlatformProperties properties;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private AccountService accountService;
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public String notice(Map<String, String> param) {
|
|
@@ -59,10 +64,10 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
return Constants.SUCCESS;
|
|
|
}
|
|
|
String infoType = param.get("InfoType");
|
|
|
- String appId = param.get("AppId");
|
|
|
+ String componentAppId = param.get("AppId");
|
|
|
String ticket = param.get("ComponentVerifyTicket");
|
|
|
- logger.info("接受微信验证票据数据,infoType:{},appId:{},ticket:{}",infoType,appId,ticket);
|
|
|
- cache.put(String.format("component_verify_ticket_%s",appId),ticket);
|
|
|
+ logger.info("接受微信验证票据数据,infoType:{},第三方平台appId:{},ticket:{}",infoType,componentAppId,ticket);
|
|
|
+ cache.put(String.format("component_verify_ticket_%s",componentAppId),ticket);
|
|
|
return Constants.SUCCESS;
|
|
|
}
|
|
|
|
|
@@ -90,10 +95,21 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void cacheAuthorizerAccessToken(String accountId, String authorizerAccessToken) {
|
|
|
+ cache.put(String.format("authorizer_access_token_%s",accountId),authorizerAccessToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean saveOrUpdateWechatAcc(Account account) {
|
|
|
+ return accountService.saveOrUpdateAccount(account);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public String getComponentAccessToken() {
|
|
|
- String appId = properties.getAppId();
|
|
|
- String componentAccessToken = cache.getIfPresent(String.format("component_access_token_%s",appId));
|
|
|
+ logger.info("获取第三方平台令牌 component_access_token start");
|
|
|
+ String componentAppId = properties.getAppId();
|
|
|
+ String componentAccessToken = cache.getIfPresent(String.format("component_access_token_%s",componentAppId));
|
|
|
if ( !StringUtils.isEmpty(componentAccessToken)) {
|
|
|
return componentAccessToken;
|
|
|
}
|
|
@@ -101,34 +117,74 @@ 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",appId));
|
|
|
- params.add("component_appid",appId);
|
|
|
+ String ticket = cache.getIfPresent(String.format("component_verify_ticket_%s",componentAppId));
|
|
|
+ params.add("component_appid",componentAppId);
|
|
|
params.add("component_appsecret",properties.getAppSecret());
|
|
|
params.add("component_verify_ticket",ticket);
|
|
|
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
|
|
|
ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.POST, requestEntity, String.class);
|
|
|
String jsonStr = response.getBody();
|
|
|
- logger.info("请求微信获取令牌component_access_token 请求appId:{},ticket:{},微信返回结果:{}",appId,ticket,jsonStr);
|
|
|
+ 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",appId),componentAccessToken);
|
|
|
+ cache.put(String.format("component_access_token_%s",componentAppId),componentAccessToken);
|
|
|
return componentAccessToken;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String getAuthorizerAccessToken(String accountId) {
|
|
|
+
|
|
|
+ logger.info("获取授权方令牌 authorizer_access_token start");
|
|
|
+ String authorizerAccessToken = cache.getIfPresent(String.format("authorizer_access_token_%s",accountId));
|
|
|
+ if ( !StringUtils.isEmpty(authorizerAccessToken)) {
|
|
|
+ return authorizerAccessToken;
|
|
|
+ }
|
|
|
+ Account account = accountService.getById(accountId);
|
|
|
+ if (Objects.isNull(account)) {
|
|
|
+ logger.error("获取授权方令牌 authorizer_access_token 发生异常===> 公众号id:{}为空",accountId);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=COMPONENT_TOKEN";
|
|
|
+ String componentAccessToken = getComponentAccessToken();
|
|
|
+ String componentAppId = properties.getAppId();
|
|
|
+ requestUrl = requestUrl.replace("COMPONENT_TOKEN",componentAccessToken);
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ MultiValueMap<String,String> params = new LinkedMultiValueMap<>(4);
|
|
|
+ params.add("component_appid",componentAppId);
|
|
|
+ params.add("component_access_token",componentAccessToken);
|
|
|
+ params.add("authorizer_appid",account.getAuthorizerAppId());
|
|
|
+ params.add("authorizer_refresh_token",account.getAuthorizerRefreshToken());
|
|
|
+ HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.POST, requestEntity, String.class);
|
|
|
+ String jsonStr = response.getBody();
|
|
|
+ logger.info("获取授权方令牌 authorizer_access_token 第三方平台appId:{},公众号id:{},授权方appId:{},微信返回结果:{}",
|
|
|
+ componentAppId,accountId,account.getAuthorizerAppId(),jsonStr);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(jsonStr);
|
|
|
+ authorizerAccessToken = jsonObject.getString("authorizer_access_token");
|
|
|
+ String authorizerRefreshToken = jsonObject.getString("authorizer_refresh_token");
|
|
|
+ account.setAuthorizerAccessToken(authorizerAccessToken);
|
|
|
+ account.setAuthorizerRefreshToken(authorizerRefreshToken);
|
|
|
+ cache.put(String.format("authorizer_access_token_%s",accountId),authorizerAccessToken);
|
|
|
+ accountService.saveOrUpdate(account);
|
|
|
+ return authorizerAccessToken;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public String getPreAuthCode() {
|
|
|
- String appId = properties.getAppId();
|
|
|
+ String componentAppId = properties.getAppId();
|
|
|
+ logger.info("请求微信获取预授权码 第三方平台appId:{} start",componentAppId);
|
|
|
String requestUrl = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=COMPONENT_ACCESS_TOKEN";
|
|
|
String componentAccessToken = getComponentAccessToken();
|
|
|
requestUrl = requestUrl.replace("COMPONENT_ACCESS_TOKEN",componentAccessToken);
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
MultiValueMap<String,String> params = new LinkedMultiValueMap<>(1);
|
|
|
- params.add("component_appid",appId);
|
|
|
+ params.add("component_appid",componentAppId);
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
|
|
|
ResponseEntity<String> response = restTemplate.exchange(requestUrl, HttpMethod.POST, requestEntity, String.class);
|
|
|
String jsonStr = response.getBody();
|
|
|
- logger.info("请求微信获取预授权码 请求appId:{},微信返回结果:{}",appId,jsonStr);
|
|
|
+ logger.info("请求微信获取预授权码 第三方平台appId:{},微信返回结果:{}",componentAppId,jsonStr);
|
|
|
JSONObject jsonObject = JSONObject.parseObject(jsonStr);
|
|
|
return jsonObject.getString("pre_auth_code");
|
|
|
}
|
|
@@ -136,9 +192,9 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
@Override
|
|
|
public String getPreAuthUrl() {
|
|
|
String preAuthUrl = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=APPID&pre_auth_code=PREAUTHCODE&redirect_uri=URI&auth_type=1";
|
|
|
- String appId = properties.getAppId();
|
|
|
+ String componentAppId = properties.getAppId();
|
|
|
String preAuthCode = getPreAuthCode();
|
|
|
- preAuthUrl = preAuthUrl.replace("APPID",appId).replace("PREAUTHCODE",preAuthCode);
|
|
|
+ preAuthUrl = preAuthUrl.replace("APPID",componentAppId).replace("PREAUTHCODE",preAuthCode);
|
|
|
logger.info("生成预授权地址, preAuthUrl is {}",preAuthUrl);
|
|
|
return preAuthUrl;
|
|
|
}
|
|
@@ -153,4 +209,42 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
+ public String getAuthorizationInfo(String authCode) {
|
|
|
+ String componentAppId = properties.getAppId();
|
|
|
+ logger.info("请求微信获取授权信息 第三方平台appId:{} start",componentAppId);
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=COMPONENT_TOKEN";
|
|
|
+ String componentToken = getComponentAccessToken();
|
|
|
+ requestUrl = requestUrl.replace("COMPONENT_TOKEN",componentToken);
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ MultiValueMap<String,String> multiValueMap = new LinkedMultiValueMap<>(2);
|
|
|
+ multiValueMap.add("component_appid",componentAppId);
|
|
|
+ multiValueMap.add("authorization_code",authCode);
|
|
|
+ HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<> (multiValueMap,headers);
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(requestUrl,HttpMethod.POST,entity,String.class);
|
|
|
+ String jsonStr = response.getBody();
|
|
|
+ logger.info("请求微信获取授权信息 第三方平台appId:{},微信返回结果:{}",componentAppId,jsonStr);
|
|
|
+ return jsonStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getAuthorizerInfo(String authorizerAppId) {
|
|
|
+ String componentAppId = properties.getAppId();
|
|
|
+ logger.info("请求微信获取授权方信息 第三方平台appId:{},授权方appId:{},start",componentAppId,authorizerAppId);
|
|
|
+ String requestUrl = "https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=COMPONENT_TOKEN";
|
|
|
+ String componentToken = getComponentAccessToken();
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ requestUrl = requestUrl.replace("COMPONENT_TOKEN",componentToken);
|
|
|
+ MultiValueMap<String,String> params = new LinkedMultiValueMap<>(4);
|
|
|
+ params.add("component_access_token",componentToken);
|
|
|
+ params.add("component_appid",componentAppId);
|
|
|
+ params.add("authorizer_appid",authorizerAppId);
|
|
|
+ HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<> (params,headers);
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(requestUrl,HttpMethod.POST,entity,String.class);
|
|
|
+ String jsonStr = response.getBody();
|
|
|
+ logger.info("请求微信获取授权方信息 第三方平台appId:{},授权方appId:{},微信返回结果:{}",componentAppId,authorizerAppId,jsonStr);
|
|
|
+ return jsonStr;
|
|
|
+ }
|
|
|
}
|