java实现短信发送

   xxxDto dto = new xxxDto();
    Map<String, String> map = getOpenIdAndCompanyId(ticket);
    String openId = String.valueOf(map.get("openId"));
    String companyId = String.valueOf(map.get("companyId"));
    List<String> cusIds = dto.getUserIds();
            cusGroupModeService.send(map,cusIds);
   //send方法为短信发送的方法
	
	
    @Override
    public int send(Map<String,String> map, List<String> cusIds){
    	{
        int count = 0;
        Map<String,List<MpCusContacts>> contactsMap = null;
        List<MpCusContacts> contactsList = null;
        SendBo bo = null;
        JSONObject param = null;
        Map<String, String> sendContent = getxxxContent(map,cusIds);
        // getxxxContent方法为获取短信内容的方法,可以自己设定短信模板,动态读取部分数据替换模板中的响应位置。
        String content = sendContent.get("content");
        List<Future<Integer>> futures = new ArrayList<>();
        List<MpCusContacts> list = mpCusContactsDao.selectByCustIds(map.get("companyId"),cusIds,null,null);
        if(list != null && list.size() > 0){
            contactsMap = list.stream().collect(Collectors.groupingBy(MpCusContacts::getCustId));
        }
        for(String id:cusIds) {
            contactsList = contactsMap == null ? null : contactsMap.get(id);
            if (contactsList == null) {
                continue;
            }
            for (MpCusContacts contacts : contactsList) {
                param = new JSONObject();
                param.put("company", map.get("officeName"));

                bo = new SendBo();
                bo.setCompanyId(map.get("companyId"));
                bo.setContent(content);
                bo.setType(type);
                bo.setSendType("JSXX");//类型自己定义,我用的是首字母拼音
                bo.setUserId(map.get("userId"));
                bo.setTelephone(contacts.getTelephone());
                bo.setParam(param);
                futures.add(smsService.send(bo));
            }
        }
            try {
                for (Future future : futures) {
                    count = count + (Integer) future.get();
                }
            }catch (ExecutionException e){
                e.printStackTrace();
                log.error("xxxSend:{}",e);
                return count;
            }catch (InterruptedException e){
                e.printStackTrace();
                log.error("xxxSend:{}",e);
                return count;
            }
            return count;
        }
   }
	
    @Async
	@Override
	public Future<Integer> send(SendBo bo) {
    // 发送短信
    JSONObject object = new JSONObject();
    object.put("mobile",bo.getTelephone());
    object.put("param",bo.getParam());
    object.put("signName",signName);
    object.put("type",bo.getType());
    log.info("发送短信,参数:{}",object.toJSONString());
    JSONObject result = smsService.MessageSend(object);
    log.info("返回参数:{}",result.toJSONString());
    // 短信记录
    if(result.getIntValue("code") == 200) {
        MpShortMsgRecord record = new MpShortMsgRecord();
        record.preInsert();
        record.setCompanyId(bo.getCompanyId());
        record.setAddresseeTelephone(bo.getTelephone());
        record.setAddresseeContent(bo.getContent());
        record.setSendType(bo.getSendType());
        record.setCreator(bo.getUserId());
        record.setUpdater(bo.getUserId());
        return new AsyncResult<>(mpShortMsgRecordDao.insertSelective(record));
    }
    return new AsyncResult<>(0);
}

@Data
public class SendBo {
private Long type; // 模板id
private String sendType; // 类型
private String companyId; //公司id
private String telephone;  //手机号
private String userId;   //操作人
private JSONObject param;   // 模板替换参数 JSON格式
private String content; // 内容

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值