阿里云短信发送

2020-01-13  本文已影响0人  IT小池

官方文档:https://help.aliyun.com/document_detail/101414.html?spm=a2c4g.11186623.4.6.93191a81i12SLk

导包:

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

创建短信发送类 SmsUtil.java

package cn.qingcheng.consumer;

import com.alibaba.fastjson.JSON;
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.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 阿里云短信发送类
 */
@Component
public class SmsUtil {

    @Value("${accessKeyId}")
    private String accessKeyId;

    @Value("${accessKeySecret}")
    private String accessKeySecret;

    /**
     * 发送短信
     * @param phone 手机号
     * @param smsCode 模板code SMS_182541596
     * @param param 参数 code验证码 {"code":"value"}
     * @return
     */
    public boolean sendSms(String phone,String smsCode,String param){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "测试签名");// 签名名称
        request.putQueryParameter("TemplateCode", smsCode);// 模板code SMS_182541596
        request.putQueryParameter("TemplateParam", param);// {"code":"value"}
        try {

            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            Map map = JSON.parseObject(data, Map.class);
            if (map.get("Code").equals("OK")){
                return true;
            }
            // 记录失败日志
            return false;
            // return response;
        } catch (ServerException e) {
            e.printStackTrace();
            return false;
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }
}

使用:

package cn.qingcheng.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.HashMap;
import java.util.Map;

/**
 *  rabbitMQ 监听类
 */

public class SmsMessageConsumer implements MessageListener {

    @Autowired
    private SmsUtil smsUtil;
    @Value("${smsCode}")
    private String smsCode;
    @Value("${param}")
    private String param;// {"code":"value"}

    /**
     * 获取消息队列中的消息
     * @param message
     */
    @Override
    public void onMessage(Message message) {
        byte[] body = message.getBody();
        String jsonString = new String(body);
        Map<String,String> map = JSON.parseObject(jsonString, Map.class);
        // 手机号
        String phone = map.get("phone");
        // 验证码
        String code = map.get("code");

        // 阿里云短信发送
        String code1 = String.format("{\"code\":\"%s\"}",123);

        /*
        JSONObject object = new JSONObject();
        object.put("code",456);
        String code = object.toString();

        Map<String, Object> map = new HashMap<>();
        map.put("code",789);
        String code = JSON.toJSON(map).toString();
         */

        smsUtil.sendSms(phone,smsCode,code1);
    }
}
上一篇下一篇

猜你喜欢

热点阅读