Pu Zhibing
3 天以前 33632d86bbf74e922ce406d9032fadc90f6bba5e
UserQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/qianyuntong/SMSUtil.java
@@ -2,8 +2,10 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.open.common.util.OpenApiClient;
import com.open.common.util.SystemParameterNames;
import com.stylefeng.guns.modular.system.util.qianyuntong.model.SendSmsRequest;
import lombok.extern.slf4j.Slf4j;
import java.text.SimpleDateFormat;
@@ -64,4 +66,61 @@
      }
      return object.getString("code");
   }
   public static void main(String[] args) {
      SendSmsRequest request = new SendSmsRequest();
      request.setDestAddress("15828353127");
      request.setTemplateId("TPL202410290001");
      Map<String, String> templateParams = new HashMap<>();
      templateParams.put("code", "1234");
      request.setTemplateParams(templateParams);
      request.setCode("code");
      request.setSpId("T8d5hdfg");
      SMSUtil.sendSms(request);
   }
   /**
    * 根据模板发送短信
    *
    * @param request
    * @return
    */
   public static Boolean sendSms(SendSmsRequest request) {
      //请求路径
      String url = QianYunTongProperties.apiUrl + "/openapi/rest/1.0/sendSmsByTpl";
      //私钥文件
      String skprivateKeyFile = QianYunTongProperties.privateKeyPath;
      //注意:私钥文件需要开发者手动新建.pem文件,将委办局提供的私钥串复制进文件里用于sign加密
      String appKey = QianYunTongProperties.appkey;//appkey
      Map<String, String> headers = new HashMap<>();
      headers.put("Content-Type", "application/json");
      Map<String, Object> contentMap = new HashMap<String, Object>();
      Date nowdate = new Date();
      SimpleDateFormat date = new SimpleDateFormat("yyyyMMddHHmmss");
      String timeStamp = date.format(nowdate);
      String messageId = UUID.randomUUID().toString().replaceAll("-", "");
      contentMap.put(SystemParameterNames.getAppKey(), appKey);
      contentMap.put(SystemParameterNames.getMessage_id(), messageId);
      contentMap.put(SystemParameterNames.getUserName(), QianYunTongProperties.userName);
      contentMap.put(SystemParameterNames.getStatus(), QianYunTongProperties.status);
      contentMap.put("content", new Gson().toJson(request));
      log.info("【根据模板发送短信】请求地址:" + url);
      log.info("【根据模板发送短信】请求参数:" + JSON.toJSONString(contentMap));
      String result = OpenApiClient.sendCommonHttpRequst(url, headers, "POST", skprivateKeyFile, timeStamp, contentMap);
      log.info("【根据模板发送短信】请求结果:" + result);
      JSONObject jsonObject = JSON.parseObject(result);
      String retCode = jsonObject.getString("retCode");
      if (!"0".equals(retCode)) {
         log.error("【根据模板发送短信】请求失败:" + result);
         throw new RuntimeException("【根据模板发送短信】请求失败:" + result);
      }
      JSONObject object = jsonObject.getJSONObject("object");
      String status = object.getString("status");
      if (!"0".equals(status)) {
         log.error("【根据模板发送短信】失败:" + object.toJSONString());
         throw new RuntimeException("【根据模板发送短信】失败:" + object.toJSONString());
      }
      return true;
   }
}