|
|
@ -2,12 +2,16 @@ package com.epmet.service.impl; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.epmet.commons.tools.constant.NumConstant; |
|
|
|
import com.epmet.commons.tools.utils.HttpClientManager; |
|
|
|
import com.epmet.constant.ModuleConstant; |
|
|
|
import com.epmet.constant.ThirdPlatformConstant; |
|
|
|
import com.epmet.dao.ComponentVerifyTicketDao; |
|
|
|
import com.epmet.dto.form.ComponentVerifyTicketFormDTO; |
|
|
|
import com.epmet.dto.result.AuthorizationInfoResultDTO; |
|
|
|
import com.epmet.exception.AesException; |
|
|
|
import com.epmet.redis.RedisThird; |
|
|
|
import com.epmet.service.ComponentVerifyTicketService; |
|
|
|
import com.epmet.util.OkHttpHelper; |
|
|
|
import com.epmet.util.WXBizMsgCrypt; |
|
|
|
import com.epmet.util.WXXmlToMapUtil; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
@ -15,9 +19,10 @@ import org.apache.commons.collections4.MapUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.IOException; |
|
|
|
import java.lang.reflect.Field; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
@ -34,15 +39,16 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
@Autowired |
|
|
|
private RedisThird redisThird; |
|
|
|
@Autowired |
|
|
|
private OkHttpHelper okHttpHelper; |
|
|
|
private ComponentVerifyTicketDao ticketDao; |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 获得授权事件的票据 |
|
|
|
* @param |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
@Override |
|
|
|
public void parseRequest(String timeStamp,String nonce,String msgSignature,String postData) { |
|
|
|
public void componentVerifyTicket(String timeStamp,String nonce,String msgSignature,String postData) { |
|
|
|
log.info(ModuleConstant.START_RECEIVE); |
|
|
|
try { |
|
|
|
//这个类是微信官网提供的解密类,需要用到消息校验Token 消息加密Key和服务平台appid
|
|
|
@ -52,9 +58,15 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
Map<String, String> result = WXXmlToMapUtil.xmlToMap(xml); |
|
|
|
String componentVerifyTicket = MapUtils.getString(result, ModuleConstant.TICKET_KEY); |
|
|
|
if (StringUtils.isNotEmpty(componentVerifyTicket)) { |
|
|
|
// 存储平台授权票据,保存ticket
|
|
|
|
// 缓存 ticket
|
|
|
|
redisThird.setComponentVerifyTicket(componentVerifyTicket); |
|
|
|
// 存数据库
|
|
|
|
ComponentVerifyTicketFormDTO ticketFormDTO = new ComponentVerifyTicketFormDTO(); |
|
|
|
ticketFormDTO.setAppid(PLATFORM_APP_ID); |
|
|
|
ticketFormDTO.setComponentVerifyTicket(componentVerifyTicket); |
|
|
|
ticketDao.insertComponentVerifyTicket(ticketFormDTO); |
|
|
|
} else { |
|
|
|
log.error(ModuleConstant.ERROR_TICKET); |
|
|
|
throw new RuntimeException(ModuleConstant.ERROR_TICKET); |
|
|
|
} |
|
|
|
} catch (AesException e) { |
|
|
@ -64,22 +76,22 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 定时获取票据 |
|
|
|
* @Description 定时获取 (令牌,component_access_token) |
|
|
|
* @param |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public void ticketJob() { |
|
|
|
public void getComponentAccessToken() { |
|
|
|
log.info("====================开始执行定时任务获取令牌【component_access_token】===================="); |
|
|
|
Map<String, String> reMap; |
|
|
|
try { |
|
|
|
// 核心定时器,每一个小时执行一次
|
|
|
|
// 核心定时器
|
|
|
|
String componentVerifyTicket = redisThird.getComponentVerifyTicket(ModuleConstant.TICKET_UNDERLINE_KEY); |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
jsonObject.put(ModuleConstant.COMPONENT_APPID, PLATFORM_APP_ID); |
|
|
|
jsonObject.put(ModuleConstant.COMPONENT_APPSECRET, PLATFORM_APP_SECRET); |
|
|
|
jsonObject.put(ModuleConstant.TICKET_UNDERLINE_KEY, componentVerifyTicket); |
|
|
|
String post = okHttpHelper.post(API_COMPONENT_TOKEN_URl, JSON.toJSONString(jsonObject)); |
|
|
|
String post = HttpClientManager.getInstance().sendPostByJSON(API_CREATE_PREAUTHCODE_URL, JSON.toJSONString(jsonObject)).getData(); |
|
|
|
HashMap<String, String> hashMap = JSON.parseObject(post, HashMap.class); |
|
|
|
String componentAccessToken = hashMap.get(ModuleConstant.COMPONENT_ACCESS_TOKEN); |
|
|
|
if (StringUtils.isNotEmpty(componentAccessToken)) { |
|
|
@ -105,12 +117,14 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
String accessToken = redisThird.getComponentAccessToken(ModuleConstant.COMPONENT_ACCESS_TOKEN); |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
jsonObject.put(ModuleConstant.COMPONENT_APPID, PLATFORM_APP_ID); |
|
|
|
String post = okHttpHelper.post(API_CREATE_PREAUTHCODE_URL + accessToken, JSON.toJSONString(jsonObject)); |
|
|
|
String post = HttpClientManager.getInstance().sendPostByJSON(API_CREATE_PREAUTHCODE_URL + accessToken, JSON.toJSONString(jsonObject)).getData(); |
|
|
|
log.info("====================返回post结果:" + post); |
|
|
|
HashMap<String, String> hashMap = JSON.parseObject(post, HashMap.class); |
|
|
|
String authCode = hashMap.get("pre_auth_code"); |
|
|
|
String authCode = hashMap.get(ModuleConstant.PRE_AUTH_CODE); |
|
|
|
String expiresIn = hashMap.get(ModuleConstant.EXPIRES_IN); |
|
|
|
int ex = Integer.valueOf(expiresIn) - NumConstant.SIXTY; |
|
|
|
if (StringUtils.isNotEmpty(authCode)) { |
|
|
|
redisThird.setPreAuthCode(authCode); |
|
|
|
redisThird.setPreAuthCode(authCode,ex); |
|
|
|
} else { |
|
|
|
throw new RuntimeException("微信开放平台,第三方平台获取【预授权码】失败"); |
|
|
|
} |
|
|
@ -120,22 +134,31 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
log.info("====================结束执行定时任务获取预授权码【pre_auth_code】===================="); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 使用授权码获取授权信息 |
|
|
|
* @param response |
|
|
|
* @param authCode |
|
|
|
* @param expiresIn |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public void weChatPlatformAuthCode(HttpServletResponse response, String authCode, String expiresIn) { |
|
|
|
public void authInfoByAuthCode(HttpServletResponse response, String authCode, String expiresIn) { |
|
|
|
// 获取令牌【component_access_token】
|
|
|
|
String accessToken = redisThird.getComponentAccessToken(ModuleConstant.COMPONENT_ACCESS_TOKEN); |
|
|
|
// 使用授权码获取授权信息
|
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
jsonObject.put(ModuleConstant.COMPONENT_APPID, PLATFORM_APP_ID); |
|
|
|
jsonObject.put(ModuleConstant.AUTHORIZATION_CODE, authCode); |
|
|
|
String post; |
|
|
|
String authInfo; |
|
|
|
try { |
|
|
|
post = okHttpHelper.post(API_QUERY_AUTH_URL + accessToken, JSON.toJSONString(jsonObject)); |
|
|
|
HashMap<String,Map> hashMap = JSON.parseObject(post, HashMap.class); |
|
|
|
authInfo = HttpClientManager.getInstance().sendPostByJSON(API_QUERY_AUTH_URL + accessToken, JSON.toJSONString(jsonObject)).getData(); |
|
|
|
HashMap<String,Map> hashMap = JSON.parseObject(authInfo, HashMap.class); |
|
|
|
Map map = hashMap.get(ModuleConstant.AUTHORIZATION_INFO); |
|
|
|
AuthorizationInfoResultDTO authorizationInfoResultDTO = mapToEntity(map, AuthorizationInfoResultDTO.class); |
|
|
|
redisThird.setAuthInfo(authorizationInfoResultDTO); |
|
|
|
String authorizerRefreshToken = map.get(ModuleConstant.AUTHORIZER_REFRESH_TOKEN).toString(); |
|
|
|
redisThird.setAuthorizerRefreshToken(authorizerRefreshToken); |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
@ -158,4 +181,39 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
jsonObject.put(ModuleConstant.COMPONENT_APPID,PLATFORM_APP_ID); |
|
|
|
jsonObject.put("",redisThird.getAuthorizerRefreshToken("")); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void redirectUri() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description map 转 Entity |
|
|
|
* @param map |
|
|
|
* @param entity |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
public <T> T mapToEntity(Map<String, Object> map, Class<T> entity) { |
|
|
|
T t = null; |
|
|
|
try { |
|
|
|
t = entity.newInstance(); |
|
|
|
for(Field field : entity.getDeclaredFields()) { |
|
|
|
if (map.containsKey(field.getName())) { |
|
|
|
boolean flag = field.isAccessible(); |
|
|
|
field.setAccessible(true); |
|
|
|
Object object = map.get(field.getName()); |
|
|
|
if (object!= null && field.getType().isAssignableFrom(object.getClass())) { |
|
|
|
field.set(t, object); |
|
|
|
} |
|
|
|
field.setAccessible(flag); |
|
|
|
} |
|
|
|
} |
|
|
|
return t; |
|
|
|
} catch (InstantiationException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} catch (IllegalAccessException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return t; |
|
|
|
} |
|
|
|
} |
|
|
|