|
@@ -0,0 +1,262 @@
|
|
|
+package com.ruoyi.app.util;
|
|
|
+
|
|
|
+
|
|
|
+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 org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 阿里发送短信验证码工具类
|
|
|
+ * @author Alex
|
|
|
+ */
|
|
|
+public class AliSMSUtil {
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(AliSMSUtil.class);
|
|
|
+
|
|
|
+ private static Map<String,String> CodeMap=new HashMap<>();
|
|
|
+ private static Map<String,Long> CodeMapTime=new HashMap<>();
|
|
|
+
|
|
|
+ private static final long EXPIRATIONTIME=1000*60*5;//5分钟
|
|
|
+ private static final int START=0;//设置执行开始时间
|
|
|
+ private static final int INTERVAL=1000*60*5;//设置间隔执行时间 单位/毫秒 设置为一分钟
|
|
|
+
|
|
|
+
|
|
|
+ //产品名称:云通信短信API产品,开发者无需替换
|
|
|
+ static final String product = "Dysmsapi";
|
|
|
+ //产品域名,开发者无需替换
|
|
|
+ static final String domain = "dysmsapi.aliyuncs.com";
|
|
|
+
|
|
|
+ // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
|
|
|
+ static final String accessKeyId = "LTAIuSBxXqyBNSCG";
|
|
|
+ static final String accessKeySecret = "K0QrsoGHQQFNya71vG9tmbjfOBnnFf";
|
|
|
+
|
|
|
+ private static AliSMSUtil INSTANCE;
|
|
|
+
|
|
|
+ private static String SIGN_NAME = "传承云";
|
|
|
+
|
|
|
+ //单例
|
|
|
+ public static AliSMSUtil getInstance(){
|
|
|
+ if (INSTANCE == null) {
|
|
|
+ synchronized (AliSMSUtil.class) {
|
|
|
+ if (INSTANCE == null) {
|
|
|
+ INSTANCE = new AliSMSUtil();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return INSTANCE;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送验证码 type类型 1、用户注册;2、登录确认;3、身份验证;4、登录异常;5、修改密码;6、信息变更;phone:电话号码
|
|
|
+ * @param type 1、用户注册;2、登录确认;3、身份验证;4、登录异常;5、修改密码;6、信息变更;0、通用验证;
|
|
|
+ * @param phone 电话号码
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Map<String,String> sendSmsCode(Integer type,String phone){
|
|
|
+ Map<String,String> map = new HashMap<>();
|
|
|
+
|
|
|
+ String templateCode = "";
|
|
|
+ switch (type){
|
|
|
+ case 0:
|
|
|
+ templateCode="SMS_140736533";
|
|
|
+ break;
|
|
|
+ case 1://用户注册
|
|
|
+ templateCode="SMS_136825040";
|
|
|
+ break;
|
|
|
+ case 2://登录确认
|
|
|
+ templateCode="SMS_136825042";
|
|
|
+ break;
|
|
|
+ case 3://身份验证
|
|
|
+ templateCode="SMS_136825043";
|
|
|
+ break;
|
|
|
+ case 4://登录异常
|
|
|
+ templateCode="SMS_136825041";
|
|
|
+ break;
|
|
|
+ case 5://修改密码
|
|
|
+ templateCode="SMS_136825039";
|
|
|
+ break;
|
|
|
+ case 6://信息变更
|
|
|
+ templateCode="SMS_136825038";
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ templateCode="";
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (templateCode.equals("")){
|
|
|
+ map.put("code","-1");
|
|
|
+ map.put("msg","验证类型不存在!");
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ String code = GetCode(phone);
|
|
|
+ String params = "{\"code\":\""+code+"\"}";
|
|
|
+ map = sendSms(phone,params,SIGN_NAME,templateCode);
|
|
|
+ return map;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证验证码 phone:电话号码 code:验证码
|
|
|
+ * @param phone
|
|
|
+ * @param code
|
|
|
+ * @return 成功返回true,
|
|
|
+ */
|
|
|
+ public Boolean validateSmsCode(String phone,String code){
|
|
|
+ boolean istrue=false;
|
|
|
+ //没有记录
|
|
|
+ if (CodeMap.size()<=0){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //找不到手机的记录
|
|
|
+ if(CodeMap.get(phone)==null){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ //时间超时了
|
|
|
+ if((CodeMapTime.get(phone).longValue()+EXPIRATIONTIME) < new Date().getTime()){
|
|
|
+ CodeMap.remove(phone);
|
|
|
+ CodeMapTime.remove(phone);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String mapCode = CodeMap.get(phone);
|
|
|
+ if(mapCode.equals(code)){
|
|
|
+ CodeMap.remove(phone);
|
|
|
+ CodeMapTime.remove(phone);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 会员充值
|
|
|
+ * @param phone 电话号码
|
|
|
+ * @param store 商家名称
|
|
|
+ * @param money 充值金额
|
|
|
+ * @param newMoney 充值后会员余额
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Map<String,String> sendSmsHycz(String phone,String store,String money,String newMoney){
|
|
|
+ Map<String,String> map = new HashMap<>();
|
|
|
+ String templateCode = "SMS_143706044";
|
|
|
+ String params = "{\"store\":\""+store+"\",\"money\":\""+money+"\",\"newMoney\":\""+newMoney+"\"}";
|
|
|
+ map = sendSms(phone,params,SIGN_NAME,templateCode);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private Map<String,String> sendSms(String phone,String params,String signName,String templateCode){
|
|
|
+ Map<String,String> map = new HashMap<>();
|
|
|
+ //可自助调整超时时间
|
|
|
+ System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
|
|
|
+ System.setProperty("sun.net.client.defaultReadTimeout", "10000");
|
|
|
+
|
|
|
+ //初始化acsClient,暂不支持region化
|
|
|
+ IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
|
|
|
+ DefaultProfile.addEndpoint("cn-hangzhou", product, domain);
|
|
|
+ IAcsClient acsClient = new DefaultAcsClient(profile);
|
|
|
+
|
|
|
+ //组装请求对象-具体描述见控制台-文档部分内容
|
|
|
+ SendSmsRequest request = new SendSmsRequest();
|
|
|
+ //必填:待发送手机号
|
|
|
+ request.setPhoneNumbers(phone);
|
|
|
+ //必填:短信签名-可在短信控制台中找到
|
|
|
+ request.setSignName(signName);
|
|
|
+ //必填:短信模板-可在短信控制台中找到
|
|
|
+ request.setTemplateCode(templateCode);
|
|
|
+ //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
|
|
|
+ request.setTemplateParam(params);
|
|
|
+
|
|
|
+ //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
|
|
|
+ //request.setSmsUpExtendCode("90997");
|
|
|
+
|
|
|
+ //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
|
|
|
+// request.setOutId("yourOutId");
|
|
|
+
|
|
|
+ //hint 此处可能会抛出异常,注意catch
|
|
|
+ try {
|
|
|
+ SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
|
|
|
+ if(sendSmsResponse!=null){
|
|
|
+ if(sendSmsResponse.getCode().toUpperCase().equals("OK")){
|
|
|
+ map.put("code","200");
|
|
|
+ map.put("msg","发送成功!");
|
|
|
+ }else{
|
|
|
+ map.put("code","500");
|
|
|
+ map.put("msg",sendSmsResponse.getMessage());
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ map.put("code","500");
|
|
|
+ map.put("msg","接口调用异常!");
|
|
|
+ }
|
|
|
+ } catch (ClientException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ map.put("code","500");
|
|
|
+ map.put("msg",e.getErrMsg());
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取验证码
|
|
|
+ * @param phone
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String GetCode(String phone){
|
|
|
+ String key = GetNum();
|
|
|
+ CodeMap.put(phone,key);
|
|
|
+ CodeMapTime.put(phone,new Date().getTime());
|
|
|
+ return key;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String GetNum(){
|
|
|
+ int number = 899999;
|
|
|
+ Random random = new Random();
|
|
|
+ int num =random.nextInt(number)+100000;
|
|
|
+ return num+"";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String args[]) throws Exception {
|
|
|
+// Map<String,String> map = getInstance().sendSmsCode(1,"18172065995");
|
|
|
+// System.out.println(map);
|
|
|
+// long tt = new Long("1000000000000");
|
|
|
+// long _new = new Date().getTime()-tt;
|
|
|
+// System.out.println(_new^1000001);
|
|
|
+// getInstance().sendSmsHycz("18778060750","测试商家","100","1000");
|
|
|
+ }
|
|
|
+
|
|
|
+ static{
|
|
|
+ Timer tt=new Timer();//定时类
|
|
|
+ tt.schedule(new TimerTask(){//创建一个定时任务
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ if (CodeMapTime.size() > 0) {
|
|
|
+ long nd = new Date().getTime();//获取系统时间
|
|
|
+ Iterator<Map.Entry<String, Long>> entries = CodeMapTime.entrySet().iterator();
|
|
|
+ while (entries.hasNext()) {
|
|
|
+ Map.Entry<String, Object> entry = (Map.Entry) entries.next();
|
|
|
+ String key = entry.getKey(); //获取key
|
|
|
+ long value = (Long) entry.getValue(); //获取value
|
|
|
+ long rt = nd - value;//获取当前时间跟存入时间的差值
|
|
|
+ if (rt > EXPIRATIONTIME) {//判断时间是否已经过期 如果过期则清除key 否则不做处理
|
|
|
+ CodeMap.remove(key);
|
|
|
+ entries.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }catch (Exception ex){
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }, START,INTERVAL);//从0秒开始,每隔10秒执行一次
|
|
|
+ }
|
|
|
+
|
|
|
+}
|