二次封装阿里云短信 验证码 发送 查询 编辑短信

1.首先要注册阿里云,购买阿里云短信服务,拿到AccessKey ID和AccessKey Secret

链接: https://usercenter.console.aliyun.com/#/manage/ak

2.创建短信签名和模板

链接: https://dysms.console.aliyun.com/dysms.htm#/domestic/text/sign

3.使用maven导入阿里提供的SDK包

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.5.3</version>
</dependency>

4.封装好的自拟定工具类

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Base64;
import java.util.Map;

/**
 * 阿里云短信工具类
 *
 * 
 * com.aliyun
 * aliyun-java-sdk-core
 * 4.5.3
 * 
 */
public class AliYunSmsUtils {
     

    //阿里云用户AccessKey ID
    private static final String ACCESS_KEY_ID = "aaa";
    //阿里云用户AccessKey Secret
    private static final String ACCESS_SECRET = "bb";
    //阿里云短信服务器区域类型
    private static final String REGION_ID = "cn-hangzhou";
    //阿里云短信请求地址
    private static final String ALIYUN_IP = "dysmsapi.aliyuncs.com";
    //阿里云短信服务版本号
    private static final String VERSION = "2017-05-25";

    /**
     * 发送短信
     *
     * @param phoneNumbers 接收短信的手机号码
     * @param mapContent   短信内容,key是对应短信模板的参数字段,value是字段对应的内容
     * @param smsCodeType  短信模板签名和短信模板code
     * @return
     */
    public static String startSendSms(String phoneNumbers, Map<String, String> mapContent, SmsCodeType smsCodeType) {
     
        if (StringUtils.getNotNull(phoneNumbers) || mapContent.isEmpty() || smsCodeType == null) {
     
            throw new NullPointerException();
        }
        //初始化设置阿里云短信发送消息体
        CommonRequest commonRequest = getCommonRequest(SmsInterfaceType.SEND_SMS);
        //初始化阿里云短信接收结果消息体
        CommonResponse commonResponse = null;

        //初始化设置阿里云用户唯一标识和短信服务器区域类型(用来认证用户的)
        DefaultProfile defaultProfile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient iAcsClient = new DefaultAcsClient(defaultProfile);

        //设置接收短信的手机号码
        commonRequest.putQueryParameter("PhoneNumbers", phoneNumbers);
        //设置阿里云短信模板签名
        commonRequest.putQueryParameter("SignName", smsCodeType.signName);
        //设置阿里云短信模板code
        commonRequest.putQueryParameter("TemplateCode", smsCodeType.templateCode);
        //设置短信内容
        commonRequest.putQueryParameter("TemplateParam", parsingKeyValue(mapContent));
        try {
     
            //确认发送短信
            commonResponse = iAcsClient.getCommonResponse(commonRequest);
        } catch (ServerException e) {
     
            e.printStackTrace();
        } catch (ClientException e) {
     
            //参数配置校验未通过错误
            e.printStackTrace();
        }
        return commonResponse.getData();
    }

    /**
     * 批量发送短信
     *
     * @param phoneAndContentMap 接收手机号码集合,短信签名集合,短信内容集合
     * @param smsCodeType        短信模板code
     * @return
     */
    public static String startSendBatchSms(Map<Map<String, SmsCodeType>, Map<String, String>> phoneAndContentMap, SmsCodeType smsCodeType) {
     
        if (phoneAndContentMap.isEmpty() || smsCodeType == null) {
     
            throw new NullPointerException();
        }
        //初始化设置阿里云短信发送消息体
        CommonRequest commonRequest = getCommonRequest(SmsInterfaceType.BATCH_SEND_SMS);
        //初始化阿里云短信接收结果消息体
        CommonResponse commonResponse = null;

        //初始化设置阿里云用户唯一标识和短信服务器区域类型(用来认证用户的)
        DefaultProfile defaultProfile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient iAcsClient = new DefaultAcsClient(defaultProfile);

        //设置接收短信的手机号码
        String a = parsingPhone(phoneAndContentMap);
        commonRequest.putQueryParameter("PhoneNumberJson", parsingPhone(phoneAndContentMap));
        //设置阿里云短信模板签名
        String b = parsingSignName(phoneAndContentMap);
        commonRequest.putQueryParameter("SignNameJson", parsingSignName(phoneAndContentMap));
        //设置阿里云短信模板code
        commonRequest.putQueryParameter("TemplateCode", smsCodeType.templateCode);
        //设置短信内容
        String c = parsingValue(phoneAndContentMap);
        commonRequest.putQueryParameter("TemplateParamJson", parsingValue(phoneAndContentMap));
        try {
     
            //确认发送短信
            commonResponse = iAcsClient.getCommonResponse(commonRequest);
        } catch (ServerException e) {
     
            e.printStackTrace();
        } catch (ClientException e) {
     
            //参数配置校验未通过错误
            e.printStackTrace();
        }
        return commonResponse.getData();
    }

    /**
     * 查询指定日期下的指定/所有短信
     *
     * @param phoneNumbers 接收短信的手机号码
     * @param time         发送短信的时间,格式 YYYYmmDD
     * @param page         当前页
     * @param limit        每页条数
     * @param bizId        发送短信成功以后返回的唯一标识
     * @return
     */
    public static String startQuerySendDetails(String phoneNumbers, String time, int page, int limit, String bizId) {
     
        if (StringUtils.getNotNull(phoneNumbers) || StringUtils.getNotNull(time) || page < 1 || limit < 1) {
     
            throw new NullPointerException();
        }
        //初始化设置阿里云短信发送消息体
        CommonRequest commonRequest = getCommonRequest(SmsInterfaceType.QUERY_SEND_DETAILS);
        //初始化阿里云短信接收结果消息体
        CommonResponse commonResponse = null;

        //初始化设置阿里云用户唯一标识和短信服务器区域类型(用来认证用户的)
        DefaultProfile defaultProfile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient iAcsClient = new DefaultAcsClient(defaultProfile);

        //设置当前页
        commonRequest.putQueryParameter("CurrentPage", String.valueOf(page));
        //设置每页条数
        commonRequest.putQueryParameter("PageSize", String.valueOf(limit));
        //设置发送短信的时间,格式 YYYYmmDD
        commonRequest.putQueryParameter("SendDate", time);
        //设置接收短信的手机号码
        commonRequest.putQueryParameter("PhoneNumber", phoneNumbers);
        if (!StringUtils.getNotNull(bizId)) {
     
            commonRequest.putQueryParameter("BizId", bizId);
        }
        try {
     
            commonResponse = iAcsClient.getCommonResponse(commonRequest);
        } catch (ServerException e) {
     
            e.printStackTrace();
        } catch (ClientException e) {
     
            e.printStackTrace();
        }
        return commonResponse.getData();
    }

    /**
     * 申请通用短信签名
     *
     * @param signName          短信签名名称
     * @param smsSignSourceType 短信签名类型
     * @param remark            短信签名说明
     * @param file              短信签名图片(针对不同的类型传不同的图片)
     * @return
     */
    public static String startAddSmsSign(String signName, SmsSignSourceType smsSignSourceType, String remark, MultipartFile file) {
     
        if (StringUtils.getNotNull(signName) || smsSignSourceType == null || StringUtils.getNotNull(remark) || file == null) {
     
            throw new NullPointerException();
        }
        //初始化设置阿里云短信发送消息体
        CommonRequest commonRequest = getCommonRequest(SmsInterfaceType.ADD_SMS_SIGN);
        //初始化阿里云短信接收结果消息体
        CommonResponse commonResponse = null;

        //初始化设置阿里云用户唯一标识和短信服务器区域类型(用来认证用户的)
        DefaultProfile defaultProfile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient iAcsClient = new DefaultAcsClient(defaultProfile);

        //设置阿里云短信签名申请签名名称
        commonRequest.putQueryParameter("SignName", signName);
        //设置阿里云短信签名申请类型
        commonRequest.putQueryParameter("SignSource", String.valueOf(smsSignSourceType.SignSourceId));
        //设置阿里云短信签名申请说明
        commonRequest.putQueryParameter("Remark", remark);
        //设置阿里云短信签名,当前图片格式
        String str = file.getOriginalFilename();
        commonRequest.putQueryParameter("SignFileList.1.FileSuffix", str.substring(str.indexOf(".") + 1));
        try {
     
            //设置阿里云短信签名,当前图片的base64字符串
            commonRequest.putQueryParameter("SignFileList.1.FileContents", Base64.getEncoder().encodeToString(file.getBytes()));
            commonResponse = iAcsClient.getCommonResponse(commonRequest);
        } catch (ServerException e) {
     
            e.printStackTrace();
        } catch (ClientException e) {
     
            e.printStackTrace();
        } catch (IOException e) {
     
            e.printStackTrace();
        }
        return commonResponse.getData();
    }

    /**
     * 申请短信模板
     *
     * @param smsTemplateType 短信模板类型
     * @param templateName    短信模板名称
     * @param templateContent 短信模板格式内容,对应的参数用 ${name} 表示,对应发送短信时候的内容参数
     * @param remark          短信模板申请说明
     * @return
     */
    public static String startAddSmsTemplate(SmsTemplateType smsTemplateType, String templateName, String templateContent, String remark) {
     
        if (smsTemplateType == null || StringUtils.getNotNull(templateName) || StringUtils.getNotNull(templateContent) || StringUtils.getNotNull(remark)) {
     
            throw new NullPointerException();
        }
        //初始化设置阿里云短信发送消息体
        CommonRequest commonRequest = getCommonRequest(SmsInterfaceType.ADD_SMS_TEMPLATE);
        //初始化阿里云短信接收结果消息体
        CommonResponse commonResponse = null;

        //初始化设置阿里云用户唯一标识和短信服务器区域类型(用来认证用户的)
        DefaultProfile defaultProfile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient iAcsClient = new DefaultAcsClient(defaultProfile);

        //设置阿里云短信模板申请类型
        commonRequest.putQueryParameter("TemplateType", String.valueOf(smsTemplateType.TemplateTypeId));
        //设置阿里云短信模板申请名称
        commonRequest.putQueryParameter("TemplateName", templateName);
        //设置阿里云短信模板申请格式内容
        commonRequest.putQueryParameter("TemplateContent", templateContent);
        //设置阿里云短信模板申请说明
        commonRequest.putQueryParameter("Remark", remark);
        try {
     
            commonResponse = iAcsClient.getCommonResponse(commonRequest);
        } catch (ServerException e) {
     
            e.printStackTrace();
        } catch (ClientException e) {
     
            e.printStackTrace();
        }
        return commonResponse.getData();
    }

    /**
     * 删除短信签名
     *
     * @param signName 短信签名名称
     * @return
     */
    public static String startDeleteSmsSign(String signName) {
     
        if (StringUtils.getNotNull(signName)) {
     
            throw new NullPointerException();
        }
        //初始化设置阿里云短信发送消息体
        CommonRequest commonRequest = getCommonRequest(SmsInterfaceType.DELETE_SMS_SIGN);
        //初始化阿里云短信接收结果消息体
        CommonResponse commonResponse = null;

        //初始化设置阿里云用户唯一标识和短信服务器区域类型(用来认证用户的)
        DefaultProfile defaultProfile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient iAcsClient = new DefaultAcsClient(defaultProfile);

        //设置要删除的短信模板签名名称
        commonRequest.putQueryParameter("SignName", signName);
        try {
     
            commonResponse = iAcsClient.getCommonResponse(commonRequest);
        } catch (ServerException e) {
     
            e.printStackTrace();
        } catch (ClientException e) {
     
            e.printStackTrace();
        }
        return commonResponse.getData();
    }

    /**
     * 删除短信模板
     *
     * @param templateCode
     * @return
     */
    public static String startDeleteSmsTemplate(String templateCode) {
     
        if (StringUtils.getNotNull(templateCode)) {
     
            throw new NullPointerException();
        }
        //初始化设置阿里云短信发送消息体
        CommonRequest commonRequest = getCommonRequest(SmsInterfaceType.DELETE_SMS_TEMPLATE);
        //初始化阿里云短信接收结果消息体
        CommonResponse commonResponse = null;

        //初始化设置阿里云用户唯一标识和短信服务器区域类型(用来认证用户的)
        DefaultProfile defaultProfile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient iAcsClient = new DefaultAcsClient(defaultProfile);

        //设置要删除的短信模板签名名称
        commonRequest.putQueryParameter("TemplateCode", templateCode);
        try {
     
            commonResponse = iAcsClient.getCommonResponse(commonRequest);
        } catch (ServerException e) {
     
            e.printStackTrace();
        } catch (ClientException e) {
     
            e.printStackTrace();
        }
        return commonResponse.getData();
    }

    /**
     * 修改申请未通过的短信签名再次申请
     *
     * @param signName          短信签名名称(必须要是申请未通过的短信签名,不然会报错)
     * @param smsSignSourceType 短信签名类型
     * @param remark            短信签名说明
     * @param file              短信签名图片(针对不同的类型传不同的图片)
     * @return
     */
    public static String startModifySmsSign(String signName, SmsSignSourceType smsSignSourceType, String remark, MultipartFile file) {
     
        if (StringUtils.getNotNull(signName) || smsSignSourceType == null || StringUtils.getNotNull(remark) || file == null) {
     
            throw new NullPointerException();
        }
        //初始化设置阿里云短信发送消息体
        CommonRequest commonRequest = getCommonRequest(SmsInterfaceType.MODIFY_SMS_SIGN);
        //初始化阿里云短信接收结果消息体
        CommonResponse commonResponse = null;

        //初始化设置阿里云用户唯一标识和短信服务器区域类型(用来认证用户的)
        DefaultProfile defaultProfile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient iAcsClient = new DefaultAcsClient(defaultProfile);

        //设置阿里云短信签名申请签名名称
        commonRequest.putQueryParameter("SignName", signName);
        //设置阿里云短信签名申请类型
        commonRequest.putQueryParameter("SignSource", String.valueOf(smsSignSourceType.SignSourceId));
        //设置阿里云短信签名申请说明
        commonRequest.putQueryParameter("Remark", remark);
        //设置阿里云短信签名,当前图片格式
        String str = file.getOriginalFilename();
        commonRequest.putQueryParameter("SignFileList.1.FileSuffix", str.substring(str.indexOf(".") + 1));
        try {
     
            //设置阿里云短信签名,当前图片的base64字符串
            commonRequest.putQueryParameter("SignFileList.1.FileContents", Base64.getEncoder().encodeToString(file.getBytes()));
            commonResponse = iAcsClient.getCommonResponse(commonRequest);
        } catch (ServerException e) {
     
            e.printStackTrace();
        } catch (ClientException e) {
     
            e.printStackTrace();
        } catch (IOException e) {
     
            e.printStackTrace();
        }
        return commonResponse.getData();
    }

    /**
     * 修改申请未通过的短信模板再次申请
     *
     * @param smsTemplateType 短信模板类型
     * @param templateName    短信模板名称
     * @param templateCode    短信模板code(申请短信模板时生成的code,修改的时候要带上,必须要是未通过的,不然报错)
     * @param templateContent 短信模板格式内容,对应的参数用 ${name} 表示,对应发送短信时候的内容参数
     * @param remark          短信模板申请说明
     * @return
     */
    public static String startModifySmsTemplate(SmsTemplateType smsTemplateType, String templateName, String templateCode, String templateContent, String remark) {
     
        if (smsTemplateType == null || StringUtils.getNotNull(templateName) || StringUtils.getNotNull(templateCode) || StringUtils.getNotNull(templateContent) || StringUtils.getNotNull(remark)) {
     
            throw new NullPointerException();
        }
        //初始化设置阿里云短信发送消息体
        CommonRequest commonRequest = getCommonRequest(SmsInterfaceType.MODIF_SMS_TEMPLATE);
        //初始化阿里云短信接收结果消息体
        CommonResponse commonResponse = null;

        //初始化设置阿里云用户唯一标识和短信服务器区域类型(用来认证用户的)
        DefaultProfile defaultProfile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient iAcsClient = new DefaultAcsClient(defaultProfile);

        //设置阿里云短信模板申请类型
        commonRequest.putQueryParameter("TemplateType", String.valueOf(smsTemplateType.TemplateTypeId));
        //设置阿里云短信模板申请名称
        commonRequest.putQueryParameter("TemplateName", templateName);
        //设置阿里云短信模板修改再次申请code
        commonRequest.putQueryParameter("TemplateCode", templateCode);
        //设置阿里云短信模板申请格式内容
        commonRequest.putQueryParameter("TemplateContent", templateContent);
        //设置阿里云短信模板申请说明
        commonRequest.putQueryParameter("Remark", remark);
        try {
     
            commonResponse = iAcsClient.getCommonResponse(commonRequest);
        } catch (ServerException e) {
     
            e.printStackTrace();
        } catch (ClientException e) {
     
            e.printStackTrace();
        }
        return commonResponse.getData();
    }

    /**
     * 查询指定名称的短信签名
     *
     * @param signName 要查询的短信签名名称
     * @return
     */
    public static String startQuerySmsSign(String signName) {
     
        if (StringUtils.getNotNull(signName)) {
     
            throw new NullPointerException();
        }
        //初始化设置阿里云短信发送消息体
        CommonRequest commonRequest = getCommonRequest(SmsInterfaceType.QUERY_SMS_SIGN);
        //初始化阿里云短信接收结果消息体
        CommonResponse commonResponse = null;

        //初始化设置阿里云用户唯一标识和短信服务器区域类型(用来认证用户的)
        DefaultProfile defaultProfile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient iAcsClient = new DefaultAcsClient(defaultProfile);

        //设置要查询的短信签名名称
        commonRequest.putQueryParameter("SignName", signName);
        try {
     
            commonResponse = iAcsClient.getCommonResponse(commonRequest);
        } catch (ServerException e) {
     
            e.printStackTrace();
        } catch (ClientException e) {
     
            e.printStackTrace();
        }
        return commonResponse.getData();
    }

    /**
     * 查询指定code的短信模板
     *
     * @param templateCode 要查询的短信模板code
     * @return
     */
    public static String startQuerySmsTemplate(String templateCode) {
     
        if (StringUtils.getNotNull(templateCode)) {
     
            throw new NullPointerException();
        }
        //初始化设置阿里云短信发送消息体
        CommonRequest commonRequest = getCommonRequest(SmsInterfaceType.QUERY_SMS_TEMPLATE);
        //初始化阿里云短信接收结果消息体
        CommonResponse commonResponse = null;

        //初始化设置阿里云用户唯一标识和短信服务器区域类型(用来认证用户的)
        DefaultProfile defaultProfile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient iAcsClient = new DefaultAcsClient(defaultProfile);

        //设置阿里云短信消息体请求类型
        commonRequest.setSysMethod(MethodType.POST);
        //设置阿里云短信消息体接口调用ip地址
        commonRequest.setSysDomain(ALIYUN_IP);
        //设置阿里云短信服务版本
        commonRequest.setSysVersion(VERSION);
        //设置阿里云短信请求接口类型
        commonRequest.setSysAction(SmsInterfaceType.QUERY_SMS_TEMPLATE.type);
        //设置阿里云短信服务器区域类型
        commonRequest.putQueryParameter("RegionId", REGION_ID);
        //设置要查询的短信签名名称
        commonRequest.putQueryParameter("TemplateCode", templateCode);
        try {
     
            commonResponse = iAcsClient.getCommonResponse(commonRequest);
        } catch (ServerException e) {
     
            e.printStackTrace();
        } catch (ClientException e) {
     
            e.printStackTrace();
        }
        return commonResponse.getData();
    }


    /**
     * 短信请求接口类型
     */
    @Getter
    @AllArgsConstructor
    private enum SmsInterfaceType {
     
        //申请短信模板签名
        ADD_SMS_SIGN("AddSmsSign"),
        //申请短信模板
        ADD_SMS_TEMPLATE("AddSmsTemplate"),
        //删除短信模板签名
        DELETE_SMS_SIGN("DeleteSmsSign"),
        //删除短信模板
        DELETE_SMS_TEMPLATE("DeleteSmsTemplate"),
        //修改未审核通过的短信模板签名,重新申请审核
        MODIFY_SMS_SIGN("ModifySmsSign"),
        //修改未审核通过的短信模板,重新申请审核
        MODIF_SMS_TEMPLATE("ModifySmsTemplate"),
        //查询所有短信模板签名
        QUERY_SMS_SIGN("QuerySmsSign"),
        //查询所有短信模板
        QUERY_SMS_TEMPLATE("QuerySmsTemplate"),
        //查询所有已发送的短信
        QUERY_SEND_DETAILS("QuerySendDetails"),
        //发送短信
        SEND_SMS("SendSms"),
        //批量发送短信
        BATCH_SEND_SMS("SendBatchSms");

        private final String type;
    }

    /**
     * 短信签名和短信模板code
     */
    @Getter
    @AllArgsConstructor
    public enum SmsCodeType {
     
        //登录确认验证码
        CONFIRM_LOGIN("道贞财务", "SMS_189465821"), 
        //登录异常验证码
        LOGIN_ABNORMAL("道贞财务", "SMS_210350075"),
        //用户注册验证码
        USER_ADD("道贞财务", "SMS_210350074"),
        //修改密码验证码
        UPDATE_PASSWORD("道贞财务", "SMS_210350073"),
        //用户信息变更验证码
        UPDATE_CONTENT("道贞财务", "SMS_210350072"),
        //身份验证验证码
        IDENTITY_CODE("道贞财务", "SMS_210350077");

        private final String signName;

        private final String templateCode;
    }

    /**
     * 短信模板签名类型
     */
    @Getter
    @AllArgsConstructor
    public enum SmsSignSourceType {
     
        //企事业单位的全称或简称。
        ENTERPRISE(0),
        //工信部备案网站的全称或简称。
        WEB(1),
        //App应用的全称或简称。
        APP(2),
        //公众号或小程序的全称或简称。
        APPLET(3),
        //电商平台店铺名的全称或简称。
        CONSULT(4),
        //商标名的全称或简称。
        TRADEMARK(5);

        private final Integer SignSourceId;
    }

    /**
     * 短信模板类型
     */
    @Getter
    @AllArgsConstructor
    public enum SmsTemplateType {
     
        //验证码。
        CODE(0),
        //短信通知。
        notice(1),
        //推广短信。
        push(2),
        //国际/港澳台消息。
        countries(3);

        private final Integer TemplateTypeId;
    }


    /**
     * 所有请求通用参数
     *
     * @param smsInterfaceType
     * @return
     */
    private static CommonRequest getCommonRequest(SmsInterfaceType smsInterfaceType) {
     
        //初始化设置阿里云短信发送消息体
        CommonRequest commonRequest = new CommonRequest();
        //设置阿里云短信消息体请求类型
        commonRequest.setSysMethod(MethodType.POST);
        //设置阿里云短信消息体接口调用ip地址
        commonRequest.setSysDomain(ALIYUN_IP);
        //设置阿里云短信服务版本
        commonRequest.setSysVersion(VERSION);
        //设置阿里云短信请求接口类型
        commonRequest.setSysAction(smsInterfaceType.type);
        //设置阿里云短信服务器区域类型
        commonRequest.putQueryParameter("RegionId", REGION_ID);
        return commonRequest;
    }

    /**
     * 把短信内容进行排版,排成阿里云要求的格式
     *
     * @param mapContent
     * @return
     */
    private static String parsingKeyValue(Map<String, String> mapContent) {
     
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("{");
        for (String key : mapContent.keySet()) {
     
            stringBuilder.append("'" + key + "':'" + mapContent.get(key) + "',");
        }
        return stringBuilder.substring(0, stringBuilder.length() - 1) + "}";
    }

    /**
     * 把批量发送短信的接收手机号码进行排版,排成阿里云要求的格式
     *
     * @param phoneAndContentMap N个手机号码
     * @return
     */
    private static String parsingPhone(Map<Map<String, SmsCodeType>, Map<String, String>> phoneAndContentMap) {
     
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (Map<String, SmsCodeType> stringSmsCodeTypeMap : phoneAndContentMap.keySet()) {
     
            if (stringSmsCodeTypeMap.size() == 1) {
     
                for (String key : stringSmsCodeTypeMap.keySet()) {
     
                    stringBuilder.append("'" + key + "',");
                }
            } else {
     
                throw new ArrayIndexOutOfBoundsException();
            }
        }
        return stringBuilder.substring(0, stringBuilder.length() - 1) + "]";
    }

    /**
     * 把批量发送短信的短信签名名称进行排版,排成阿里云要求的格式
     *
     * @param phoneAndContentMap
     * @return
     */
    private static String parsingSignName(Map<Map<String, SmsCodeType>, Map<String, String>> phoneAndContentMap) {
     
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (Map<String, SmsCodeType> stringSmsCodeTypeMap : phoneAndContentMap.keySet()) {
     
            if (stringSmsCodeTypeMap.size() == 1) {
     
                for (SmsCodeType value : stringSmsCodeTypeMap.values()) {
     
                    stringBuilder.append("'" + value.signName + "',");
                }
            } else {
     
                throw new ArrayIndexOutOfBoundsException();
            }
        }
        return stringBuilder.substring(0, stringBuilder.length() - 1) + "]";
    }

    /**
     * 把批量发送短信的短信内容进行排版,排成阿里云要求的格式
     *
     * @param phoneAndContentMap 短信参数名和对应的内容
     * @return
     */
    private static String parsingValue(Map<Map<String, SmsCodeType>, Map<String, String>> phoneAndContentMap) {
     
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (Map<String, SmsCodeType> key : phoneAndContentMap.keySet()) {
     
            stringBuilder.append(parsingKeyValue(phoneAndContentMap.get(key)) + ",");
        }
        return stringBuilder.substring(0, stringBuilder.length() - 1) + "]";
    }

}

5.更详细的官方教程看这里

链接: https://help.aliyun.com/document_detail/59210.html?spm=a2c4g.11174283.4.1.1b442c4229MHcp
链接: https://api.aliyun.com/new?spm=a2c4g.11186623.2.4.1c5a1e08EoAftX#/?product=Dysmsapi

你可能感兴趣的:(短信,验证码,消息通知,java,阿里云)