MenuMatchRule.java

  1. package com.foxinmy.weixin4j.mp.model;

  2. import java.io.Serializable;

  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson.annotation.JSONField;
  5. import com.foxinmy.weixin4j.mp.type.ClientPlatformType;
  6. import com.foxinmy.weixin4j.mp.type.Lang;
  7. import com.foxinmy.weixin4j.type.Gender;

  8. /**
  9.  * 个性化菜单匹配规则
  10.  *
  11.  * @className MenuMatchRule
  12.  * @author jinyu(foxinmy@gmail.com)
  13.  * @date 2015年12月17日
  14.  * @since JDK 1.6
  15.  * @see
  16.  */
  17. public class MenuMatchRule implements Serializable {

  18.     private static final long serialVersionUID = 8115117407710728580L;

  19.     private JSONObject matchRule;

  20.     public MenuMatchRule() {
  21.         this.matchRule = new JSONObject();
  22.     }

  23.     /**
  24.      * 用户标签id,可通过用户表情管理接口获取
  25.      */
  26.     private Integer tagId;

  27.     @JSONField(name = "tag_id")
  28.     public MenuMatchRule group(int tagId) {
  29.         matchRule.put("tag_id", tagId);
  30.         this.tagId = tagId;
  31.         return this;
  32.     }

  33.     /**
  34.      * 性别
  35.      */
  36.     private Gender gender;

  37.     @JSONField(name = "sex")
  38.     public void gender0(int sex) {
  39.         this.gender = Gender.values().length >= sex ? Gender.values()[sex - 1]
  40.                 : null;
  41.     }

  42.     public MenuMatchRule gender(Gender gender) {
  43.         if (gender != null && gender != Gender.unknown) {
  44.             matchRule.put("sex", gender.ordinal() + 1);
  45.         }
  46.         this.gender = gender;
  47.         return this;
  48.     }

  49.     /**
  50.      * 客户端版本
  51.      */
  52.     private ClientPlatformType platformType;

  53.     /**
  54.      * 请使用 {@link #platform(ClientPlatformType platformType)}}
  55.      * @param platform
  56.      */
  57.     @JSONField(name = "client_platform_type")
  58.     public void platform0(int platform) {
  59.         this.platformType = ClientPlatformType.values().length >= platform ? ClientPlatformType
  60.                 .values()[platform - 1] : null;
  61.     }

  62.     public MenuMatchRule platform(ClientPlatformType platformType) {
  63.         if (platformType != null) {
  64.             matchRule.put("client_platform_type", platformType.ordinal() + 1);
  65.         }
  66.         this.platformType = platformType;
  67.         return this;
  68.     }

  69.     private String country;

  70.     /**
  71.      * 国家信息,是用户在微信中设置的地区
  72.      * <p>
  73.      * country、province、city组成地区信息,将按照country、province、city的顺序进行验证
  74.      * ,要符合地区信息表的内容。地区信息从大到小验证,小的可以不填,即若填写了省份信息,则国家信息也必填并且匹配,城市信息可以不填。 例如 “中国
  75.      * 广东省 广州市”、“中国 广东省”都是合法的地域信息,而“中国 广州市”则不合法,因为填写了城市信息但没有填写省份信息
  76.      *
  77.      * @param country
  78.      * @return
  79.      */
  80.     @JSONField(name = "country")
  81.     public MenuMatchRule country(String country) {
  82.         matchRule.put("country", country);
  83.         this.country = country;
  84.         return this;
  85.     }

  86.     private String province;

  87.     /**
  88.      * 省份信息,是用户在微信中设置的地区
  89.      * <p>
  90.      * country、province、city组成地区信息,将按照country、province、city的顺序进行验证,要符合地区信息表的内容。
  91.      * 地区信息从大到小验证,小的可以不填,即若填写了省份信息,则国家信息也必填并且匹配,城市信息可以不填。 例如 “中国 广东省 广州市”、“中国
  92.      * 广东省”都是合法的地域信息,而“中国 广州市”则不合法,因为填写了城市信息但没有填写省份信息
  93.      *
  94.      * @param country
  95.      * @return
  96.      */
  97.     @JSONField(name = "province")
  98.     public MenuMatchRule province(String province) {
  99.         matchRule.put("province", province);
  100.         this.province = province;
  101.         return this;
  102.     }

  103.     private String city;

  104.     /**
  105.      * 城市信息,是用户在微信中设置的地区
  106.      * <p>
  107.      * country、province、city组成地区信息,将按照country、province、city的顺序进行验证,要符合地区信息表的内容。
  108.      * 地区信息从大到小验证,小的可以不填,即若填写了省份信息,则国家信息也必填并且匹配,城市信息可以不填。 例如 “中国 广东省 广州市”、“中国
  109.      * 广东省”都是合法的地域信息,而“中国 广州市”则不合法,因为填写了城市信息但没有填写省份信息
  110.      *
  111.      * @param city
  112.      * @return
  113.      */
  114.     @JSONField(name = "city")
  115.     public MenuMatchRule city(String city) {
  116.         matchRule.put("city", city);
  117.         this.city = city;
  118.         return this;
  119.     }

  120.     /**
  121.      * 语言信息,是用户在微信中设置的语言
  122.      */
  123.     private Lang language;

  124.     /**
  125.      * 请使用 {@link #language(Lang language)}
  126.      * @param language
  127.      */
  128.     @JSONField(name = "language")
  129.     public void language0(int language) {
  130.         this.language = Lang.values().length >= language ? Lang.values()[language - 1]
  131.                 : null;
  132.     }

  133.     public MenuMatchRule language(Lang language) {
  134.         if (language != null) {
  135.             matchRule.put("language", language.ordinal() + 1);
  136.         }
  137.         this.language = language;
  138.         return this;
  139.     }

  140.     public ClientPlatformType getPlatformType() {
  141.         return platformType;
  142.     }

  143.     public Integer getTagId() {
  144.         return tagId;
  145.     }

  146.     public Gender getGender() {
  147.         return gender;
  148.     }

  149.     public String getCountry() {
  150.         return country;
  151.     }

  152.     public String getProvince() {
  153.         return province;
  154.     }

  155.     public String getCity() {
  156.         return city;
  157.     }

  158.     public Lang getLanguage() {
  159.         return language;
  160.     }

  161.     public boolean hasRule() {
  162.         return !matchRule.isEmpty();
  163.     }

  164.     public JSONObject getRule() {
  165.         return this.matchRule;
  166.     }

  167.     @Override
  168.     public String toString() {
  169.         return "MenuMatchRule [tagId=" + tagId + ", gender=" + gender
  170.                 + ", platformType=" + platformType + ", country=" + country
  171.                 + ", province=" + province + ", city=" + city + ", language="
  172.                 + language + "]";
  173.     }
  174. }