package com.yc.phoneQRLogin.action; import com.yc.action.BaseAction; import com.yc.duanxin.Sms; import com.yc.duanxin.SmsParm; import com.yc.exception.ApplicationException; import com.yc.factory.FactoryBean; import com.yc.multiData.SpObserver; import com.yc.sdk.shopping.service.imagedata.ShoppingImageDataIfc; import com.yc.service.BaseService; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Properties; import java.util.concurrent.TimeUnit; /** * 用来执行手机、Email验证码 * * @author xinyb_ * */ @Controller public class verification extends BaseAction { @Autowired @Qualifier("redisTemplate") RedisTemplate redisTemplate; @Autowired ShoppingImageDataIfc imgData; /** * 检查是否存在ERP系统 * @param telephone * @return * @throws Exception */ public static boolean checkUserRegisteredByERP(String telephone,String dbid)throws Exception { try { SpObserver.setDBtoInstance("_"+dbid); BaseService baseService = (BaseService) FactoryBean.getBean("BaseService"); Integer result = baseService.getJdbcTemplate().queryForObject("declare @num int\n" + "select @num=count(1) from _sys_LoginUser where tel='" + telephone + "'\n" + "select @num", Integer.class); return result != null && result > 0; }catch (Exception ex){ throw new ApplicationException(ex.getMessage()); }finally { SpObserver.setDBtoInstance(); } } /** * 手机发送6位随机验证码 xin 2020-5-19 16:17:17 * * @param dbId * @param telephone * @param request * @param response * @throws IOException */ @SuppressWarnings("unchecked") @RequestMapping("/ws/phoneCode.do") public void phoneCode(String dbId, String telephone, int type, HttpServletRequest request, HttpServletResponse response) throws IOException { try { if(StringUtils.isBlank(telephone)){ throw new Exception("手机号不能为空"); } String captcha = request.getParameter("captcha"); String key = request.getParameter("key"); if(StringUtils.isBlank(captcha)){ throw new Exception("图片验证码不能为空"); } if(StringUtils.isBlank(key)){ throw new Exception("图片验证码key不能为空"); } Object o = redisTemplate.opsForValue().get(key); if(o == null){ throw new ApplicationException("图片验证码已失效,请重新刷新"); }else { String txt=o.toString(); if(!captcha.equalsIgnoreCase(txt)){ throw new ApplicationException("图片验证码不正确"); } } //验证是否已在系统存在这个手机号 //"您的手机号[" + telephone + "】还没注册,不能发送请求!" try{ if(!checkUserRegisteredByERP(telephone,dbId)){ this.printJson(response, "{\"code\":-1,\"info\":\"您的手机号[" + telephone + "]还没注册,不能发送请求!\"}"); return; } }catch (Exception ex){ this.printJson(response, "{\"code\":-1,\"info\":\"" + ex.getMessage() + "\"}"); return; } String isRepeat= (String) redisTemplate.opsForValue().get("repeat:" + telephone + ":" + dbId); if(StringUtils.isNotBlank(isRepeat)){//存在表示在一分钟内还没有过期,不能再次发送短信 xin 2023-4-17 09:57:02 throw new Exception("在一分钟内,请不要重复发送"); } redisTemplate.opsForValue().set("repeat:" + telephone + ":" + dbId, telephone, 1, TimeUnit.MINUTES);// 1分钟后失效 int code = getRandom(); String text = ""; switch (type) { case 1: text = "您的验证码是" + code + ",您正在使用登录功能。该验证码仅用于身份验证,请勿泄露给他人使用。"; break; case 2: text = "您的验证码是" + code + ",您正在更改系统手机号。该验证码仅用于身份验证,请勿泄露给他人使用。"; break; case 3: text = "您的验证码是" + code + ",您正在绑定该手机号。该验证码仅用于身份验证,请勿泄露给他人使用。"; break; case 4: text = "您的验证码是" + code + ",您正在进行找回密码操作。该验证码仅用于身份验证,请勿泄露给他人使用。"; break; case 5: text = "您的验证码是" + code + ",您正在进行解绑操作。该验证码仅用于身份验证,请勿泄露给他人使用。"; break; default: text = "您的验证码是" + code + ",该验证码仅用于身份验证,请勿泄露给他人使用。"; break; } Sms sms = new Sms(); SmsParm smsParm = new SmsParm(); sms.setKey(smsParm.getKey()); sms.setUid(smsParm.getUid()); String res = sms.sendSMS(telephone, text); if ("1".equals(res)) { redisTemplate.opsForValue().set("CODE:" + telephone + ":" + dbId, code, 10, TimeUnit.MINUTES);// 10分钟后失效 this.printJson(response, "{\"code\":0,\"info\":\"手机验证已发送\"}"); } else { this.printJson(response, "{\"code\":-1,\"info\":\"手机验证发送失败,返回集:" + res + "\"}"); } } catch (Exception e) { this.printJson(response, "{\"code\":-1,\"info\":\""+ (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()) + "\"}"); } } /** * 邮箱发送6位随机验证码 xin 2020-5-19 16:17:17 * * @param dbId * @param email * @param request * @param response */ @SuppressWarnings("unchecked") @RequestMapping("/emailCode.do") public void verifyEmail(String dbId, String email, HttpServletRequest request, HttpServletResponse response) { Properties props = new Properties(); props.setProperty("mail.smtp.host", "smtp.qq.com");// 设置邮件服务器主机名 props.put("mail.smtp.host", "smtp.qq.com"); props.put("mail.smtp.auth", "true");// 发送服务器需要身份验证 Session session = Session.getDefaultInstance(props);// 设置环境信息 session.setDebug(true); MimeMessage message = new MimeMessage(session); Multipart multipart = null; BodyPart contentPart = null; Transport transport = null; try { int code = getRandom(); message.setFrom(new InternetAddress("2486080222@qq.com"));// 设置发件人 message.addRecipient(Message.RecipientType.TO, new InternetAddress(email)); message.setSubject("巴士软件邮箱验证码"); multipart = new MimeMultipart();// 设置附件 contentPart = new MimeBodyPart(); contentPart.setContent("你的巴士软件邮箱验证码是:" + code + ",该验证码仅用于身份验证,请勿泄露给他人使用。", "text/html;charset=utf-8"); multipart.addBodyPart(contentPart); message.setContent(multipart); message.saveChanges(); transport = session.getTransport("smtp"); transport.connect("smtp.qq.com", "2486080222@qq.com", "tbshhnlrjexqecid"); transport.sendMessage(message, message.getAllRecipients()); redisTemplate.opsForValue().set("CODE:" + email + ":" + dbId, code, 10, TimeUnit.MINUTES); this.printJson(response, "{\"info\":\"验证码发送成功\"}"); } catch (Exception e) { this.printJson(response, "验证码发送失败:" + e.getMessage()); } } /** * 验证码匹配是否正确 xin 2020-5-19 16:17:17 * * @param code * @param telephone * @param dbId * @param request * @param response * @throws IOException */ @SuppressWarnings("unchecked") @RequestMapping("/ws/getIsCode.do") public void getIsCode(String code, String v, String dbId, HttpServletRequest request, HttpServletResponse response) throws IOException { try { if ("".equals(code)) { this.printJson(response, "{\"info\":\"请输入正确的验证码。\"}"); return; } if (!redisTemplate.hasKey("CODE:" + v + ":" + dbId)) { this.printJson(response, "{\"info\":\"验证码已失效,请重新获取验证码。\"}"); return; } String rediscode = redisTemplate.opsForValue().get("CODE:" + v + ":" + dbId) + ""; if (!code.equals(rediscode)) {// 验证码匹配 this.printJson(response, "{\"info\":\"验证码错误,请输入正确的验证码。\"}"); return; } redisTemplate.delete("CODE:" + v + ":" + dbId);// 删除 this.printJson(response, "{\"info\":1}"); } catch (Exception e) { this.printJson(response, "{\"info\":\"验证过程出现错误:" + (e.getCause() != null ? e.getCause().getMessage() : e.getMessage()) + "\"}"); } } // 生成二维码图片 @SuppressWarnings("unchecked") // @RequestMapping("/ws/qrCode.do") // public void getQeCode(String appId, HttpServletRequest request, HttpServletResponse response) throws Exception { // try { // SpObserver.setDBtoInstance("_" + appId); // QrCodeForAppEntity qrCodeForAppEntity = new QrCodeForAppEntity(); // qrCodeForAppEntity.setAction(QrCodeForAppEntity.Login); // 设置行为 Action // qrCodeForAppEntity.setAuthorCode("SYSTEM"); // qrCodeForAppEntity.setAuthorName("系统管理员"); // qrCodeForAppEntity = GenerationQrCodeForApp.saveQrCodeForApp(qrCodeForAppEntity, false); // String url = imgData.getImageUrl(qrCodeForAppEntity.getQrCodeUnid(), 250, 250, true, false, request); // // 写入一个key到redis,并且设置过期时间为60s // redisTemplate.opsForValue().set(qrCodeForAppEntity.getQrCode(), qrCodeForAppEntity.getQrCode(), 10, // TimeUnit.SECONDS); // redisTemplate.opsForValue().set("STATE" + qrCodeForAppEntity.getQrCode(), 0);// 二维码当前状态 // redisTemplate.opsForValue().set("DBID" + qrCodeForAppEntity.getQrCode(), appId);// 二维码当前数据源ID // this.print(response, "{\"qrcode\":\"" + qrCodeForAppEntity.getQrCode() + "\",\"url\":\"" + url + "\"}"); // } catch (Exception e) { // this.print(response, e.getCause() != null ? (e.getCause().getMessage()).trim() : e.getMessage()); // } finally { // SpObserver.setDBtoInstance(); // } // } /** * 生成随机6位数 * * @return */ private int getRandom() { return (int) ((Math.random() * 9 + 1) * 100000); } }