JavaUtilS | 短信工具类 - SendSms

1.pom


        
            com.aliyun
            aliyun-java-sdk-core
            4.5.3
        

2.SendSms

package com.springboot.util;

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.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;

/**
 * 阿里云短信
 *
 * @author java
 */
public class SendSms {

    private static final Logger logger = LoggerFactory.getLogger(SendSms.class);

    /*
    * AliKey
    */
    private final static String accessKeyId = "accessKeyId";
    /*
     * AliSecret
     */
    private final static String accessSecret = "accessSecret";


    public static Integer forgetCode(String telephone){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        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",telephone);
        request.putQueryParameter("SignName","一点清柠");
        request.putQueryParameter("TemplateCode","SMS_193246329");
        int code = code();//创建验证码
        request.putQueryParameter("TemplateParam","{\"code\":\""+code+"\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);

            JSONObject data = JSON.parseObject(response.getData());
            if (data.getString("Message") != null && data.getString("Message").equalsIgnoreCase("OK")){
                return code;
            }
            return 0;
        } catch (ServerException e) {
            logger.info("找回密码:阿里云短信发送失败!");
            return 0;
        } catch (ClientException e) {
            logger.info("找回密码:平台短信发送失败!");
            return 0;
        }
    }

    /**
     * 登录验证码
     *
     * @param telephone - 手机号
     */
    public static Integer loginCode(String telephone){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        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",telephone);
        request.putQueryParameter("SignName","一点清柠");
        request.putQueryParameter("TemplateCode","SMS_193246329");
        int code = code();//创建验证码
        request.putQueryParameter("TemplateParam","{\"code\":\""+code+"\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);

            JSONObject data = JSON.parseObject(response.getData());
            if (data.getString("Message") != null && data.getString("Message").equalsIgnoreCase("OK")){
                return code;
            }
            return 0;
        } catch (ServerException e) {
            logger.info("登录:阿里云短信发送失败!");
            return 0;
        } catch (ClientException e) {
            logger.info("登录:平台短信发送失败!");
            return 0;
        }
    }

    private static Integer getInteger(String telephone) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        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",telephone);
        request.putQueryParameter("SignName","一点清柠");
        request.putQueryParameter("TemplateCode","SMS_193246329");
        int code = code();//创建验证码
        request.putQueryParameter("TemplateParam","{\"code\":\""+code+"\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);

            JSONObject data = JSON.parseObject(response.getData());
            if (data.getString("Message") != null && data.getString("Message").equalsIgnoreCase("OK")){
                return code;
            }
            return 0;
        } catch (ServerException e) {
            logger.info("注册:阿里云短信发送失败!");
            return 0;
        } catch (ClientException e) {
            logger.info("注册:平台短信发送失败!");
            return 0;
        }
    }

    /**
     * 注册验证码
     *
     * @param telephone - 手机号
     */
    public static Integer registerCode(String telephone){
        return getInteger(telephone);
    }

    /**
     * 生成六位随机码
     *
     * @return
     */
    private static Integer code(){
        return new Random().nextInt(900000) + 100000;
    }

}

 

你可能感兴趣的:(JavaUtilS)