View Javadoc
1   package com.foxinmy.weixin4j.mp.api;
2   
3   import java.util.List;
4   
5   import com.alibaba.fastjson.JSON;
6   import com.alibaba.fastjson.JSONObject;
7   import com.alibaba.fastjson.serializer.NameFilter;
8   import com.foxinmy.weixin4j.exception.WeixinException;
9   import com.foxinmy.weixin4j.http.weixin.ApiResult;
10  import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
11  import com.foxinmy.weixin4j.model.Token;
12  import com.foxinmy.weixin4j.mp.message.TemplateMessage;
13  import com.foxinmy.weixin4j.mp.model.TemplateMessageInfo;
14  import com.foxinmy.weixin4j.mp.type.IndustryType;
15  import com.foxinmy.weixin4j.token.TokenManager;
16  import com.foxinmy.weixin4j.util.NameValue;
17  
18  /**
19   * 模板消息相关API
20   * 
21   * @className TemplApi
22   * @author jinyu(foxinmy@gmail.com)
23   * @date 2014年9月30日
24   * @since JDK 1.6
25   * @see
26   */
27  public class TmplApi extends MpApi {
28  
29  	private final TokenManager tokenManager;
30  
31  	public TmplApi(TokenManager tokenManager) {
32  		this.tokenManager = tokenManager;
33  	}
34  
35  	/**
36  	 * 设置所属行业(每月可修改行业1次,账号仅可使用所属行业中相关的模板)
37  	 * 
38  	 * @param industryTypes
39  	 *            所处行业 目前不超过两个
40  	 * @return 操作结果
41  	 * @throws WeixinException
42  	 * @see com.foxinmy.weixin4j.mp.type.IndustryType
43  	 * @see <a
44  	 *      href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">设置所处行业</a>
45  	 */
46  	public ApiResult setTmplIndustry(IndustryType... industryTypes)
47  			throws WeixinException {
48  		JSONObject obj = new JSONObject();
49  		for (int i = 0; i < industryTypes.length; i++) {
50  			obj.put(String.format("industry_id%d", i + 1),
51  					Integer.toString(industryTypes[i].getTypeId()));
52  		}
53  		Token token = tokenManager.getCache();
54  		String template_set_industry_uri = getRequestUri("template_set_industry_uri");
55  		WeixinResponse response = weixinExecutor.post(String.format(
56  				template_set_industry_uri, token.getAccessToken()), obj
57  				.toJSONString());
58  
59  		return response.getAsResult();
60  	}
61  
62  	/**
63  	 * 获取设置的行业信息
64  	 * 
65  	 * @return 行业信息数组 第一个元素为帐号设置的主营行业 第二个元素为帐号设置的副营行业
66  	 * @throws WeixinException
67  	 * @see com.foxinmy.weixin4j.mp.type.IndustryType
68  	 * @see <a
69  	 *      href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">获取设置的行业信息</a>
70  	 */
71  	public IndustryType[] getTmplIndustry() throws WeixinException {
72  		String template_get_industry_uri = getRequestUri("template_get_industry_uri");
73  		WeixinResponse response = weixinExecutor.get(String.format(
74  				template_get_industry_uri, tokenManager.getAccessToken()));
75  		JSONObject primary = response.getAsJson().getJSONObject(
76  				"primary_industry");
77  		JSONObject secondary = response.getAsJson().getJSONObject(
78  				"secondary_industry");
79  		return new IndustryType[] {
80  				primary != null ? IndustryType.getIndustry(
81  						primary.getString("first_class"),
82  						primary.getString("second_class")) : null,
83  				secondary != null ? IndustryType.getIndustry(
84  						secondary.getString("first_class"),
85  						secondary.getString("second_class")) : null };
86  	}
87  
88  	/**
89  	 * 获取模板ID
90  	 * 
91  	 * @param shortId
92  	 *            模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
93  	 * @return 模板ID
94  	 * @throws WeixinException
95  	 * @see <a
96  	 *      href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">获得模板ID</a>
97  	 */
98  	public String getTemplateId(String shortId) throws WeixinException {
99  		Token token = tokenManager.getCache();
100 		String template_getid_uri = getRequestUri("template_getid_uri");
101 		WeixinResponse response = weixinExecutor.post(
102 				String.format(template_getid_uri, token.getAccessToken()),
103 				String.format("{\"template_id_short\":\"%s\"}", shortId));
104 
105 		return response.getAsJson().getString("template_id");
106 	}
107 
108 	/**
109 	 * 获取模板列表
110 	 * 
111 	 * @return 模板列表
112 	 * @see com.foxinmy.weixin4j.mp.model.TemplateMessageInfo
113 	 * @see <a
114 	 *      href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">获取模板列表</a>
115 	 * @throws WeixinException
116 	 */
117 	public List<TemplateMessageInfo> getAllTemplates() throws WeixinException {
118 		Token token = tokenManager.getCache();
119 		String template_getall_uri = getRequestUri("template_getall_uri");
120 		WeixinResponse response = weixinExecutor.get(String.format(
121 				template_getall_uri, token.getAccessToken()));
122 		return JSON.parseArray(response.getAsJson().getString("template_list"),
123 				TemplateMessageInfo.class);
124 	}
125 
126 	/**
127 	 * 删除模板
128 	 * 
129 	 * @param templateId
130 	 *            公众帐号下模板消息ID
131 	 * @return 处理结果
132 	 * @see <a
133 	 *      href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">删除模板</a>
134 	 * @throws WeixinException
135 	 */
136 	public ApiResult deleteTemplate(String templateId) throws WeixinException {
137 		Token token = tokenManager.getCache();
138 		String template_del_uri = getRequestUri("template_del_uri");
139 		WeixinResponse response = weixinExecutor.post(
140 				String.format(template_del_uri, token.getAccessToken()),
141 				String.format("{\"template_id\":\"%s\"}", templateId));
142 		return response.getAsResult();
143 	}
144 
145 	/**
146 	 * 发送模板消息
147 	 * 
148 	 * @param tplMessage
149 	 *            消息对象
150 	 * @return 发送的消息ID
151 	 * @throws WeixinException
152 	 * @see <a
153 	 *      href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277&token=&lang=zh_CN">模板消息</a>
154 	 * @see <a href=
155 	 *      "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751288&token=&lang=zh_CN"
156 	 *      >运营规范</a>
157 	 * @see com.foxinmy.weixin4j.mp.message.TemplateMessage
158 	 * @see com.foxinmy.weixin4j.msg.event.TemplatesendjobfinishMessage
159 	 */
160 	public String sendTmplMessage(TemplateMessage tplMessage)
161 			throws WeixinException {
162 		Token token = tokenManager.getCache();
163 		String template_send_uri = getRequestUri("template_send_uri");
164 		WeixinResponse response = weixinExecutor.post(
165 				String.format(template_send_uri, token.getAccessToken()),
166 				JSON.toJSONString(tplMessage, new NameFilter() {
167 					@Override
168 					public String process(Object object, String name,
169 							Object value) {
170 						if (object instanceof NameValue && name.equals("name")) {
171 							return "color";
172 						}
173 						return name;
174 					}
175 				}));
176 
177 		return response.getAsJson().getString("msgid");
178 	}
179 }