|
|
@@ -0,0 +1,66 @@
|
|
|
+package com.happy.Until.Tencent;
|
|
|
+
|
|
|
+import com.github.qcloudsms.SmsSenderUtil;
|
|
|
+import com.github.qcloudsms.SmsSingleSender;
|
|
|
+import com.github.qcloudsms.SmsSingleSenderResult;
|
|
|
+import com.github.qcloudsms.httpclient.*;
|
|
|
+import org.json.JSONException;
|
|
|
+import org.json.JSONObject;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.URISyntaxException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+
|
|
|
+
|
|
|
+public class SmsSend extends SmsSingleSender {
|
|
|
+
|
|
|
+ private String url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms";
|
|
|
+
|
|
|
+ public SmsSend(int appid, String appkey) {
|
|
|
+ super(appid, appkey);
|
|
|
+ }
|
|
|
+
|
|
|
+ public SmsSend(int appid, String appkey, HTTPClient httpclient) {
|
|
|
+ super(appid, appkey, httpclient);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public SmsSingleSenderResult send(int type, String nationCode, String phoneNumber, String msg, String extend, String ext) throws HTTPException, JSONException, IOException {
|
|
|
+ long random = SmsSenderUtil.getRandom();
|
|
|
+ long now = SmsSenderUtil.getCurrentTime();
|
|
|
+ JSONObject body = (new JSONObject()).put("tel", (new JSONObject()).put("nationcode", nationCode).put("mobile", phoneNumber)).put("type", type).put("msg", msg).put("sig", SmsSenderUtil.calculateSignature(this.appkey, random, now, phoneNumber)).put("time", now).put("extend", SmsSenderUtil.isNotEmpty(extend) ? extend : "").put("ext", SmsSenderUtil.isNotEmpty(ext) ? ext : "");
|
|
|
+ HTTPRequest req = (new HTTPRequest(HTTPMethod.POST, this.url)).addHeader("Conetent-Type", "application/json").addQueryParameter("sdkappid", this.appid).addQueryParameter("random", random).setConnectionTimeout(60000).setRequestTimeout(60000).setBody(body.toString());
|
|
|
+
|
|
|
+ try {
|
|
|
+ HTTPResponse res = this.httpclient.fetch(req);
|
|
|
+ this.handleError(res);
|
|
|
+ return (new SmsSingleSenderResult()).parseFromHTTPResponse(res);
|
|
|
+ } catch (URISyntaxException var14) {
|
|
|
+ throw new RuntimeException("API url has been modified, current url: " + this.url);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public SmsSingleSenderResult sendWithParam(String nationCode, String phoneNumber, int templateId, ArrayList<String> params, String sign, String extend, String ext) throws HTTPException, JSONException, IOException {
|
|
|
+ long random = SmsSenderUtil.getRandom();
|
|
|
+ long now = SmsSenderUtil.getCurrentTime();
|
|
|
+ JSONObject body = (new JSONObject()).put("tel", (new JSONObject()).put("nationcode", nationCode).put("mobile", phoneNumber)).put("sig", SmsSenderUtil.calculateSignature(this.appkey, random, now, phoneNumber)).put("tpl_id", templateId).put("params", params).put("sign", sign).put("time", now).put("extend", SmsSenderUtil.isNotEmpty(extend) ? extend : "").put("ext", SmsSenderUtil.isNotEmpty(ext) ? ext : "");
|
|
|
+ HTTPRequest req = (new HTTPRequest(HTTPMethod.POST, this.url)).addHeader("Conetent-Type", "application/json").addQueryParameter("sdkappid", this.appid).addQueryParameter("random", random).setConnectionTimeout(60000).setRequestTimeout(60000).setBody(body.toString());
|
|
|
+
|
|
|
+ try {
|
|
|
+ HTTPResponse res = this.httpclient.fetch(req);
|
|
|
+ this.handleError(res);
|
|
|
+ return (new SmsSingleSenderResult()).parseFromHTTPResponse(res);
|
|
|
+ } catch (URISyntaxException var15) {
|
|
|
+ throw new RuntimeException("API url has been modified, current url: " + this.url);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public SmsSingleSenderResult sendWithParam(String nationCode, String phoneNumber, int templateId, String[] params, String sign, String extend, String ext) throws HTTPException, JSONException, IOException {
|
|
|
+ return this.sendWithParam(nationCode, phoneNumber, templateId, new ArrayList(Arrays.asList(params)), sign, extend, ext);
|
|
|
+ }
|
|
|
+}
|