diff --git a/src/main/java/com/qiniu/sms/SmsManager.java b/src/main/java/com/qiniu/sms/SmsManager.java index 227dc344f..943e84825 100644 --- a/src/main/java/com/qiniu/sms/SmsManager.java +++ b/src/main/java/com/qiniu/sms/SmsManager.java @@ -3,8 +3,8 @@ import com.qiniu.common.Constants; import com.qiniu.common.QiniuException; import com.qiniu.http.Client; -import com.qiniu.http.MethodType; 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; @@ -31,6 +31,7 @@ public class SmsManager { * 该类相关的域名配置,解析配置,HTTP请求超时时间设置等 */ private Configuration configuration; + private SmsRequestHelper smsRequestHelper; /** * 构建一个新的 SmsManager 对象 @@ -41,6 +42,7 @@ public SmsManager(Auth auth) { this.auth = auth; this.configuration = new Configuration(); client = new Client(this.configuration); + smsRequestHelper = new SmsRequestHelper(configuration, client, auth); } /** @@ -53,6 +55,7 @@ public SmsManager(Auth auth, Configuration cfg) { this.auth = auth; this.configuration = cfg.clone(); client = new Client(this.configuration); + smsRequestHelper = new SmsRequestHelper(configuration, client, auth); } /** @@ -60,7 +63,7 @@ public SmsManager(Auth auth, Configuration cfg) { * * @param templateId 模板Id,必填 * @param mobiles 手机号码数组,必填 - * @param parameters 参数,必填 + * @param parameters 参数,选填 * @return Response * @throws QiniuException 异常 */ @@ -70,8 +73,29 @@ public Response sendMessage(String templateId, String[] mobiles, Map 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)); } /** @@ -79,7 +103,7 @@ public Response sendMessage(String templateId, String[] mobiles, Map 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 post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8)); + return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8)); } /** @@ -123,10 +180,33 @@ public Response sendOverseaMessage(String templateId, String mobile, Map 0); + queryMap.putWhen("page_size", pageSize, pageSize > 0); + requestUrl = UrlUtils.composeUrlWithQueries(requestUrl, queryMap); + return smsRequestHelper.get(requestUrl); } /** @@ -140,13 +220,7 @@ public Response sendFulltextMessage(String[] mobiles, String content) throws Qin * @throws QiniuException 异常 */ public Response describeSignature(String auditStatus, int page, int pageSize) throws QiniuException { - String requestUrl = String.format("%s/v1/signature", configuration.smsHost()); - 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 get(requestUrl); + return describeResource("signature", auditStatus, page, pageSize); } public SignatureInfo describeSignatureItems(String auditStatus, int page, int pageSize) throws QiniuException { @@ -177,7 +251,7 @@ public Response createSignature(String signature, String source, String[] pics) bodyMap.put("signature", signature); bodyMap.put("source", source); bodyMap.putWhen("pics", pics, pics.length > 0); - return post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8)); + return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8)); } /** @@ -192,7 +266,7 @@ public Response modifySignature(String signatureId, String signature) throws Qin String reqUrl = String.format("%s/v1/signature/%s", configuration.smsHost(), signatureId); StringMap bodyMap = new StringMap(); bodyMap.put("signature", signature); - return put(reqUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8)); + return smsRequestHelper.put(reqUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8)); } /** @@ -204,7 +278,7 @@ public Response modifySignature(String signatureId, String signature) throws Qin */ public Response deleteSignature(String signatureId) throws QiniuException { String requestUrl = String.format("%s/v1/signature/%s", configuration.smsHost(), signatureId); - return delete(requestUrl); + return smsRequestHelper.delete(requestUrl); } /** @@ -218,13 +292,7 @@ public Response deleteSignature(String signatureId) throws QiniuException { * @throws QiniuException 异常 */ public Response describeTemplate(String auditStatus, int page, int pageSize) throws QiniuException { - String requestUrl = String.format("%s/v1/template", configuration.smsHost()); - 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 get(requestUrl); + return describeResource("template", auditStatus, page, pageSize); } public TemplateInfo describeTemplateItems(String auditStatus, int page, int pageSize) throws QiniuException { @@ -242,7 +310,7 @@ public TemplateInfo describeTemplateItems(String auditStatus, int page, int page */ public Response describeTemplate(String templateId) throws QiniuException { String requestUrl = String.format("%s/v1/template/%s", configuration.smsHost(), templateId); - return get(requestUrl); + return smsRequestHelper.get(requestUrl); } /** @@ -279,7 +347,7 @@ public Response createTemplate(String name, String template, String type, String bodyMap.put("type", type); bodyMap.put("description", description); bodyMap.put("signature_id", signatureId); - return post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8)); + return smsRequestHelper.post(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8)); } /** @@ -301,7 +369,7 @@ public Response modifyTemplate(String templateId, String name, String template, bodyMap.put("template", template); bodyMap.put("description", description); bodyMap.put("signature_id", signatureId); - return put(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8)); + return smsRequestHelper.put(requestUrl, Json.encode(bodyMap).getBytes(Constants.UTF_8)); } /** @@ -313,36 +381,6 @@ public Response modifyTemplate(String templateId, String name, String template, */ public Response deleteTemplate(String templateId) throws QiniuException { String requestUrl = String.format("%s/v1/template/%s", configuration.smsHost(), templateId); - return delete(requestUrl); - } - - /* - * 相关请求的方法列表 - * */ - private Response get(String url) throws QiniuException { - StringMap headers = composeHeader(url, MethodType.GET.toString(), null, Client.FormMime); - return client.get(url, headers); + return smsRequestHelper.delete(requestUrl); } - - private Response post(String url, byte[] body) throws QiniuException { - StringMap headers = composeHeader(url, MethodType.POST.toString(), body, Client.JsonMime); - return client.post(url, body, headers, Client.JsonMime); - } - - private Response put(String url, byte[] body) throws QiniuException { - StringMap headers = composeHeader(url, MethodType.PUT.toString(), body, Client.JsonMime); - return client.put(url, body, headers, Client.JsonMime); - } - - private Response delete(String url) throws QiniuException { - StringMap headers = composeHeader(url, MethodType.DELETE.toString(), null, Client.DefaultMime); - return client.delete(url, headers); - } - - private StringMap composeHeader(String url, String method, byte[] body, String contentType) { - StringMap headers = auth.authorizationV2(url, method, body, contentType); - headers.put("Content-Type", contentType); - return headers; - } - } diff --git a/src/main/java/com/qiniu/sms/SmsMap.java b/src/main/java/com/qiniu/sms/SmsMap.java new file mode 100644 index 000000000..da79a1c52 --- /dev/null +++ b/src/main/java/com/qiniu/sms/SmsMap.java @@ -0,0 +1,38 @@ +package com.qiniu.sms; + +import com.qiniu.sms.model.MessageInfo; +import com.qiniu.util.StringMap; + +/** + * @author hugo + * @date 2023-06-07 21:37 + */ +public class SmsMap { + private SmsMap() { + } + + public static StringMap createSingleMessageMap(MessageInfo messageInfo) { + StringMap bodyMap = new StringMap(); + bodyMap.putNotEmpty("signature_id", messageInfo.getSignatureId()); + bodyMap.put("template_id", messageInfo.getTemplateId()); + bodyMap.put("mobile", messageInfo.getMobile()); + bodyMap.putNotNull("parameters", messageInfo.getParameters()); + bodyMap.putNotEmpty("seq", messageInfo.getSeq()); + return bodyMap; + } + + public static StringMap createFulltextMessageMap(String[] mobiles, String content) { + StringMap bodyMap = new StringMap(); + bodyMap.put("mobiles", mobiles); + bodyMap.put("content", content); + return bodyMap; + } + + public static StringMap createFulltextMessageMap(String[] mobiles, String content, String templateType) { + StringMap bodyMap = new StringMap(); + bodyMap.put("mobiles", mobiles); + bodyMap.put("content", content); + bodyMap.put("template_type", templateType); + return bodyMap; + } +} diff --git a/src/main/java/com/qiniu/sms/SmsRequestHelper.java b/src/main/java/com/qiniu/sms/SmsRequestHelper.java new file mode 100644 index 000000000..0ec1240aa --- /dev/null +++ b/src/main/java/com/qiniu/sms/SmsRequestHelper.java @@ -0,0 +1,50 @@ +package com.qiniu.sms; + +import com.qiniu.common.QiniuException; +import com.qiniu.http.Client; +import com.qiniu.http.MethodType; +import com.qiniu.http.Response; +import com.qiniu.util.Auth; +import com.qiniu.util.StringMap; + +/** + * @author hugo + * @date 2023-06-07 21:25 + */ +public class SmsRequestHelper { + private Configuration configuration; + private Client client; + private Auth auth; + + public SmsRequestHelper(Configuration configuration, Client client, Auth auth) { + this.configuration = configuration; + this.client = client; + this.auth = auth; + } + + public Response get(String url) throws QiniuException { + StringMap headers = composeHeader(url, MethodType.GET.toString(), null, Client.FormMime); + return client.get(url, headers); + } + + public Response post(String url, byte[] body) throws QiniuException { + StringMap headers = composeHeader(url, MethodType.POST.toString(), body, Client.JsonMime); + return client.post(url, body, headers, Client.JsonMime); + } + + public Response put(String url, byte[] body) throws QiniuException { + StringMap headers = composeHeader(url, MethodType.PUT.toString(), body, Client.JsonMime); + return client.put(url, body, headers, Client.JsonMime); + } + + public Response delete(String url) throws QiniuException { + StringMap headers = composeHeader(url, MethodType.DELETE.toString(), null, Client.DefaultMime); + return client.delete(url, headers); + } + + private StringMap composeHeader(String url, String method, byte[] body, String contentType) { + StringMap headers = auth.authorizationV2(url, method, body, contentType); + headers.put("Content-Type", contentType); + return headers; + } +} diff --git a/src/main/java/com/qiniu/sms/model/MessageInfo.java b/src/main/java/com/qiniu/sms/model/MessageInfo.java new file mode 100644 index 000000000..6ed466d6a --- /dev/null +++ b/src/main/java/com/qiniu/sms/model/MessageInfo.java @@ -0,0 +1,57 @@ +package com.qiniu.sms.model; + +import java.util.Map; + +public class MessageInfo { + private String signatureId; // 签名Id,选填 + private String templateId; // 模板Id,必填 + private String mobile; // 手机号码,必填 + private Map parameters; // 参数,选填 + private String seq; // 序列号,选填 + + public MessageInfo(String templateId, String mobile, Map parameters) { + this.templateId = templateId; + this.mobile = mobile; + this.parameters = parameters; + } + + public void setSignatureId(String signatureId) { + this.signatureId = signatureId; + } + + public void setTemplateId(String templateId) { + this.templateId = templateId; + } + + public void setMobile(String mobile) { + this.mobile = mobile; + } + + public void setParameters(Map parameters) { + this.parameters = parameters; + } + + public void setSeq(String seq) { + this.seq = seq; + } + + public String getSignatureId() { + return signatureId; + } + + public String getTemplateId() { + return templateId; + } + + public String getMobile() { + return mobile; + } + + public Map getParameters() { + return parameters; + } + + public String getSeq() { + return seq; + } +}