@@ -3,13 +3,13 @@ package me.chanjar.weixin.common.bean; | |||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.io.InputStreamReader; | import java.io.InputStreamReader; | ||||
import java.io.Serializable; | import java.io.Serializable; | ||||
import java.nio.charset.Charset; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.List; | import java.util.List; | ||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||||
import org.apache.commons.codec.Charsets; | import org.apache.commons.codec.Charsets; | ||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder; | |||||
/** | /** | ||||
* 企业号菜单 | * 企业号菜单 | ||||
* @author Daniel Qian | * @author Daniel Qian | ||||
@@ -19,6 +19,8 @@ public class WxMenu implements Serializable { | |||||
private List<WxMenuButton> buttons = new ArrayList<WxMenuButton>(); | private List<WxMenuButton> buttons = new ArrayList<WxMenuButton>(); | ||||
private WxMenuRule matchRule; | |||||
public List<WxMenuButton> getButtons() { | public List<WxMenuButton> getButtons() { | ||||
return buttons; | return buttons; | ||||
} | } | ||||
@@ -27,6 +29,14 @@ public class WxMenu implements Serializable { | |||||
this.buttons = buttons; | this.buttons = buttons; | ||||
} | } | ||||
public WxMenuRule getMatchRule() { | |||||
return matchRule; | |||||
} | |||||
public void setMatchRule(WxMenuRule matchRule) { | |||||
this.matchRule = matchRule; | |||||
} | |||||
public String toJson() { | public String toJson() { | ||||
return WxGsonBuilder.create().toJson(this); | return WxGsonBuilder.create().toJson(this); | ||||
} | } | ||||
@@ -118,5 +128,74 @@ public class WxMenu implements Serializable { | |||||
'}'; | '}'; | ||||
} | } | ||||
} | } | ||||
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 + '\'' + | |||||
"}"; | |||||
} | |||||
} | |||||
} | } |
@@ -10,8 +10,7 @@ package me.chanjar.weixin.common.util.json; | |||||
import java.lang.reflect.Type; | 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.JsonArray; | ||||
import com.google.gson.JsonDeserializationContext; | import com.google.gson.JsonDeserializationContext; | ||||
import com.google.gson.JsonDeserializer; | import com.google.gson.JsonDeserializer; | ||||
@@ -21,6 +20,8 @@ import com.google.gson.JsonParseException; | |||||
import com.google.gson.JsonSerializationContext; | import com.google.gson.JsonSerializationContext; | ||||
import com.google.gson.JsonSerializer; | import com.google.gson.JsonSerializer; | ||||
import me.chanjar.weixin.common.bean.WxMenu; | |||||
/** | /** | ||||
* | * | ||||
* @author Daniel Qian | * @author Daniel Qian | ||||
@@ -38,6 +39,11 @@ public class WxMenuGsonAdapter implements JsonSerializer<WxMenu>, JsonDeserializ | |||||
} | } | ||||
json.add("button", buttonArray); | json.add("button", buttonArray); | ||||
if (menu.getMatchRule() != null) { | |||||
Gson gson = new Gson(); | |||||
json.add("matchrule", gson.toJsonTree(menu.getMatchRule())); | |||||
} | |||||
return json; | return json; | ||||
} | } | ||||
@@ -324,6 +324,8 @@ public interface WxMpService { | |||||
* <pre> | * <pre> | ||||
* 自定义菜单创建接口 | * 自定义菜单创建接口 | ||||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口 | * 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口 | ||||
* 如果要创建个性化菜单,请设置matchrule属性 | |||||
* 详情请见:http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html | |||||
* </pre> | * </pre> | ||||
* @param menu | * @param menu | ||||
* @throws WxErrorException | * @throws WxErrorException | ||||
@@ -339,6 +341,16 @@ public interface WxMpService { | |||||
*/ | */ | ||||
public void menuDelete() throws WxErrorException; | 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> | * <pre> | ||||
* 自定义菜单查询接口 | * 自定义菜单查询接口 | ||||
@@ -348,6 +360,16 @@ public interface WxMpService { | |||||
* @throws WxErrorException | * @throws WxErrorException | ||||
*/ | */ | ||||
public WxMenu menuGet() 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> | * <pre> | ||||
@@ -230,14 +230,24 @@ public class WxMpServiceImpl implements WxMpService { | |||||
} | } | ||||
public void menuCreate(WxMenu menu) 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 { | public void menuDelete() throws WxErrorException { | ||||
String url = "https://api.weixin.qq.com/cgi-bin/menu/delete"; | String url = "https://api.weixin.qq.com/cgi-bin/menu/delete"; | ||||
execute(new SimpleGetRequestExecutor(), url, null); | 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 { | public WxMenu menuGet() throws WxErrorException { | ||||
String url = "https://api.weixin.qq.com/cgi-bin/menu/get"; | String url = "https://api.weixin.qq.com/cgi-bin/menu/get"; | ||||
@@ -252,6 +262,20 @@ public class WxMpServiceImpl implements WxMpService { | |||||
throw e; | 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 { | public WxMediaUploadResult mediaUpload(String mediaType, String fileType, InputStream inputStream) throws WxErrorException, IOException { | ||||
return mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); | return mediaUpload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); | ||||