微信后端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.6 KiB

package com.ynxbd.wx.wxfactory.menu;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ynxbd.common.helper.http.OkHttpHelper;
import com.ynxbd.wx.wxfactory.WxCacheHelper;
import com.ynxbd.wx.wxfactory.menu.bean.WxMenu;
import com.ynxbd.wx.wxfactory.menu.result.WxMenuResult;
import lombok.extern.slf4j.Slf4j;
import okhttp3.RequestBody;
import java.util.Map;
import static com.ynxbd.common.helper.http.OkHttpHelper.TYPE_JSON;
@Slf4j
public class WxService {
public void createMenu(WxMenu menu) throws Exception {
createMenu(menu, false);
}
public void createMenu(WxMenu menu, boolean condition) throws Exception {
String url = (condition
? "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token="
: "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=")
+ WxCacheHelper.getAccessToken();
try {
String result = this.post(url, new ObjectMapper().writeValueAsString(menu));
System.out.println("[wx-tools]Create Menu result:" + result);
} catch (Exception e) {
throw new Exception("[wx-tools]createMenu failure.");
}
}
public String deleteMenu() throws Exception {
String url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + WxCacheHelper.getAccessToken();
String result = this.get(url, null);
System.out.println("[wx-tools]Delete Menu result:" + result);
return result;
}
public String deleteMenu(String menuId) throws Exception {
String url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=" + WxCacheHelper.getAccessToken();
String json = "{\"menuid\":" + menuId + "}";
String result = this.post(url, json);
System.out.println("[wx-tools]Delete Conditional Menu result:" + result);
return result;
}
public WxMenuResult getMenu() throws Exception {
String url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + WxCacheHelper.getAccessToken();
try {
return WxMenuResult.fromJson(get(url, null));
} catch (Exception e) {
throw new Exception("[wx-tools]getMenu failure.");
}
}
public String get(String url, Map<String, String> params) throws Exception {
return OkHttpHelper.get(url, (OkHttpHelper.MapParams) params);
}
public String post(String url, String params) throws Exception {
return OkHttpHelper.post(url, RequestBody.create(TYPE_JSON, params), null);
}
}