Commit e3f29ac5 authored by fangxinjiang's avatar fangxinjiang

MVP1.2:短信发送工具

parent b7c0fb36
......@@ -104,5 +104,11 @@
<version>3.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.30</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
package com.yifu.cloud.plus.v1.yifu.common.core.util.alisms;
import lombok.Data;
/**
* @program: master
* @description: 阿里短信服务返回结果
* @author: fxj
* @create: 2022-12-12
**/
@Data
public class AliSmsResult {
/**
* 成功的code
* @Author fxj
* @Date 2022-12-12
**/
public static final String SUCCESS_CODE = "OK";
//发送回执ID,可根据该ID在接口QuerySendDetails中查询具体的发送状态。
private String bizId;
/**
* 请求状态码。
* 返回OK代表请求成功。
* 其他错误码详见错误码列表。
**/
private String code;
//状态码的描述。
private String message;
//请求ID。
private String requestId;
}
package com.yifu.cloud.plus.v1.yifu.common.core.util.alisms;
import lombok.Data;
/**
* 节日祝福 参数封装对象
* @Author fxj
* @Date 2021-05-07
* @return
**/
@Data
public class ParamVo {
private String name;
private String zhengjian;
private String time;
}
package com.yifu.cloud.plus.v1.yifu.common.core.util.alisms;
import lombok.Data;
import java.io.Serializable;
@Data
public class QuerySendResult implements Serializable {
private int totalCount;
private String message;
private String requestId;
private String code;
private SmsSendDetailDTOs smsSendDetailDTOs;
}
package com.yifu.cloud.plus.v1.yifu.common.core.util.alisms;
import lombok.Data;
import java.io.Serializable;
/**
* 短信发送回执结果
* @Author fxj
* @Date 2021-05-08
* @return
**/
@Data
public class SmsSendDetailDTO implements Serializable {
private String sendDate;
private String outId;
/**
* 短信发送状态,包括:
* 1:等待回执。
* 2:发送失败。
* 3:发送成功。
**/
private Integer sendStatus;
private String receiveDate;
private String errCode;
private String templateCode;
private String content;
private String phoneNum;
}
package com.yifu.cloud.plus.v1.yifu.common.core.util.alisms;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class SmsSendDetailDTOs implements Serializable {
private List<SmsSendDetailDTO> smsSendDetailDTO;
}
package com.yifu.cloud.plus.v1.yifu.common.core.util.alisms;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
/**
* @program: master
* @description: 阿里云短信工具
* @author: pwang
* @create: 2020-08-24 11:44
**/
@Slf4j
public class SmsUtil {
private static final String ACCESS_KEY_ID = "LTAI4G1h6voBYGPCV4RNmqMo";
private static final String ACCESS_KEY_SECRET = "1e4TWryIdScpsIqPxXZynGNMASdCkl";
private static final String ERROR_INFO = "调取阿里云服务异常";
private SmsUtil() { throw new IllegalStateException("Utility class");}
/**
* 短信发送方法
* @Author fxj
* @Date 2022-12-12
* @param phone
* @param code
* @param signName
* @return 正确结果{"Message":"OK","RequestId":"38FA1BE1-52C1-4833-9211-AD55CF264988","BizId":"342207398251524244^0","Code":"OK"}
**/
public static AliSmsResult sendSms(String phone,String code,String signName) {
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
IAcsClient client = new DefaultAcsClient(profile);
JSONObject exceptionResult = new JSONObject();
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("dysmsapi.aliyuncs.com");
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", phone);
request.putQueryParameter("SignName", signName);
request.putQueryParameter("TemplateCode", "SMS_200465275");
request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
try {
CommonResponse response = client.getCommonResponse(request);
return JSON.parseObject(response.getData(),AliSmsResult.class);
} catch (Exception e) {
log.error(ERROR_INFO,e);
exceptionResult.put("Message", ERROR_INFO);
exceptionResult.put("Code", "ERROR");
return exceptionResult.toJavaObject(AliSmsResult.class);
}
}
/**
* 发送皖信人力云的短信方法
* @Author fxj
* @Date 2022-12-12
* @param phone
* @param code
* @return 错误示例 {"Message":"模板不合法(不存在或被拉黑)","RequestId":"D7923A5B-AE30-481C-84D4-56BB0F230848","Code":"isv.SMS_TEMPLATE_ILLEGAL"}
**/
public static AliSmsResult sendSms(String phone,String code) {
return sendSms(phone,code,"皖信人力云");
}
}
package com.yifu.cloud.plus.v1.yifu.common.core.util.alisms;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import java.util.ArrayList;
import java.util.List;
public class YiFuSmsTookit {
private YiFuSmsTookit() { throw new IllegalStateException("Utility class");}
/**
* 批量发送提前校验或封装签名
* @Author fxj
* @Date 2021-05-07
* @param phones
* @param templateParams
* @param signName
* @param templateCode
* @return
**/
public static AliSmsResult sendBatchSms(List<String> phones,
List<String> templateParams,
String signName,
String templateCode){
if (null == phones || null == templateParams){
return getSmsResult(CommonConstants.FAIL.toString(),"电话和变量参数不可为空!");
}
if (phones.size() != templateParams.size()){
return getSmsResult(CommonConstants.FAIL.toString(),"电话号码与变量参数数据不一致!");
}
List<String> signNames= new ArrayList<>();
List<ParamVo> templateParamList = new ArrayList<>();
for (int i=0;i<phones.size();i++){
signNames.add(signName);
}
ParamVo paramVo = null;
for (String param:templateParams){
paramVo = new ParamVo();
paramVo.setName(param);
templateParamList.add(paramVo);
}
return YiFuSmsUtil.sendBatchSms(phones,templateParamList, signNames,templateCode);
}
/**
* 批量发送提前校验或封装签名
* @Author fxj
* @Date 2021-05-07
* @param phones
* @param templateParamList
* @param signName
* @param templateCode
* @return
**/
public static AliSmsResult sendBusBatchSms(List<String> phones,
List<ParamVo> templateParamList,
String signName,
String templateCode){
if (null == phones || null == templateParamList){
return getSmsResult(CommonConstants.FAIL.toString(),"电话和变量参数不可为空!");
}
List<String> signNames= new ArrayList<>();
if (phones.size() != templateParamList.size()){
return getSmsResult(CommonConstants.FAIL.toString(),"电话号码与变量参数数据不一致!");
}
for (int i=0;i<phones.size();i++){
signNames.add(signName);
}
return YiFuSmsUtil.sendBatchSms(phones,templateParamList, signNames,templateCode);
}
private static AliSmsResult getSmsResult(String code,String msg){
AliSmsResult aliSmsResult = new AliSmsResult();
aliSmsResult.setCode(code);
aliSmsResult.setMessage(msg);
return aliSmsResult;
}
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("18909690612");
list.add("18909690533");
YiFuSmsUtil.querySendDetails(list,"","20210509",null,null);
}
}
package com.yifu.cloud.plus.v1.yifu.common.core.util.alisms;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @program: master
* @description: 阿里云短信工具 - 安徽易服 的阿里云短信
* @author: fang
* @create: 2021-05-07
**/
@Slf4j
public class YiFuSmsUtil {
private static final String ACCESS_KEY_ID = "LTAIDh9goA3jgpun";
private static final String ACCESS_KEY_SECRET = "eN8EHPAZxglNfcUCMhAT02cy93omLa";
private static final String ERROR_INFO = "调取阿里云服务异常";
private static final String REGIONID = "cn-hangzhou";
private static final String SYS_DOMAIN = "dysmsapi.aliyuncs.com";
private static final String SYS_VERSION = "2017-05-25";
private static final String MESSAGE = "Message";
private static final String CODE = "Code";
private YiFuSmsUtil() { throw new IllegalStateException("Utility class");}
/**
* 防寒、防暑、节假日短信 单条发送
* @Author fxj
* @Date 2021-05-07
* @param phone
* @param templateParam
* @param signName
* @param templateCode
* @return
* 错误示例 {"Message":"模板不合法(不存在或被拉黑)","RequestId":"D7923A5B-AE30-481C-84D4-56BB0F230848","Code":"isv.SMS_TEMPLATE_ILLEGAL"}
* 正确结果{"Message":"OK","RequestId":"38FA1BE1-52C1-4833-9211-AD55CF264988","BizId":"342207398251524244^0","Code":"OK"}
**/
public static AliSmsResult sendSms(String phone,String templateParam,String signName,String templateCode){
DefaultProfile profile = DefaultProfile.getProfile(REGIONID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
IAcsClient client = new DefaultAcsClient(profile);
JSONObject exceptionResult = new JSONObject();
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain(SYS_DOMAIN);
request.setSysVersion(SYS_VERSION);
request.setSysAction("SendSms");
request.putQueryParameter("RegionId", REGIONID);
request.putQueryParameter("PhoneNumbers", phone);
request.putQueryParameter("SignName", signName);
request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam", "{\"name\":\""+templateParam+"\"}");
try {
CommonResponse response = client.getCommonResponse(request);
return JSON.parseObject(response.getData(),AliSmsResult.class);
} catch (Exception e) {
log.error(ERROR_INFO,e);
exceptionResult.put(MESSAGE, ERROR_INFO);
exceptionResult.put(CODE, e.getMessage());
return exceptionResult.toJavaObject(AliSmsResult.class);
}
}
/**
* 防寒、防暑、节假日短信 批量发送
* @Author fxj
* @Date 2021-05-07
* @param phones
* @param templateParams
* @param signNames
* @param templateCode
* @return
* 错误示例 {"Message":"模板不合法(不存在或被拉黑)","RequestId":"D7923A5B-AE30-481C-84D4-56BB0F230848","Code":"isv.SMS_TEMPLATE_ILLEGAL"}
* 正确结果{"Message":"OK","RequestId":"38FA1BE1-52C1-4833-9211-AD55CF264988","BizId":"342207398251524244^0","Code":"OK"}
**/
public static AliSmsResult sendBatchSms(List<String> phones,
List<ParamVo> templateParams,
List<String> signNames,
String templateCode){
DefaultProfile profile = DefaultProfile.getProfile(REGIONID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
IAcsClient client = new DefaultAcsClient(profile);
JSONObject exceptionResult = new JSONObject();
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain(SYS_DOMAIN);
request.setSysVersion(SYS_VERSION);
request.setSysAction("SendBatchSms");
request.putQueryParameter("PhoneNumberJson", JSONObject.toJSONString(phones));
request.putQueryParameter("SignNameJson", JSONObject.toJSONString(signNames));
request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParamJson", JSONObject.toJSONString(templateParams));
try {
CommonResponse response = client.getCommonResponse(request);
return JSON.parseObject(response.getData(),AliSmsResult.class);
} catch (Exception e) {
log.error(ERROR_INFO,e);
exceptionResult.put(MESSAGE, ERROR_INFO);
exceptionResult.put(CODE, e.getMessage());
return exceptionResult.toJavaObject(AliSmsResult.class);
}
}
public static QuerySendResult querySendDetail(String phone, String bizId, String sendDate, String pageSize, String curPage){
DefaultProfile profile = DefaultProfile.getProfile(REGIONID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
IAcsClient client = new DefaultAcsClient(profile);
JSONObject exceptionResult = new JSONObject();
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain(SYS_DOMAIN);
request.setSysVersion(SYS_VERSION);
request.setSysAction("QuerySendDetails");
request.putQueryParameter("PhoneNumber", phone);
request.putQueryParameter("BizId", bizId);
request.putQueryParameter("SendDate", sendDate);
request.putQueryParameter("PageSize", null==pageSize?"10":pageSize);
request.putQueryParameter("CurrentPage", null ==curPage?"1":curPage);
try {
CommonResponse response = client.getCommonResponse(request);
return JSON.parseObject(response.getData(),QuerySendResult.class);
} catch (Exception e) {
log.error(ERROR_INFO,e);
exceptionResult.put(MESSAGE, ERROR_INFO);
exceptionResult.put(CODE, e.getMessage());
return exceptionResult.toJavaObject(QuerySendResult.class);
}
}
public static Map<String,SmsSendDetailDTO> querySendDetails(List<String> phones, String bizId, String sendDate, String pageSize, String curPage){
DefaultProfile profile = DefaultProfile.getProfile(REGIONID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
IAcsClient client = new DefaultAcsClient(profile);
JSONObject exceptionResult = new JSONObject();
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain(SYS_DOMAIN);
request.setSysVersion(SYS_VERSION);
request.setSysAction("QuerySendDetails");
request.putQueryParameter("BizId", bizId);
request.putQueryParameter("SendDate", sendDate);
request.putQueryParameter("PageSize", null==pageSize?"10":pageSize);
request.putQueryParameter("CurrentPage", null ==curPage?"1":curPage);
Map<String,SmsSendDetailDTO> smsSendDetailMap = new HashMap<>();
try {
if (Common.isNotEmpty(phones)){
CommonResponse response;
QuerySendResult sendResult;
for (String phone:phones){
request.putQueryParameter("PhoneNumber", phone);
response = client.getCommonResponse(request);
sendResult =JSON.parseObject(response.getData(),QuerySendResult.class);
if (null != sendResult &&
null != sendResult.getSmsSendDetailDTOs() &&
Common.isNotNull(sendResult.getSmsSendDetailDTOs().getSmsSendDetailDTO())){
smsSendDetailMap.put(phone,sendResult.getSmsSendDetailDTOs().getSmsSendDetailDTO().get(0));
}
}
}
} catch (Exception e) {
log.error(ERROR_INFO,e);
exceptionResult.put(MESSAGE, ERROR_INFO);
exceptionResult.put(CODE, e.getMessage());
}
return smsSendDetailMap;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment