menus) {
35 | return create(weixin.getAccessToken().getToken(), menus);
36 | }
37 |
38 | /**
39 | * 自定义菜单创建接口
40 | *
41 | * 注意:只有菜单类型为 MenuType.VIEW 时才需要url属性,其它情况都是使用key
42 | *
43 | *
44 | * @param accessToken 调用接口凭证
45 | * @param menus 菜单
46 | */
47 | public Result create(String accessToken, List menus) {
48 | JSONArray menuArray = new JSONArray();
49 | for (com.belerweb.social.weixin.bean.Menu menu : menus) {
50 | JSONObject obj = new JSONObject();
51 | MenuType type = menu.getType();
52 | obj.put("name", menu.getName());
53 | if (type != null) {
54 | obj.put("type", type.value());
55 | if (type == MenuType.VIEW) {
56 | obj.put("url", menu.getUrl());
57 | } else {
58 | obj.put("key", menu.getKey());
59 | }
60 | }
61 | List subs = menu.getSubs();
62 | if (subs != null) {
63 | JSONArray _menuArray = new JSONArray();
64 | for (com.belerweb.social.weixin.bean.Menu _menu : subs) {
65 | JSONObject _obj = new JSONObject();
66 | MenuType _type = _menu.getType();
67 | _obj.put("name", _menu.getName());
68 | if (_type != null) {
69 | _obj.put("type", _type.value());
70 | if (_type == MenuType.VIEW) {
71 | _obj.put("url", _menu.getUrl());
72 | } else {
73 | _obj.put("key", _menu.getKey());
74 | }
75 | }
76 | _menuArray.put(_obj);
77 | }
78 | obj.put("sub_button", _menuArray);
79 | }
80 | menuArray.put(obj);
81 | }
82 | JSONObject request = new JSONObject();
83 | request.put("button", menuArray);
84 | try {
85 | String json =
86 | weixin.post("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken,
87 | new StringEntity(request.toString(), "UTF-8"));
88 | return Result.parse(json, Error.class);
89 | } catch (UnsupportedEncodingException e) {
90 | throw new SocialException(e);
91 | }
92 | }
93 |
94 | /**
95 | * 自定义菜单查询接口
96 | */
97 | public Result> get() {
98 | return get(weixin.getAccessToken().getToken());
99 | }
100 |
101 | /**
102 | * 自定义菜单查询接口
103 | *
104 | * @param accessToken 调用接口凭证
105 | */
106 | public Result> get(String accessToken) {
107 | List params = new ArrayList();
108 | weixin.addParameter(params, "access_token", accessToken);
109 | String json = weixin.get("https://api.weixin.qq.com/cgi-bin/menu/get", params);
110 | JSONObject jsonObject = new JSONObject(json);
111 | Error error = Error.parse(jsonObject);
112 | if (error != null) {
113 | return new Result>(error);
114 | }
115 | List menus =
116 | new ArrayList();
117 | JSONObject menu = jsonObject.optJSONObject("menu");
118 | if (menu != null) {
119 | menus = Result.parse(menu.optJSONArray("button"), com.belerweb.social.weixin.bean.Menu.class);
120 | }
121 | return new Result>(menus);
122 | }
123 |
124 | /**
125 | * 自定义菜单删除接口
126 | */
127 | public Result delete() {
128 | return delete(weixin.getAccessToken().getToken());
129 | }
130 |
131 | /**
132 | * 自定义菜单删除接口
133 | *
134 | * @param accessToken 调用接口凭证
135 | */
136 | public Result delete(String accessToken) {
137 | List params = new ArrayList();
138 | weixin.addParameter(params, "access_token", accessToken);
139 | String json = weixin.get("https://api.weixin.qq.com/cgi-bin/menu/delete", params);
140 | return Result.parse(json, Error.class);
141 | }
142 |
143 | }
144 |
--------------------------------------------------------------------------------
/src/main/java/com/belerweb/social/weixin/api/User.java:
--------------------------------------------------------------------------------
1 | package com.belerweb.social.weixin.api;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.apache.commons.lang.StringUtils;
7 | import org.apache.http.NameValuePair;
8 |
9 | import com.belerweb.social.API;
10 | import com.belerweb.social.bean.Result;
11 | import com.belerweb.social.weixin.bean.GetFollowersResult;
12 |
13 | /**
14 | * 网页授权获取用户基本信息
15 | *
16 | * 如果用户在微信中(Web微信除外)访问公众号的第三方网页,公众号开发者可以通过此接口获取当前用户基本信息(包括昵称、性别、城市、国家)。利用用户信息,可以实现体验优化、用户来源统计、帐号绑定、
17 | * 用户身份鉴权等功能
18 | * 。请注意,“获取用户基本信息接口是在用户和公众号产生消息交互时,才能根据用户OpenID获取用户基本信息,而网页授权的方式获取用户基本信息,则无需消息交互,只是用户进入到公众号的网页
19 | * ,就可弹出请求用户授权的界面,用户授权后,就可获得其基本信息(此过程甚至不需要用户已经关注公众号。)”
20 | *
21 | * 本接口是通过OAuth2.0来完成网页授权的,是安全可靠的,关于OAuth2.0的详细介绍,可以参考OAuth2.0协议标准。在微信公众号请求用户网页授权之前,
22 | * 开发者需要先到公众平台网站中配置授权回调域名。
23 | */
24 | public class User extends API {
25 |
26 | protected User(Weixin weixin) {
27 | super(weixin);
28 | }
29 |
30 | /**
31 | * 拉取用户信息(需scope为 snsapi_userinfo)。适用于网页授权的用户。
32 | *
33 | * 如果网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息了。
34 | *
35 | * @param accessToken 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
36 | * @param openId 用户的唯一标识
37 | */
38 | public Result snsapiUserInfo(String accessToken,
39 | String openId) {
40 | List params = new ArrayList();
41 | weixin.addParameter(params, "access_token", accessToken);
42 | weixin.addParameter(params, "openid", openId);
43 | String json = weixin.get("https://api.weixin.qq.com/sns/userinfo", params);
44 | return Result.parse(json, com.belerweb.social.weixin.bean.User.class);
45 | }
46 |
47 | /**
48 | * 获取用户基本信息,适用于已关注公众帐号的用户。
49 | *
50 | * 在关注者与公众号产生消息交互后,公众号可获得关注者的OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的。对于不同公众号,同一用户的openid不同)。
51 | * 公众号可通过本接口来根据OpenID获取用户基本信息,包括昵称、头像、性别、所在城市、语言和关注时间。
52 | *
53 | * @param accessToken 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
54 | * @param openId 用户的唯一标识
55 | */
56 | public Result userInfo(String accessToken, String openId) {
57 | List params = new ArrayList();
58 | weixin.addParameter(params, "access_token", accessToken);
59 | weixin.addParameter(params, "openid", openId);
60 | String json = weixin.get("https://api.weixin.qq.com/cgi-bin/user/info", params);
61 | return Result.parse(json, com.belerweb.social.weixin.bean.User.class);
62 | }
63 |
64 | /**
65 | * 获取所欲关注者列表,包含用户详细信息,该接口采用循环多次获取用户详细信息的方式。如果关注者太多可能会很慢。还会超过微信API调用次数限制。请谨慎调用。建议只在第一次同步关注着信息时调用。
66 | */
67 | public Result> getFollowUsers() {
68 | return getFollowUsers(weixin.getAccessToken().getToken());
69 | }
70 |
71 | /**
72 | * 获取所欲关注者列表,包含用户详细信息,该接口采用循环多次获取用户详细信息的方式。如果关注者太多可能会很慢。还会超过微信API调用次数限制。请谨慎调用。建议只在第一次同步关注着信息时调用。
73 | *
74 | * @param accessToken 调用接口凭证
75 | */
76 | public Result> getFollowUsers(String accessToken) {
77 | List users =
78 | new ArrayList();
79 | Result followersResult = getFollowers(accessToken);
80 | if (followersResult.success()) {
81 | for (String openId : followersResult.getResult().getOpenIds()) {
82 | Result userResult = userInfo(accessToken, openId);
83 | if (userResult.success()) {
84 | users.add(userResult.getResult());
85 | } else {
86 | return new Result>(userResult.getError());
87 | }
88 | }
89 |
90 | return new Result>(users);
91 | }
92 | return new Result>(followersResult.getError());
93 | }
94 |
95 | /**
96 | * 获取所欲关注者列表
97 | */
98 | public Result getFollowers() {
99 | return getFollowers(weixin.getAccessToken().getToken());
100 | }
101 |
102 | /**
103 | * 获取所欲关注者列表
104 | *
105 | * @param accessToken 调用接口凭证
106 | */
107 | public Result getFollowers(String accessToken) {
108 | GetFollowersResult result = new GetFollowersResult();
109 | List openIds = new ArrayList();
110 | Result followers = getFollowers(accessToken, null);
111 | while (followers.success()) {
112 | for (String openId : followers.getResult().getOpenIds()) {
113 | openIds.add(openId);
114 | }
115 | String nextOpenid = followers.getResult().getNextOpenid();
116 | if (StringUtils.isBlank(nextOpenid) || followers.getResult().getTotal() == openIds.size()) {
117 | break;
118 | }
119 | followers = getFollowers(accessToken, nextOpenid);
120 | }
121 | if (!followers.success()) {
122 | return new Result(followers.getError());
123 | }
124 | result.setTotal(openIds.size());
125 | result.setCount(openIds.size());
126 | result.setOpenIds(openIds);
127 | return new Result(result);
128 | }
129 |
130 | /**
131 | * 获取关注者列表
132 | *
133 | * 公众号可通过本接口来获取帐号的关注者列表,关注者列表由一串OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的)组成。一次拉取调用最多拉取10000个关注者的OpenID
134 | * ,可以通过多次拉取的方式来满足需求。
135 | *
136 | * 文档地址:http://mp.weixin.qq.com/wiki/index.php?title=获取关注者列表
137 | *
138 | * @param accessToken 调用接口凭证
139 | * @param openId 第一个拉取的OPENID,不填默认从头开始拉取
140 | */
141 | public Result getFollowers(String accessToken, String openId) {
142 | List params = new ArrayList();
143 | weixin.addParameter(params, "access_token", accessToken);
144 | weixin.addNotNullParameter(params, "next_openid", openId);
145 | String json = weixin.get("https://api.weixin.qq.com/cgi-bin/user/get", params);
146 | return Result.parse(json, GetFollowersResult.class);
147 | }
148 |
149 | }
150 |
--------------------------------------------------------------------------------
/src/main/java/com/belerweb/social/weixin/bean/AccessToken.java:
--------------------------------------------------------------------------------
1 | package com.belerweb.social.weixin.bean;
2 |
3 | import org.json.JSONObject;
4 |
5 | import com.belerweb.social.bean.JsonBean;
6 | import com.belerweb.social.bean.Result;
7 |
8 | /**
9 | * 网页授权接口调用凭证
10 | */
11 | public class AccessToken extends JsonBean {
12 |
13 | public AccessToken() {}
14 |
15 | private AccessToken(JSONObject jsonObject) {
16 | super(jsonObject);
17 | }
18 |
19 | private String token;// 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
20 | private Long expiresIn;// access_token接口调用凭证超时时间,单位(秒)
21 | private String refreshToken;// 用户刷新access_token
22 | private String openId;// 用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
23 | private Scope scope;// 用户授权的作用域,使用逗号(,)分隔
24 | private String unionid;// 当且仅当该网站应用已获得该用户的userinfo授权时,才会出现该字段
25 |
26 | /**
27 | * 网页授权接口调用凭证,注意:此access_token与基础支持的access_token不同
28 | */
29 | public String getToken() {
30 | return token;
31 | }
32 |
33 | public void setToken(String token) {
34 | this.token = token;
35 | }
36 |
37 | /**
38 | * access_token接口调用凭证超时时间,单位(秒)
39 | */
40 | public Long getExpiresIn() {
41 | return expiresIn;
42 | }
43 |
44 | public void setExpiresIn(Long expiresIn) {
45 | this.expiresIn = expiresIn;
46 | }
47 |
48 | /**
49 | * 用户刷新access_token
50 | */
51 | public String getRefreshToken() {
52 | return refreshToken;
53 | }
54 |
55 | public void setRefreshToken(String refreshToken) {
56 | this.refreshToken = refreshToken;
57 | }
58 |
59 | /**
60 | * 用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
61 | */
62 | public String getOpenId() {
63 | return openId;
64 | }
65 |
66 | public void setOpenId(String openId) {
67 | this.openId = openId;
68 | }
69 |
70 | /**
71 | * 用户授权的作用域,使用逗号(,)分隔
72 | */
73 | public Scope getScope() {
74 | return scope;
75 | }
76 |
77 | public void setScope(Scope scope) {
78 | this.scope = scope;
79 | }
80 |
81 | public String getUnionid() {
82 | return unionid;
83 | }
84 |
85 | public void setUnionid(String unionid) {
86 | this.unionid = unionid;
87 | }
88 |
89 | public static AccessToken parse(JSONObject jsonObject) {
90 | if (jsonObject == null) {
91 | return null;
92 | }
93 | AccessToken obj = new AccessToken(jsonObject);
94 | obj.token = jsonObject.getString("access_token");
95 | obj.openId = Result.toString(jsonObject.opt("openid"));
96 | obj.expiresIn = Result.parseLong(jsonObject.opt("expires_in"));
97 | obj.refreshToken = Result.toString(jsonObject.opt("refresh_token"));
98 | obj.scope = Scope.parse(jsonObject.opt("scope"));
99 | obj.unionid = Result.toString(jsonObject.opt("unionid"));
100 | return obj;
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/src/main/java/com/belerweb/social/weixin/bean/ApiTicket.java:
--------------------------------------------------------------------------------
1 | package com.belerweb.social.weixin.bean;
2 |
3 | import org.json.JSONObject;
4 |
5 | import com.belerweb.social.bean.JsonBean;
6 | import com.belerweb.social.bean.Result;
7 |
8 | /**
9 | * api_ticket 是用于调用微信卡券JS API的临时票据,有效期为7200 秒,通过access_token 来获取。
10 | */
11 | public class ApiTicket extends JsonBean {
12 |
13 | public ApiTicket() {}
14 |
15 | private ApiTicket(JSONObject jsonObject) {
16 | super(jsonObject);
17 | }
18 |
19 | private String ticket;// ticket是公众号用于调用微信卡券JS API的临时票据
20 | private Long expiresIn;// api_ticket接口调用凭证超时时间,单位(秒)
21 |
22 | public String getTicket() {
23 | return ticket;
24 | }
25 |
26 | public void setTicket(String ticket) {
27 | this.ticket = ticket;
28 | }
29 |
30 | public Long getExpiresIn() {
31 | return expiresIn;
32 | }
33 |
34 | public void setExpiresIn(Long expiresIn) {
35 | this.expiresIn = expiresIn;
36 | }
37 |
38 | public static ApiTicket parse(JSONObject jsonObject) {
39 | if (jsonObject == null) {
40 | return null;
41 | }
42 | ApiTicket obj = new ApiTicket(jsonObject);
43 | obj.ticket = jsonObject.getString("ticket");
44 | obj.expiresIn = Result.parseLong(jsonObject.opt("expires_in"));
45 | return obj;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/com/belerweb/social/weixin/bean/Article.java:
--------------------------------------------------------------------------------
1 | package com.belerweb.social.weixin.bean;
2 |
3 | /**
4 | * 图文
5 | */
6 | public class Article {
7 |
8 | private String title;// 图文消息标题
9 | private String description;// 图文消息描述
10 | private String picUrl;// 图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200
11 | private String url;// 点击图文消息跳转链接
12 |
13 | /**
14 | * 图文消息标题
15 | */
16 | public String getTitle() {
17 | return title;
18 | }
19 |
20 | public void setTitle(String title) {
21 | this.title = title;
22 | }
23 |
24 | /**
25 | * 图文消息描述
26 | */
27 | public String getDescription() {
28 | return description;
29 | }
30 |
31 | public void setDescription(String description) {
32 | this.description = description;
33 | }
34 |
35 | /**
36 | * 图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200
37 | */
38 | public String getPicUrl() {
39 | return picUrl;
40 | }
41 |
42 | public void setPicUrl(String picUrl) {
43 | this.picUrl = picUrl;
44 | }
45 |
46 | /**
47 | * 点击图文消息跳转链接
48 | */
49 | public String getUrl() {
50 | return url;
51 | }
52 |
53 | public void setUrl(String url) {
54 | this.url = url;
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/com/belerweb/social/weixin/bean/EventType.java:
--------------------------------------------------------------------------------
1 | package com.belerweb.social.weixin.bean;
2 |
3 |
4 | /**
5 | * 事件类型
6 | */
7 | public enum EventType {
8 |
9 | /**
10 | * 订阅
11 | */
12 | SUBSCRIBE("subscribe"),
13 |
14 | /**
15 | * 取消订阅
16 | */
17 | UNSUBSCRIBE("unsubscribe"),
18 |
19 | /**
20 | * 扫描二维码:用户已关注时的事件推送
21 | */
22 | SCAN("SCAN"),
23 |
24 | /**
25 | * 上报地理位置事件
26 | */
27 | LOCATION("LOCATION"),
28 |
29 | /**
30 | * 点击链接事件
31 | */
32 | VIEW("VIEW"),
33 |
34 | /**
35 | * 自定义菜单事件
36 | */
37 | CLICK("CLICK");
38 |
39 | private String type;
40 |
41 | private EventType(String type) {
42 | this.type = type;
43 | }
44 |
45 | public String value() {
46 | return type;
47 | }
48 |
49 | @Override
50 | public String toString() {
51 | return type;
52 | }
53 |
54 | public static EventType parse(Object val) {
55 | if (SUBSCRIBE.type.equals(val)) {
56 | return SUBSCRIBE;
57 | }
58 | if (UNSUBSCRIBE.type.equals(val)) {
59 | return UNSUBSCRIBE;
60 | }
61 | if (SCAN.type.equals(val)) {
62 | return SCAN;
63 | }
64 | if (LOCATION.type.equals(val)) {
65 | return LOCATION;
66 | }
67 | if (CLICK.type.equals(val)) {
68 | return CLICK;
69 | }
70 | if (VIEW.type.equals(val)) {
71 | return VIEW;
72 | }
73 | return null;
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/src/main/java/com/belerweb/social/weixin/bean/GetFollowersResult.java:
--------------------------------------------------------------------------------
1 | package com.belerweb.social.weixin.bean;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import org.json.JSONArray;
7 | import org.json.JSONObject;
8 |
9 | import com.belerweb.social.bean.JsonBean;
10 | import com.belerweb.social.bean.Result;
11 |
12 | /**
13 | * 获取关注者列表结果
14 | */
15 | public class GetFollowersResult extends JsonBean {
16 |
17 | public GetFollowersResult() {}
18 |
19 | private GetFollowersResult(JSONObject jsonObject) {
20 | super(jsonObject);
21 | }
22 |
23 | private Integer total;// 关注该公众账号的总用户数
24 | private Integer count;// 拉取的OPENID个数,最大值为10000
25 | private List openIds;// OPENID的列表
26 | private String nextOpenid;// 拉取列表的后一个用户的OPENID
27 |
28 | /**
29 | * 关注该公众账号的总用户数
30 | */
31 | public Integer getTotal() {
32 | return total;
33 | }
34 |
35 | public void setTotal(Integer total) {
36 | this.total = total;
37 | }
38 |
39 | /**
40 | * 拉取的OPENID个数,最大值为10000
41 | */
42 | public Integer getCount() {
43 | return count;
44 | }
45 |
46 | public void setCount(Integer count) {
47 | this.count = count;
48 | }
49 |
50 | /**
51 | * OPENID的列表
52 | */
53 | public List getOpenIds() {
54 | return openIds;
55 | }
56 |
57 | public void setOpenIds(List openIds) {
58 | this.openIds = openIds;
59 | }
60 |
61 | /**
62 | * 拉取列表的后一个用户的OPENID
63 | */
64 | public String getNextOpenid() {
65 | return nextOpenid;
66 | }
67 |
68 | public void setNextOpenid(String nextOpenid) {
69 | this.nextOpenid = nextOpenid;
70 | }
71 |
72 | public static GetFollowersResult parse(JSONObject jsonObject) {
73 | if (jsonObject == null) {
74 | return null;
75 | }
76 | GetFollowersResult obj = new GetFollowersResult(jsonObject);
77 | obj.total = Result.parseInteger(jsonObject.get("total"));
78 | obj.count = Result.parseInteger(jsonObject.get("count"));
79 | obj.nextOpenid = Result.toString(jsonObject.opt("next_openid"));
80 | JSONArray openIdArray = jsonObject.getJSONObject("data").getJSONArray("openid");
81 | List openIds = new ArrayList();
82 | for (int i = 0; i < openIdArray.length(); i++) {
83 | openIds.add(openIdArray.getString(i));
84 | }
85 | obj.openIds = openIds;
86 | return obj;
87 | }
88 |
89 | }
90 |
--------------------------------------------------------------------------------
/src/main/java/com/belerweb/social/weixin/bean/Group.java:
--------------------------------------------------------------------------------
1 | package com.belerweb.social.weixin.bean;
2 |
3 | import org.json.JSONObject;
4 |
5 | import com.belerweb.social.bean.JsonBean;
6 | import com.belerweb.social.bean.Result;
7 |
8 | /**
9 | * 分组
10 | */
11 | public class Group extends JsonBean {
12 |
13 | public Group() {}
14 |
15 | private Group(JSONObject jsonObject) {
16 | super(jsonObject);
17 | }
18 |
19 | private String id;// 分组id,由微信分配
20 | private String name;// 分组名字,UTF8编码
21 | private Integer count;// 分组内用户数量
22 |
23 | /**
24 | * 分组id,由微信分配
25 | */
26 | public String getId() {
27 | return id;
28 | }
29 |
30 | public void setId(String id) {
31 | this.id = id;
32 | }
33 |
34 | /**
35 | * 分组名字,UTF8编码
36 | */
37 | public String getName() {
38 | return name;
39 | }
40 |
41 | public void setName(String name) {
42 | this.name = name;
43 | }
44 |
45 | /**
46 | * 分组内用户数量
47 | */
48 | public Integer getCount() {
49 | return count;
50 | }
51 |
52 | public void setCount(Integer count) {
53 | this.count = count;
54 | }
55 |
56 | public static Group parse(JSONObject jsonObject) {
57 | if (jsonObject == null) {
58 | return null;
59 | }
60 | Group obj = new Group(jsonObject);
61 | obj.id = Result.toString(jsonObject.get("id"));
62 | obj.name = Result.toString(jsonObject.get("name"));
63 | obj.count = Result.parseInteger(jsonObject.opt("count"));
64 | return obj;
65 | }
66 |
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/src/main/java/com/belerweb/social/weixin/bean/JSApiTicket.java:
--------------------------------------------------------------------------------
1 | package com.belerweb.social.weixin.bean;
2 |
3 | import org.json.JSONObject;
4 |
5 | import com.belerweb.social.bean.JsonBean;
6 | import com.belerweb.social.bean.Result;
7 |
8 | /**
9 | * jsapi_ticket是公众号用于调用微信JS接口的临时票据。正常情况下,jsapi_ticket的有效期为7200秒,通过access_token来获取。
10 | */
11 | public class JSApiTicket extends JsonBean {
12 |
13 | public JSApiTicket() {}
14 |
15 | private JSApiTicket(JSONObject jsonObject) {
16 | super(jsonObject);
17 | }
18 |
19 | private String ticket;// ticket是公众号用于调用微信JS接口的临时票据
20 | private Long expiresIn;// jsapi_ticket接口调用凭证超时时间,单位(秒)
21 |
22 | public String getTicket() {
23 | return ticket;
24 | }
25 |
26 | public void setTicket(String ticket) {
27 | this.ticket = ticket;
28 | }
29 |
30 | public Long getExpiresIn() {
31 | return expiresIn;
32 | }
33 |
34 | public void setExpiresIn(Long expiresIn) {
35 | this.expiresIn = expiresIn;
36 | }
37 |
38 | public static JSApiTicket parse(JSONObject jsonObject) {
39 | if (jsonObject == null) {
40 | return null;
41 | }
42 | JSApiTicket obj = new JSApiTicket(jsonObject);
43 | obj.ticket = jsonObject.getString("ticket");
44 | obj.expiresIn = Result.parseLong(jsonObject.opt("expires_in"));
45 | return obj;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/com/belerweb/social/weixin/bean/Media.java:
--------------------------------------------------------------------------------
1 | package com.belerweb.social.weixin.bean;
2 |
3 |
4 | /**
5 | * 媒体文件
6 | */
7 | public class Media {
8 |
9 | private String id;
10 | private String name;
11 | private String contentType;
12 | private byte[] content;
13 |
14 | public String getId() {
15 | return id;
16 | }
17 |
18 | public void setId(String id) {
19 | this.id = id;
20 | }
21 |
22 | public String getName() {
23 | return name;
24 | }
25 |
26 | public void setName(String name) {
27 | this.name = name;
28 | }
29 |
30 | public String getContentType() {
31 | return contentType;
32 | }
33 |
34 | public void setContentType(String contentType) {
35 | this.contentType = contentType;
36 | }
37 |
38 | public byte[] getContent() {
39 | return content;
40 | }
41 |
42 | public void setContent(byte[] content) {
43 | this.content = content;
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/com/belerweb/social/weixin/bean/MediaType.java:
--------------------------------------------------------------------------------
1 | package com.belerweb.social.weixin.bean;
2 |
3 |
4 | /**
5 | * 媒体文件类型
6 | */
7 | public enum MediaType {
8 |
9 | /**
10 | * 图片
11 | */
12 | IMAGE("image"),
13 |
14 | /**
15 | * 语音
16 | */
17 | VOICE("voice"),
18 |
19 | /**
20 | * 语音
21 | */
22 | VOICE_AMR("voice"),
23 |
24 | /**
25 | * 语音
26 | */
27 | VOICE_MP3("voice"),
28 |
29 | /**
30 | * 视频
31 | */
32 | VIDEO("video"),
33 |
34 | /**
35 | * 缩略图
36 | */
37 | THUMB("thumb");
38 |
39 | private String type;
40 |
41 | private MediaType(String type) {
42 | this.type = type;
43 | }
44 |
45 | public String value() {
46 | return type;
47 | }
48 |
49 | @Override
50 | public String toString() {
51 | return type;
52 | }
53 |
54 | public String contentType() {
55 | if (this == IMAGE || this == THUMB) {
56 | return "image/jpeg";
57 | }
58 | if (this == VOICE || this == VOICE_AMR) {
59 | return "audio/amr";
60 | }
61 | if (this == VOICE_MP3) {
62 | return "audio/mp3";
63 | }
64 | if (this == VIDEO) {
65 | return "audio/mp4";
66 | }
67 | return null;
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/src/main/java/com/belerweb/social/weixin/bean/Menu.java:
--------------------------------------------------------------------------------
1 | package com.belerweb.social.weixin.bean;
2 |
3 | import java.util.List;
4 |
5 | import org.json.JSONObject;
6 |
7 | import com.belerweb.social.bean.JsonBean;
8 | import com.belerweb.social.bean.Result;
9 |
10 |
11 | /**
12 | * 自定义菜单
13 | */
14 | public class Menu extends JsonBean {
15 |
16 | public Menu() {}
17 |
18 | private Menu(JSONObject jsonObject) {
19 | super(jsonObject);
20 | }
21 |
22 | private MenuType type;
23 | private String key;
24 | private String url;
25 | private String name;
26 | private List