Skip to content

Commit

Permalink
Merge pull request #249 from tianmu/master
Browse files Browse the repository at this point in the history
add conditional menu method
  • Loading branch information
chanjarster committed Dec 23, 2015
2 parents 5309e13 + 6b522da commit 05777c6
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import me.chanjar.weixin.common.util.json.WxGsonBuilder;
import org.apache.commons.codec.Charsets;

import me.chanjar.weixin.common.util.json.WxGsonBuilder;

/**
* 企业号菜单
* @author Daniel Qian
Expand All @@ -19,6 +19,8 @@ public class WxMenu implements Serializable {

private List<WxMenuButton> buttons = new ArrayList<WxMenuButton>();

private WxMenuRule matchRule;

public List<WxMenuButton> getButtons() {
return buttons;
}
Expand All @@ -27,6 +29,14 @@ public void setButtons(List<WxMenuButton> buttons) {
this.buttons = buttons;
}

public WxMenuRule getMatchRule() {
return matchRule;
}

public void setMatchRule(WxMenuRule matchRule) {
this.matchRule = matchRule;
}

public String toJson() {
return WxGsonBuilder.create().toJson(this);
}
Expand Down Expand Up @@ -118,5 +128,74 @@ public String toString() {
'}';
}
}


public static class WxMenuRule {
private String groupId;
private String sex;
private String country;
private String province;
private String city;
private String clientPlatformType;

public String getGroupId() {
return groupId;
}

public void setGroupId(String groupId) {
this.groupId = groupId;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public String getProvince() {
return province;
}

public void setProvince(String province) {
this.province = province;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getClientPlatformType() {
return clientPlatformType;
}

public void setClientPlatformType(String clientPlatformType) {
this.clientPlatformType = clientPlatformType;
}

@Override
public String toString() {
return "matchrule:{" +
"group_id='" + groupId + '\'' +
", sex='" + sex + '\'' +
", country" + country + '\'' +
", province" + province + '\'' +
", city" + city + '\'' +
", client_platform_type" + clientPlatformType + '\'' +
"}";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

import java.lang.reflect.Type;

import me.chanjar.weixin.common.bean.WxMenu;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
Expand All @@ -21,6 +20,8 @@
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import me.chanjar.weixin.common.bean.WxMenu;

/**
*
* @author Daniel Qian
Expand All @@ -38,6 +39,11 @@ public JsonElement serialize(WxMenu menu, Type typeOfSrc, JsonSerializationConte
}
json.add("button", buttonArray);

if (menu.getMatchRule() != null) {
Gson gson = new Gson();
json.add("matchrule", gson.toJsonTree(menu.getMatchRule()));
}

return json;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ public interface WxMpService {
* <pre>
* 自定义菜单创建接口
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口
* 如果要创建个性化菜单,请设置matchrule属性
* 详情请见:http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
* </pre>
* @param menu
* @throws WxErrorException
Expand All @@ -339,6 +341,16 @@ public interface WxMpService {
*/
public void menuDelete() throws WxErrorException;

/**
* <pre>
* 删除个性化菜单接口
* 详情请见: http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
* </pre>
* @param menuid
* @throws WxErrorException
*/
public void menuDelete(String menuid) throws WxErrorException;

/**
* <pre>
* 自定义菜单查询接口
Expand All @@ -348,6 +360,16 @@ public interface WxMpService {
* @throws WxErrorException
*/
public WxMenu menuGet() throws WxErrorException;

/**
* <pre>
* 测试个性化菜单匹配结果
* 详情请见: http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
* </pre>
* @param userid 可以是粉丝的OpenID,也可以是粉丝的微信号。
* @throws WxErrorException
*/
public WxMenu menuTryMatch(String userid) throws WxErrorException;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,24 @@ public void customMessageSend(WxMpCustomMessage message) throws WxErrorException
}

public void menuCreate(WxMenu menu) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/create";
execute(new SimplePostRequestExecutor(), url, menu.toJson());
if (menu.getMatchRule() != null) {
String url = "https://api.weixin.qq.com/cgi-bin/menu/addconditional";
execute(new SimplePostRequestExecutor(), url, menu.toJson());
} else {
String url = "https://api.weixin.qq.com/cgi-bin/menu/create";
execute(new SimplePostRequestExecutor(), url, menu.toJson());
}
}

public void menuDelete() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/delete";
execute(new SimpleGetRequestExecutor(), url, null);
}

public void menuDelete(String menuid) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional";
execute(new SimpleGetRequestExecutor(), url, "menuid=" + menuid);
}

public WxMenu menuGet() throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/get";
Expand All @@ -252,6 +262,20 @@ public WxMenu menuGet() throws WxErrorException {
throw e;
}
}

public WxMenu menuTryMatch(String userid) throws WxErrorException {
String url = "https://api.weixin.qq.com/cgi-bin/menu/trymatch";
try {
String resultContent = execute(new SimpleGetRequestExecutor(), url, "user_id=" + userid);
return WxMenu.fromJson(resultContent);
} catch (WxErrorException e) {
// 46003 不存在的菜单数据 46002 不存在的菜单版本
if (e.getError().getErrorCode() == 46003 || e.getError().getErrorCode() == 46002) {
return null;
}
throw e;
}
}

public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException {
return mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType));
Expand Down

0 comments on commit 05777c6

Please sign in to comment.