🎨 #1189 优化错误异常输出,移除冗余代码

This commit is contained in:
Binary Wang
2019-09-05 12:31:26 +08:00
parent eecd4bec76
commit 8ab4af031a
81 changed files with 789 additions and 800 deletions

View File

@@ -9,20 +9,29 @@ import me.chanjar.weixin.common.error.WxErrorException;
import java.util.List;
public interface WxMaTemplateService {
//获取小程序模板库标题列表
/**
* 获取小程序模板库标题列表
*/
String TEMPLATE_LIBRARY_LIST_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list";
//获取模板库某个模板标题下关键词库
/**
* 获取模板库某个模板标题下关键词库
*/
String TEMPLATE_LIBRARY_KEYWORD_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get";
//组合模板并添加至帐号下的个人模板库
/**
* 组合模板并添加至帐号下的个人模板库
*/
String TEMPLATE_ADD_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/add";
//获取帐号下已存在的模板列表
/**
* 获取帐号下已存在的模板列表
*/
String TEMPLATE_LIST_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/list";
//删除帐号下的某个模板
/**
* 删除帐号下的某个模板
*/
String TEMPLATE_DEL_URL = "https://api.weixin.qq.com/cgi-bin/wxopen/template/del";
/**
@@ -32,6 +41,7 @@ public interface WxMaTemplateService {
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list?access_token=ACCESS_TOKEN
* </pre>
*
* @param offset
* @param count
* @return
@@ -45,6 +55,7 @@ public interface WxMaTemplateService {
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get?access_token=ACCESS_TOKEN
* </pre>
*
* @param id
* @return
*/
@@ -57,6 +68,7 @@ public interface WxMaTemplateService {
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/add?access_token=ACCESS_TOKEN
* </pre>
*
* @param id
* @param keywordIdList
* @return
@@ -70,6 +82,7 @@ public interface WxMaTemplateService {
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=ACCESS_TOKEN
* </pre>
*
* @param offset
* @param count
* @return
@@ -83,6 +96,7 @@ public interface WxMaTemplateService {
* 详情请见: <a href="https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1500465446_j4CgR&token=&lang=zh_CN">获取小程序模板库标题列表</a>
* 接口url格式: https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=ACCESS_TOKEN
* </pre>
*
* @param templateId
*/
boolean delTemplate(String templateId) throws WxErrorException;

View File

@@ -12,6 +12,7 @@ import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.time.DateFormatUtils;
@@ -23,14 +24,11 @@ import java.util.List;
* @author <a href="https://github.com/charmingoh">Charming</a>
* @since 2018-04-28
*/
@AllArgsConstructor
public class WxMaAnalysisServiceImpl implements WxMaAnalysisService {
private static final JsonParser JSON_PARSER = new JsonParser();
private WxMaService wxMaService;
public WxMaAnalysisServiceImpl(WxMaService wxMaService) {
this.wxMaService = wxMaService;
}
@Override
public List<WxMaSummaryTrend> getDailySummaryTrend(Date beginDate, Date endDate) throws WxErrorException {
return getAnalysisResultAsList(GET_DAILY_SUMMARY_TREND_URL, beginDate, endDate,

View File

@@ -8,6 +8,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import cn.binarywang.wx.miniapp.api.WxMaCodeService;
@@ -31,14 +32,11 @@ import me.chanjar.weixin.common.util.json.GsonHelper;
* @author <a href="https://github.com/charmingoh">Charming</a>
* @since 2018-04-26 20:00
*/
@AllArgsConstructor
public class WxMaCodeServiceImpl implements WxMaCodeService {
private static final JsonParser JSON_PARSER = new JsonParser();
private WxMaService wxMaService;
public WxMaCodeServiceImpl(WxMaService wxMaService) {
this.wxMaService = wxMaService;
}
@Override
public void commit(WxMaCodeCommitRequest commitRequest) throws WxErrorException {
this.wxMaService.post(COMMIT_URL, commitRequest.toJson());
@@ -50,7 +48,7 @@ public class WxMaCodeServiceImpl implements WxMaCodeService {
Path qrCodeFilePath = null;
try {
RequestExecutor<File, String> executor = BaseMediaDownloadRequestExecutor
.create(this.wxMaService.getRequestHttp(), Files.createTempDirectory("weixin-java-tools-ma-" + appId).toFile());
.create(this.wxMaService.getRequestHttp(), Files.createTempDirectory("wxjava-ma-" + appId).toFile());
final StringBuilder url = new StringBuilder(GET_QRCODE_URL);
if (StringUtils.isNotBlank(path)) {

View File

@@ -5,6 +5,7 @@ import cn.binarywang.wx.miniapp.api.WxMaService;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.RandomUtils;
@@ -19,19 +20,18 @@ import java.util.concurrent.locks.Lock;
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@AllArgsConstructor
public class WxMaJsapiServiceImpl implements WxMaJsapiService {
private static final JsonParser JSON_PARSER = new JsonParser();
private WxMaService wxMaService;
public WxMaJsapiServiceImpl(WxMaService wxMaService) {
this.wxMaService = wxMaService;
}
@Override
public String getCardApiTicket() throws WxErrorException {
return getCardApiTicket(false);
}
@Override
public String getCardApiTicket(boolean forceRefresh) throws WxErrorException {
Lock lock = this.wxMaService.getWxMaConfig().getCardApiTicketLock();
try {

View File

@@ -2,8 +2,9 @@ package cn.binarywang.wx.miniapp.api.impl;
import cn.binarywang.wx.miniapp.api.WxMaMediaService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import me.chanjar.weixin.common.error.WxError;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
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.BaseMediaDownloadRequestExecutor;
@@ -19,13 +20,10 @@ import java.util.UUID;
/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@AllArgsConstructor
public class WxMaMediaServiceImpl implements WxMaMediaService {
private WxMaService wxMaService;
public WxMaMediaServiceImpl(WxMaService wxMaService) {
this.wxMaService = wxMaService;
}
@Override
public WxMediaUploadResult uploadMedia(String mediaType, String fileType, InputStream inputStream) throws WxErrorException {
try {

View File

@@ -8,20 +8,19 @@ import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@AllArgsConstructor
public class WxMaMsgServiceImpl implements WxMaMsgService {
private static final JsonParser JSON_PARSER = new JsonParser();
private WxMaService wxMaService;
public WxMaMsgServiceImpl(WxMaService wxMaService) {
this.wxMaService = wxMaService;
}
@Override
public boolean sendKefuMsg(WxMaKefuMessage message) throws WxErrorException {
String responseContent = this.wxMaService.post(KEFU_MESSAGE_SEND_URL, message.toJson());
@@ -33,7 +32,7 @@ public class WxMaMsgServiceImpl implements WxMaMsgService {
String responseContent = this.wxMaService.post(TEMPLATE_MSG_SEND_URL, templateMessage.toJson());
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
if (jsonObject.get(WxMaConstants.ERRCODE).getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent));
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
}
}
@@ -42,7 +41,7 @@ public class WxMaMsgServiceImpl implements WxMaMsgService {
String responseContent = this.wxMaService.post(UNIFORM_MSG_SEND_URL, uniformMessage.toJson());
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
if (jsonObject.get(WxMaConstants.ERRCODE).getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent));
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
}
}

View File

@@ -4,33 +4,28 @@ import cn.binarywang.wx.miniapp.api.WxMaPluginService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaPluginListResult;
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
import com.google.common.collect.ImmutableMap;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import java.util.HashMap;
import java.util.Map;
@AllArgsConstructor
public class WxMaPluginServiceImpl implements WxMaPluginService {
private WxMaService wxMaService;
public WxMaPluginServiceImpl(WxMaService wxMaService) {
this.wxMaService = wxMaService;
}
@Override
public void applyPlugin(String pluginAppId, String reason) throws WxErrorException {
Map<String, String> params = new HashMap<>();
params.put("action", "apply");
params.put("plugin_appid", pluginAppId);
params.put("reason", reason);
Map<String, String> params = ImmutableMap.of("action", "apply",
"plugin_appid", pluginAppId,
"reason", reason);
this.wxMaService.post(PLUGIN_URL, WxMaGsonBuilder.create().toJson(params));
}
@Override
public WxMaPluginListResult getPluginList() throws WxErrorException {
Map<String, String> params = new HashMap<>();
params.put("action", "list");
Map<String, String> params = ImmutableMap.of("action", "list");
String responseContent = this.wxMaService.post(PLUGIN_URL, WxMaGsonBuilder.create().toJson(params));
return WxMaPluginListResult.fromJson(responseContent);
@@ -38,19 +33,15 @@ public class WxMaPluginServiceImpl implements WxMaPluginService {
@Override
public void unbindPlugin(String pluginAppId) throws WxErrorException {
Map<String, String> params = new HashMap<>();
params.put("action", "unbind");
params.put("plugin_appid", pluginAppId);
Map<String, String> params = ImmutableMap.of("action", "unbind", "plugin_appid", pluginAppId);
this.wxMaService.post(PLUGIN_URL, WxMaGsonBuilder.create().toJson(params));
}
@Override
public void updatePlugin(String pluginAppId, String userVersion) throws WxErrorException {
Map<String, String> params = new HashMap<>();
params.put("action", "update");
params.put("plugin_appid", pluginAppId);
params.put("user_version", userVersion);
Map<String, String> params = ImmutableMap.of("action", "update",
"plugin_appid", pluginAppId,
"user_version", userVersion);
this.wxMaService.post(PLUGIN_URL, WxMaGsonBuilder.create().toJson(params));
}

View File

@@ -1,7 +1,5 @@
package cn.binarywang.wx.miniapp.api.impl;
import java.io.File;
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
@@ -10,18 +8,18 @@ import cn.binarywang.wx.miniapp.bean.WxaCode;
import cn.binarywang.wx.miniapp.bean.WxaCodeUnlimit;
import cn.binarywang.wx.miniapp.util.QrcodeBytesRequestExecutor;
import cn.binarywang.wx.miniapp.util.QrcodeRequestExecutor;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import java.io.File;
/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@AllArgsConstructor
public class WxMaQrcodeServiceImpl implements WxMaQrcodeService {
private WxMaService wxMaService;
public WxMaQrcodeServiceImpl(WxMaService wxMaService) {
this.wxMaService = wxMaService;
}
@Override
public byte[] createQrcodeBytes(String path, int width) throws WxErrorException {
final QrcodeBytesRequestExecutor executor = new QrcodeBytesRequestExecutor(this.wxMaService.getRequestHttp());

View File

@@ -6,6 +6,7 @@ import cn.binarywang.wx.miniapp.api.WxMaRunService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaRunStepInfo;
import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
import lombok.AllArgsConstructor;
/**
* <pre>
@@ -15,13 +16,10 @@ import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@AllArgsConstructor
public class WxMaRunServiceImpl implements WxMaRunService {
private WxMaService service;
public WxMaRunServiceImpl(WxMaService service) {
this.service = service;
}
@Override
public List<WxMaRunStepInfo> getRunStepInfo(String sessionKey, String encryptedData, String ivStr) {
return WxMaRunStepInfo.fromJson(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr));

View File

@@ -1,14 +1,15 @@
package cn.binarywang.wx.miniapp.api.impl;
import java.io.File;
import cn.binarywang.wx.miniapp.api.WxMaSecCheckService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import com.google.gson.JsonObject;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
import java.io.File;
/**
* <pre>
*
@@ -17,13 +18,10 @@ import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@AllArgsConstructor
public class WxMaSecCheckServiceImpl implements WxMaSecCheckService {
private WxMaService service;
public WxMaSecCheckServiceImpl(WxMaService service) {
this.service = service;
}
@Override
public boolean checkImage(File file) throws WxErrorException {
//这里只是借用MediaUploadRequestExecutor并不使用其返回值WxMediaUploadResult

View File

@@ -119,7 +119,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
}
try (CloseableHttpResponse response = getRequestHttpClient().execute(httpGet)) {
String resultContent = new BasicResponseHandler().handleResponse(response);
WxError error = WxError.fromJson(resultContent);
WxError error = WxError.fromJson(resultContent, WxType.MiniApp);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
@@ -251,7 +251,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "access_token=" + accessToken;
try {
T result = executor.execute(uriWithAccessToken, data);
T result = executor.execute(uriWithAccessToken, data, WxType.MiniApp);
log.debug("\n【请求地址】: {}\n【请求参数】{}\n【响应数据】{}", uriWithAccessToken, dataForLog, result);
return result;
} catch (WxErrorException e) {

View File

@@ -4,6 +4,7 @@ import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaSettingService;
import cn.binarywang.wx.miniapp.bean.WxMaDomainAction;
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import java.util.HashMap;
@@ -13,13 +14,10 @@ import java.util.Map;
* @author <a href="https://github.com/charmingoh">Charming</a>
* @since 2018-04-27 15:46
*/
@AllArgsConstructor
public class WxMaSettingServiceImpl implements WxMaSettingService {
private WxMaService wxMaService;
public WxMaSettingServiceImpl(WxMaService wxMaService) {
this.wxMaService = wxMaService;
}
@Override
public WxMaDomainAction modifyDomain(WxMaDomainAction domainAction) throws WxErrorException {
String responseContent = this.wxMaService.post(MODIFY_DOMAIN_URL, domainAction.toJson());

View File

@@ -4,17 +4,15 @@ import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaShareService;
import cn.binarywang.wx.miniapp.bean.WxMaShareInfo;
import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
import lombok.AllArgsConstructor;
/**
* @author zhfish
*/
@AllArgsConstructor
public class WxMaShareServiceImpl implements WxMaShareService {
private WxMaService service;
public WxMaShareServiceImpl(WxMaService service) {
this.service = service;
}
@Override
public WxMaShareInfo getShareInfo(String sessionKey, String encryptedData, String ivStr) {
return WxMaShareInfo.fromJson(WxMaCryptUtils.decrypt(sessionKey, encryptedData, ivStr));

View File

@@ -1,101 +1,58 @@
package cn.binarywang.wx.miniapp.api.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaTemplateService;
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateAddResult;
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryGetResult;
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateLibraryListResult;
import cn.binarywang.wx.miniapp.bean.template.WxMaTemplateListResult;
import me.chanjar.weixin.common.error.WxError;
import com.google.common.collect.ImmutableMap;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import java.util.List;
import java.util.Map;
/**
* @author Binary
*/
@AllArgsConstructor
public class WxMaTemplateServiceImpl implements WxMaTemplateService {
private WxMaService wxMaService;
public WxMaTemplateServiceImpl(WxMaService wxMaService) {
this.wxMaService = wxMaService;
}
@Override
public WxMaTemplateLibraryListResult findTemplateLibraryList(int offset, int count) throws WxErrorException {
Map<String, Integer> params = new HashMap<>();
params.put("offset", offset);
params.put("count", count);
Map<String, Integer> params = ImmutableMap.of("offset", offset, "count", count);
String responseText = this.wxMaService.post(TEMPLATE_LIBRARY_LIST_URL, WxGsonBuilder.create().toJson(params));
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return WxMaTemplateLibraryListResult.fromJson(responseText);
} else {
throw new WxErrorException(wxError);
}
return WxMaTemplateLibraryListResult.fromJson(responseText);
}
@Override
public WxMaTemplateLibraryGetResult findTemplateLibraryKeywordList(String id) throws WxErrorException {
Map<String, String> params = new HashMap<>();
params.put("id", id);
String responseText = this.wxMaService.post(TEMPLATE_LIBRARY_KEYWORD_URL, WxGsonBuilder.create().toJson(params));
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return WxMaTemplateLibraryGetResult.fromJson(responseText);
} else {
throw new WxErrorException(wxError);
}
String responseText = this.wxMaService.post(TEMPLATE_LIBRARY_KEYWORD_URL,
WxGsonBuilder.create().toJson(ImmutableMap.of("id", id)));
return WxMaTemplateLibraryGetResult.fromJson(responseText);
}
@Override
public WxMaTemplateAddResult addTemplate(String id, List<Integer> keywordIdList) throws WxErrorException {
Map<String, Object> params = new HashMap<>();
params.put("id", id);
params.put("keyword_id_list", keywordIdList.toArray());
String responseText = this.wxMaService.post(TEMPLATE_ADD_URL, WxGsonBuilder.create().toJson(params));
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return WxMaTemplateAddResult.fromJson(responseText);
} else {
throw new WxErrorException(wxError);
}
String responseText = this.wxMaService.post(TEMPLATE_ADD_URL,
WxGsonBuilder.create().toJson(ImmutableMap.of("id", id, "keyword_id_list", keywordIdList.toArray())));
return WxMaTemplateAddResult.fromJson(responseText);
}
@Override
public WxMaTemplateListResult findTemplateList(int offset, int count) throws WxErrorException {
Map<String, Integer> params = new HashMap<>();
params.put("offset", offset);
params.put("count", count);
Map<String, Integer> params = ImmutableMap.of("offset", offset, "count", count);
String responseText = this.wxMaService.post(TEMPLATE_LIST_URL, WxGsonBuilder.create().toJson(params));
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return WxMaTemplateListResult.fromJson(responseText);
} else {
throw new WxErrorException(wxError);
}
return WxMaTemplateListResult.fromJson(responseText);
}
@Override
public boolean delTemplate(String templateId) throws WxErrorException {
Map<String, String> params = new HashMap<>();
params.put("template_id", templateId);
String responseText = this.wxMaService.post(TEMPLATE_DEL_URL, WxGsonBuilder.create().toJson(params));
WxError wxError = WxError.fromJson(responseText);
if (wxError.getErrorCode() == 0) {
return true;
} else {
throw new WxErrorException(wxError);
}
Map<String, String> params = ImmutableMap.of("template_id", templateId);
this.wxMaService.post(TEMPLATE_DEL_URL, WxGsonBuilder.create().toJson(params));
return true;
}
}

View File

@@ -10,7 +10,6 @@ import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.SignUtils;
import org.apache.commons.codec.digest.DigestUtils;
@@ -49,11 +48,7 @@ public class WxMaUserServiceImpl implements WxMaUserService {
String params = param.toString();
String signature = SignUtils.createHmacSha256Sign(params, sessionKey);
String url = String.format(SET_USER_STORAGE, config.getAppid(), signature, openid, "hmac_sha256");
String result = this.service.post(url, params);
WxError error = WxError.fromJson(result);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
this.service.post(url, params);
}
@Override

View File

@@ -1,8 +1,14 @@
package cn.binarywang.wx.miniapp.util;
import java.io.IOException;
import java.io.InputStream;
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
import me.chanjar.weixin.common.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;
@@ -13,14 +19,8 @@ import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
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 java.io.IOException;
import java.io.InputStream;
/**
* @author <a href="https://github.com/binarywang">Binary Wang</a>
@@ -33,12 +33,12 @@ public class QrcodeBytesRequestExecutor implements RequestExecutor<byte[], Abstr
}
@Override
public void execute(String uri, AbstractWxMaQrcodeWrapper data, ResponseHandler<byte[]> handler) throws WxErrorException, IOException {
handler.handle(this.execute(uri, data));
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) throws WxErrorException, IOException {
public byte[] execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (requestHttp.getRequestHttpProxy() != null) {
httpPost.setConfig(
@@ -55,7 +55,7 @@ public class QrcodeBytesRequestExecutor implements RequestExecutor<byte[], Abstr
&& ContentType.APPLICATION_JSON.getMimeType()
.equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
throw new WxErrorException(WxError.fromJson(responseContent));
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
}
return IOUtils.toByteArray(inputStream);

View File

@@ -1,10 +1,15 @@
package cn.binarywang.wx.miniapp.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
import me.chanjar.weixin.common.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;
@@ -14,15 +19,10 @@ import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import cn.binarywang.wx.miniapp.bean.AbstractWxMaQrcodeWrapper;
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 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>
@@ -35,12 +35,12 @@ public class QrcodeRequestExecutor implements RequestExecutor<File, AbstractWxMa
}
@Override
public void execute(String uri, AbstractWxMaQrcodeWrapper data, ResponseHandler<File> handler) throws WxErrorException, IOException {
handler.handle(this.execute(uri, data));
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) throws WxErrorException, IOException {
public File execute(String uri, AbstractWxMaQrcodeWrapper qrcodeWrapper, WxType wxType) throws WxErrorException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (requestHttp.getRequestHttpProxy() != null) {
httpPost.setConfig(
@@ -57,7 +57,7 @@ public class QrcodeRequestExecutor implements RequestExecutor<File, AbstractWxMa
&& ContentType.APPLICATION_JSON.getMimeType()
.equals(ContentType.parse(contentTypeHeader[0].getValue()).getMimeType())) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
throw new WxErrorException(WxError.fromJson(responseContent));
throw new WxErrorException(WxError.fromJson(responseContent, wxType));
}
return FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");