package com.ctrl.aSms;
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.profile.DefaultProfile;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.JedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 发送短信
*/
public class SMS {
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
@Autowired
public static JedisUtils jedisUtils;
public static JSONObject json;
//短息accessKeyId
private static String accessKeyId;
//短信accessKeySecret
private static String accessKeySecret;
//区域ID
private static String regionId;
//域名
private static String domain;
//版本
private static String version;
//签名
private static String signName;
//验证码过期时间 单位秒
private static Integer seconds;
static {
try {
accessKeyId = Global.getConfig("sms.accessKeyId");
accessKeySecret = Global.getConfig("sms.accessKeySecret");
regionId = Global.getConfig("sms.regionId");
domain = Global.getConfig("sms.domain");
version = Global.getConfig("sms.version");
signName = Global.getConfig("sms.signName");
seconds = Integer.parseInt(Global.getConfig("sms.seconds"));
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
// 设置鉴权参数,初始化客户端
private static DefaultProfile profile = DefaultProfile.getProfile(
regionId,// 地域ID
accessKeyId,// 您的AccessKey ID
accessKeySecret);// 您的AccessKey Secret
private static IAcsClient client = new DefaultAcsClient(profile);
/**
* @Description TODO //发送短信
* @Param [templateCode, phone]
* @Return java.lang.String
* @Author wuyu
* @Date 16:12 2019/11/19
*/
public static CommonResponse sendSms(String templateCode, String phone) throws Exception {
CommonRequest request = new CommonRequest();
request.setSysDomain(domain);
request.setSysVersion(version);
request.setSysAction("SendSms");
// 接收短信的手机号码
request.putQueryParameter("PhoneNumbers", phone);
// 短信签名名称。请在控制台签名管理页面签名名称一列查看(必须是已添加、并通过审核的短信签名)。
request.putQueryParameter("SignName", signName);
// 短信模板ID
request.putQueryParameter("TemplateCode", templateCode);
// 短信模板变量对应的实际值,JSON格式。
String code = genCode();
request.putQueryParameter("TemplateParam", getJsonData("code", code).toJSONString());
CommonResponse response = null;
response = client.getCommonResponse(request);
System.out.println(response.getData());
// 设置key、value 的过期时间 0 为不过期
jedisUtils.setRedis("phone_" + phone, code, seconds);
System.out.println("===========================================>>>>>>>>>>>" + code);
return response;
}
/**
* @Description TODO //短信模板变量对应的实际值,JSON格式。
* @Param [key, code]
* @Return com.alibaba.fastjson.JSONObject
* @Author wuyu
* @Date 14:44 2019/11/19
*/
public static JSONObject getJsonData(String key, String code) {
json = new JSONObject();
json.put(key, code);
return json;
}
/**
* @Description TODO //生成6位数验证码
* @Param []
* @Return java.lang.String
* @Author wuyu
* @Date 14:45 2019/11/19
*/
public static String genCode() {
//生成
String orderNo = "1" + sdf.format(new Date()) + (1 + (int) (Math.random() * 10000)) + 530L;
String code = orderNo.substring(orderNo.length() - 6, orderNo.length());
return code;
}
}
/**
* @Description TODO //发送短信
* @Param [response, request, ctrlMemberInfo]
* @Return java.lang.String
* @Author wuyu
* @Date 17:08 2019/11/19
*/
@RequestMapping("getSMSCode")
public String getSMSCode(HttpServletResponse response, HttpServletRequest request, CtrlMemberInfo ctrlMemberInfo) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
//判断手机号不是空的 然后判断手机号的格式
if (StringUtils.isNotBlank(ctrlMemberInfo.getLoginName())) {
if (CheckPhone.isPhone(ctrlMemberInfo.getLoginName())) {
//redis中的短信未60秒 如果能获取到证明60内发送过短信
String code = JedisUtils.get("phone_" + ctrlMemberInfo.getLoginName());
if (StringUtils.isNotBlank(code)) {
RespUtils.getFallWhy(map, "60秒内不能重复发送短信");
} else {
//调用发送短信接口 registered是短信模板的编号
try {
SMS.sendSms(registered, ctrlMemberInfo.getLoginName());
} catch (Exception e) {
// e.printStackTrace();
RespUtils.getFallWhy(map, "redis没有启动");
return RespUtils.returnRespJson(response, map);
}
RespUtils.getMsgSuccess(map);
}
}else{
RespUtils.getFallWhy(map, "手机号不正确");
}
} else {
RespUtils.getRespParamException(map);
}
return RespUtils.returnRespJson(response, map);
}
来源:oschina
链接:https://my.oschina.net/u/4149060/blog/3135472