|
|
@@ -5,8 +5,6 @@ import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
import me.chanjar.weixin.common.bean.menu.WxMenu; |
|
|
|
import me.chanjar.weixin.common.exception.WxErrorException; |
|
|
|
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; |
|
|
|
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; |
|
|
|
import me.chanjar.weixin.mp.api.WxMpMenuService; |
|
|
|
import me.chanjar.weixin.mp.api.WxMpService; |
|
|
|
|
|
|
@@ -15,7 +13,8 @@ import me.chanjar.weixin.mp.api.WxMpService; |
|
|
|
*/ |
|
|
|
public class WxMpMenuServiceImpl implements WxMpMenuService { |
|
|
|
private static final String API_URL_PREFIX = "https://api.weixin.qq.com/cgi-bin/menu"; |
|
|
|
private static Logger log = LoggerFactory.getLogger(WxMpMenuServiceImpl.class); |
|
|
|
private static Logger log = LoggerFactory |
|
|
|
.getLogger(WxMpMenuServiceImpl.class); |
|
|
|
|
|
|
|
private WxMpService wxMpService; |
|
|
|
|
|
|
@@ -25,36 +24,37 @@ public class WxMpMenuServiceImpl implements WxMpMenuService { |
|
|
|
|
|
|
|
@Override |
|
|
|
public void menuCreate(WxMenu menu) throws WxErrorException { |
|
|
|
String menuJson = menu.toJson(); |
|
|
|
String url = API_URL_PREFIX + "/create"; |
|
|
|
String menuJson = menu.toJson(); |
|
|
|
String url = API_URL_PREFIX + "/create"; |
|
|
|
if (menu.getMatchRule() != null) { |
|
|
|
url = API_URL_PREFIX + "/addconditional"; |
|
|
|
} |
|
|
|
log.trace("开始创建菜单:{}", menuJson); |
|
|
|
|
|
|
|
String result = this.wxMpService.execute(new SimplePostRequestExecutor(), url,menuJson); |
|
|
|
} |
|
|
|
|
|
|
|
log.debug("开始创建菜单:{}", menuJson); |
|
|
|
|
|
|
|
String result = this.wxMpService.post(url, menuJson); |
|
|
|
log.debug("创建菜单:{},结果:{}", menuJson, result); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void menuDelete() throws WxErrorException { |
|
|
|
String url = API_URL_PREFIX + "/delete"; |
|
|
|
String result = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, null); |
|
|
|
String result = this.wxMpService.get(url, null); |
|
|
|
log.debug("删除菜单结果:{}", result); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void menuDelete(String menuid) throws WxErrorException { |
|
|
|
String url = API_URL_PREFIX + "/delconditional"; |
|
|
|
String result = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, "menuid=" + menuid); |
|
|
|
log.debug("根据MeunId({})删除菜单结果:{}", menuid,result); |
|
|
|
String result = this.wxMpService.get(url, "menuid=" + menuid); |
|
|
|
log.debug("根据MeunId({})删除菜单结果:{}", menuid, result); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxMenu menuGet() throws WxErrorException { |
|
|
|
String url = API_URL_PREFIX + "/get"; |
|
|
|
try { |
|
|
|
String resultContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, null); |
|
|
|
String resultContent = this.wxMpService.get(url, null); |
|
|
|
return WxMenu.fromJson(resultContent); |
|
|
|
} catch (WxErrorException e) { |
|
|
|
// 46003 不存在的菜单数据 |
|
|
@@ -69,11 +69,12 @@ public class WxMpMenuServiceImpl implements WxMpMenuService { |
|
|
|
public WxMenu menuTryMatch(String userid) throws WxErrorException { |
|
|
|
String url = API_URL_PREFIX + "/trymatch"; |
|
|
|
try { |
|
|
|
String resultContent = this.wxMpService.execute(new SimpleGetRequestExecutor(), url, "user_id=" + userid); |
|
|
|
String resultContent = this.wxMpService.get(url, "user_id=" + userid); |
|
|
|
return WxMenu.fromJson(resultContent); |
|
|
|
} catch (WxErrorException e) { |
|
|
|
// 46003 不存在的菜单数据 46002 不存在的菜单版本 |
|
|
|
if (e.getError().getErrorCode() == 46003 || e.getError().getErrorCode() == 46002) { |
|
|
|
if (e.getError().getErrorCode() == 46003 |
|
|
|
|| e.getError().getErrorCode() == 46002) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
throw e; |
|
|
|