mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-11-02 12:38:22 +08:00
🎨 统一抽取errcode常量,同步优化规范部分代码
This commit is contained in:
@ -161,7 +161,7 @@ public class WxMpCardServiceImpl implements WxMpCardService {
|
||||
|
||||
// 判断返回值
|
||||
JsonObject json = GsonParser.parse(responseContent);
|
||||
String errcode = json.get("errcode").getAsString();
|
||||
String errcode = json.get(WxConsts.ERR_CODE).getAsString();
|
||||
if (!"0".equals(errcode)) {
|
||||
String errmsg = json.get("errmsg").getAsString();
|
||||
throw new WxErrorException(WxError.builder()
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package me.chanjar.weixin.mp.api.impl;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
@ -27,7 +28,6 @@ public class WxMpDraftServiceImpl implements WxMpDraftService {
|
||||
|
||||
private static final String MEDIA_ID = "media_id";
|
||||
private static final String ERRCODE_SUCCESS = "0";
|
||||
private static final String ERRCODE = "errcode";
|
||||
private final WxMpService mpService;
|
||||
|
||||
@Override
|
||||
@ -49,7 +49,7 @@ public class WxMpDraftServiceImpl implements WxMpDraftService {
|
||||
@Override
|
||||
public Boolean updateDraft(WxMpUpdateDraft updateDraftInfo) throws WxErrorException {
|
||||
String json = this.mpService.post(WxMpApiUrl.Draft.UPDATE_DRAFT, updateDraftInfo);
|
||||
return GsonParser.parse(json).get(ERRCODE).getAsString().equals(ERRCODE_SUCCESS);
|
||||
return GsonParser.parse(json).get(WxConsts.ERR_CODE).getAsString().equals(ERRCODE_SUCCESS);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -62,7 +62,7 @@ public class WxMpDraftServiceImpl implements WxMpDraftService {
|
||||
public Boolean delDraft(String mediaId) throws WxErrorException {
|
||||
String json = this.mpService.post(WxMpApiUrl.Draft.DEL_DRAFT,
|
||||
GsonHelper.buildJsonObject(MEDIA_ID, mediaId));
|
||||
return GsonParser.parse(json).get(ERRCODE).getAsString().equals(ERRCODE_SUCCESS);
|
||||
return GsonParser.parse(json).get(WxConsts.ERR_CODE).getAsString().equals(ERRCODE_SUCCESS);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package me.chanjar.weixin.mp.api.impl;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
@ -24,7 +25,6 @@ public class WxMpFreePublishServiceImpl implements WxMpFreePublishService {
|
||||
private static final String PUBLISH_ID = "publish_id";
|
||||
private static final String ARTICLE_ID = "article_id";
|
||||
private static final String ERRCODE_SUCCESS = "0";
|
||||
private static final String ERRCODE = "errcode";
|
||||
private final WxMpService mpService;
|
||||
|
||||
@Override
|
||||
@ -44,7 +44,7 @@ public class WxMpFreePublishServiceImpl implements WxMpFreePublishService {
|
||||
public Boolean deletePush(String articleId, Integer index) throws WxErrorException {
|
||||
String json = this.mpService.post(WxMpApiUrl.FreePublish.DEL_PUSH,
|
||||
GsonHelper.buildJsonObject(ARTICLE_ID, articleId, "index", index));
|
||||
return GsonParser.parse(json).get(ERRCODE).toString().equals(ERRCODE_SUCCESS);
|
||||
return GsonParser.parse(json).get(WxConsts.ERR_CODE).toString().equals(ERRCODE_SUCCESS);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -34,7 +34,6 @@ import static me.chanjar.weixin.mp.enums.WxMpApiUrl.SubscribeMsg.*;
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class WxMpSubscribeMsgServiceImpl implements WxMpSubscribeMsgService {
|
||||
private static final String ERR_CODE = "errcode";
|
||||
private final WxMpService service;
|
||||
|
||||
@Override
|
||||
|
||||
@ -2,6 +2,7 @@ package me.chanjar.weixin.mp.api.impl;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
@ -25,15 +26,13 @@ import static me.chanjar.weixin.mp.enums.WxMpApiUrl.TemplateMsg.*;
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class WxMpTemplateMsgServiceImpl implements WxMpTemplateMsgService {
|
||||
|
||||
|
||||
private final WxMpService wxMpService;
|
||||
|
||||
@Override
|
||||
public String sendTemplateMsg(WxMpTemplateMessage templateMessage) throws WxErrorException {
|
||||
String responseContent = this.wxMpService.post(MESSAGE_TEMPLATE_SEND, templateMessage.toJson());
|
||||
final JsonObject jsonObject = GsonParser.parse(responseContent);
|
||||
if (jsonObject.get("errcode").getAsInt() == 0) {
|
||||
if (jsonObject.get(WxConsts.ERR_CODE).getAsInt() == 0) {
|
||||
return jsonObject.get("msgid").getAsString();
|
||||
}
|
||||
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MP));
|
||||
@ -62,7 +61,7 @@ public class WxMpTemplateMsgServiceImpl implements WxMpTemplateMsgService {
|
||||
jsonObject.addProperty("template_id_short", shortTemplateId);
|
||||
String responseContent = this.wxMpService.post(TEMPLATE_API_ADD_TEMPLATE, jsonObject.toString());
|
||||
final JsonObject result = GsonParser.parse(responseContent);
|
||||
if (result.get("errcode").getAsInt() == 0) {
|
||||
if (result.get(WxConsts.ERR_CODE).getAsInt() == 0) {
|
||||
return result.get("template_id").getAsString();
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package me.chanjar.weixin.mp.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.util.json.GsonHelper;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;
|
||||
|
||||
@ -16,8 +17,8 @@ public class WxMpMassSendResultAdapter implements JsonDeserializer<WxMpMassSendR
|
||||
WxMpMassSendResult sendResult = new WxMpMassSendResult();
|
||||
JsonObject sendResultJsonObject = json.getAsJsonObject();
|
||||
|
||||
if (sendResultJsonObject.get("errcode") != null && !sendResultJsonObject.get("errcode").isJsonNull()) {
|
||||
sendResult.setErrorCode(GsonHelper.getAsString(sendResultJsonObject.get("errcode")));
|
||||
if (sendResultJsonObject.get(WxConsts.ERR_CODE) != null && !sendResultJsonObject.get(WxConsts.ERR_CODE).isJsonNull()) {
|
||||
sendResult.setErrorCode(GsonHelper.getAsString(sendResultJsonObject.get(WxConsts.ERR_CODE)));
|
||||
}
|
||||
if (sendResultJsonObject.get("errmsg") != null && !sendResultJsonObject.get("errmsg").isJsonNull()) {
|
||||
sendResult.setErrorMsg(GsonHelper.getAsString(sendResultJsonObject.get("errmsg")));
|
||||
|
||||
@ -22,7 +22,7 @@ public class WxMpMemberCardActivateTempInfoResultGsonAdapter implements JsonDese
|
||||
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
|
||||
result.setErrorCode(GsonHelper.getString(jsonObject, "errcode"));
|
||||
result.setErrorCode(GsonHelper.getString(jsonObject, WxConsts.ERR_CODE));
|
||||
result.setErrorMsg(GsonHelper.getString(jsonObject, "errmsg"));
|
||||
|
||||
JsonObject userInfoJsonObject = jsonObject.getAsJsonObject("info");
|
||||
|
||||
@ -27,7 +27,7 @@ public class WxMpMemberCardUpdateResultGsonAdapter implements JsonDeserializer<W
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
|
||||
result.setOpenId(GsonHelper.getString(jsonObject, "openid"));
|
||||
result.setErrorCode(GsonHelper.getString(jsonObject, "errcode"));
|
||||
result.setErrorCode(GsonHelper.getString(jsonObject, WxConsts.ERR_CODE));
|
||||
result.setErrorMsg(GsonHelper.getString(jsonObject, "errmsg"));
|
||||
result.setResultBalance(GsonHelper.getDouble(jsonObject, "result_balance"));
|
||||
result.setResultBonus(GsonHelper.getInteger(jsonObject, "result_bonus"));
|
||||
|
||||
@ -31,7 +31,7 @@ public class WxMpMemberCardUserInfoResultGsonAdapter implements JsonDeserializer
|
||||
JsonObject jsonObject = jsonElement.getAsJsonObject();
|
||||
|
||||
result.setOpenId(getString(jsonObject, "openid"));
|
||||
result.setErrorCode(getString(jsonObject, "errcode"));
|
||||
result.setErrorCode(getString(jsonObject, WxConsts.ERR_CODE));
|
||||
result.setErrorMsg(getString(jsonObject, "errmsg"));
|
||||
result.setNickname(getString(jsonObject, "nickname"));
|
||||
result.setMembershipNumber(getString(jsonObject, "membership_number"));
|
||||
|
||||
Reference in New Issue
Block a user