View Javadoc
1   package com.foxinmy.weixin4j.qy.jssdk;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import com.alibaba.fastjson.JSONObject;
7   import com.foxinmy.weixin4j.exception.WeixinException;
8   import com.foxinmy.weixin4j.model.Token;
9   import com.foxinmy.weixin4j.token.TokenManager;
10  import com.foxinmy.weixin4j.util.DateUtil;
11  import com.foxinmy.weixin4j.util.DigestUtil;
12  import com.foxinmy.weixin4j.util.MapUtil;
13  import com.foxinmy.weixin4j.util.RandomUtil;
14  
15  /**
16   * JSSDK联系人筛选配置
17   *
18   * @className JSSDKContactConfigurator
19   * @author jinyu(foxinmy@gmail.com)
20   * @date 2015年12月25日
21   * @since JDK 1.6
22   */
23  public class JSSDKContactConfigurator {
24  	private final TokenManager ticketTokenManager;
25  	private JSSDKContactParameter contactParameter;
26  
27  	/**
28  	 * ticket保存类 可调用WeixinProxy#getTicketManager获取
29  	 *
30  	 * @param ticketTokenManager
31  	 */
32  	public JSSDKContactConfigurator(TokenManager ticketTokenManager) {
33  		this.ticketTokenManager = ticketTokenManager;
34  		this.contactParameter = new JSSDKContactParameter();
35  	}
36  
37  	/**
38  	 * 可选范围:部门ID列表(如果partyIds为0则表示显示管理组下所有部门)
39  	 *
40  	 * @param departmentIds
41  	 * @return
42  	 */
43  	public JSSDKContactConfigurator partyIds(Integer... partyIds) {
44  		contactParameter.putPartyIds(partyIds);
45  		return this;
46  	}
47  
48  	/**
49  	 * 可选范围:标签ID列表(如果tagIds为0则表示显示所有标签)
50  	 *
51  	 * @param tagIds
52  	 * @return
53  	 */
54  	public JSSDKContactConfigurator tagIds(Integer... tagIds) {
55  		contactParameter.putTagIds(tagIds);
56  		return this;
57  	}
58  
59  	/**
60  	 * 可选范围:用户ID列表
61  	 *
62  	 * @param userIds
63  	 * @return
64  	 */
65  	public JSSDKContactConfigurator userIds(String... userIds) {
66  		contactParameter.putUserIds(userIds);
67  		return this;
68  	}
69  
70  	/**
71  	 * 单选模式
72  	 *
73  	 * @return
74  	 */
75  	public JSSDKContactConfigurator singleMode() {
76  		contactParameter.setMode("single");
77  		return this;
78  	}
79  
80  	/**
81  	 * 多选模式
82  	 *
83  	 * @return
84  	 */
85  	public JSSDKContactConfigurator multiMode() {
86  		contactParameter.setMode("multi");
87  		return this;
88  	}
89  
90  	/**
91  	 * 限制部门
92  	 *
93  	 * @return
94  	 */
95  	public JSSDKContactConfigurator limitDepartment() {
96  		contactParameter.putLimitType("department");
97  		return this;
98  	}
99  
100 	/**
101 	 * 限制标签
102 	 *
103 	 * @return
104 	 */
105 	public JSSDKContactConfigurator limitTag() {
106 		contactParameter.putLimitType("tag");
107 		return this;
108 	}
109 
110 	/**
111 	 * 限制用户
112 	 *
113 	 * @return
114 	 */
115 	public JSSDKContactConfigurator limitUser() {
116 		contactParameter.putLimitType("user");
117 		return this;
118 	}
119 
120 	/**
121 	 * 已选部门ID
122 	 *
123 	 * @param selectedDepartmentIds
124 	 * @return
125 	 */
126 	public JSSDKContactConfigurator selectedDepartmentIds(
127 			Integer... selectedDepartmentIds) {
128 		contactParameter.putSelectedDepartmentIds(selectedDepartmentIds);
129 		return this;
130 	}
131 
132 	/**
133 	 * 已选标签ID
134 	 *
135 	 * @param selectedTagIds
136 	 * @return
137 	 */
138 	public JSSDKContactConfigurator selectedTagIds(Integer... selectedTagIds) {
139 		contactParameter.putSelectedTagIds(selectedTagIds);
140 		return this;
141 	}
142 
143 	/**
144 	 * 已选用户ID
145 	 *
146 	 * @param selectedUserIds
147 	 * @return
148 	 */
149 	public JSSDKContactConfigurator selectedUserIds(String... selectedUserIds) {
150 		contactParameter.putSelectedUserIds(selectedUserIds);
151 		return this;
152 	}
153 
154 	/**
155 	 * 生成config配置JSON串
156 	 *
157 	 * @param url
158 	 *            当前网页的URL,不包含#及其后面部分
159 	 * @return
160 	 * @throws WeixinException
161 	 */
162 	public String toJSONConfig(String url) throws WeixinException {
163 		return toJSONConfig(url, contactParameter);
164 	}
165 
166 	/**
167 	 * 生成config配置JSON串
168 	 *
169 	 * @param url
170 	 *            当前网页的URL,不包含#及其后面部分
171 	 * @param parameter
172 	 *            自定义传入参数对象
173 	 * @return
174 	 * @throws WeixinException
175 	 */
176 	public String toJSONConfig(String url, JSSDKContactParameter parameter)
177 			throws WeixinException {
178 		Map<String, String> signMap = new HashMap<String, String>();
179 		String timestamp = DateUtil.timestamp2string();
180 		String noncestr = RandomUtil.generateString(24);
181 		Token token = this.ticketTokenManager.getCache();
182 		signMap.put("timestamp", timestamp);
183 		signMap.put("nonceStr", noncestr);
184 		signMap.put("group_ticket", token.getAccessToken());
185 		signMap.put("url", url);
186 		String sign = DigestUtil.SHA1(MapUtil
187 				.toJoinString(signMap, false, true));
188 		JSONObject config = new JSONObject();
189 		config.put("signature", sign);
190 		config.put("groupId", token.getExtra().get("group_id"));
191 		config.put("timestamp", timestamp);
192 		config.put("noncestr", noncestr);
193 		config.put("params", parameter);
194 		return config.toJSONString();
195 	}
196 }