Jelajahi Sumber

华为云短信

lyhzzz 7 bulan lalu
induk
melakukan
096e2da749

+ 12 - 7
4dkankan-utils-sms/src/main/java/com/fdkankan/sms/SmsServiceHuawei.java

@@ -78,6 +78,8 @@ public class SmsServiceHuawei {
 
 
     public  void sendSms(String phoneNum, String templateParas) throws Exception {
+        templateParas = "["+templateParas+"]";
+
         String receiver = "+86"+phoneNum; //短信接收人号码
         String statusCallBack = "";
         /**
@@ -88,19 +90,20 @@ public class SmsServiceHuawei {
          * 查看更多模板规范和变量规范:产品介绍>短信模板须知和短信变量须知
          */
         //String templateParas = "[\"369751\"]"; //模板变量,此处以单变量验证码短信为例,请客户自行生成6位验证码,并定义为字符串类型,以杜绝首位0丢失的问题(例如:002569变成了2569)。
-
-        templateParas =templateParas.replace("{","[").replace("}","]").replace( "\"code\":","");
+        //templateParas =templateParas.replace("{","[").replace("}","]").replace( "\"code\":","");
         //请求Body,不携带签名名称时,signature请填null
         String body = buildRequestBody(sender, receiver, templateId, templateParas, statusCallBack, signature);
+        log.info("sendSms:body{}",body);
         if (null == body || body.isEmpty()) {
-            System.out.println("body is null.");
+            log.info("body is null.");
             return;
         }
 
         //请求Headers中的X-WSSE参数值
         String wsseHeader = buildWsseHeader(appKey, appSecret);
+        log.info("sendSms:wsseHeader{}",wsseHeader);
         if (null == wsseHeader || wsseHeader.isEmpty()) {
-            System.out.println("wsse header is null.");
+            log.info("wsse header is null.");
             return;
         }
 
@@ -121,6 +124,8 @@ public class SmsServiceHuawei {
         trustAllHttpsCertificates();
 
         try {
+            log.info("sendSms:url{}",url);
+
             URL realUrl = new URL(url);
             connection = (HttpsURLConnection) realUrl.openConnection();
 
@@ -152,7 +157,7 @@ public class SmsServiceHuawei {
             while ((line = in.readLine()) != null) {
                 result.append(line);
             }
-            System.out.println(result.toString()); //打印响应消息实体
+            log.info(result.toString()); //打印响应消息实体
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
@@ -186,7 +191,7 @@ public class SmsServiceHuawei {
                                    String statusCallBack, String signature) {
         if (null == sender || null == receiver || null == templateId || sender.isEmpty() || receiver.isEmpty()
                 || templateId.isEmpty()) {
-            System.out.println("buildRequestBody(): sender, receiver or templateId is null.");
+            log.info("buildRequestBody(): sender, receiver or templateId is null.");
             return null;
         }
         Map<String, String> map = new HashMap<String, String>();
@@ -227,7 +232,7 @@ public class SmsServiceHuawei {
      */
     static String buildWsseHeader(String appKey, String appSecret) {
         if (null == appKey || null == appSecret || appKey.isEmpty() || appSecret.isEmpty()) {
-            System.out.println("buildWsseHeader(): appKey or appSecret is null.");
+            log.info("buildWsseHeader(): appKey or appSecret is null.");
             return null;
         }
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");