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  
20  
21  
22  
23  
24  
25  
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  
37  
38  
39  
40  
41  
42  
43  
44  
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  
66  
67  
68  
69  
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  
90  
91  
92  
93  
94  
95  
96  
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 
112 
113 
114 
115 
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 
130 
131 
132 
133 
134 
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 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
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 }