移动云mas 通过HTTP请求发送普通短信和 模板短信
【摘要】 一、短信发送配置
tnar: mobileCloud: sms: url: http://112.35.1.155:1992/sms/norsubmit ecName: 公司名称 apId: 账号 secretKey: 密码 sign: 签名
二、短信发送工具类
package com.tnar.utils; import com.alibaba.fastjso...
一、短信发送配置
-
-
-
tnar:
-
mobileCloud:
-
sms:
-
url: http://112.35.1.155:1992/sms/norsubmit
-
ecName: 公司名称
-
apId: 账号
-
secretKey: 密码
-
sign: 签名
二、短信发送工具类
-
package com.tnar.utils;
-
-
import com.alibaba.fastjson.JSON;
-
import com.alibaba.fastjson.JSONObject;
-
import com.tnar.dto.request.SendReq;
-
import com.tnar.dto.request.SendTemplateReq;
-
import com.tnar.dto.response.SendRes;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.beans.factory.annotation.Value;
-
import org.springframework.http.HttpEntity;
-
import org.springframework.http.HttpHeaders;
-
import org.springframework.http.MediaType;
-
import org.springframework.stereotype.Component;
-
import org.springframework.util.DigestUtils;
-
import org.springframework.web.client.RestTemplate;
-
-
import java.io.UnsupportedEncodingException;
-
import java.util.Base64;
-
import java.util.List;
-
-
/**
-
* @author dzx
-
* @ClassName:
-
* @Description: 移动云mas短信工具类
-
* @date 2019年05月15日 15:01:21
-
*/
-
@Component
-
public class MobileCloudSmsUtil {
-
-
@Value("${tnar.mobileCloud.sms.url}")
-
private String url;
-
-
@Value("${tnar.mobileCloud.sms.ecName}")
-
private String ecName;
-
@Value("${tnar.mobileCloud.sms.apId}")
-
private String apId;
-
@Value("${tnar.mobileCloud.sms.secretKey}")
-
private String secretKey;
-
@Value("${tnar.mobileCloud.sms.sign}")
-
private String sign;
-
-
private String addSerial = "";
-
-
@Autowired
-
private RestTemplate restTemplate;
-
-
-
/**
-
* 发送普通短信
-
*
-
* @param mobiles 多个手机号用逗号隔开
-
* @param content 短信内容
-
* @return
-
* @throws UnsupportedEncodingException
-
*/
-
public SendRes sendOrdinarySms(String mobiles, String content) throws UnsupportedEncodingException {
-
HttpHeaders httpHeaders = new HttpHeaders();
-
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
-
SendReq sendReq = new SendReq();
-
sendReq.setEcName(ecName).setApId(apId).setSecretKey(secretKey).setMobiles(mobiles).setContent(content).
-
setSign(sign).setAddSerial(addSerial);
-
-
StringBuffer stringBuffer = new StringBuffer();
-
stringBuffer.append(sendReq.getEcName())
-
.append(sendReq.getApId())
-
.append(sendReq.getSecretKey())
-
.append(sendReq.getMobiles())
-
.append(sendReq.getContent())
-
.append(sendReq.getSign())
-
.append(sendReq.getAddSerial());
-
-
sendReq.setMac(DigestUtils.md5DigestAsHex(stringBuffer.toString().getBytes()).toLowerCase());
-
String jsonText = JSON.toJSONString(sendReq);
-
String body = Base64.getEncoder().encodeToString(jsonText.getBytes("UTF-8"));
-
HttpEntity<String> httpEntity = new HttpEntity<String>(body, httpHeaders);
-
JSONObject result = restTemplate.postForObject(url, httpEntity, JSONObject.class);
-
return result.toJavaObject(SendRes.class);
-
}
-
-
-
/**
-
* 发送模板短信
-
*
-
* @param mobiles 手机号
-
* @param templateId 模板id
-
* @param params 参数列表
-
* @return
-
* @throws UnsupportedEncodingException
-
*/
-
public SendRes sendTemplateSms(String mobiles, String templateId, List<String> params) throws UnsupportedEncodingException {
-
HttpHeaders httpHeaders = new HttpHeaders();
-
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
-
-
SendTemplateReq sendTemplateReq = new SendTemplateReq();
-
sendTemplateReq.setEcName(ecName).setApId(apId).setSecretKey(secretKey).setMobiles(mobiles)
-
.setSign(sign).setAddSerial(addSerial)
-
.setTemplateId(templateId)
-
.setParams(JSON.toJSONString(params.toArray()));
-
-
StringBuffer stringBuffer = new StringBuffer();
-
stringBuffer.append(sendTemplateReq.getEcName())
-
.append(sendTemplateReq.getApId())
-
.append(sendTemplateReq.getSecretKey())
-
.append(sendTemplateReq.getTemplateId())
-
.append(sendTemplateReq.getMobiles())
-
.append(sendTemplateReq.getParams())
-
.append(sendTemplateReq.getSign())
-
.append(sendTemplateReq.getAddSerial());
-
-
sendTemplateReq.setMac(DigestUtils.md5DigestAsHex(stringBuffer.toString().getBytes()).toLowerCase());
-
String jsonText = JSON.toJSONString(sendTemplateReq);
-
String body = Base64.getEncoder().encodeToString(jsonText.getBytes("UTF-8"));
-
HttpEntity<String> httpEntity = new HttpEntity<String>(body, httpHeaders);
-
JSONObject result = restTemplate.postForObject(url, httpEntity, JSONObject.class);
-
return result.toJavaObject(SendRes.class);
-
}
-
}
文章来源: blog.csdn.net,作者:血煞风雨城2018,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_31905135/article/details/90369489
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)