mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
🎨 重构规范小程序部分代码包结构
This commit is contained in:
@@ -1,66 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.util;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class QrcodeBytesRequestExecutor implements RequestExecutor<byte[], AbstractWxMaQrcodeWrapper> {
|
||||
protected RequestHttp<CloseableHttpClient, HttpHost> requestHttp;
|
||||
|
||||
public QrcodeBytesRequestExecutor(RequestHttp requestHttp) {
|
||||
this.requestHttp = requestHttp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, AbstractWxMaQrcodeWrapper data, ResponseHandler<byte[]> handler, WxType wxType) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
httpPost.setConfig(
|
||||
RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build()
|
||||
);
|
||||
}
|
||||
|
||||
httpPost.setEntity(new StringEntity(qrcodeWrapper.toJson()));
|
||||
|
||||
try (final CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost);
|
||||
final InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) {
|
||||
Header[] contentTypeHeader = response.getHeaders("Content-Type");
|
||||
if (contentTypeHeader != null && contentTypeHeader.length > 0
|
||||
&& ContentType.APPLICATION_JSON.getMimeType()
|
||||
.equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
|
||||
}
|
||||
|
||||
return IOUtils.toByteArray(inputStream);
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.util;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/gentryhuang">gentryhuang</a>
|
||||
*/
|
||||
public class QrcodeFileRequestExecutor extends QrcodeRequestExecutor {
|
||||
/**
|
||||
* 二维码生成的文件路径,例如: /var/temp
|
||||
*/
|
||||
private final String filePath;
|
||||
|
||||
public QrcodeFileRequestExecutor(RequestHttp requestHttp, String filePath) {
|
||||
super(requestHttp);
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行http请求.
|
||||
*
|
||||
* @param uri uri
|
||||
* @param qrcodeWrapper 数据
|
||||
* @param wxType 微信模块类型
|
||||
* @return 响应结果
|
||||
* @throws WxErrorException 自定义异常
|
||||
* @throws IOException io异常
|
||||
*/
|
||||
@Override
|
||||
public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
httpPost.setConfig(
|
||||
RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build()
|
||||
);
|
||||
}
|
||||
|
||||
httpPost.setEntity(new StringEntity(qrcodeWrapper.toJson(), ContentType.APPLICATION_JSON));
|
||||
|
||||
try (final CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost);
|
||||
final InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) {
|
||||
Header[] contentTypeHeader = response.getHeaders("Content-Type");
|
||||
if (contentTypeHeader != null && contentTypeHeader.length > 0
|
||||
&& ContentType.APPLICATION_JSON.getMimeType()
|
||||
.equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
|
||||
}
|
||||
if (StringUtils.isBlank(filePath)) {
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
||||
}
|
||||
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg", Paths.get(filePath).toFile());
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.util;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||
import me.chanjar.weixin.common.util.http.ResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.InputStreamResponseHandler;
|
||||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class QrcodeRequestExecutor implements RequestExecutor<File, AbstractWxMaQrcodeWrapper> {
|
||||
protected RequestHttp<CloseableHttpClient, HttpHost> requestHttp;
|
||||
|
||||
public QrcodeRequestExecutor(RequestHttp requestHttp) {
|
||||
this.requestHttp = requestHttp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String uri, AbstractWxMaQrcodeWrapper data, ResponseHandler<File> handler, WxType wxType) throws WxErrorException, IOException {
|
||||
handler.handle(this.execute(uri, data, wxType));
|
||||
}
|
||||
|
||||
@Override
|
||||
public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
|
||||
HttpPost httpPost = new HttpPost(uri);
|
||||
if (requestHttp.getRequestHttpProxy() != null) {
|
||||
httpPost.setConfig(
|
||||
RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build()
|
||||
);
|
||||
}
|
||||
|
||||
httpPost.setEntity(new StringEntity(qrcodeWrapper.toJson(), ContentType.APPLICATION_JSON));
|
||||
|
||||
try (final CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost);
|
||||
final InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response)) {
|
||||
Header[] contentTypeHeader = response.getHeaders("Content-Type");
|
||||
if (contentTypeHeader != null && contentTypeHeader.length > 0
|
||||
&& ContentType.APPLICATION_JSON.getMimeType()
|
||||
.equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) {
|
||||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
|
||||
}
|
||||
|
||||
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
|
||||
} finally {
|
||||
httpPost.releaseConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.util.json;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeCommitRequest;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/charmingoh">Charming</a>
|
||||
* @since 2018-04-26 19:47
|
||||
*/
|
||||
public class WxMaCodeCommitRequestGsonAdapter implements JsonSerializer<WxMaCodeCommitRequest> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(WxMaCodeCommitRequest request, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject requestJson = new JsonObject();
|
||||
requestJson.addProperty("template_id", request.getTemplateId());
|
||||
requestJson.addProperty("user_version", request.getUserVersion());
|
||||
requestJson.addProperty("user_desc", request.getUserDesc());
|
||||
if (request.getExtConfig() != null) {
|
||||
requestJson.addProperty("ext_json", WxMaGsonBuilder.create().toJson(request.getExtConfig()));
|
||||
}
|
||||
return requestJson;
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.util.json;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeVersionDistribution;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/charmingoh">Charming</a>
|
||||
* @since 2018-04-26 19:47
|
||||
*/
|
||||
public class WxMaCodeVersionDistributionGsonAdapter implements JsonDeserializer<WxMaCodeVersionDistribution> {
|
||||
@Override
|
||||
public WxMaCodeVersionDistribution deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
WxMaCodeVersionDistribution distribution = new WxMaCodeVersionDistribution();
|
||||
JsonObject object = json.getAsJsonObject();
|
||||
distribution.setNowVersion(GsonHelper.getString(object, "now_version"));
|
||||
distribution.setUvInfo(getAsMap(object.getAsJsonObject("uv_info"), "items"));
|
||||
return distribution;
|
||||
}
|
||||
|
||||
private Map<String, Float> getAsMap(JsonObject object, String memberName) {
|
||||
JsonArray array = object.getAsJsonArray(memberName);
|
||||
if (array != null && array.size() > 0) {
|
||||
Map<String, Float> map = new LinkedHashMap<>(array.size());
|
||||
for (JsonElement element : array) {
|
||||
JsonObject elementObject = element.getAsJsonObject();
|
||||
String version = GsonHelper.getString(elementObject, "version");
|
||||
if (version != null) {
|
||||
Float percentage = GsonHelper.getFloat(elementObject, "percentage");
|
||||
map.put(version, percentage);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.util.json;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage;
|
||||
import cn.binarywang.wx.miniapp.bean.analysis.WxMaRetainInfo;
|
||||
import cn.binarywang.wx.miniapp.bean.analysis.WxMaUserPortrait;
|
||||
import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitDistribution;
|
||||
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeCommitRequest;
|
||||
import cn.binarywang.wx.miniapp.bean.code.WxMaCodeVersionDistribution;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class WxMaGsonBuilder {
|
||||
private static final GsonBuilder INSTANCE = new GsonBuilder();
|
||||
|
||||
static {
|
||||
INSTANCE.disableHtmlEscaping();
|
||||
INSTANCE.registerTypeAdapter(WxMaSubscribeMessage.class, new WxMaSubscribeMessageGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMaUniformMessage.class, new WxMaUniformMessageGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMaCodeCommitRequest.class, new WxMaCodeCommitRequestGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMaCodeVersionDistribution.class, new WxMaCodeVersionDistributionGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMaVisitDistribution.class, new WxMaVisitDistributionGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMaRetainInfo.class, new WxMaRetainInfoGsonAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMaUserPortrait.class, new WxMaUserPortraitGsonAdapter());
|
||||
}
|
||||
|
||||
public static Gson create() {
|
||||
return INSTANCE.create();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.util.json;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.analysis.WxMaRetainInfo;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/charmingoh">Charming</a>
|
||||
* @since 2018-04-28
|
||||
*/
|
||||
public class WxMaRetainInfoGsonAdapter implements JsonDeserializer<WxMaRetainInfo> {
|
||||
@Override
|
||||
public WxMaRetainInfo deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
WxMaRetainInfo retainInfo = new WxMaRetainInfo();
|
||||
JsonObject object = json.getAsJsonObject();
|
||||
String refDate = GsonHelper.getString(object, "ref_date");
|
||||
retainInfo.setRefDate(refDate);
|
||||
retainInfo.setVisitUvNew(getAsMap(object, "visit_uv_new"));
|
||||
retainInfo.setVisitUv(getAsMap(object, "visit_uv"));
|
||||
return retainInfo;
|
||||
}
|
||||
|
||||
private Map<Integer, Integer> getAsMap(JsonObject object, String memberName) {
|
||||
JsonArray array = object.getAsJsonArray(memberName);
|
||||
if (array != null && array.size() > 0) {
|
||||
Map<Integer, Integer> map = new LinkedHashMap<>(array.size());
|
||||
for (JsonElement element : array) {
|
||||
JsonObject elementObject = element.getAsJsonObject();
|
||||
Integer key = GsonHelper.getInteger(elementObject, "key");
|
||||
if (key != null) {
|
||||
Integer value = GsonHelper.getInteger(elementObject, "value");
|
||||
map.put(key, value);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.util.json;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author S
|
||||
*/
|
||||
public class WxMaSubscribeMessageGsonAdapter implements JsonSerializer<WxMaSubscribeMessage> {
|
||||
@Override
|
||||
public JsonElement serialize(WxMaSubscribeMessage message, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject messageJson = new JsonObject();
|
||||
messageJson.addProperty("touser", message.getToUser());
|
||||
messageJson.addProperty("template_id", message.getTemplateId());
|
||||
if (message.getPage() != null) {
|
||||
messageJson.addProperty("page", message.getPage());
|
||||
}
|
||||
|
||||
if (message.getMiniprogramState() != null) {
|
||||
messageJson.addProperty("miniprogram_state", message.getMiniprogramState());
|
||||
}
|
||||
|
||||
if (message.getLang() != null) {
|
||||
messageJson.addProperty("lang", message.getLang());
|
||||
}
|
||||
|
||||
JsonObject data = new JsonObject();
|
||||
messageJson.add("data", data);
|
||||
|
||||
if (message.getData() == null) {
|
||||
return messageJson;
|
||||
}
|
||||
|
||||
for (WxMaSubscribeMessage.Data datum : message.getData()) {
|
||||
JsonObject dataJson = new JsonObject();
|
||||
dataJson.addProperty("value", datum.getValue());
|
||||
data.add(datum.getName(), dataJson);
|
||||
}
|
||||
|
||||
return messageJson;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.util.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class WxMaUniformMessageGsonAdapter implements JsonSerializer<WxMaUniformMessage> {
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(WxMaUniformMessage message, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject messageJson = new JsonObject();
|
||||
messageJson.addProperty("touser", message.getToUser());
|
||||
if (message.isMpTemplateMsg()) {
|
||||
JsonObject msg = new JsonObject();
|
||||
if (message.getAppid() != null) {
|
||||
msg.addProperty("appid", message.getAppid());
|
||||
}
|
||||
|
||||
msg.addProperty("template_id", message.getTemplateId());
|
||||
|
||||
if (message.getUrl() != null) {
|
||||
msg.addProperty("url", message.getUrl());
|
||||
}
|
||||
|
||||
final WxMaUniformMessage.MiniProgram miniProgram = message.getMiniProgram();
|
||||
if (miniProgram != null) {
|
||||
JsonObject miniProgramJson = new JsonObject();
|
||||
miniProgramJson.addProperty("appid", miniProgram.getAppid());
|
||||
if (miniProgram.isUsePath()) {
|
||||
miniProgramJson.addProperty("path", miniProgram.getPagePath());
|
||||
} else if (miniProgram.isUsePagePath()) {
|
||||
miniProgramJson.addProperty("pagePath", miniProgram.getPagePath());
|
||||
} else {
|
||||
miniProgramJson.addProperty("pagepath", miniProgram.getPagePath());
|
||||
}
|
||||
msg.add("miniprogram", miniProgramJson);
|
||||
}
|
||||
|
||||
if (message.getData() != null) {
|
||||
JsonObject data = new JsonObject();
|
||||
for (WxMaTemplateData templateData : message.getData()) {
|
||||
JsonObject dataJson = new JsonObject();
|
||||
dataJson.addProperty("value", templateData.getValue());
|
||||
if (templateData.getColor() != null) {
|
||||
dataJson.addProperty("color", templateData.getColor());
|
||||
}
|
||||
data.add(templateData.getName(), dataJson);
|
||||
}
|
||||
msg.add("data", data);
|
||||
}
|
||||
|
||||
|
||||
messageJson.add("mp_template_msg", msg);
|
||||
return messageJson;
|
||||
}
|
||||
|
||||
//小程序模版消息
|
||||
JsonObject msg = new JsonObject();
|
||||
msg.addProperty("template_id", message.getTemplateId());
|
||||
|
||||
if (message.getPage() != null) {
|
||||
msg.addProperty("page", message.getPage());
|
||||
}
|
||||
|
||||
if (message.getFormId() != null) {
|
||||
msg.addProperty("form_id", message.getFormId());
|
||||
}
|
||||
|
||||
JsonObject data = new JsonObject();
|
||||
msg.add("data", data);
|
||||
|
||||
if (message.getData() != null) {
|
||||
for (WxMaTemplateData templateData : message.getData()) {
|
||||
JsonObject dataJson = new JsonObject();
|
||||
dataJson.addProperty("value", templateData.getValue());
|
||||
data.add(templateData.getName(), dataJson);
|
||||
}
|
||||
}
|
||||
|
||||
if (message.getEmphasisKeyword() != null) {
|
||||
msg.addProperty("emphasis_keyword", message.getEmphasisKeyword());
|
||||
}
|
||||
|
||||
messageJson.add("weapp_template_msg", msg);
|
||||
|
||||
return messageJson;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.util.json;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.analysis.WxMaUserPortrait;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/charmingoh">Charming</a>
|
||||
* @since 2018-04-28
|
||||
*/
|
||||
public class WxMaUserPortraitGsonAdapter implements JsonDeserializer<WxMaUserPortrait> {
|
||||
@Override
|
||||
public WxMaUserPortrait deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
WxMaUserPortrait portrait = new WxMaUserPortrait();
|
||||
JsonObject object = json.getAsJsonObject();
|
||||
String refDate = GsonHelper.getString(object, "ref_date");
|
||||
portrait.setRefDate(refDate);
|
||||
portrait.setVisitUvNew(getPortraitItem(object.getAsJsonObject("visit_uv_new")));
|
||||
portrait.setVisitUv(getPortraitItem(object.getAsJsonObject("visit_uv")));
|
||||
return portrait;
|
||||
}
|
||||
|
||||
private WxMaUserPortrait.Item getPortraitItem(JsonObject object) {
|
||||
if (object == null) {
|
||||
return null;
|
||||
}
|
||||
WxMaUserPortrait.Item item = new WxMaUserPortrait.Item();
|
||||
item.setProvince(getAsMap(object, "province"));
|
||||
item.setCity(getAsMap(object, "city"));
|
||||
item.setGenders(getAsMap(object, "genders"));
|
||||
item.setPlatforms(getAsMap(object, "platforms"));
|
||||
item.setDevices(getAsMap(object, "devices"));
|
||||
item.setAges(getAsMap(object, "ages"));
|
||||
return item;
|
||||
}
|
||||
|
||||
private Map<String, Long> getAsMap(JsonObject object, String memberName) {
|
||||
JsonArray array = object.getAsJsonArray(memberName);
|
||||
if (array != null && array.size() > 0) {
|
||||
Map<String, Long> map = new LinkedHashMap<>(array.size());
|
||||
for (JsonElement element : array) {
|
||||
JsonObject elementObject = element.getAsJsonObject();
|
||||
String name = GsonHelper.getString(elementObject, "name");
|
||||
if (StringUtils.isNotBlank(name)) {
|
||||
Long value = GsonHelper.getLong(elementObject, "value");
|
||||
map.put(name, value);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package cn.binarywang.wx.miniapp.util.json;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitDistribution;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/charmingoh">Charming</a>
|
||||
* @since 2018-04-28
|
||||
*/
|
||||
public class WxMaVisitDistributionGsonAdapter implements JsonDeserializer<WxMaVisitDistribution> {
|
||||
@Override
|
||||
public WxMaVisitDistribution deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
||||
if (json == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
WxMaVisitDistribution distribution = new WxMaVisitDistribution();
|
||||
JsonObject object = json.getAsJsonObject();
|
||||
String refDate = GsonHelper.getString(object, "ref_date");
|
||||
distribution.setRefDate(refDate);
|
||||
|
||||
boolean hasList = object.has("list");
|
||||
if (!hasList) {
|
||||
return distribution;
|
||||
}
|
||||
|
||||
JsonArray listArray = object.getAsJsonArray("list");
|
||||
Map<String, Map<Integer, Integer>> list = new ConcurrentHashMap<>(listArray.size());
|
||||
for (JsonElement indexElement : listArray) {
|
||||
JsonObject indexObject = indexElement.getAsJsonObject();
|
||||
String index = GsonHelper.getString(indexObject, "index");
|
||||
if (index == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Map<Integer, Integer> itemList = new LinkedHashMap<>();
|
||||
JsonArray itemArray = indexObject.getAsJsonArray("item_list");
|
||||
if (itemArray == null || itemArray.size() <= 0) {
|
||||
list.put(index, itemList);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (JsonElement itemElement : itemArray) {
|
||||
JsonObject itemObject = itemElement.getAsJsonObject();
|
||||
Integer key = GsonHelper.getInteger(itemObject, "key");
|
||||
Integer value = GsonHelper.getInteger(itemObject, "value");
|
||||
if (key != null) {
|
||||
itemList.put(key, value);
|
||||
}
|
||||
}
|
||||
list.put(index, itemList);
|
||||
}
|
||||
distribution.setList(list);
|
||||
return distribution;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user