123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package caimei.utils;
- import com.aliyuncs.DefaultAcsClient;
- import com.aliyuncs.IAcsClient;
- import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
- import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
- import com.aliyuncs.exceptions.ClientException;
- import com.aliyuncs.http.MethodType;
- import com.aliyuncs.profile.DefaultProfile;
- import com.aliyuncs.profile.IClientProfile;
- import lombok.extern.slf4j.Slf4j;
- /**
- * 阿里云短信Sms
- *
- * @author : Charles
- * @date : 2021/3/5
- */
- @Slf4j
- public class AliyunSmsUtil {
- /** 短信API产品名称(短信产品名固定,无需修改) */
- private static final String PRODUCT = "Dysmsapi";
- /** 短信API产品域名(接口地址固定,无需修改) */
- private static final String DOMAIN = "dysmsapi.aliyuncs.com";
- /** 开发者自己的AK(在阿里云访问控制台寻找) */
- private static final String ACCESS_KEY_ID = "LTAI4GBL3o4YkWnbKYgf2Xia";
- private static final String ACCESS_KEY_SECRET = "dBjAXqbYiEPP6Ukuk2ZsXQeET7FVkK";
- /**
- * 可登录阿里云查看和定义短信模板:https://dysms.console.aliyun.com/dysms.htm#/domestic/text/template
- */
- public static boolean sendSms(String mobile, Integer type, String templateParam) {
- String templateCode = null;
- String signName = "采美";
- if (null == type || type<0) {
- return false;
- } else if (type == 0) {
- // 模版内容: 验证码${code},您正在尝试变更重要信息,请妥善保管账户信息。
- templateCode = "SMS_205240539";
- } else if (type == 1) {
- // 模版内容: 验证码${code},您正在尝试修改登录密码,请妥善保管账户信息。
- templateCode = "SMS_205240540";
- } else if (type == 2) {
- // 模版内容: 验证码${code},您正在注册成为新用户,感谢您的支持!
- templateCode = "SMS_205240541";
- } else if (type == 3) {
- // 模版内容: 验证码${code},您正尝试异地登录,若非本人操作,请勿泄露。
- templateCode = "SMS_205240542";
- } else if (type == 4) {
- // 模版内容: 验证码${code},您正在登录,若非本人操作,请勿泄露。
- templateCode = "SMS_205240543";
- } else if (type == 5) {
- // 模版内容: 验证码${code},您正在进行身份验证,打死不要告诉别人哦!
- templateCode = "SMS_205240544";
- } else if (type == 6) {
- // 模版内容: 欢迎成为采美机构用户,您的登录账号为:${name},初始密码为:cm${content},您可使用该账号密码登录采美365网和“采美采购商城”小程序。
- templateCode = "SMS_215122672";
- } else if (type == 7) {
- // 模版内容: 您"呵呵商城"小程序的验证码为:${code},验证码 5 分钟内有效。
- templateCode = "SMS_215334982";
- signName = "采美365";
- } else if (type == 8) {
- // 模版内容: 欢迎注册采美365网,您的短信验证码为:${code},该验证码 5 分钟内有效,请勿泄漏于他人。
- templateCode = "SMS_205435882";
- } else if (type == 9) {
- // 模版内容: 欢迎注册采美365网供应商账号,您的短信验证码为:${code},该验证码 5 分钟内有效,请勿泄漏于他人。
- templateCode = "SMS_205445750";
- } else if (type == 10) {
- // 模版内容: 注册成功!您可以通过手机号${name}登录。
- templateCode = "SMS_205445760";
- } else if (type == 11) {
- // 模版内容: 欢迎成为${name}的运营人员,您的邀请码为${code}。您可使用以下两种方式激活您的身份:1. 您可在微信搜索“采美采购商城”小程序,使用邀请码登录并绑定微信;2. 进入“采美采购商城”小程序后,使用邀请码登录并绑定微信。绑定微信后,您可通过微信授权直接登录“采美采购商城”小程序或微信扫码直接登录采美365网站。
- templateCode = "SMS_205445789";
- } else if (type == 12) {
- // 模版内容: 欢迎成为机构运营人员,您的验证码为${code}。运营人员可通过微信授权直接登录采美365网。
- templateCode = "SMS_205435899";
- } else if (type == 13) {
- // 模版内容: 您正在更换联系人手机号,您的验证码为:${code}。
- templateCode = "SMS_205435893";
- } else if (type == 14) {
- // 模版内容: 您正品联盟登录密码为:${password}。
- templateCode = "SMS_217145278";
- }
- try {
- //可自助调整超时时间
- System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
- System.setProperty("sun.net.client.defaultReadTimeout", "10000");
- //初始化acsClient,暂不支持region化
- IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
- DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
- IAcsClient acsClient = new DefaultAcsClient(profile);
- //组装请求对象
- SendSmsRequest request = new SendSmsRequest();
- //使用post提交
- request.setMethod(MethodType.POST);
- //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为国际区号+号码,如“85200000000”
- request.setPhoneNumbers(mobile);
- //必填:短信签名-可在短信控制台中找到
- request.setSignName(signName);
- //必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
- request.setTemplateCode(templateCode);
- //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为{\"name\":\"Tom\", \"code\":\"123\"}";
- request.setTemplateParam(templateParam);
- //请求失败这里会抛ClientException异常
- SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
- log.info(">阿里云短信接口返回的数据: "
- +"Code="+ sendSmsResponse.getCode()
- +",Message=" + sendSmsResponse.getMessage()
- +",RequestId=" + sendSmsResponse.getRequestId()
- +",BizId=" + sendSmsResponse.getBizId());
- //请求成功
- return sendSmsResponse.getCode() != null && "OK".equals(sendSmsResponse.getCode());
- } catch (ClientException e) {
- // log.error(e.getErrCode(), e.getErrMsg(), e.getErrorDescription());
- return false;
- }
- }
- }
|