package com.ots.project.tool.sms; import cn.hutool.core.bean.BeanUtil; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Stream; @Slf4j @Component public class ShortMessageImpl implements ShortMessage { @Autowired ZjunShortMessageImpl zjunShortMessage; @Override public Returnsms sendSms(List mobiles, String sign, String message) { Returnsms returnsms = null; try { String telnoStrs = getMobilesStr(mobiles); return zjunShortMessage.sendmsg(telnoStrs, sign, message); } catch (Exception e) { log.error("【发送{}短信失败:】", mobiles, e); returnsms.setReturnstatus(ZjunShortMessageImpl.RET_STATUS_FAIL); returnsms.setMessage(e.getMessage()); } return returnsms; } @Override public List getReport() { return zjunShortMessage.getReport(); } @Override public ResponseSms sendInternationalSms(List mobiles, String message) { ResponseSms responseSms = new ResponseSms(); try { String substring = getMobilesStr(mobiles); String retStr = InternationalMessage.SendPost(InternationalMessage.INTERNATIONAL_SMS_URL, InternationalMessage.INTERNATIONAL_SMS_COMMAND, "gpcz68", "vlcfXHa4", substring, message, null); Map mapValue = new HashMap(); Stream.of(retStr.split("&")).forEach(p -> { String[] split = p.split("="); mapValue.put(split[0], split[1]); }); responseSms = BeanUtil.mapToBeanIgnoreCase(mapValue, ResponseSms.class, true); } catch (Exception e) { e.printStackTrace(); responseSms.setMtstat(ResponseSms.Mterrcode.RET_502.getCode() + " 程序异常:" + e.getMessage()); } return responseSms; } private String getMobilesStr(List mobiles) { StringBuilder builder = new StringBuilder(); mobiles.stream().forEach(it -> builder.append(it + ",")); return builder.substring(0, builder.length() - 1); } public static void main(String[] args) { ShortMessageImpl impl = new ShortMessageImpl(); String mobilesStr = impl.getMobilesStr(Arrays.asList("13512745445", "13512745445", "13512745445", "13512745445")); System.out.println(mobilesStr); String str = "command=MT_RESPONSE&cpid=gpcz68&cppwd=000&mtmsgid=059acdf58165ba&mtstat=ACCEPTD&mterrcode=000"; Map mapValue = new HashMap(); Stream.of(str.split("&")).forEach(p -> { String[] split = p.split("="); mapValue.put(split[0], split[1]); }); ResponseSms responseSms = BeanUtil.mapToBeanIgnoreCase(mapValue, ResponseSms.class, true); System.out.println(JSON.toJSON(responseSms)); } }