driver-server/src/main/java/com/jwl/driver/server/util/SmsUtil.java

145 lines
4.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.jwl.driver.server.util;
import com.alibaba.fastjson.JSONObject;
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.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.jwl.driver.server.constant.Constants;
import com.jwl.driver.server.exception.BusinessException;
import jdk.vm.ci.meta.Constant;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author zcx
* 短信工具
*/
@Data
public class SmsUtil {
private static Logger logger = LoggerFactory.getLogger(SmsUtil.class);
/**
* 基础信息
*/
private static String product = "Dysmsapi";
private static String url = "dysmsapi.aliyuncs.com";
private static String appkey = "LTAI4GH1CqJtaUVpmD1oYEUZ";
private static String secret = "DFG848ea8U0QaQcdiHAYznUrCWfJzn";
private static String smsFreeSignName = "对嘛科技";
//用于存储短信验证码
private static Map<String, String> codeMap = new HashMap<>();
/**
* 发送验证码
* @param sendPhone
* @return
*/
public static String sendMessage(String sendPhone) {
String code = getMessageCode(sendPhone);
Map<String, Object> param = new HashMap<>();
param.put("code", code);
//发送短信
currencySendMessage(sendPhone, Constants.MESSAGE_TEMPLATE, JSONObject.toJSONString(param));
return code;
}
/**
* 生成短信验证码并存储
*/
public static String getMessageCode(String phone){
String code = (int)(Math.random()*999999)+ "";
if (code.length() == 5){
code = "0" + code;
}
codeMap.put(phone, code);
return code;
}
/**
* 获取6位验证码
*/
public static String getCodeByPhone(String phone){
return codeMap.get(phone);
}
/**
* 通用的发送短信的方法
*
* @param sendPhone
* 要发送的目标号码
* @param templet
* 要使用的模板
* @param jsonText
* 拼接对应模板的json格式的字符串
*/
public static String currencySendMessage(String sendPhone, String templet,
String jsonText) {
IAcsClient aliClient = createClient();
//组装请求对象
SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号。支持以逗号分隔的形式进行批量调用批量上限为20个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
request.setPhoneNumbers(sendPhone);
//必填:短信签名-可在短信控制台中找到
request.setSignName(smsFreeSignName);
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(templet);
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam(jsonText);
SendSmsResponse sendSmsResponse = null;
logger.info("发送短信参数内容:"+jsonText+"\n手机号码是"+sendPhone+"\n模板编号"+templet);
try {
sendSmsResponse = aliClient.getAcsResponse(request);
} catch (ClientException e) {
logger.error("发送短信失败:"+e.getErrMsg()+"\n"+e.getMessage());
e.printStackTrace();
throw new BusinessException("获取验证码失败");
}
if (sendSmsResponse!=null){
logger.info("message:"+sendSmsResponse.getMessage()+"\n"+
"code:"+sendSmsResponse.getCode()+"\n");
}else{
logger.info("发送短信响应为null");
throw new BusinessException("获取验证码失败");
}
return JSONObject.toJSONString(sendSmsResponse);
}
/**
* 创建客户端
* @return
*/
private static IAcsClient createClient() {
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", appkey,
secret);
try {
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, url);
} catch (ClientException e) {
e.printStackTrace();
}
IAcsClient acsClient = new DefaultAcsClient(profile);
return acsClient;
}
public static void main(String[] args) throws IOException {
Map<String, Object> param = new HashMap<>();
param.put("code", getMessageCode("18255439337"));
SmsUtil.currencySendMessage("18255439337", "SMS_198880447", JSONObject.toJSONString(param) );
}
}