IdentifyingCodeUtil.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. package com.miniframe.tools.msg;
  2. import com.aliyun.dysmsapi20170525.Client;
  3. import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
  4. import com.aliyun.tea.TeaException;
  5. import com.aliyun.teautil.Common;
  6. import com.aliyun.teautil.models.RuntimeOptions;
  7. import com.miniframe.core.IniFileReader;
  8. import com.miniframe.core.exception.BusinessException;
  9. import com.miniframe.core.ext.UtilTools;
  10. import com.miniframe.tools.XIDateTimeUtils;
  11. import com.miniframe.tools.ali.Returnsms;
  12. import com.taobao.api.ApiException;
  13. import com.taobao.api.DefaultTaobaoClient;
  14. import com.taobao.api.TaobaoClient;
  15. import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
  16. import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
  17. import org.apache.http.client.config.RequestConfig;
  18. import org.slf4j.Logger;
  19. import org.slf4j.LoggerFactory;
  20. /**
  21. * 验证码,管理类
  22. *
  23. * @author tianyubing
  24. */
  25. public class IdentifyingCodeUtil {
  26. private static final Logger logger = LoggerFactory.getLogger(IdentifyingCodeUtil.class);
  27. private static String IDENTIFYINGCODE = "IDENTIFYINGCODE_";
  28. private static int max = 999999;
  29. private static int min = 100000;
  30. public static int maxTotal = 100; // 连接池最大并发连接数
  31. public static int maxPerRoute = 100; // 单路由最大并发数
  32. public static int connectionTimeOut = 10000; // 连接超时
  33. public static int socketTimeOut = 120000; // 应用超时
  34. public static RequestConfig requestConfig;
  35. public static String userid;
  36. public static String account;
  37. public static String password;
  38. public static String contentHead;
  39. public static String sendTime;
  40. public static String action;
  41. static {
  42. // IniFileReader ini = new IniFileReader("sms.properties");
  43. //
  44. // userid = ini.getStrValue("userid");//
  45. // account = ini.getStrValue("account");// 发送用户帐号 用户帐号,由系统管理员
  46. // password = ini.getStrValue("password");// 发送帐号密码 用户账号对应的密码
  47. // contentHead = ini.getStrValue("contentHead");// 发送内容 短信的内容头,内容需要UTF-8编码
  48. // contentHead = UtilTools.unicodeToUtf8(contentHead);// unicode转中文
  49. // sendTime = ini.getStrValue("sendTime");// 定时发送时间,为空表示立即发送,定时发送格式2010-10-24 09:08:10
  50. // action = ini.getStrValue("action"); // 发送任务命令 设置为固定的:send
  51. // requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeOut).setSocketTimeout(socketTimeOut).setConnectionRequestTimeout(connectionTimeOut).build();
  52. }
  53. private static String appkey = "LTAI5tEupgaUejG7mC6VHNF6";
  54. private static String secret = "QlRvqFRc51OzANL4uYlLx7MKZUB2rx";
  55. private static String url = "http://gw.api.taobao.com/router/rest";
  56. // private static String appkey = "24239810";
  57. // private static String secret = "ea55a8bf52aa2672b802716e82a15dea";
  58. // private static String url = "http://gw.api.taobao.com/router/rest";
  59. /**
  60. * 生成 验证码
  61. *
  62. * @return
  63. */
  64. public static String createCode() {
  65. java.util.Random r = new java.util.Random(System.currentTimeMillis());
  66. int tmp = r.nextInt();
  67. if (tmp < 0) {
  68. tmp = -1 * tmp;
  69. }
  70. return String.valueOf(tmp % (max - min + 1) + min);
  71. }
  72. /**
  73. * 发送验证码
  74. *
  75. * @return
  76. * @throws Exception
  77. */
  78. public static void sendCode(String tel) throws Exception {
  79. // 生成验证码
  80. String code = createCode();
  81. // String code = "111111";
  82. code="123456";//测试
  83. String lastTimeKey = IDENTIFYINGCODE+UtilTools.getBaseCode() + "_" + tel+"_lasttime";
  84. Long lastTime= (Long) UtilTools.getUserCache(lastTimeKey);
  85. Long currentTime = XIDateTimeUtils.getNowTimeStampMs();
  86. if(null != lastTime && currentTime-lastTime < 60000L) {
  87. throw new BusinessException("EB8000028");
  88. }else {
  89. UtilTools.putUserCache(lastTimeKey, currentTime);
  90. }
  91. // 唯一标识
  92. String ID = IDENTIFYINGCODE + UtilTools.getBaseCode() + "_" + tel;
  93. // 保存到cache中
  94. UtilTools.putUserCache(ID, code);
  95. // 发送短信
  96. // return true;
  97. // MsgCodeUtil.sendMsg(tel, code);//直接返回来,暂时不调用
  98. }
  99. /**
  100. * 验证验证码
  101. *
  102. * @return
  103. */
  104. public static boolean verification(String tel, String code) {
  105. // return true;
  106. String ID = IDENTIFYINGCODE + UtilTools.getBaseCode() + "_" + tel; // 唯一标识
  107. String saveCode = (String) UtilTools.getUserCache(ID); // 获取上次保存
  108. logger.info("=========短信验证码============>start");
  109. logger.info("系统存储的验证信息"+saveCode);
  110. logger.info("用户输入的验证信息"+code);
  111. logger.info("=========短信验证码============>end");
  112. if (saveCode == null) {
  113. return false;
  114. }
  115. if (code == null) {
  116. return false;
  117. } // 删除保存在cache中的数据
  118. if(saveCode.equals(code)) {
  119. UtilTools.removeUserCache(ID);
  120. return true;
  121. }else {
  122. return false;
  123. }
  124. }
  125. /**
  126. * 验证码使用一次后不删除,因此可以使用多次; 验证成功后删除
  127. * @param tel
  128. * @param code
  129. * @return
  130. */
  131. public static boolean verificationMoreTime(String tel, String code) {
  132. // return true;
  133. String ID = IDENTIFYINGCODE + UtilTools.getBaseCode() + "_" + tel; // 唯一标识
  134. String saveCode = (String) UtilTools.getUserCache(ID); // 获取上次保存
  135. if (saveCode == null) {
  136. return false;
  137. }
  138. if (code == null) {
  139. return false;
  140. } // 删除保存在cache中的数据
  141. if(saveCode.equals(code)) {
  142. UtilTools.removeUserCache(ID);
  143. return true;
  144. }else {
  145. //是否添加验证码的时间限制
  146. //暂不需要,缓存默认30分钟过期
  147. return false;
  148. }
  149. }
  150. /**
  151. * 报名到期提醒
  152. * 尊敬的${name},第一届“智博杯”全国大学生仿真软件应用设计大赛报名,将于${duedate}正式截止;
  153. * 请于报名截止前及时安排学生进行参赛。
  154. * @param mobileNo
  155. * @param name
  156. * @param duedate
  157. * @return
  158. */
  159. public static boolean sendDS1(String mobileNo, String name,String duedate) {
  160. TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
  161. AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
  162. req.setExtend("123456");
  163. req.setSmsType("normal");
  164. req.setSmsFreeSignName("小犀智能");
  165. req.setSmsParamString("{\"name\":\"" + name + "\"; \"duedate\":\""+duedate+"\"}");
  166. req.setRecNum(mobileNo);
  167. req.setSmsTemplateCode("SMS_216427923");
  168. AlibabaAliqinFcSmsNumSendResponse rsp = null;
  169. try {
  170. rsp = client.execute(req);
  171. } catch (ApiException e) {
  172. logger.error("",e);
  173. return false;
  174. }
  175. // System.out.println(rsp.getBody());
  176. if ((rsp != null) && (rsp.getResult() != null) && rsp.getResult().getSuccess()) {
  177. return true;
  178. } else {
  179. return false;
  180. }
  181. }
  182. /**
  183. * 报名成功提醒
  184. * 尊敬的${name},第一届“智博杯”全国大学生仿真软件应用设计大赛您参赛成功;请您尽快参与预演题目的学习!
  185. * @param mobileNo
  186. * @param name
  187. * @return
  188. */
  189. public static boolean sendDS2(String mobileNo, String name) {
  190. TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
  191. AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
  192. req.setExtend("123456");
  193. req.setSmsType("normal");
  194. req.setSmsFreeSignName("小犀智能");
  195. req.setSmsParamString("{\"name\":\"" + name + "\"}");
  196. req.setRecNum(mobileNo);
  197. req.setSmsTemplateCode("SMS_216427963");
  198. AlibabaAliqinFcSmsNumSendResponse rsp = null;
  199. try {
  200. rsp = client.execute(req);
  201. } catch (ApiException e) {
  202. logger.error("",e);
  203. return false;
  204. }
  205. // System.out.println(rsp.getBody());
  206. if ((rsp != null) && (rsp.getResult() != null) && rsp.getResult().getSuccess()) {
  207. return true;
  208. } else {
  209. return false;
  210. }
  211. }
  212. /**
  213. * 正式比赛消息提醒
  214. * 尊敬的${name},第一届“智博杯”全国大学生仿真软件应用设计大赛,正式比赛将于${sdate}正式开始;
  215. * 请您于${ledate}之前完成题目领取,${qedate}之前完成题目求解,${redate}之前完成题目报告上传。
  216. * @param mobileNo
  217. * @param name
  218. * @return
  219. */
  220. public static boolean sendDS3(String mobileNo, String name,String sdate,String ledate,String qedate,String redate) {
  221. TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
  222. AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
  223. req.setExtend("123456");
  224. req.setSmsType("normal");
  225. req.setSmsFreeSignName("小犀智能");
  226. req.setSmsParamString("{\"name\":\"" + name + "\"; "
  227. + "\"sdate\":\""+sdate+"\"; "
  228. + "\"ledate\":\""+ledate+"\"; "
  229. + "\"qedate\":\""+qedate+"\"; "
  230. + "\"redate\":\""+redate+"\""
  231. +"}");
  232. req.setRecNum(mobileNo);
  233. req.setSmsTemplateCode("SMS_216372839");
  234. AlibabaAliqinFcSmsNumSendResponse rsp = null;
  235. try {
  236. rsp = client.execute(req);
  237. } catch (ApiException e) {
  238. logger.error("",e);
  239. return false;
  240. }
  241. // System.out.println(rsp.getBody());
  242. if ((rsp != null) && (rsp.getResult() != null) && rsp.getResult().getSuccess()) {
  243. return true;
  244. } else {
  245. return false;
  246. }
  247. }
  248. /**
  249. * 报告提交消息提醒
  250. * 尊敬的${name},第一届“智博杯”全国大学生仿真软件应用设计大赛,报告提交将于${redate}正式截止;请您于报告提交截止期之前完成题目报告上传。
  251. * @param mobileNo
  252. * @param name
  253. * @return
  254. */
  255. public static boolean sendDS4(String mobileNo, String name,String redate) {
  256. TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
  257. AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
  258. req.setExtend("123456");
  259. req.setSmsType("normal");
  260. req.setSmsFreeSignName("小犀智能");
  261. req.setSmsParamString("{\"name\":\"" + name + "\"; "
  262. + "\"redate\":\""+redate+"\""
  263. +"}");
  264. req.setRecNum(mobileNo);
  265. req.setSmsTemplateCode("SMS_216427966");
  266. AlibabaAliqinFcSmsNumSendResponse rsp = null;
  267. try {
  268. rsp = client.execute(req);
  269. } catch (ApiException e) {
  270. logger.error("",e);
  271. return false;
  272. }
  273. // System.out.println(rsp.getBody());
  274. if ((rsp != null) && (rsp.getResult() != null) && rsp.getResult().getSuccess()) {
  275. return true;
  276. } else {
  277. return false;
  278. }
  279. }
  280. // 发送短信的
  281. public static boolean sendSMSAli(String mobileNo, String text) throws Exception {
  282. Client client = ShotMsgUtil.createClient("LTAI5tEupgaUejG7mC6VHNF6", "QlRvqFRc51OzANL4uYlLx7MKZUB2rx");
  283. SendSmsRequest sendSmsRequest = new SendSmsRequest()
  284. .setPhoneNumbers(mobileNo)
  285. .setSignName("前沿动力")
  286. .setTemplateCode("SMS_221636828")
  287. .setTemplateParam("{code:" + text + "}");
  288. RuntimeOptions runtime = new RuntimeOptions();
  289. try {
  290. // 复制代码运行请自行打印 API 的返回值
  291. client.sendSmsWithOptions(sendSmsRequest, runtime);
  292. } catch (TeaException error) {
  293. // 如有需要,请打印 error
  294. Common.assertAsString(error.message);
  295. } catch (Exception _error) {
  296. TeaException error = new TeaException(_error.getMessage(), _error);
  297. // 如有需要,请打印 error
  298. Common.assertAsString(error.message);
  299. }
  300. return true;
  301. }
  302. /**
  303. * 验证发送短信是否成功
  304. *
  305. * @param returnXml 发送短信后的返回信息
  306. * @return
  307. */
  308. public static boolean isSuccess(String returnXml) {
  309. String returnJson;
  310. try {
  311. returnJson = UtilTools.xml2json(returnXml);
  312. Returnsms returnsms = (Returnsms) UtilTools.json2Obj(returnJson, Returnsms.class);
  313. System.out.println(returnsms.getReturnstatus());
  314. if ("Success".equals(returnsms.getReturnstatus())) {
  315. return true;
  316. } else {
  317. return false;
  318. }
  319. } catch (Exception e) {
  320. e.printStackTrace();
  321. logger.error("xml解析失败", e);
  322. return false;
  323. }
  324. }
  325. public static void main(String[] args) throws Exception {
  326. // String tel = "17180135310";
  327. // String tel = "18410269923";
  328. String tel = "19802329506";
  329. IdentifyingCodeUtil.sendSMSAli(tel,"123");
  330. // boolean result = IdentifyingCodeUtil.sendDS1(tel, "黄先生","2021年5月30日");
  331. // System.out.println(result);
  332. // IdentifyingCodeUtil.sendDS1(tel, "黄先生","2021年5月30日");
  333. // IdentifyingCodeUtil.sendDS2(tel, "黄先生");
  334. // IdentifyingCodeUtil.sendds3(tel, "黄先生","2021年5月30日","2021年6月15日","2021年6月30日","2021年7月30日");
  335. // IdentifyingCodeUtil.sendDS4(tel, "黄先生","2021年5月30日");
  336. }
  337. }