123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- package com.miniframe.tools.msg;
- import com.aliyun.dysmsapi20170525.Client;
- import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
- import com.aliyun.tea.TeaException;
- import com.aliyun.teautil.Common;
- import com.aliyun.teautil.models.RuntimeOptions;
- import com.miniframe.core.IniFileReader;
- import com.miniframe.core.exception.BusinessException;
- import com.miniframe.core.ext.UtilTools;
- import com.miniframe.tools.XIDateTimeUtils;
- import com.miniframe.tools.ali.Returnsms;
- import com.taobao.api.ApiException;
- import com.taobao.api.DefaultTaobaoClient;
- import com.taobao.api.TaobaoClient;
- import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
- import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
- import org.apache.http.client.config.RequestConfig;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- * 验证码,管理类
- *
- * @author tianyubing
- */
- public class IdentifyingCodeUtil {
- private static final Logger logger = LoggerFactory.getLogger(IdentifyingCodeUtil.class);
- private static String IDENTIFYINGCODE = "IDENTIFYINGCODE_";
- private static int max = 999999;
- private static int min = 100000;
- public static int maxTotal = 100; // 连接池最大并发连接数
- public static int maxPerRoute = 100; // 单路由最大并发数
- public static int connectionTimeOut = 10000; // 连接超时
- public static int socketTimeOut = 120000; // 应用超时
- public static RequestConfig requestConfig;
- public static String userid;
- public static String account;
- public static String password;
- public static String contentHead;
- public static String sendTime;
- public static String action;
- static {
- // IniFileReader ini = new IniFileReader("sms.properties");
- //
- // userid = ini.getStrValue("userid");//
- // account = ini.getStrValue("account");// 发送用户帐号 用户帐号,由系统管理员
- // password = ini.getStrValue("password");// 发送帐号密码 用户账号对应的密码
- // contentHead = ini.getStrValue("contentHead");// 发送内容 短信的内容头,内容需要UTF-8编码
- // contentHead = UtilTools.unicodeToUtf8(contentHead);// unicode转中文
- // sendTime = ini.getStrValue("sendTime");// 定时发送时间,为空表示立即发送,定时发送格式2010-10-24 09:08:10
- // action = ini.getStrValue("action"); // 发送任务命令 设置为固定的:send
- // requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeOut).setSocketTimeout(socketTimeOut).setConnectionRequestTimeout(connectionTimeOut).build();
- }
- private static String appkey = "LTAI5tEupgaUejG7mC6VHNF6";
- private static String secret = "QlRvqFRc51OzANL4uYlLx7MKZUB2rx";
- private static String url = "http://gw.api.taobao.com/router/rest";
- // private static String appkey = "24239810";
- // private static String secret = "ea55a8bf52aa2672b802716e82a15dea";
- // private static String url = "http://gw.api.taobao.com/router/rest";
- /**
- * 生成 验证码
- *
- * @return
- */
- public static String createCode() {
- java.util.Random r = new java.util.Random(System.currentTimeMillis());
- int tmp = r.nextInt();
- if (tmp < 0) {
- tmp = -1 * tmp;
- }
- return String.valueOf(tmp % (max - min + 1) + min);
- }
- /**
- * 发送验证码
- *
- * @return
- * @throws Exception
- */
- public static void sendCode(String tel) throws Exception {
- // 生成验证码
- String code = createCode();
- // String code = "111111";
- code="123456";//测试
- String lastTimeKey = IDENTIFYINGCODE+UtilTools.getBaseCode() + "_" + tel+"_lasttime";
- Long lastTime= (Long) UtilTools.getUserCache(lastTimeKey);
- Long currentTime = XIDateTimeUtils.getNowTimeStampMs();
- if(null != lastTime && currentTime-lastTime < 60000L) {
- throw new BusinessException("EB8000028");
- }else {
- UtilTools.putUserCache(lastTimeKey, currentTime);
- }
- // 唯一标识
- String ID = IDENTIFYINGCODE + UtilTools.getBaseCode() + "_" + tel;
- // 保存到cache中
- UtilTools.putUserCache(ID, code);
- // 发送短信
- // return true;
- // MsgCodeUtil.sendMsg(tel, code);//直接返回来,暂时不调用
- }
- /**
- * 验证验证码
- *
- * @return
- */
- public static boolean verification(String tel, String code) {
- // return true;
- String ID = IDENTIFYINGCODE + UtilTools.getBaseCode() + "_" + tel; // 唯一标识
- String saveCode = (String) UtilTools.getUserCache(ID); // 获取上次保存
- logger.info("=========短信验证码============>start");
- logger.info("系统存储的验证信息"+saveCode);
- logger.info("用户输入的验证信息"+code);
- logger.info("=========短信验证码============>end");
- if (saveCode == null) {
- return false;
- }
- if (code == null) {
- return false;
- } // 删除保存在cache中的数据
- if(saveCode.equals(code)) {
- UtilTools.removeUserCache(ID);
- return true;
- }else {
- return false;
- }
- }
- /**
- * 验证码使用一次后不删除,因此可以使用多次; 验证成功后删除
- * @param tel
- * @param code
- * @return
- */
- public static boolean verificationMoreTime(String tel, String code) {
- // return true;
- String ID = IDENTIFYINGCODE + UtilTools.getBaseCode() + "_" + tel; // 唯一标识
- String saveCode = (String) UtilTools.getUserCache(ID); // 获取上次保存
- if (saveCode == null) {
- return false;
- }
- if (code == null) {
- return false;
- } // 删除保存在cache中的数据
- if(saveCode.equals(code)) {
- UtilTools.removeUserCache(ID);
- return true;
- }else {
- //是否添加验证码的时间限制
- //暂不需要,缓存默认30分钟过期
- return false;
- }
- }
- /**
- * 报名到期提醒
- * 尊敬的${name},第一届“智博杯”全国大学生仿真软件应用设计大赛报名,将于${duedate}正式截止;
- * 请于报名截止前及时安排学生进行参赛。
- * @param mobileNo
- * @param name
- * @param duedate
- * @return
- */
- public static boolean sendDS1(String mobileNo, String name,String duedate) {
- TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
- AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
- req.setExtend("123456");
- req.setSmsType("normal");
- req.setSmsFreeSignName("小犀智能");
- req.setSmsParamString("{\"name\":\"" + name + "\"; \"duedate\":\""+duedate+"\"}");
- req.setRecNum(mobileNo);
- req.setSmsTemplateCode("SMS_216427923");
- AlibabaAliqinFcSmsNumSendResponse rsp = null;
- try {
- rsp = client.execute(req);
- } catch (ApiException e) {
- logger.error("",e);
- return false;
- }
- // System.out.println(rsp.getBody());
- if ((rsp != null) && (rsp.getResult() != null) && rsp.getResult().getSuccess()) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * 报名成功提醒
- * 尊敬的${name},第一届“智博杯”全国大学生仿真软件应用设计大赛您参赛成功;请您尽快参与预演题目的学习!
- * @param mobileNo
- * @param name
- * @return
- */
- public static boolean sendDS2(String mobileNo, String name) {
- TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
- AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
- req.setExtend("123456");
- req.setSmsType("normal");
- req.setSmsFreeSignName("小犀智能");
- req.setSmsParamString("{\"name\":\"" + name + "\"}");
- req.setRecNum(mobileNo);
- req.setSmsTemplateCode("SMS_216427963");
- AlibabaAliqinFcSmsNumSendResponse rsp = null;
- try {
- rsp = client.execute(req);
- } catch (ApiException e) {
- logger.error("",e);
- return false;
- }
- // System.out.println(rsp.getBody());
- if ((rsp != null) && (rsp.getResult() != null) && rsp.getResult().getSuccess()) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * 正式比赛消息提醒
- * 尊敬的${name},第一届“智博杯”全国大学生仿真软件应用设计大赛,正式比赛将于${sdate}正式开始;
- * 请您于${ledate}之前完成题目领取,${qedate}之前完成题目求解,${redate}之前完成题目报告上传。
- * @param mobileNo
- * @param name
- * @return
- */
- public static boolean sendDS3(String mobileNo, String name,String sdate,String ledate,String qedate,String redate) {
- TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
- AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
- req.setExtend("123456");
- req.setSmsType("normal");
- req.setSmsFreeSignName("小犀智能");
- req.setSmsParamString("{\"name\":\"" + name + "\"; "
- + "\"sdate\":\""+sdate+"\"; "
- + "\"ledate\":\""+ledate+"\"; "
- + "\"qedate\":\""+qedate+"\"; "
- + "\"redate\":\""+redate+"\""
- +"}");
- req.setRecNum(mobileNo);
- req.setSmsTemplateCode("SMS_216372839");
- AlibabaAliqinFcSmsNumSendResponse rsp = null;
- try {
- rsp = client.execute(req);
- } catch (ApiException e) {
- logger.error("",e);
- return false;
- }
- // System.out.println(rsp.getBody());
- if ((rsp != null) && (rsp.getResult() != null) && rsp.getResult().getSuccess()) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * 报告提交消息提醒
- * 尊敬的${name},第一届“智博杯”全国大学生仿真软件应用设计大赛,报告提交将于${redate}正式截止;请您于报告提交截止期之前完成题目报告上传。
- * @param mobileNo
- * @param name
- * @return
- */
- public static boolean sendDS4(String mobileNo, String name,String redate) {
- TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
- AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
- req.setExtend("123456");
- req.setSmsType("normal");
- req.setSmsFreeSignName("小犀智能");
- req.setSmsParamString("{\"name\":\"" + name + "\"; "
- + "\"redate\":\""+redate+"\""
- +"}");
- req.setRecNum(mobileNo);
- req.setSmsTemplateCode("SMS_216427966");
- AlibabaAliqinFcSmsNumSendResponse rsp = null;
- try {
- rsp = client.execute(req);
- } catch (ApiException e) {
- logger.error("",e);
- return false;
- }
- // System.out.println(rsp.getBody());
- if ((rsp != null) && (rsp.getResult() != null) && rsp.getResult().getSuccess()) {
- return true;
- } else {
- return false;
- }
- }
- // 发送短信的
- public static boolean sendSMSAli(String mobileNo, String text) throws Exception {
- Client client = ShotMsgUtil.createClient("LTAI5tEupgaUejG7mC6VHNF6", "QlRvqFRc51OzANL4uYlLx7MKZUB2rx");
- SendSmsRequest sendSmsRequest = new SendSmsRequest()
- .setPhoneNumbers(mobileNo)
- .setSignName("前沿动力")
- .setTemplateCode("SMS_221636828")
- .setTemplateParam("{code:" + text + "}");
- RuntimeOptions runtime = new RuntimeOptions();
- try {
- // 复制代码运行请自行打印 API 的返回值
- client.sendSmsWithOptions(sendSmsRequest, runtime);
- } catch (TeaException error) {
- // 如有需要,请打印 error
- Common.assertAsString(error.message);
- } catch (Exception _error) {
- TeaException error = new TeaException(_error.getMessage(), _error);
- // 如有需要,请打印 error
- Common.assertAsString(error.message);
- }
- return true;
- }
- /**
- * 验证发送短信是否成功
- *
- * @param returnXml 发送短信后的返回信息
- * @return
- */
- public static boolean isSuccess(String returnXml) {
- String returnJson;
- try {
- returnJson = UtilTools.xml2json(returnXml);
- Returnsms returnsms = (Returnsms) UtilTools.json2Obj(returnJson, Returnsms.class);
- System.out.println(returnsms.getReturnstatus());
- if ("Success".equals(returnsms.getReturnstatus())) {
- return true;
- } else {
- return false;
- }
- } catch (Exception e) {
- e.printStackTrace();
- logger.error("xml解析失败", e);
- return false;
- }
- }
- public static void main(String[] args) throws Exception {
- // String tel = "17180135310";
- // String tel = "18410269923";
- String tel = "19802329506";
- IdentifyingCodeUtil.sendSMSAli(tel,"123");
- // boolean result = IdentifyingCodeUtil.sendDS1(tel, "黄先生","2021年5月30日");
- // System.out.println(result);
- // IdentifyingCodeUtil.sendDS1(tel, "黄先生","2021年5月30日");
- // IdentifyingCodeUtil.sendDS2(tel, "黄先生");
- // IdentifyingCodeUtil.sendds3(tel, "黄先生","2021年5月30日","2021年6月15日","2021年6月30日","2021年7月30日");
- // IdentifyingCodeUtil.sendDS4(tel, "黄先生","2021年5月30日");
- }
- }
|