-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathSmsManager.java
More file actions
386 lines (359 loc) · 15.7 KB
/
SmsManager.java
File metadata and controls
386 lines (359 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package com.qiniu.sms;
import com.qiniu.common.Constants;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.sms.model.MessageInfo;
import com.qiniu.sms.model.SignatureInfo;
import com.qiniu.sms.model.TemplateInfo;
import com.qiniu.util.Auth;
import com.qiniu.util.Json;
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlUtils;
import java.util.Map;
public class SmsManager {
/**
* Auth 对象
* 该类需要使用鉴权,所以需要指定Auth对象
*/
private final Auth auth;
/**
* HTTP Client 对象
* 该类需要通过该对象来发送HTTP请求
*/
private final Client client;
/**
* Configuration 对象
* 该类相关的域名配置,解析配置,HTTP请求超时时间设置等
*/
private Configuration configuration;
private SmsRequestHelper smsRequestHelper;
/**
* 构建一个新的 SmsManager 对象
*
* @param auth Auth对象
*/
public SmsManager(Auth auth) {
this.auth = auth;
this.configuration = new Configuration();
client = new Client(this.configuration);
smsRequestHelper = new SmsRequestHelper(configuration, client, auth);
}
/**
* 构建一个新的 SmsManager 对象
*
* @param auth Auth对象
* @param cfg Configuration对象
*/
public SmsManager(Auth auth, Configuration cfg) {
this.auth = auth;
this.configuration = cfg.clone();
client = new Client(this.configuration);
smsRequestHelper = new SmsRequestHelper(configuration, client, auth);
}
/**
* 发送短信
*
* @param templateId 模板Id,必填
* @param mobiles 手机号码数组,必填
* @param parameters 参数,选填
* @return Response
* @throws QiniuException 异常
*/
public Response sendMessage(String templateId, String[] mobiles, Map<String, String> parameters)
throws QiniuException {
String requestUrl = String.format("%s/v1/message", configuration.smsHost());
StringMap bodyMap = new StringMap();
bodyMap.put("template_id", templateId);
bodyMap.put("mobiles", mobiles);
bodyMap.putNotNull("parameters", parameters);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}
/**
* 发送短信
*
* @param signatureId 签名Id,选填
* @param templateId 模板Id,必填
* @param mobiles 手机号码数组,必填
* @param parameters 参数,选填
* @return Response
* @throws QiniuException 异常
*/
public Response sendMessage(String signatureId, String templateId, String[] mobiles, Map<String, String> parameters)
throws QiniuException {
String requestUrl = String.format("%s/v1/message", configuration.smsHost());
StringMap bodyMap = new StringMap();
bodyMap.putNotEmpty("signature_id", signatureId);
bodyMap.put("template_id", templateId);
bodyMap.put("mobiles", mobiles);
bodyMap.putNotNull("parameters", parameters);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}
/**
* 发送单条短信
*
* @param templateId 模板Id,必填
* @param mobile 手机号码,必填
* @param parameters 参数,选填
* @return Response
* @throws QiniuException 异常
*/
public Response sendSingleMessage(String templateId, String mobile, Map<String, String> parameters)
throws QiniuException {
String requestUrl = String.format("%s/v1/message/single", configuration.smsHost());
StringMap bodyMap = new StringMap();
bodyMap.put("template_id", templateId);
bodyMap.put("mobile", mobile);
bodyMap.putNotNull("parameters", parameters);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}
/**
* 发送单条短信
*
* @param messageInfo@return Response
* @throws QiniuException 异常
*/
public Response sendSingleMessage(MessageInfo messageInfo) throws QiniuException {
String requestUrl = String.format("%s/v1/message/single", configuration.smsHost());
StringMap bodyMap = SmsMap.createSingleMessageMap(messageInfo);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}
/**
* 发送国际短信
*
* @param templateId 模板Id,必填
* @param mobile 手机号码,必填
* @param parameters 参数,选填
* @return Response
* @throws QiniuException 异常
*/
public Response sendOverseaMessage(String templateId, String mobile, Map<String, String> parameters)
throws QiniuException {
String requestUrl = String.format("%s/v1/message/oversea", configuration.smsHost());
StringMap bodyMap = new StringMap();
bodyMap.put("template_id", templateId);
bodyMap.put("mobile", mobile);
bodyMap.putNotNull("parameters", parameters);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}
/**
* 发送国际短信
*
* @param signatureId 签名Id,选填
* @param templateId 模板Id,必填
* @param mobile 手机号码,必填
* @param parameters 参数,选填
* @return Response
* @throws QiniuException 异常
*/
public Response sendOverseaMessage(String signatureId, String templateId, String mobile, Map<String, String> parameters)
throws QiniuException {
String requestUrl = String.format("%s/v1/message/oversea", configuration.smsHost());
StringMap bodyMap = new StringMap();
bodyMap.putNotEmpty("signature_id", signatureId);
bodyMap.put("template_id", templateId);
bodyMap.put("mobile", mobile);
bodyMap.put("parameters", parameters);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}
/**
* 发送全文本短信(不需要传模版 ID)
*
* @param mobiles 手机号码数组,必填
* @param content 短信内容,必须是已经审核通过的签名和模版,必填。
* 例如:【七牛云】您的验证码是 287712,5分钟内有效
* @return Response
* @throws QiniuException 异常
*/
public Response sendFulltextMessage(String[] mobiles, String content) throws QiniuException {
String requestUrl = String.format("%s/v1/message/fulltext", configuration.smsHost());
StringMap bodyMap = SmsMap.createFulltextMessageMap(mobiles, content);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}
/**
* 发送全文本短信(不需要传模版 ID)
*
* @param mobiles 手机号码数组,必填
* @param content 短信内容,必须是已经审核通过的签名和模版,必填。例如:【七牛云】您的验证码是 287712,5分钟内有效
* @param templateType 短信类型,支持的值:notification(通知类)、verification(验证码类)、marketing(营销推广类)、voice(语音类),必填
* @return Response
* @throws QiniuException 异常
*/
public Response sendFulltextMessage(String[] mobiles, String content, String templateType) throws QiniuException {
String requestUrl = String.format("%s/v1/message/fulltext", configuration.smsHost());
StringMap bodyMap = SmsMap.createFulltextMessageMap(mobiles, content, templateType);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}
public Response describeResource(String resourceType, String auditStatus, int page, int pageSize) throws QiniuException {
String requestUrl = String.format("%s/v1/%s", configuration.smsHost(), resourceType);
StringMap queryMap = new StringMap();
queryMap.putNotEmpty("audit_status", auditStatus);
queryMap.putWhen("page", page, page > 0);
queryMap.putWhen("page_size", pageSize, pageSize > 0);
requestUrl = UrlUtils.composeUrlWithQueries(requestUrl, queryMap);
return smsRequestHelper.get(requestUrl);
}
/**
* 查询签名
*
* @param auditStatus 审核状态,非必填
* 取值范围为: "passed"(通过), "rejected"(未通过), "reviewing"(审核中)。
* @param page 页码。默认为 1,非必填
* @param pageSize 分页大小。默认为 20,非必填
* @return Response
* @throws QiniuException 异常
*/
public Response describeSignature(String auditStatus, int page, int pageSize) throws QiniuException {
return describeResource("signature", auditStatus, page, pageSize);
}
public SignatureInfo describeSignatureItems(String auditStatus, int page, int pageSize) throws QiniuException {
Response resp = describeSignature(auditStatus, page, pageSize);
SignatureInfo signatureInfo = Json.decode(resp.bodyString(), SignatureInfo.class);
return signatureInfo;
}
/**
* 创建签名
*
* @param signature 签名,必填
* @param source 签名来源,申请签名时必须指定签名来源。必填
* 取值范围为:
* enterprises_and_institutions 企事业单位的全称或简称
* website 工信部备案网站的全称或简称
* app APP应用的全称或简称
* public_number_or_small_program 公众号或小程序的全称或简称
* store_name 电商平台店铺名的全称或简称
* trade_name 商标名的全称或简称
* @param pics 签名对应的资质证明图片进行 base64 编码格式转换后的字符串,非必填
* @return Response
* @throws QiniuException 异常
*/
public Response createSignature(String signature, String source, String[] pics) throws QiniuException {
String requestUrl = String.format("%s/v1/signature", configuration.smsHost());
StringMap bodyMap = new StringMap();
bodyMap.put("signature", signature);
bodyMap.put("source", source);
bodyMap.putWhen("pics", pics, pics.length > 0);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}
/**
* 编辑签名 审核不通过的情况下才可以重新编辑签名,已经审核通过的签名无法重新编辑。
*
* @param signatureId 签名Id, 必填
* @param signature 签名,必填
* @return Response
* @throws QiniuException 异常
*/
public Response modifySignature(String signatureId, String signature) throws QiniuException {
String reqUrl = String.format("%s/v1/signature/%s", configuration.smsHost(), signatureId);
StringMap bodyMap = new StringMap();
bodyMap.put("signature", signature);
return smsRequestHelper.put(reqUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}
/**
* 删除签名 审核不通过的情况下才可以重新编辑签名,已经审核通过的签名无法重新编辑。
*
* @param signatureId 签名Id, 必填
* @return Response
* @throws QiniuException 异常
*/
public Response deleteSignature(String signatureId) throws QiniuException {
String requestUrl = String.format("%s/v1/signature/%s", configuration.smsHost(), signatureId);
return smsRequestHelper.delete(requestUrl);
}
/**
* 查询模板
*
* @param auditStatus 审核状态, 非必填
* 取值范围为: "passed"(通过), "rejected"(未通过), "reviewing"(审核中)。
* @param page 页码。默认为 1,非必填
* @param pageSize 分页大小。默认为 20,非必填
* @return Response
* @throws QiniuException 异常
*/
public Response describeTemplate(String auditStatus, int page, int pageSize) throws QiniuException {
return describeResource("template", auditStatus, page, pageSize);
}
public TemplateInfo describeTemplateItems(String auditStatus, int page, int pageSize) throws QiniuException {
Response resp = describeTemplate(auditStatus, page, pageSize);
TemplateInfo templateInfo = Json.decode(resp.bodyString(), TemplateInfo.class);
return templateInfo;
}
/**
* 查询单个模板信息
*
* @param templateId 模板ID
* @return Response
* @throws QiniuException 异常
*/
public Response describeTemplate(String templateId) throws QiniuException {
String requestUrl = String.format("%s/v1/template/%s", configuration.smsHost(), templateId);
return smsRequestHelper.get(requestUrl);
}
/**
* 查询单个模板信息
*
* @param templateId 模板ID
* @return Response
* @throws QiniuException 异常
*/
public TemplateInfo.Item describeTemplateItem(String templateId) throws QiniuException {
Response resp = describeTemplate(templateId);
TemplateInfo.Item item = Json.decode(resp.bodyString(), TemplateInfo.Item.class);
return item;
}
/**
* 创建模板
*
* @param name 模板名称,必填
* @param template 模板内容,必填
* @param type 模板类型,必填
* 取值范围为: notification (通知类短信), verification (验证码短信), marketing (营销类短信)。
* @param description 申请理由简述,必填
* @param signatureId 已经审核通过的签名,必填
* @return Response
* @throws QiniuException 异常
*/
public Response createTemplate(String name, String template, String type, String description, String signatureId)
throws QiniuException {
String requestUrl = String.format("%s/v1/template", configuration.smsHost());
StringMap bodyMap = new StringMap();
bodyMap.put("name", name);
bodyMap.put("template", template);
bodyMap.put("type", type);
bodyMap.put("description", description);
bodyMap.put("signature_id", signatureId);
return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}
/**
* 编辑模板 审核不通过的情况下才可以重新编辑模板,已经审核通过的模板无法重新编辑。
*
* @param templateId 模板Id,必填
* @param name 模板名称,必填
* @param template 模板内容,必填
* @param description 申请理由简述,必填
* @param signatureId 已经审核通过的签名,必填
* @return Response
* @throws QiniuException 异常
*/
public Response modifyTemplate(String templateId, String name, String template, String description,
String signatureId) throws QiniuException {
String requestUrl = String.format("%s/v1/template/%s", configuration.smsHost(), templateId);
StringMap bodyMap = new StringMap();
bodyMap.put("name", name);
bodyMap.put("template", template);
bodyMap.put("description", description);
bodyMap.put("signature_id", signatureId);
return smsRequestHelper.put(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8));
}
/**
* 删除模板
*
* @param templateId 模板Id, 必填
* @return Response
* @throws QiniuException 异常
*/
public Response deleteTemplate(String templateId) throws QiniuException {
String requestUrl = String.format("%s/v1/template/%s", configuration.smsHost(), templateId);
return smsRequestHelper.delete(requestUrl);
}
}