jsonStr内的内容组合:
package com.creatar.creatar.admin.api.entity; import com.baomidou.mybatisplus.extension.activerecord.Model; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; @Data @EqualsAndHashCode(callSuper = true) @ApiModel(value = "短信实体类") public class SMS extends Model{ @ApiModelProperty(value = "接收人") private String receive; @ApiModelProperty(value = "短信内容") private String smsContent; @ApiModelProperty(value = "发送次数") private String sendNum; @ApiModelProperty(value = "发送通道 1 代表移动接口 2 代表12379接口") private String sendAisle; @ApiModelProperty(value = "短信类型 1预警短信 2值班短信 3灾情处置短信") private String sendSmsType; @ApiModelProperty(value = "细化短信类型") private String sendSmsTypeRefine; @ApiModelProperty(value = "ID标识符") private String identifier; @ApiModelProperty(value = "行政区域编码") private String areaCode; }
@Override
// 注册用户,发送手机验证码
public R sendVerificationCode(String phone) {
//先判断缓存中有没有验证码
String key = CacheConstants.MOBILE_CODE_KEY + phone;
redisTemplate.setKeySerializer(new StringRedisSerializer());
if (redisTemplate.hasKey(key)) {
Object codeObj = redisTemplate.opsForValue().get(key);
if (codeObj == null) {
redisTemplate.delete(key);
}else {
String saveCode = codeObj.toString();
if (StrUtil.isBlank(saveCode)) {
redisTemplate.delete(key);
}else {
// throw new ValidateCodeException("请勿重复发送验证码");
return R.ok(Boolean.FALSE,"请勿重复发送验证码!");
}
}
}
String c = RandomUtil.randomNumbers(6);
SMS sms=new SMS();
sms.setReceive("注册用户:"+phone);
sms.setSmsContent("您的手机验证码是"+c+",此验证码1分钟内有效,为保障账户安全,请不要泄露给他人");
sms.setSendNum("2");
sms.setSendAisle("1");
sms.setSendSmsType("4");
sms.setSendSmsTypeRefine("0");
sms.setIdentifier(UUID.fastUUID().toString());
sms.setAreaCode("43");
R r = remoteSmsGroupSendRecordService.sendSMS1(JSONUtil.toJsonStr(sms), SecurityConstants.FROM_IN);
if(r.getMsg().contains("成功")){
//短信发送成功
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.opsForValue().set(CacheConstants.MOBILE_CODE_KEY + phone, c
, 60L, TimeUnit.SECONDS);
return R.ok(Boolean.TRUE,"短信发送成功,请注意查收!");
}
log.error(r.getMsg());
return R.ok(Boolean.FALSE,"发送失败,请重试!");
}
Controller:
@ApiOperation(value = "短信发送接口", notes = "短信发送接口")
//@SysLog("短信发送接口" )
@PostMapping("/sendSMS1")
@Inner(value = false)
public R sendSMS1(@RequestBody String jsonStr){
try {
// CreatarUser user = SecurityUtils.getUser();
// if(user == null){
// return new R(400, "未获取到登录信息!", "");
// }
JSonObject json = JSONObject.parseObject(jsonStr);
String mobiles = json.getString("receive");
String smsContent = json.getString("smsContent");
String sendNum = json.getString("sendNum");
String sendAisle = json.getString("sendAisle");
String sendSmsType = json.getString("sendSmsType");
String sendSmsTypeRefine = json.getString("sendSmsTypeRefine");
String identifier = json.getString("identifier");
String areaCode = json.getString("areaCode");
if (mobiles != null && !mobiles.isEmpty()) {
if (smsContent != null && !smsContent.isEmpty()) {
//检验手机号正确性
Map map = SmsClientUtil.checkSendParam(mobiles);
if ((boolean) map.get("success")) {
Integer sendcount = 1;
if (sendNum != null && !sendNum.isEmpty()) {
if (!SmsClientUtil.isNumeric(sendNum)) {
return new R(400, "请求参数错误,sendNum发送次数不是整数类型。", "");
}
if (Integer.parseInt(sendNum) > 10) {
return new R(400, "请求参数错误,sendNum发送次数最大不超过10。", "");
}
sendcount = Integer.parseInt(sendNum);
}
String[] receiveArray = (String[]) map.get("receiveArray");
String[] mobilesArray = (String[]) map.get("mobilesArray");
if (sendAisle != null && !sendAisle.isEmpty()) {
//1 代表移动接口 2 代表12379接口
if (sendAisle.equals("2")) {
//执行12379接口发送短信
return personnelGroupService.sendEMS(null, mobiles, receiveArray, mobilesArray, smsContent, sendcount, sendSmsType, sendAisle, identifier, sendSmsTypeRefine, areaCode);
} else {
//执行移动运营商发送短信接口
return smsGroupSendRecordService.sendSMS(null, mobiles, receiveArray, mobilesArray, smsContent, sendcount, sendSmsType, sendAisle, identifier, sendSmsTypeRefine, areaCode);
}
} else {
return new R(400, "请求参数错误,sendAisle发送渠道为空。", "");
}
} else {
return new R(400, map.get("message").toString(), "");
}
} else {
return new R(400, "请求参数错误,smsContent短信内容为空。", "");
}
} else {
return new R(400, "请求参数错误,mobiles手机号为空。", "");
}
} catch (Exception e) {
e.printStackTrace();
return new R(500, "执行时发生异常!", "");
}
}
lmpl:发送短信 12379短信接口
@Override
public R sendEMS(CreatarUser user, String receive, String[] receiveArray, String[] mobilesArray, String smsContent, Integer sendNum,String sendSmsType,String sendAisle,String identifier,String sendSmsTypeRefine,String areaCode) {
List smsGroupSendRecordList = new ArrayList();
List smsSingleSendRecordList = new ArrayList();
//返回状态码
Integer code = 400;
try {
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sf3 = new SimpleDateFormat("MM月dd日HH时mm分");
Integer count = 1;
while (sendNum>=count) {
count++;
SmsGroupSendRecord smsGroupSendRecord = new SmsGroupSendRecord();
//生成请求消息批次号
String msgNo = "HNDZ_BECS_ZRZY_"+sf.format(new Date())+Math.round((Math.random()+1) * 1000);
smsGroupSendRecord.setSendTime(new Date());
String nickName="未获取到发送人";
if(null !=user){
nickName = user.getNickname();
}
smsGroupSendRecord.setAreaCode(areaCode);
smsGroupSendRecord.setSendPeople(nickName);
smsGroupSendRecord.setSendContent(smsContent);
smsGroupSendRecord.setSendNum(mobilesArray.length);
smsGroupSendRecord.setAcceptPeople(receive);
smsGroupSendRecord.setAcceptPeopleNum(mobilesArray.length);
smsGroupSendRecord.setMessageNum(msgNo);
smsGroupSendRecord.setSendAisle(sendAisle);
smsGroupSendRecord.setSendSmsType(sendSmsType);
smsGroupSendRecord.setIdentifier(identifier);
//接口调用次数
smsGroupSendRecord.setServiceNumber(1);
//发送状态默认为发送失败 0:发送中 1:发送成功 2:发送失败';
smsGroupSendRecord.setSendStatus("2");
//细化短信类型
smsGroupSendRecord.setSendSmsTypeRefine(sendSmsTypeRefine);
//拼接短信接口参数
RequestEMSParamVO requestEMSParamVO = new RequestEMSParamVO();
requestEMSParamVO.setPlatformId("");
requestEMSParamVO.setIdentifier(msgNo);
requestEMSParamVO.setSender("地质灾害防治技术部");
requestEMSParamVO.setSenderCode("430000000000_333");
requestEMSParamVO.setDelegateCode("");
Date sendTime = new Date();
requestEMSParamVO.setSendTime(sf2.format(sendTime));
requestEMSParamVO.setExpires(sf2.format(dateRoll(new Date(), Calendar.HOUR, 4)));
requestEMSParamVO.setMsgType("alert");
requestEMSParamVO.setReferences("");
requestEMSParamVO.setAlmType(10);
requestEMSParamVO.setChannelPlatformId("");
requestEMSParamVO.setIsRecord(0);
//请求正式预警和测试预警
requestEMSParamVO.setEarlyType(emsClientVo.getEarlyType());
requestEMSParamVO.setNextProcess(0);
List paramCodeVoList = new ArrayList<>();
StringBuffer areaDescBu = new StringBuffer();
StringBuffer geoCodeBu = new StringBuffer();
//循环数组长度 查询组id 拼接请求参数
for (int i=0;i0){
// areacode+="0";
// a--;
// }
// }
// geoCodeBu.append(areacode+",");
//area.setAreaCode(areacode);
//默认为湖南省,因为造数据都是湖南省
area.setAreaCode("430000000000");
}
String dizhizaihai = "地质灾害";
if(sendSmsTypeRefine!=null && sendSmsTypeRefine.equals("0")){
dizhizaihai += "综合";
}else if(sendSmsTypeRefine.equals("1")){
dizhizaihai += "红色";
}else if(sendSmsTypeRefine.equals("2")){
dizhizaihai += "橙色";
}else if(sendSmsTypeRefine.equals("3")){
dizhizaihai += "黄色";
}else if(sendSmsTypeRefine.equals("4")){
dizhizaihai += "蓝色";
}
area.setMessage(smsContent+dizhizaihai+",发布日期:"+sf3.format(sendTime));
List strList = new ArrayList<>();
strList.add(personnelGroup.getGroupId());
area.setGroupIds(strList);
paramCodeVo.setAera(area);
paramCodeVoList.add(paramCodeVo);
}
requestEMSParamVO.setParamCodeVo(paramCodeVoList);
ParamInfoVo ParamInfoVo = new ParamInfoVo();
ParamInfoVo.setEventType("11D99");
if(sendSmsTypeRefine!=null && sendSmsTypeRefine.equals("0")){
ParamInfoVo.setSeverity("GREEN");
}else if(sendSmsTypeRefine.equals("1")){
ParamInfoVo.setSeverity("RED");
}else if(sendSmsTypeRefine.equals("2")){
ParamInfoVo.setSeverity("ORANGE");
}else if(sendSmsTypeRefine.equals("3")){
ParamInfoVo.setSeverity("YELLOW");
}else if(sendSmsTypeRefine.equals("4")){
ParamInfoVo.setSeverity("BLUE");
}
ParamInfoVo.setWarningSign("1");
ParamInfoVo.setHeadline("");
ParamInfoVo.Resource resource = new ParamInfoVo.Resource();
resource.setResourceDesc("");
resource.setDigest("");
resource.setSize("");
ParamInfoVo.setResource(resource);
ParamInfoVo.Areas Areas = new ParamInfoVo.Areas();
// Areas.setAreaDesc(areaDescBu.toString().substring(0, areaDescBu.toString().length() -1));
// Areas.setGeoCode(geoCodeBu.toString().substring(0, geoCodeBu.toString().length() -1));
//默认为湖南省,因为造数据都是湖南省
Areas.setAreaDesc("湖南省");
Areas.setGeoCode("430000000000");
ParamInfoVo.setAreas(Areas);
requestEMSParamVO.setParamInfoVo(ParamInfoVo);
String xmlStr = JaxbUtil.convertToXml(requestEMSParamVO);
StringBuffer sbff = new StringBuffer();
sbff.append("loginUser="+emsClientVo.getEmsUser()+"&");
// 密码规则: MD5(密钥+MD5(原始密码))
String sn = emsClientVo.getEmsSecretKey()+DigestUtils.md5DigestAsHex(emsClientVo.getEmsPwd().getBytes()).toUpperCase();
sbff.append("password="+ DigestUtils.md5DigestAsHex(sn.getBytes()).toUpperCase() +"&");
sbff.append("special=1&xmlStr=");
sbff.append(xmlStr);
String reqStr = sbff.toString().replaceAll("standalone="yes"","");
System.out.println("发送12379短信参数:"+reqStr);
//执行发送接口
String resultStr = HttpRequestUtil.sendPost(emsClientVo.getEmsRequestUrl(),reqStr);
JSonObject resultObj = JSONObject.parseObject(resultStr);
if (resultObj.getString("status").equals("0") || resultObj.getString("status").equals("2")) {
code = 200;
smsGroupSendRecord.setSendStatus("0");
//smsGroupSendRecord.setSendSuccessNum(mobilesArray.length);
smsGroupSendRecord.setErrorMessage(resultObj.getString("message"));
} else{
code = 500;
smsGroupSendRecord.setSendStatus("2");
smsGroupSendRecord.setErrorMessage(resultObj.getString("message"));
}
//执行添加群体短信
smsGroupSendRecordList.add(smsGroupSendRecord);
//执行添加单个短信
for (int i=0;i groupList = smsGroupSendRecordMapper.selectSmsGroupSendRecordList(group);
if(groupList!=null && groupList.size() > 0){
for (SmsGroupSendRecord groupObj : groupList) {
//调用次数+1
groupObj.setServiceNumber(groupObj.getServiceNumber()+1);
//本地调用消息批次号 覆盖老的
groupObj.setMessageNum(smsGroupSendRecordList.get(0).getMessageNum());
//更新发送状态 和发送时间
groupObj.setSendStatus(smsGroupSendRecordList.get(0).getSendStatus());
groupObj.setSendTime(smsGroupSendRecordList.get(0).getSendTime());
groupObj.setErrorMessage(smsGroupSendRecordList.get(0).getErrorMessage());
smsGroupSendRecordMapper.updateById(groupObj);
}
SmsSingleSendRecord single = new SmsSingleSendRecord();
List singleList = smsSingleSendRecordMapper.selectSmsSingleSendRecordList(single);
for (SmsSingleSendRecord singleSendRecord : singleList) {
singleSendRecord.setMessageNum(smsGroupSendRecordList.get(0).getMessageNum());
singleSendRecord.setSendStatus("0");
singleSendRecord.setSendTime(smsGroupSendRecordList.get(0).getSendTime());
smsSingleSendRecordMapper.updateById(singleSendRecord);
}
}else{
//批量添加群体短信发送记录
if(smsGroupSendRecordList!=null && smsGroupSendRecordList.size() > 0){
smsGroupSendRecordMapper.insertBatchSomeColumn(smsGroupSendRecordList);
}
//批量添加单个短信发送记录
if(smsSingleSendRecordList!=null && smsSingleSendRecordList.size() > 0){
smsSingleSendRecordMapper.insertBatchSomeColumn(smsSingleSendRecordList);
}
}
} catch (Exception e) {
e.printStackTrace();
return new R(500,"执行时发生异常!","");
}
return new R(code,"执行成功!","");
}
短信移动接口:
@Override
public R sendSMS(CreatarUser user, String receive, String[] receiveArray, String[] mobilesArray, String smsContent, Integer sendNum,String sendSmsType,String sendAisle,String identifier,String sendSmsTypeRefine,String areaCode) {
List smsGroupSendRecordList = new ArrayList();
List smsSingleSendRecordList = new ArrayList();
//返回状态码
Integer code = 400;
try {
Integer count = 1;
while (sendNum>=count) {
count++;
SmsGroupSendRecord smsGroupSendRecord = new SmsGroupSendRecord();
//生成请求消息批次号
String msgNo = UUID.randomUUID().toString().replaceAll("-", "");
smsGroupSendRecord.setSendTime(new Date());
String nickName="未获取到发送人";
if(null !=user){
nickName = user.getNickname();
}
smsGroupSendRecord.setAreaCode(areaCode);
smsGroupSendRecord.setSendPeople(nickName);
smsGroupSendRecord.setSendContent(smsContent);
smsGroupSendRecord.setSendNum(mobilesArray.length);
smsGroupSendRecord.setAcceptPeople(receive);
smsGroupSendRecord.setAcceptPeopleNum(mobilesArray.length);
smsGroupSendRecord.setMessageNum(msgNo);
smsGroupSendRecord.setSendAisle(sendAisle);
smsGroupSendRecord.setSendSmsType(sendSmsType);
smsGroupSendRecord.setIdentifier(identifier);
//发送状态默认为失败
smsGroupSendRecord.setSendStatus("2");
//细化短信类型
smsGroupSendRecord.setSendSmsTypeRefine(sendSmsTypeRefine);
//执行发送接口
int rt = SmsClientUtil.client.sendDSMS(mobilesArray, smsContent, "", 1, smsClientUtil.getSmsClientSign(), msgNo, true);
if (rt == 101) {
smsGroupSendRecord.setErrorMessage("请求smsContent 短信内容为空。");
} else if (rt == 102) {
smsGroupSendRecord.setErrorMessage("请求mobiles 手机号为空。");
} else if (rt == 103 || rt == 104) {
smsGroupSendRecord.setErrorMessage("请求mobiles 手机号为空。");
} else if (rt == 105) {
smsGroupSendRecord.setErrorMessage("请求短信接口,未进行身份认证或认证失败。");
} else if (rt == 106) {
smsGroupSendRecord.setErrorMessage("请求短信接口,签名为空。");
} else if (rt == 107) {
smsGroupSendRecord.setErrorMessage("请求短信接口,其它错误。");
} else if (rt == 109) {
smsGroupSendRecord.setErrorMessage("mobiles含有重复号码。");
} else if (rt == 110) {
smsGroupSendRecord.setErrorMessage("mobiles中号码数量超限(>5000),应≤5000。");
} else if (rt == 112) {
code = 500;
smsGroupSendRecord.setErrorMessage("线程池忙。");
} else if (rt == 1) {
code = 200;
smsGroupSendRecord.setSendStatus("1");
smsGroupSendRecord.setSendSuccessNum(mobilesArray.length);
smsGroupSendRecord.setErrorMessage("执行成功!");
} else {
code = 500;
smsGroupSendRecord.setErrorMessage("请求短信接口,发生未知错误!");
}
//执行添加群体短信
smsGroupSendRecordList.add(smsGroupSendRecord);
//执行添加单个短信
for (int i=0;i groupList = smsGroupSendRecordMapper.selectSmsGroupSendRecordList(group);
if(groupList!=null && groupList.size() > 0){
for (SmsGroupSendRecord groupObj : groupList) {
//调用次数+1
groupObj.setServiceNumber(groupObj.getServiceNumber()+1);
//本地调用消息批次号 覆盖老的
groupObj.setMessageNum(smsGroupSendRecordList.get(0).getMessageNum());
//更新发送状态 和发送时间
groupObj.setSendStatus(smsGroupSendRecordList.get(0).getSendStatus());
groupObj.setSendTime(smsGroupSendRecordList.get(0).getSendTime());
groupObj.setErrorMessage(smsGroupSendRecordList.get(0).getErrorMessage());
smsGroupSendRecordMapper.updateById(groupObj);
}
SmsSingleSendRecord single = new SmsSingleSendRecord();
single.setMessageNum(identifier);
List singleList = smsSingleSendRecordMapper.selectSmsSingleSendRecordList(single);
if(singleList!=null && singleList.size() > 0){
for (SmsSingleSendRecord singleSendRecord : singleList) {
singleSendRecord.setMessageNum(smsGroupSendRecordList.get(0).getMessageNum());
singleSendRecord.setSendStatus("0");
singleSendRecord.setSendTime(smsGroupSendRecordList.get(0).getSendTime());
smsSingleSendRecordMapper.updateById(singleSendRecord);
}
}
}else{
//批量添加群体短信发送记录
if (smsGroupSendRecordList != null && smsGroupSendRecordList.size() > 0) {
smsGroupSendRecordMapper.insertBatchSomeColumn(smsGroupSendRecordList);
}
//批量添加单个短信发送记录
if (smsSingleSendRecordList != null && smsSingleSendRecordList.size() > 0) {
smsSingleSendRecordMapper.insertBatchSomeColumn(smsSingleSendRecordList);
}
}
} catch (Exception e) {
e.printStackTrace();
return new R(500,"执行时发生异常!","");
}
return new R(code,"执行成功!","");
}
SmsClientUtil:
package com.creatar.creatar.opms.utils;
import com.mascloud.sdkclient.Client;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
@Data
public class SmsClientUtil implements ApplicationRunner {
@Value("${sms.client.url}")
public String smsClientUrl;
@Value("${sms.client.user}")
public String smsClientUser;
@Value("${sms.client.pwd}")
public String smsClientPwd;
@Value("${sms.client.ecname}")
public String smsClientEcname;
@Value("${sms.client.sign}")
public String smsClientSign;
@Value("${sms.client.runinit}")
public String smsRunInit;
public static Client client = Client.getInstance() ;
@Override
public void run(ApplicationArguments args) throws Exception {
if(smsRunInit!=null && "true".equals(smsRunInit)){
// 短信初始化
boolean isLoggedin = client.login( smsClientUrl, smsClientUser, smsClientPwd, smsClientEcname );
if( isLoggedin ) {
System.out.println( "SMS Login successed" );
} else {
System.out.println( "SMS Login failed" );
}
}
}
public static Map checkSendParam(String receives){
Map result = new HashMap(0);
try {
List mobilesList = new ArrayList();
String[] receiveArray = receives.split(";");
boolean suss = true;
for (String s : receiveArray) {
String[] receive = s.split(":");
suss = PhoneUtil.isPhoneLegal(receive[1]);
mobilesList.add(receive[1]);
if(!suss){
result.put("success",suss);
result.put("message","参数错误,手机号:"+s+"不是正确的手机号。");
}
}
result.put("receiveArray",receiveArray);
result.put("mobilesArray",mobilesList.toArray(new String[mobilesList.size()]));
result.put("success",suss);
}catch (Exception e) {
e.printStackTrace();
result.put("success",false);
result.put("message","执行时发生异常!");
}
return result;
}
public static boolean isNumeric(String str){
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
}
bootstrap.yml 文件:
server:
port: 4003
spring:
application:
name: @artifactId@
cloud:
nacos:
discovery:
server-addr: ${NACOS_HOST:creatar-register}:${NACOS_PORT:8848}
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
file-extension: yml
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
autoconfigure:
exclude: org.springframework.cloud.gateway.config.GatewayAutoConfiguration,org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration
profiles:
active: @profiles.active@
#移动短信接口初始化参数
sms:
client:
url: http://112.35.4.197:15000
user: hndzc
pwd: Pwd123456.
ecname: 湖南省自然资源事务中心.
sign: WKhO0EbLU
runinit: false
#EMS 12379 短信接口
ems:
client:
earlytype: 2 #接口请求 1为正式预警,2为测试预警
user: GtSysJk
pwd: GtSysJk123
secretkey: GTZYT
request:
url: http://10.111.127.147:8088/hnems/thirdpart/almsendUserType
receipt:
url: http://10.111.127.147:8088/hnems/filing/findSendUserReturn
syngroupurl: http://10.111.127.147:8088/hnems/usertype/add
synuserurl: http://10.111.127.147:8088/hnems/users/update
#网站抓取数据
website:
meteorological:
imgurl: http://www.nmc.cn/publish/weatherperday/index.htm
videourl: http://113.247.231.116/c157/index.html



