You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					283 lines
				
				9.2 KiB
			
		
		
			
		
	
	
					283 lines
				
				9.2 KiB
			| 
											3 years ago
										 | package com.ynxbd.common.helper.common;
 | ||
|  | 
 | ||
|  | import com.alibaba.fastjson.JSONObject;
 | ||
|  | import com.aliyuncs.CommonRequest;
 | ||
|  | import com.aliyuncs.CommonResponse;
 | ||
|  | import com.aliyuncs.DefaultAcsClient;
 | ||
|  | import com.aliyuncs.IAcsClient;
 | ||
|  | import com.aliyuncs.http.MethodType;
 | ||
|  | import com.aliyuncs.profile.DefaultProfile;
 | ||
|  | import com.ynxbd.common.bean.sms.SmsRegTem;
 | ||
|  | import com.ynxbd.common.bean.sms.SmsTemplate;
 | ||
|  | import com.ynxbd.common.config.EhCacheConfig;
 | ||
|  | import com.ynxbd.common.helper.ProperHelper;
 | ||
|  | import com.ynxbd.common.result.ResultEnum;
 | ||
|  | import com.ynxbd.wx.config.WeChatConfig;
 | ||
|  | import lombok.extern.slf4j.Slf4j;
 | ||
|  | import org.apache.commons.lang3.StringUtils;
 | ||
|  | import org.ehcache.Cache;
 | ||
|  | 
 | ||
|  | import java.util.Date;
 | ||
|  | import java.util.Map;
 | ||
|  | 
 | ||
|  | @Slf4j
 | ||
|  | public class SmsHelper {
 | ||
|  |     private SmsHelper() {
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     private static final String OK = "OK";
 | ||
|  |     public static final String ACCESS_KEY_ID;
 | ||
|  |     public static final String ACCESS_KEY_SECRET;
 | ||
|  |     public static final String SIGN_NAME;
 | ||
|  | 
 | ||
|  |     // 缓存
 | ||
|  |     public static Cache<String, SmsTemplate> CACHE;
 | ||
|  | 
 | ||
|  |     static {
 | ||
|  |         ProperHelper config = new ProperHelper().read("sms.properties");
 | ||
|  |         ACCESS_KEY_ID = config.getString("sms.access_key_id");
 | ||
|  |         ACCESS_KEY_SECRET = config.getString("sms.access_key_secret");
 | ||
|  |         SIGN_NAME = config.getString("sms.sign_name");
 | ||
|  | 
 | ||
|  |         createCache();
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     private synchronized static void createCache() {
 | ||
|  |         if (CACHE == null) {
 | ||
|  |             // 创建一个缓存实例(5分钟最大存活时间)
 | ||
|  |             CACHE = EhCacheConfig.createCacheTTL(String.class, SmsTemplate.class, "sms_cache", (60L * 5));
 | ||
|  |         }
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 通用-发送短信
 | ||
|  |      *
 | ||
|  |      * @param template 模板
 | ||
|  |      * @param tel      电话号码
 | ||
|  |      */
 | ||
|  |     public static boolean send(String template, String tel, Object smsObj) {
 | ||
|  |         SmsTemplate smsTemplate = new SmsTemplate();
 | ||
|  |         smsTemplate.setTemParam(JsonHelper.toJsonString(smsObj));
 | ||
|  |         return send(template, tel, null, smsTemplate);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 通用-发送短信
 | ||
|  |      *
 | ||
|  |      * @param template 模板
 | ||
|  |      * @param tel      电话号码
 | ||
|  |      */
 | ||
|  |     public static boolean send(String template, String tel, SmsTemplate smsTemplate) {
 | ||
|  |         return send(template, tel, null, smsTemplate);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 通用-发送短信
 | ||
|  |      *
 | ||
|  |      * @param template    模板
 | ||
|  |      * @param tel         电话号码
 | ||
|  |      * @param signName    签名
 | ||
|  |      * @param smsTemplate 模板内容
 | ||
|  |      */
 | ||
|  |     public static boolean send(String template, String tel, String signName, SmsTemplate smsTemplate) {
 | ||
|  |         return sendAli(template, tel, signName, smsTemplate);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 发送短信验证码
 | ||
|  |      *
 | ||
|  |      * @param template 短信模板
 | ||
|  |      * @param tel      电话号码
 | ||
|  |      * @param code     验证码
 | ||
|  |      * @return 是否发送成功
 | ||
|  |      */
 | ||
|  |     public static boolean sendCode(String template, String tel, String code) {
 | ||
|  |         return sendCode(template, tel, null, code);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 发送短信验证码
 | ||
|  |      *
 | ||
|  |      * @param template 短信模板
 | ||
|  |      * @param tel      电话号码
 | ||
|  |      * @return 是否发送成功
 | ||
|  |      */
 | ||
|  |     public static boolean sendCode(String template, String tel) {
 | ||
|  |         return sendCode(template, tel, null, CodeHelper.getRandomCode(6));
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 发送短信验证码
 | ||
|  |      *
 | ||
|  |      * @param template 短信模板
 | ||
|  |      * @param tel      电话号码
 | ||
|  |      * @param signName 签名
 | ||
|  |      * @return 是否发送成功
 | ||
|  |      * @code code 验证码
 | ||
|  |      */
 | ||
|  |     public static boolean sendCode(String template, String tel, String signName, String code) {
 | ||
|  |         SmsTemplate smsTemplate = new SmsTemplate();
 | ||
|  |         smsTemplate.setCode(code);
 | ||
|  |         smsTemplate.setTel(tel);
 | ||
|  |         smsTemplate.setCountDown(new Date().getTime());
 | ||
|  |         return sendAli(template, tel, signName, smsTemplate);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 发送请求到阿里云
 | ||
|  |      *
 | ||
|  |      * @param template    模板
 | ||
|  |      * @param tel         电话号码
 | ||
|  |      * @param smsTemplate 模板参数
 | ||
|  |      */
 | ||
|  |     public static boolean sendAli(String template, String tel, String signName, SmsTemplate smsTemplate) {
 | ||
|  |         if (StringUtils.isEmpty(signName)) {
 | ||
|  |             signName = SIGN_NAME;
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         if (CACHE == null) createCache();
 | ||
|  | 
 | ||
|  |         if (CACHE != null) {
 | ||
|  |             CACHE.put(tel, smsTemplate); //添加缓存值
 | ||
|  |         }
 | ||
|  | 
 | ||
|  |         boolean flag = false;
 | ||
|  |         try {
 | ||
|  |             DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
 | ||
|  |             IAcsClient client = new DefaultAcsClient(profile);
 | ||
|  | 
 | ||
|  |             CommonRequest request = new CommonRequest();
 | ||
|  |             request.setSysMethod(MethodType.POST);
 | ||
|  |             request.setSysDomain("dysmsapi.aliyuncs.com");
 | ||
|  |             request.setSysVersion("2017-05-25");
 | ||
|  |             // 发送短信的类型
 | ||
|  |             request.setSysAction("SendSms");
 | ||
|  |             request.putQueryParameter("RegionId", "cn-hangzhou");
 | ||
|  |             // 签名模版
 | ||
|  |             request.putQueryParameter("SignName", signName);
 | ||
|  |             // 短信模版ID
 | ||
|  |             request.putQueryParameter("TemplateCode", template);
 | ||
|  |             // 接收短信的手机号
 | ||
|  |             request.putQueryParameter("PhoneNumbers", tel);
 | ||
|  |             // 设置模板需要的参数==>验证码code
 | ||
|  |             if (smsTemplate.getCode() == null) {
 | ||
|  |                 request.putQueryParameter("TemplateParam", smsTemplate.getTemParam());
 | ||
|  |             } else {
 | ||
|  |                 request.putQueryParameter("TemplateParam", JsonHelper.toJsonString(smsTemplate));
 | ||
|  |             }
 | ||
|  |             // 发送并获取响应消息
 | ||
|  |             CommonResponse response = client.getCommonResponse(request);
 | ||
|  |             String result = response.getData();
 | ||
|  |             log.info("[短信]请求返回 resp={}", result);
 | ||
|  | 
 | ||
|  |             // {"Message":"签名不合法(不存在或被拉黑)","RequestId":"B929C1F5-0DF6-4678-98B8-118060EFBC96","Code":"isv.SMS_SIGNATURE_ILLEGAL"}
 | ||
|  |             JSONObject respMsg = JsonHelper.parseObject(result);
 | ||
|  |             String respCode = respMsg.getString("Code");
 | ||
|  |             String respMessage = respMsg.getString("Message");
 | ||
|  | 
 | ||
|  |             if (OK.equals(respCode) && OK.equals(respMessage)) { // 发送成功
 | ||
|  |                 flag = true;
 | ||
|  |             }
 | ||
|  |         } catch (Exception e) {
 | ||
|  |             e.printStackTrace();
 | ||
|  |             ErrorHelper.println(e);
 | ||
|  |         }
 | ||
|  |         return flag;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 是否重复发送
 | ||
|  |      *
 | ||
|  |      * @param tel 储电话号码的集合
 | ||
|  |      * @return 验证码 | null = 发送异常
 | ||
|  |      */
 | ||
|  |     public static SmsTemplate isRepeat(String tel) {
 | ||
|  |         return isRepeat(tel, null);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 是否重复发送
 | ||
|  |      *
 | ||
|  |      * @param tel     储电话号码的集合
 | ||
|  |      * @param seconds 等待时间(s)
 | ||
|  |      * @return 验证码 | null = 发送异常
 | ||
|  |      */
 | ||
|  |     public static SmsTemplate isRepeat(String tel, Integer seconds) {
 | ||
|  |         if (seconds == null) seconds = 60;
 | ||
|  | 
 | ||
|  |         if (tel == null) return null;
 | ||
|  | 
 | ||
|  |         if (CACHE != null) {
 | ||
|  |             SmsTemplate cache = CACHE.get(tel);
 | ||
|  |             if (cache == null) {
 | ||
|  |                 return null;
 | ||
|  |             }
 | ||
|  | 
 | ||
|  |             Long countDown = cache.getCountDown();
 | ||
|  |             if (countDown == null) {
 | ||
|  |                 return null;
 | ||
|  |             }
 | ||
|  | 
 | ||
|  |             countDown = new Date().getTime() - countDown; // 计算倒计时
 | ||
|  | 
 | ||
|  |             seconds = seconds * 1000;
 | ||
|  |             if (countDown < seconds) { // 60s内请求,返回重复提示
 | ||
|  |                 countDown = ((seconds - countDown) / 1000);
 | ||
|  |                 log.info("[短信发送]还需等待 tel={}, countDown={}", tel, countDown);
 | ||
|  |                 SmsTemplate sms = new SmsTemplate();
 | ||
|  |                 sms.setCountDown(countDown);
 | ||
|  |                 return sms;
 | ||
|  |             }
 | ||
|  |             CACHE.remove(tel); // 超过60s,如果还存在则移除
 | ||
|  |         }
 | ||
|  |         return null;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 
 | ||
|  |     /**
 | ||
|  |      * 验证码验证
 | ||
|  |      *
 | ||
|  |      * @param tel      手机号码
 | ||
|  |      * @param code     验证码
 | ||
|  |      * @param isRemove 是否验证成功后移除
 | ||
|  |      */
 | ||
|  |     public static boolean codeVerify(String tel, String code, boolean isRemove) {
 | ||
|  |         if (CACHE == null) {
 | ||
|  |             createCache();
 | ||
|  |         }
 | ||
|  |         if (CACHE == null) return false;
 | ||
|  | 
 | ||
|  |         SmsTemplate smsTemplate = CACHE.get(tel);
 | ||
|  | 
 | ||
|  |         if (smsTemplate == null) return false;
 | ||
|  |         if (code.equals(smsTemplate.getCode())) {
 | ||
|  |             if (isRemove) {
 | ||
|  |                 CACHE.remove(tel);
 | ||
|  |             }
 | ||
|  |             return true;
 | ||
|  |         }
 | ||
|  |         return false;
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | 
 | ||
|  | //    private void isCache() {
 | ||
|  | //        if (CACHE == null) { // 判断是否重复发送验证码
 | ||
|  | //            // 建立一个缓存实例
 | ||
|  | //            CACHE = new Cache("smsCache", 5000, false, false, 120, 60);
 | ||
|  | //            //在内存管理器中添加缓存实例
 | ||
|  | //            if (CACHE_MANAGER == null) {
 | ||
|  | //                CACHE_MANAGER = EHCacheUtil.initCacheManager();
 | ||
|  | //            }
 | ||
|  | //            CACHE_MANAGER.addCache(CACHE);
 | ||
|  | //        }
 | ||
|  | //
 | ||
|  | //        // 清除key,ehcache不会主动清除key,需要通过遍历来清除
 | ||
|  | //        for (Object key : CACHE.getKeys()) {
 | ||
|  | //            CACHE.get(key);
 | ||
|  | //        }
 | ||
|  | //        logger.info("smsCache当前缓存数量:" + CACHE.getSize());
 | ||
|  | //    }
 | ||
|  | }
 |