package com.javaweb.platform.utils;
|
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Random;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import com.javaweb.common.utils.IdGenerate;
|
import com.javaweb.common.utils.StringUtils;
|
import com.javaweb.platform.domain.Validcode;
|
import com.javaweb.platform.mapper.ValidcodeMapper;
|
|
import com.aliyun.tea.*;
|
import com.aliyun.dysmsapi20170525.*;
|
import com.aliyun.dysmsapi20170525.models.*;
|
import com.aliyun.teaopenapi.*;
|
import com.aliyun.teaopenapi.models.*;
|
|
@Service
|
public class ValidcodeUtils {
|
|
@Autowired
|
private ValidcodeMapper validcodeMapper;
|
|
private static final String accessKeyId="LTAI5tJjstk9c2qZs43fbGx9";
|
|
private static final String accessKeySecret="sWHErgvNkain06tOBIxmyht1kMwq2H";
|
|
private static final String SignName="北京梦之岩";
|
|
private static final String SignName2="梦之岩";
|
|
private static String Regist="1";//注册用户
|
private static String Forgot ="2";//忘记密码
|
private static String Approve ="3";//通过申请
|
private static String Reject ="4";//不通过审核
|
|
private static String Approve_Hole="5";
|
private static String Reject_Hole="6";
|
|
private static int phonelength = 11;
|
|
|
/**
|
* 向手机发送验证码
|
* @param type 验证码的类型,1:注册用户,2:修改密码,3:通过审核,4:不通过审核 , 5 钻孔申请通过 6 钻孔申请不通过
|
* @param phone
|
* @param code
|
* @return 0:发送过于频繁,1:正常发送
|
* @throws Exception
|
*/
|
public int sendMesgToPhone(String type,String phone) throws Exception {
|
if(phone.length() != phonelength){
|
return 0;
|
}
|
if(hasExistsCode(phone,type))
|
{
|
return 0;
|
}
|
String code = createCode();
|
String templateParam = "{code:"+code+"}";
|
//创建客户端连接
|
com.aliyun.dysmsapi20170525.Client client = createClient();
|
SendSmsRequest sendSmsRequest = null;
|
if(type.equals(Regist)||type.equals(Forgot)){
|
|
sendSmsRequest = createSendSmsRequest(phone, "SMS_237405548", templateParam,SignName);
|
|
}else if(type.equals(Approve)){
|
|
sendSmsRequest = createSendSmsRequest(phone, "SMS_238461833", null,SignName2);
|
|
}else if(type.equals(Reject)){
|
|
sendSmsRequest = createSendSmsRequest(phone, "SMS_238466927", null,SignName2);
|
|
}else if(type.equals(Approve_Hole)){
|
|
sendSmsRequest = createSendSmsRequest(phone, "SMS_240396449", null,SignName2);
|
|
}else if(type.equals(Reject_Hole)){
|
|
sendSmsRequest = createSendSmsRequest(phone, "SMS_240376549", null,SignName2);
|
}
|
|
//发送数据
|
sendMsg(client,sendSmsRequest);
|
|
//写入数据库
|
Validcode validcode = new Validcode();
|
validcode.setId(IdGenerate.nextId());
|
validcode.setPhone(phone);
|
validcode.setType(type);
|
validcode.setCreateTime(new Date());
|
Calendar calendar = Calendar.getInstance();
|
calendar.add(Calendar.MINUTE, 5);
|
validcode.setSendTime(new Date());
|
validcode.setUntilTime(calendar.getTime());
|
validcode.setCode(code);
|
validcodeMapper.insertValidcode(validcode);
|
return 1;
|
}
|
|
private void sendMsg(com.aliyun.dysmsapi20170525.Client client, SendSmsRequest sendSmsRequest){
|
SendSmsResponse r = new SendSmsResponse();
|
// 复制代码运行请自行打印 API 的返回值
|
try {
|
r = client.sendSms(sendSmsRequest);
|
System.out.println(r.getBody().getMessage());
|
|
} catch (Exception e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
}
|
/**
|
* 验证码是否有效
|
* @param code
|
* @param phone
|
* @param type
|
* @return
|
*/
|
public boolean validCode(String code,String phone,String type) {
|
Validcode validcode = new Validcode();
|
validcode.setCode(code);
|
validcode.setPhone(phone);
|
validcode.setType(type);
|
validcode.setUntilTime(new Date());
|
List<Validcode> codeList = validcodeMapper.selectValidcodeList(validcode);
|
if(codeList.size() > 0)
|
{
|
return true;
|
}
|
return false;
|
}
|
/**
|
* 查询数据库中,最近50秒是否发送了短信
|
* @param phone
|
* @return
|
*/
|
private boolean hasExistsCode(String phone,String type) {
|
Validcode validcode = new Validcode();
|
validcode.setPhone(phone);
|
validcode.setType(type);
|
Calendar calendar = Calendar.getInstance();
|
calendar.add(Calendar.SECOND, -50);
|
validcode.setSendTime(calendar.getTime());
|
List<Validcode> exisList=validcodeMapper.selectValidcodeList(validcode);
|
if(exisList.size()>0)
|
{
|
return true;
|
}
|
return false;
|
}
|
private String createCode()
|
{
|
//生成六位随机数验证码
|
String ret = "";
|
for(int i = 0;i<6;i++)
|
{
|
Random r = new Random();
|
int j = r.nextInt(10);
|
if(i == 0 && j == 0)
|
{
|
ret += 1;
|
}
|
else
|
{
|
ret += j;
|
}
|
}
|
return ret;
|
}
|
/**
|
* 使用AK&SK初始化账号Client
|
* @param accessKeyId
|
* @param accessKeySecret
|
* @return Client
|
* @throws Exception
|
*/
|
private com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
|
Config config = new Config()
|
// 您的AccessKey ID
|
.setAccessKeyId(accessKeyId)
|
// 您的AccessKey Secret
|
.setAccessKeySecret(accessKeySecret);
|
// 访问的域名
|
config.endpoint = "dysmsapi.aliyuncs.com";
|
return new com.aliyun.dysmsapi20170525.Client(config);
|
}
|
/**
|
* 创建发送命令的参数
|
* @return
|
*/
|
private SendSmsRequest createSendSmsRequest(String phone,String templateCode,String templateParam,String signname){
|
|
SendSmsRequest sendSmsRequest = new SendSmsRequest();
|
sendSmsRequest.setSignName(signname);
|
sendSmsRequest.setPhoneNumbers(phone);
|
|
sendSmsRequest.setTemplateCode(templateCode);
|
if(!StringUtils.isEmpty(templateParam)){
|
sendSmsRequest.setTemplateParam(templateParam);
|
}
|
return sendSmsRequest;
|
|
}
|
|
}
|