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.
		
		
		
		
			
				
					
					
						
							230 lines
						
					
					
						
							7.8 KiB
						
					
					
				
			
		
		
	
	
							230 lines
						
					
					
						
							7.8 KiB
						
					
					
				//package com.ynxbd.common.helper.common;
 | 
						|
//
 | 
						|
//import com.alibaba.fastjson.JSON;
 | 
						|
//import com.ynxbd.common.bean.sms.SmsTemplate;
 | 
						|
//import com.ynxbd.common.config.EhcacheConfig;
 | 
						|
//import lombok.extern.slf4j.Slf4j;
 | 
						|
//import org.ehcache.Cache;
 | 
						|
//
 | 
						|
//import java.util.Date;
 | 
						|
//import java.util.Properties;
 | 
						|
//
 | 
						|
//@Slf4j
 | 
						|
//public class SmsNewHelper {
 | 
						|
//    private SmsNewHelper() {
 | 
						|
//    }
 | 
						|
//
 | 
						|
//    final private static String OK = "OK";
 | 
						|
//
 | 
						|
//    private static String ACCESS_KEY_ID;
 | 
						|
//    private static String ACCESS_KEY_SECRET;
 | 
						|
//    private static String SIGN_NAME;
 | 
						|
//    // 缓存
 | 
						|
//    private static Cache<String, SmsTemplate> CACHE;
 | 
						|
//
 | 
						|
//    static {
 | 
						|
//        Properties properties = FileHelper.readProperties("sms.properties");
 | 
						|
//        // 读取配置文件...\wx\web\WEB-INF\classes\sms.properties
 | 
						|
//        if (properties != null) {
 | 
						|
//            ACCESS_KEY_ID = FileHelper.propertiesGetString(properties, "sms.accessKeyId");
 | 
						|
//            ACCESS_KEY_SECRET = FileHelper.propertiesGetString(properties, "sms.accessKeySecret");
 | 
						|
//            SIGN_NAME = FileHelper.propertiesGetString(properties, "sms.signName");
 | 
						|
//        }
 | 
						|
//        createCache();
 | 
						|
//    }
 | 
						|
//
 | 
						|
//    private synchronized static void createCache() {
 | 
						|
//        if (CACHE == null) {
 | 
						|
//            // 创建一个缓存实例(5分钟最大存活时间)
 | 
						|
//            CACHE = EhcacheConfig.createCacheTTL(String.class, SmsTemplate.class, "sms_cache", (60L * 5));
 | 
						|
//        }
 | 
						|
//    }
 | 
						|
//
 | 
						|
//    /**
 | 
						|
//     * 使用AK&SK初始化账号Client
 | 
						|
//     *
 | 
						|
//     * @param accessKeyId     accessKeyId
 | 
						|
//     * @param accessKeySecret accessKeySecret
 | 
						|
//     * @return Client
 | 
						|
//     */
 | 
						|
//    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) {
 | 
						|
//        try {
 | 
						|
//            com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
 | 
						|
//                    // 您的AccessKey ID
 | 
						|
//                    .setAccessKeyId(accessKeyId)
 | 
						|
//                    // 您的AccessKey Secret
 | 
						|
//                    .setAccessKeySecret(accessKeySecret);
 | 
						|
//            // 访问的域名
 | 
						|
//            config.endpoint = "dysmsapi.aliyuncs.com";
 | 
						|
//            return new com.aliyun.dysmsapi20170525.Client(config);
 | 
						|
//        } catch (Exception e) {
 | 
						|
//            e.printStackTrace();
 | 
						|
//        }
 | 
						|
//        return null;
 | 
						|
//    }
 | 
						|
//
 | 
						|
//
 | 
						|
//
 | 
						|
//    public static boolean sendAli(String template, String tel, String signName, Object smsTemplate) {
 | 
						|
//        try {
 | 
						|
//            com.aliyun.dysmsapi20170525.Client client = createClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);
 | 
						|
//            if (client == null) {
 | 
						|
//                return false;
 | 
						|
//            }
 | 
						|
//            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
 | 
						|
//                    .setPhoneNumbers(tel)
 | 
						|
//                    .setSignName(signName)
 | 
						|
//                    .setTemplateCode(template)
 | 
						|
//                    .setTemplateParam(JSON.toJSONString(smsTemplate));
 | 
						|
//
 | 
						|
//            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
 | 
						|
//            // 复制代码运行请自行打印 API 的返回值
 | 
						|
//            com.aliyun.dysmsapi20170525.models.SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
 | 
						|
//            if (resp == null) {
 | 
						|
//                return false;
 | 
						|
//            }
 | 
						|
//            com.aliyun.dysmsapi20170525.models.SendSmsResponseBody body = resp.body;
 | 
						|
//            if (body == null) {
 | 
						|
//                return false;
 | 
						|
//            }
 | 
						|
//
 | 
						|
//            if (OK.equals(body.code) && OK.equals(body.message)) {
 | 
						|
//                return true;
 | 
						|
//            }
 | 
						|
//            log.error("[短信]发送失败:{}", body.message);
 | 
						|
//        } catch (Exception e) {
 | 
						|
//            e.printStackTrace();
 | 
						|
//            log.error("[短信]发送失败:{}", e.getMessage());
 | 
						|
//        }
 | 
						|
//        return false;
 | 
						|
//    }
 | 
						|
//
 | 
						|
//
 | 
						|
//    /**
 | 
						|
//     * 是否重复发送
 | 
						|
//     *
 | 
						|
//     * @param tel 储电话号码的集合
 | 
						|
//     * @return 验证码 | null = 发送异常
 | 
						|
//     */
 | 
						|
//    public static String isRepeat(String tel) {
 | 
						|
//        if (CACHE == null) {
 | 
						|
//            createCache();
 | 
						|
//        }
 | 
						|
//        if (CACHE != null) {
 | 
						|
//            SmsTemplate smsTemplate = CACHE.get(tel);
 | 
						|
//            if (smsTemplate == null) {
 | 
						|
//                return null;
 | 
						|
//            }
 | 
						|
//
 | 
						|
//            Long countDown = smsTemplate.getCountDown();
 | 
						|
//            if (countDown == null) {
 | 
						|
//                return null;
 | 
						|
//            }
 | 
						|
//
 | 
						|
//            countDown = new Date().getTime() - countDown; // 计算倒计时
 | 
						|
//
 | 
						|
//            if (countDown < 60000) { // 60s内请求,返回重复提示
 | 
						|
//                countDown = ((60000 - countDown) / 1000);
 | 
						|
//                String message = "重复请求" + countDown + "s";
 | 
						|
//                log.info(message);
 | 
						|
//                return message;
 | 
						|
//            }
 | 
						|
//            CACHE.remove(tel); // 超过60s,如果还存在则移除
 | 
						|
//        }
 | 
						|
//        return null;
 | 
						|
//    }
 | 
						|
//
 | 
						|
//
 | 
						|
//    /**
 | 
						|
//     * 验证码验证
 | 
						|
//     *
 | 
						|
//     * @param phone 手机号码
 | 
						|
//     * @param code  验证码
 | 
						|
//     */
 | 
						|
//    public static boolean codeVerify(String phone, String code) {
 | 
						|
//        if (CACHE == null) {
 | 
						|
//            createCache();
 | 
						|
//        }
 | 
						|
//        if (CACHE == null) return false;
 | 
						|
//
 | 
						|
//        SmsTemplate smsTemplate = CACHE.get(phone);
 | 
						|
//
 | 
						|
//        if (smsTemplate == null) return false;
 | 
						|
//
 | 
						|
//        return code.equals(smsTemplate.getCode());
 | 
						|
//    }
 | 
						|
//
 | 
						|
//
 | 
						|
//
 | 
						|
//    /**
 | 
						|
//     * 通用-发送短信
 | 
						|
//     *
 | 
						|
//     * @param template 模板
 | 
						|
//     * @param tel      电话号码
 | 
						|
//     * @param smsObj   模板内容
 | 
						|
//     */
 | 
						|
//    public static boolean send(String template, String tel, Object smsObj) {
 | 
						|
//        return send(template, tel, null, smsObj);
 | 
						|
//    }
 | 
						|
//
 | 
						|
//    /**
 | 
						|
//     * 通用-发送短信
 | 
						|
//     *
 | 
						|
//     * @param template 模板
 | 
						|
//     * @param tel      电话号码
 | 
						|
//     * @param signName 签名
 | 
						|
//     * @param smsObj   模板内容
 | 
						|
//     */
 | 
						|
//    public static boolean send(String template, String tel, String signName, Object smsObj) {
 | 
						|
//        boolean status = sendAli(template, tel, signName, smsObj);
 | 
						|
//        log.info("发送短信 tel={} {}", tel, status ? "成功" : "失败");
 | 
						|
//        return status;
 | 
						|
//    }
 | 
						|
//
 | 
						|
//
 | 
						|
//    public static void main(String[] args) {
 | 
						|
//        SmsTemplate sms = new SmsTemplate();
 | 
						|
//        sms.setCode("222111");
 | 
						|
//        SmsNewHelper.send("SMS_164268358", "15559603353", "云南新八达科技有限公司", sms);
 | 
						|
//    }
 | 
						|
//
 | 
						|
//    /**
 | 
						|
//     * 发送短信验证码
 | 
						|
//     *
 | 
						|
//     * @param template 短信模板
 | 
						|
//     * @param tel      电话号码
 | 
						|
//     * @return 是否发送成功
 | 
						|
//     * @code code 验证码
 | 
						|
//     */
 | 
						|
//    public static boolean sendCode(String template, String tel, String code) {
 | 
						|
//        return sendCode(template, tel, null, code);
 | 
						|
//    }
 | 
						|
//
 | 
						|
//    /**
 | 
						|
//     * 发送短信验证码
 | 
						|
//     *
 | 
						|
//     * @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());
 | 
						|
//
 | 
						|
//        boolean status = send(template, tel, signName, smsTemplate);
 | 
						|
//
 | 
						|
//        if (CACHE == null) {
 | 
						|
//            createCache();
 | 
						|
//        }
 | 
						|
//        if (status && CACHE != null) {
 | 
						|
//            CACHE.put(tel, smsTemplate); //添加缓存值
 | 
						|
//            return true;
 | 
						|
//        }
 | 
						|
//        return false;
 | 
						|
//    }
 | 
						|
//
 | 
						|
//}
 | 
						|
 |