Skip to content

Commit 0a61a03

Browse files
committed
🎨 优化代码,抽取常量
1 parent e504578 commit 0a61a03

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpMenuServiceImpl.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
@Slf4j
2323
@RequiredArgsConstructor
2424
public class WxMpMenuServiceImpl implements WxMpMenuService {
25+
private static final String MENU_ID = "menuid";
26+
private static final String MATCH_RULE = "matchrule";
27+
2528
private final WxMpService wxMpService;
2629

2730
@Override
@@ -38,7 +41,7 @@ public String menuCreate(WxMenu menu) throws WxErrorException {
3841
log.debug("创建菜单:{},结果:{}", menuJson, result);
3942

4043
if (menu.getMatchRule() != null) {
41-
return GsonParser.parse(result).get("menuid").getAsString();
44+
return GsonParser.parse(result).get(MENU_ID).getAsString();
4245
}
4346

4447
return null;
@@ -48,13 +51,13 @@ public String menuCreate(WxMenu menu) throws WxErrorException {
4851
public String menuCreate(String json) throws WxErrorException {
4952
JsonObject jsonObject = GsonParser.parse(json);
5053
WxMpApiUrl.Menu url = MENU_CREATE;
51-
if (jsonObject.get("matchrule") != null) {
54+
if (jsonObject.get(MATCH_RULE) != null) {
5255
url = MENU_ADDCONDITIONAL;
5356
}
5457

5558
String result = this.wxMpService.post(url, json);
56-
if (jsonObject.get("matchrule") != null) {
57-
return GsonParser.parse(result).get("menuid").getAsString();
59+
if (jsonObject.get(MATCH_RULE) != null) {
60+
return GsonParser.parse(result).get(MENU_ID).getAsString();
5861
}
5962

6063
return null;
@@ -69,9 +72,9 @@ public void menuDelete() throws WxErrorException {
6972
@Override
7073
public void menuDelete(String menuId) throws WxErrorException {
7174
JsonObject jsonObject = new JsonObject();
72-
jsonObject.addProperty("menuid", menuId);
75+
jsonObject.addProperty(MENU_ID, menuId);
7376
String result = this.wxMpService.post(MENU_DELCONDITIONAL, jsonObject.toString());
74-
log.debug("根据MeunId({})删除个性化菜单结果:{}", menuId, result);
77+
log.debug("根据MenuId({})删除个性化菜单结果:{}", menuId, result);
7578
}
7679

7780
@Override

0 commit comments

Comments
 (0)