SpringBoot项目实现短信发送接口开发的实践

  /**

  * 发送短信

  * @param sendMessageVo

  * @return

  */

  @Override

  public SendSmsResult sendMessage(SendSmsVo sendMessageVo) {

  // 0.参数校验

  sendMessageVo = this.validParams(sendMessageVo);

  // 1.创建返回结果集

  SendSmsResult smsResult = new SendSmsResult();

  try {

  //2.拼装发送手机号码

  String mobiles = "";

  for (int i = 0; i < sendMessageVo.getMobile().size(); i++) {

  if (i == 0) {

  mobiles = sendMessageVo.getMobile().get(i);

  } else {

  mobiles = mobiles + "," + sendMessageVo.getMobile().get(i);

  }

  }

  //3.发送短信

  log.info("开始请求发送短信>>>>>>>>>>>>>>>>>>"+"开始发送短信");

  SMSNewAllService service = new SMSNewAllService();

  ISMSNewAll ismsNewAll = service.getISMSNewAllPort();

  String resultSend = ismsNewAll.sendSMSWithExt(smsProperties.getUserName(), smsProperties.getPassword(), mobiles, sendMessageVo.getSmsBody(), smsProperties.getSmsType(), sendMessageVo.getReplyFlag(), smsProperties.getFeeDept(), sendMessageVo.getAllowStartTime(), sendMessageVo.getAllowEndTime(), sendMessageVo.getDeadLineTime(), sendMessageVo.getAppointTime(), sendMessageVo.getArea(), sendMessageVo.getExtCode());

  log.info("请求发送短信返回值>>>>>>>>>>>>>>>>>>{}: "+resultSend);

  //4.判断返回结果

  if (ObjectUtils.isEmpty(resultSend)) {

  return SmsResultUtil.error("请求发送短信失败,短信服务商无响应!");

  }

  //5.处理返回信息

  JSONObject jsonObject = JSONObject.parseObject(resultSend);

  String errorCode = jsonObject.get("errorCode").toString();

  String status = jsonObject.get("status").toString();

  if ("0".equals(errorCode) && "ok".equals(status)) {

  String msgInfo = jsonObject.getString("msgInfo");

  smsResult = SmsResultUtil.returnMessage(SmsConstants.SystemCode.OK, SmsConstants.MessageCode.SUCCESS_CODE, SmsConstants.MessageMsg.SUCCESS_MSG, msgInfo);

  return smsResult;

  } else {

  String errorMsg = jsonObject.getString("errorMsg");

  smsResult = SmsResultUtil.returnMessage(SmsConstants.SystemCode.ERROR, SmsConstants.MessageCode.FAIL_CODE, "请求发送短信失败,服务商返回错误信息为:" + errorMsg, "");

  return smsResult;

  }

  }catch (Exception e){

  log.info("请求发送短信失败,系统异常{}", e);

  e.printStackTrace();

  return SmsResultUtil.error("请求发送短信失败,系统异常!");

  }finally {

  log.info("发送短信结果{}", JSONObject.toJSONString(smsResult));

  }

  }