View Javadoc
1   package com.foxinmy.weixin4j.mp.model;
2   
3   import java.io.Serializable;
4   import java.util.Date;
5   import java.util.List;
6   
7   import com.alibaba.fastjson.JSONArray;
8   import com.alibaba.fastjson.annotation.JSONField;
9   import com.foxinmy.weixin4j.mp.type.FaceSize;
10  import com.foxinmy.weixin4j.mp.type.Lang;
11  import com.foxinmy.weixin4j.type.Gender;
12  import com.foxinmy.weixin4j.util.StringUtil;
13  
14  /**
15   * 用户对象
16   * <p>
17   * 当用户与公众号有交互时,可通过openid获取信息
18   * </p>
19   * 
20   * @author jinyu(foxinmy@gmail.com)
21   * @date 2014年4月8日
22   * @since JDK 1.6
23   */
24  public class User implements Serializable {
25  
26  	private static final long serialVersionUID = 1638176217299286265L;
27  
28  	/**
29  	 * 用户的唯一标识
30  	 */
31  	@JSONField(name = "openid")
32  	private String openId;
33  	/**
34  	 * 用户昵称
35  	 */
36  	@JSONField(name = "nickname")
37  	private String nickName;
38  	/**
39  	 * 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
40  	 */
41  	@JSONField(name = "sex")
42  	private int gender;
43  	/**
44  	 * 用户个人资料填写的省份
45  	 */
46  	private String province;
47  	/**
48  	 * 普通用户个人资料填写的城市
49  	 */
50  	private String city;
51  	/**
52  	 * 国家,如中国为CN
53  	 */
54  	private String country;
55  	/**
56  	 * 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空
57  	 */
58  	private String headimgurl;
59  	/**
60  	 * 用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)
61  	 */
62  	private JSONArray privilege;
63  	/**
64  	 * 用户是否订阅该公众号标识,值为0时,代表此用户没有关注该公众号,拉取不到其余信息。
65  	 */
66  	@JSONField(name = "subscribe")
67  	private boolean isSubscribe;
68  	/**
69  	 * 关注时间
70  	 */
71  	@JSONField(name = "subscribe_time")
72  	private long subscribeTime;
73  	/**
74  	 * 使用语言
75  	 */
76  	private String language;
77  	/**
78  	 * 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段
79  	 */
80  	@JSONField(name = "unionid")
81  	private String unionId;
82  	/**
83  	 * 公众号运营者对粉丝的备注,公众号运营者可在微信公众平台用户管理界面对粉丝添加备注
84  	 */
85  	private String remark;
86  	/**
87  	 * 用户所在的分组ID
88  	 */
89  	@JSONField(name = "groupid")
90  	private int groupId;
91  	/**
92  	 * 用户被打上的标签ID列表
93  	 */
94  	@JSONField(name = "tagid_list")
95  	private List<Integer> tagIds;
96  
97  	public String getOpenId() {
98  		return openId;
99  	}
100 
101 	public void setOpenId(String openId) {
102 		this.openId = openId;
103 	}
104 
105 	public String getNickName() {
106 		return nickName;
107 	}
108 
109 	public void setNickName(String nickName) {
110 		this.nickName = nickName;
111 	}
112 
113 	public int getGender() {
114 		return gender;
115 	}
116 
117 	@JSONField(serialize = false)
118 	public Gender getFormatGender() {
119 		if (gender == 1) {
120 			return Gender.male;
121 		} else if (gender == 2) {
122 			return Gender.female;
123 		} else {
124 			return Gender.unknown;
125 		}
126 	}
127 
128 	public void setGender(int gender) {
129 		this.gender = gender;
130 	}
131 
132 	public String getProvince() {
133 		return province;
134 	}
135 
136 	public void setProvince(String province) {
137 		this.province = province;
138 	}
139 
140 	public String getCity() {
141 		return city;
142 	}
143 
144 	public void setCity(String city) {
145 		this.city = city;
146 	}
147 
148 	public String getCountry() {
149 		return country;
150 	}
151 
152 	public void setCountry(String country) {
153 		this.country = country;
154 	}
155 
156 	public String getHeadimgurl() {
157 		return headimgurl;
158 	}
159 
160 	public String getHeadimgurl(FaceSize size) {
161 		if (StringUtil.isNotBlank(headimgurl)) {
162 			StringBuilder sb = new StringBuilder(headimgurl);
163 			return sb.substring(0, headimgurl.lastIndexOf('/') + 1)
164 					+ size.getInt();
165 		}
166 		return "";
167 	}
168 
169 	public void setHeadimgurl(String headimgurl) {
170 		this.headimgurl = headimgurl;
171 	}
172 
173 	public JSONArray getPrivilege() {
174 		return privilege;
175 	}
176 
177 	public void setPrivilege(JSONArray privilege) {
178 		this.privilege = privilege;
179 	}
180 
181 	public String getLanguage() {
182 		return language;
183 	}
184 
185 	@JSONField(serialize = false)
186 	public Lang getFormatLanguage() {
187 		return language != null ? Lang.valueOf(language) : null;
188 	}
189 
190 	public void setLanguage(String language) {
191 		this.language = language;
192 	}
193 
194 	public boolean isSubscribe() {
195 		return isSubscribe;
196 	}
197 
198 	public void setSubscribe(boolean isSubscribe) {
199 		this.isSubscribe = isSubscribe;
200 	}
201 
202 	public long getSubscribeTime() {
203 		return subscribeTime;
204 	}
205 
206 	@JSONField(serialize = false)
207 	public Date getFormatSubscribeTime() {
208 		return new Date(subscribeTime * 1000l);
209 	}
210 
211 	public void setSubscribeTime(long subscribeTime) {
212 		this.subscribeTime = subscribeTime;
213 	}
214 
215 	public String getUnionId() {
216 		return unionId;
217 	}
218 
219 	public void setUnionId(String unionId) {
220 		this.unionId = unionId;
221 	}
222 
223 	public String getRemark() {
224 		return remark;
225 	}
226 
227 	public void setRemark(String remark) {
228 		this.remark = remark;
229 	}
230 
231 	public int getGroupId() {
232 		return groupId;
233 	}
234 
235 	public void setGroupId(int groupId) {
236 		this.groupId = groupId;
237 	}
238 
239 	public List<Integer> getTagIds() {
240 		return tagIds;
241 	}
242 
243 	public void setTagIds(List<Integer> tagIds) {
244 		this.tagIds = tagIds;
245 	}
246 
247 	@Override
248 	public int hashCode() {
249 		return super.hashCode();
250 	}
251 
252 	@Override
253 	public boolean equals(Object obj) {
254 		if (obj instanceof User) {
255 			User another = (User) obj;
256 			if (unionId != null && another.getUnionId() != null) {
257 				return unionId.equals(another.getUnionId());
258 			}
259 			if (openId != null && another.getOpenId() != null) {
260 				return openId.equals(another.getOpenId());
261 			}
262 		}
263 		return false;
264 	}
265 
266 	@Override
267 	public String toString() {
268 		return "User [openId=" + openId + ", nickName=" + nickName
269 				+ ", gender=" + gender + ", province=" + province + ", city="
270 				+ city + ", country=" + country + ", headimgurl=" + headimgurl
271 				+ ", privilege=" + privilege + ", isSubscribe=" + isSubscribe
272 				+ ", subscribeTime=" + subscribeTime + ", language=" + language
273 				+ ", unionId=" + unionId + ", remark=" + remark + ", groupId="
274 				+ groupId + ", tagIds=" + tagIds + "]";
275 	}
276 }