mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
# 368 增加 微信第三方平台 模块
* 增加 微信第三方平台 接口 * WxOpenXmlMessage 消息处理
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package me.chanjar.weixin.open.util;
|
||||
|
||||
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
/**
|
||||
* @author <a href="https://github.com/007gzs">007</a>
|
||||
*/
|
||||
public class WxOpenCryptUtil extends me.chanjar.weixin.common.util.crypto.WxCryptUtil {
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param wxOpenConfigStorage
|
||||
*/
|
||||
public WxOpenCryptUtil(WxOpenConfigStorage wxOpenConfigStorage) {
|
||||
/*
|
||||
* @param token 公众平台上,开发者设置的token
|
||||
* @param encodingAesKey 公众平台上,开发者设置的EncodingAESKey
|
||||
* @param appId 公众平台appid
|
||||
*/
|
||||
String encodingAesKey = wxOpenConfigStorage.getComponentAesKey();
|
||||
String token = wxOpenConfigStorage.getComponentToken();
|
||||
String appId = wxOpenConfigStorage.getComponentAppId();
|
||||
|
||||
this.token = token;
|
||||
this.appidOrCorpid = appId;
|
||||
this.aesKey = Base64.decodeBase64(encodingAesKey + "=");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package me.chanjar.weixin.open.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizationInfo;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/007gzs">007</a>
|
||||
*/
|
||||
public class WxOpenAuthorizationInfoGsonAdapter implements JsonDeserializer<WxOpenAuthorizationInfo> {
|
||||
@Override
|
||||
public WxOpenAuthorizationInfo deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
WxOpenAuthorizationInfo authorizationInfo = new WxOpenAuthorizationInfo();
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
authorizationInfo.setAuthorizerAppid(GsonHelper.getString(jsonObject, "authorizer_appid"));
|
||||
authorizationInfo.setAuthorizerAccessToken(GsonHelper.getString(jsonObject, "authorizer_access_token"));
|
||||
authorizationInfo.setExpiresIn(GsonHelper.getPrimitiveInteger(jsonObject, "expires_in"));
|
||||
authorizationInfo.setAuthorizerRefreshToken(GsonHelper.getString(jsonObject, "authorizer_refresh_token"));
|
||||
List<Integer> funcInfo = new ArrayList<>();
|
||||
JsonArray jsonArray = GsonHelper.getAsJsonArray(jsonObject.get("func_info"));
|
||||
if(jsonArray != null && !jsonArray.isJsonNull()){
|
||||
for(int i = 0; i < jsonArray.size(); i++){
|
||||
jsonObject = jsonArray.get(i).getAsJsonObject();
|
||||
if(jsonObject == null || jsonObject.isJsonNull()){
|
||||
continue;
|
||||
}
|
||||
jsonObject = jsonObject.getAsJsonObject("funcscope_category");
|
||||
if(jsonObject == null || jsonObject.isJsonNull()){
|
||||
continue;
|
||||
}
|
||||
Integer id = GsonHelper.getInteger(jsonObject, "id");
|
||||
if(id == null) {
|
||||
continue;
|
||||
}
|
||||
funcInfo.add(id);
|
||||
}
|
||||
}
|
||||
authorizationInfo.setFuncInfo(funcInfo);
|
||||
return authorizationInfo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package me.chanjar.weixin.open.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/007gzs">007</a>
|
||||
*/
|
||||
public class WxOpenAuthorizerAccessTokenGsonAdapter implements JsonDeserializer<WxOpenAuthorizerAccessToken> {
|
||||
@Override
|
||||
public WxOpenAuthorizerAccessToken deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
WxOpenAuthorizerAccessToken authorizerAccessToken = new WxOpenAuthorizerAccessToken();
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
authorizerAccessToken.setAuthorizerAccessToken(GsonHelper.getString(jsonObject, "authorizer_access_token"));
|
||||
authorizerAccessToken.setExpiresIn(GsonHelper.getPrimitiveInteger(jsonObject, "expires_in"));
|
||||
return authorizerAccessToken;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package me.chanjar.weixin.open.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizerInfo;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/007gzs">007</a>
|
||||
*/
|
||||
public class WxOpenAuthorizerInfoGsonAdapter implements JsonDeserializer<WxOpenAuthorizerInfo> {
|
||||
@Override
|
||||
public WxOpenAuthorizerInfo deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
WxOpenAuthorizerInfo authorizationInfo = new WxOpenAuthorizerInfo();
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
|
||||
authorizationInfo.setNickName(GsonHelper.getString(jsonObject, "nick_name"));
|
||||
authorizationInfo.setHeadImg(GsonHelper.getString(jsonObject, "head_img"));
|
||||
authorizationInfo.setUserName(GsonHelper.getString(jsonObject, "user_name"));
|
||||
authorizationInfo.setPrincipalName(GsonHelper.getString(jsonObject, "principal_name"));
|
||||
authorizationInfo.setAlias(GsonHelper.getString(jsonObject, "alias"));
|
||||
authorizationInfo.setQrcodeUrl(GsonHelper.getString(jsonObject, "qrcode_url"));
|
||||
if(jsonObject.has("service_type_info")) {
|
||||
authorizationInfo.setServiceTypeInfo(GsonHelper.getInteger(jsonObject.getAsJsonObject("service_type_info"), "id"));
|
||||
}
|
||||
if(jsonObject.has("verify_type_info")) {
|
||||
authorizationInfo.setVerifyTypeInfo(GsonHelper.getInteger(jsonObject.getAsJsonObject("verify_type_info"), "id"));
|
||||
}
|
||||
Map<String, Integer> businessInfo = WxOpenGsonBuilder.create().fromJson(jsonObject.get("business_info"),
|
||||
new TypeToken<Map<String, Integer>>() {
|
||||
}.getType());
|
||||
authorizationInfo.setBusinessInfo(businessInfo);
|
||||
return authorizationInfo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package me.chanjar.weixin.open.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizationInfo;
|
||||
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizerInfo;
|
||||
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/007gzs">007</a>
|
||||
*/
|
||||
public class WxOpenAuthorizerInfoResultGsonAdapter implements JsonDeserializer<WxOpenAuthorizerInfoResult> {
|
||||
@Override
|
||||
public WxOpenAuthorizerInfoResult deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
WxOpenAuthorizerInfoResult authorizerInfoResult = new WxOpenAuthorizerInfoResult();
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
|
||||
WxOpenAuthorizationInfo authorizationInfo = WxOpenGsonBuilder.INSTANCE.create().fromJson(jsonObject.get("authorization_info"),
|
||||
new TypeToken<WxOpenAuthorizationInfo>() {
|
||||
}.getType());
|
||||
|
||||
authorizerInfoResult.setAuthorizationInfo(authorizationInfo);
|
||||
WxOpenAuthorizerInfo authorizerInfo = WxOpenGsonBuilder.INSTANCE.create().fromJson(jsonObject.get("authorizer_info"),
|
||||
new TypeToken<WxOpenAuthorizerInfo>() {
|
||||
}.getType());
|
||||
|
||||
authorizerInfoResult.setAuthorizerInfo(authorizerInfo);
|
||||
return authorizerInfoResult;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package me.chanjar.weixin.open.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerOptionResult;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/007gzs">007</a>
|
||||
*/
|
||||
public class WxOpenAuthorizerOptionResultGsonAdapter implements JsonDeserializer<WxOpenAuthorizerOptionResult> {
|
||||
@Override
|
||||
public WxOpenAuthorizerOptionResult deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
WxOpenAuthorizerOptionResult authorizerOptionResult = new WxOpenAuthorizerOptionResult();
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
authorizerOptionResult.setAuthorizerAppid(GsonHelper.getString(jsonObject, "authorizer_appid"));
|
||||
authorizerOptionResult.setOptionName(GsonHelper.getString(jsonObject, "option_name"));
|
||||
authorizerOptionResult.setOptionValue(GsonHelper.getString(jsonObject, "option_value"));
|
||||
return authorizerOptionResult;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package me.chanjar.weixin.open.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/007gzs">007</a>
|
||||
*/
|
||||
public class WxOpenComponentAccessTokenGsonAdapter implements JsonDeserializer<WxOpenComponentAccessToken> {
|
||||
@Override
|
||||
public WxOpenComponentAccessToken deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
WxOpenComponentAccessToken componentAccessToken = new WxOpenComponentAccessToken();
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
componentAccessToken.setComponentAccessToken(GsonHelper.getString(jsonObject, "component_access_token"));
|
||||
componentAccessToken.setExpiresIn(GsonHelper.getPrimitiveInteger(jsonObject, "expires_in"));
|
||||
return componentAccessToken;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package me.chanjar.weixin.open.util.json;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
|
||||
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
|
||||
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizationInfo;
|
||||
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizerInfo;
|
||||
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerInfoResult;
|
||||
import me.chanjar.weixin.open.bean.result.WxOpenAuthorizerOptionResult;
|
||||
import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/007gzs">007</a>
|
||||
*/
|
||||
public class WxOpenGsonBuilder {
|
||||
|
||||
public static final GsonBuilder INSTANCE = new GsonBuilder();
|
||||
|
||||
static {
|
||||
INSTANCE.disableHtmlEscaping();
|
||||
INSTANCE.registerTypeAdapter(WxOpenComponentAccessToken.class, new WxOpenComponentAccessTokenGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxOpenAuthorizerAccessToken.class, new WxOpenAuthorizerAccessTokenGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxOpenAuthorizationInfo.class, new WxOpenAuthorizationInfoGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxOpenAuthorizerInfo.class, new WxOpenAuthorizerInfoGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxOpenQueryAuthResult.class, new WxOpenQueryAuthResultGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxOpenAuthorizerInfoResult.class, new WxOpenAuthorizerInfoResultGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxOpenAuthorizerOptionResult.class, new WxOpenAuthorizerOptionResultGsonAdapter());
|
||||
|
||||
}
|
||||
|
||||
public static Gson create() {
|
||||
return INSTANCE.create();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package me.chanjar.weixin.open.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizationInfo;
|
||||
import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/007gzs">007</a>
|
||||
*/
|
||||
public class WxOpenQueryAuthResultGsonAdapter implements JsonDeserializer<WxOpenQueryAuthResult> {
|
||||
@Override
|
||||
public WxOpenQueryAuthResult deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
WxOpenQueryAuthResult queryAuthResult = new WxOpenQueryAuthResult();
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
|
||||
WxOpenAuthorizationInfo authorizationInfo = WxOpenGsonBuilder.INSTANCE.create().fromJson(jsonObject.get("authorization_info"),
|
||||
new TypeToken<WxOpenAuthorizationInfo>() {
|
||||
}.getType());
|
||||
|
||||
queryAuthResult.setAuthorizationInfo(authorizationInfo);
|
||||
return queryAuthResult;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package me.chanjar.weixin.open.util.xml;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
|
||||
import me.chanjar.weixin.open.bean.message.WxOpenXmlMessage;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/007gzs">007</a>
|
||||
*/
|
||||
public class XStreamTransformer {
|
||||
private static final Map<Class<?>, XStream> CLASS_2_XSTREAM_INSTANCE = new HashMap<>();
|
||||
|
||||
static {
|
||||
registerClass(WxOpenXmlMessage.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* xml -> pojo
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T fromXml(Class<T> clazz, String xml) {
|
||||
T object = (T) CLASS_2_XSTREAM_INSTANCE.get(clazz).fromXML(xml);
|
||||
return object;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T fromXml(Class<T> clazz, InputStream is) {
|
||||
T object = (T) CLASS_2_XSTREAM_INSTANCE.get(clazz).fromXML(is);
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* pojo -> xml
|
||||
*/
|
||||
public static <T> String toXml(Class<T> clazz, T object) {
|
||||
return CLASS_2_XSTREAM_INSTANCE.get(clazz).toXML(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册扩展消息的解析器
|
||||
*
|
||||
* @param clz 类型
|
||||
* @param xStream xml解析器
|
||||
*/
|
||||
private static void register(Class<?> clz, XStream xStream) {
|
||||
CLASS_2_XSTREAM_INSTANCE.put(clz, xStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会自动注册该类及其子类
|
||||
*
|
||||
* @param clz 要注册的类
|
||||
*/
|
||||
private static void registerClass(Class<?> clz) {
|
||||
XStream xstream = XStreamInitializer.getInstance();
|
||||
xstream.setClassLoader(Thread.currentThread().getContextClassLoader());
|
||||
|
||||
xstream.processAnnotations(clz);
|
||||
xstream.processAnnotations(getInnerClasses(clz));
|
||||
if (clz.equals(WxOpenXmlMessage.class)) {
|
||||
// 操蛋的微信,模板消息推送成功的消息是MsgID,其他消息推送过来是MsgId
|
||||
xstream.aliasField("MsgID", WxOpenXmlMessage.class, "msgId");
|
||||
}
|
||||
|
||||
register(clz, xstream);
|
||||
}
|
||||
|
||||
private static Class<?>[] getInnerClasses(Class<?> clz) {
|
||||
Class<?>[] innerClasses = clz.getClasses();
|
||||
if (innerClasses == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Class<?>> result = new ArrayList<>();
|
||||
result.addAll(Arrays.asList(innerClasses));
|
||||
for (Class<?> inner : innerClasses) {
|
||||
Class<?>[] innerClz = getInnerClasses(inner);
|
||||
if (innerClz == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.addAll(Arrays.asList(innerClz));
|
||||
}
|
||||
|
||||
return result.toArray(new Class<?>[0]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user