微信菜单:
Button类:
private String type;
private String name;
private String key;
private String url;
private String media_id;
private List<Button2> sub_button =new ArrayList<Button2>();
public void addSubButton(Button2 bt) {
sub_button.add(bt);
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMedia_id() {
return media_id;
}
public void setMedia_id(String media_id) {
this.media_id = media_id;
}
public List<Button2> getSub_button() {
return sub_button;
}
public void setSub_button(List<Button2> sub_button) {
this.sub_button = sub_button;
}
Menu类
private List< Button2> button =new ArrayList<Button2>();
public void addButton(Button2 btn) {
button.add(btn);
}
public List<Button2> getButton() {
return button;
}
public void setButton(List<Button2> button) {
this.button = button;
}
设置按钮时 要设置好type、key、name
imageUtil:用于图像获取和图像合成
public static BufferedImage getImage(File file) throws IOException {
return ImageIO.read(file);
}
public static BufferedImage getImage(String url) throws IOException {
return ImageIO.read(new URL(url));
}
public static BufferedImage merge(BufferedImage bgimage,BufferedImage hdimage,int x,int y,int width,int height) {
Graphics2D gh=bgimage.createGraphics();
gh.drawImage(hdimage, x, y,width,height,null);
gh.dispose();
return bgimage;
}
public static void saveImage(BufferedImage image,String formate,String savePath) throws Exception {
FileOutputStream output=new FileOutputStream(savePath);
ImageIO.write(image, formate, output);
}
Weixinapi类 用于操作微信提供的接口 如:获取用户信息,上传素材生成二维码等
private static final String GET_TOKEN ="https://api.weixin.qq.com/cgi-bin/token?"
+ "grant_type=client_credential&appid=%s&secret=%s";
private static final String GET="GET";
private static final String POST="POST";
private static final String CHARSET=ConfigUtil.getValue("charset");
private static final String APPID=ConfigUtil.getValue("appID");
private static final String SECRET=ConfigUtil.getValue("appsecret");
private static Token token;
private static final String CreateMenu="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s";
public static Token getToken() throws Exception {
if (token!=null&&!token.isExpires()) {
return token;
}
String jsonstr=HttpUtil.sendHttpReq(String.format(GET_TOKEN, APPID,SECRET), GET, null,CHARSET);
Token token=JSONObject.parseObject(jsonstr,Token.class);
return token;
}
//创建菜单
public static Result createMenu(String menujson) throws Exception {
Token token=getToken();
String resultjson=HttpUtil.sendHttpReq(String.format(CreateMenu,token.getAccess_token()), POST, menujson, CHARSET);
return (Result) JSONObject.parseObject(resultjson,Result.class);
}
private static final String GETUSERINFO="https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN";
public static UserInfo getUserInfo(String openid) throws Exception{
System.out.println("getUserInfo openid"+openid);
Token tk=getToken();
String jsonstr=HttpUtil.sendHttpReq(String.format(GETUSERINFO, tk.getAccess_token(),openid), GET, null, CHARSET);
System.out.println(jsonstr);
UserInfo user=JSONObject.parseObject(jsonstr,UserInfo.class);
return user;
}
private static final String GET_QRTK="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s";
public static QRticket getQRticket(int sceneid) throws Exception{
Token tk=getToken();
QRcode qr=new QRcode();
qr.setSceneId(sceneid);
String jsonstr=HttpUtil.sendHttpReq(String.format(GET_QRTK, tk.getAccess_token()), POST, JSONObject.toJSONString(qr), CHARSET);
System.out.println(jsonstr);
QRticket qrtk=JSONObject.parseObject(jsonstr,QRticket.class);
System.out.println(qrtk.getTicket());
return qrtk;
}
private static final String UPLOAD="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s";
public static MediaResult uploadMedia(String path,String type) throws Exception {
Token kt=getToken();
System.out.println(kt.getAccess_token());
String jsonstr=HttpUtil.upload(String.format(UPLOAD, kt.getAccess_token(),type),path, CHARSET);
System.out.println(jsonstr);
MediaResult result=JSONObject.parseObject(jsonstr,MediaResult.class);
return result;
}
private static final String SEND_MSG="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s";
public static void sendCustomMessage(String msg) throws Exception {
Token tk=getToken();
String jsonstr=HttpUtil.sendHttpReq(String.format(SEND_MSG, tk.getAccess_token()), POST,msg, CHARSET);
System.out.println(jsonstr);
}