|
@@ -1,135 +0,0 @@
|
|
|
-package com.huyi.service.util.impl;
|
|
|
-
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
-import com.huyi.service.base.entity.PubVerifyCode;
|
|
|
-import com.huyi.service.base.service.IPubVerifyCodeService;
|
|
|
-import com.huyi.service.base.service.ISysConfigService;
|
|
|
-import com.huyi.service.util.IMessageUtilsService;
|
|
|
-import com.tianhu.common.core.aliyun.SendSms;
|
|
|
-import com.tianhu.common.core.exception.BaseException;
|
|
|
-import com.tianhu.common.core.utils.CommonUtil;
|
|
|
-import com.tianhu.common.core.utils.DateUtils;
|
|
|
-import com.tianhu.common.core.utils.IdUtils;
|
|
|
-import com.tianhu.common.core.utils.StringUtils;
|
|
|
-import com.tianhu.common.redis.common.RedisUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.util.*;
|
|
|
-import java.util.regex.Matcher;
|
|
|
-import java.util.regex.Pattern;
|
|
|
-
|
|
|
-/**
|
|
|
- * 消息推送工具类
|
|
|
- *
|
|
|
- * @author tianhu
|
|
|
- */
|
|
|
-@Service
|
|
|
-public class MessageUtilsServiceImpl implements IMessageUtilsService
|
|
|
-{
|
|
|
- @Autowired
|
|
|
- private IPubVerifyCodeService iPubVerifyCodeService;
|
|
|
- @Autowired
|
|
|
- private ISysConfigService configService;
|
|
|
- //手机号格式校验
|
|
|
- public static String MOBILE = "^1\\d{10}$";
|
|
|
- //验证码状态(未校验)
|
|
|
- static final String NO_CHECK = "00";
|
|
|
-
|
|
|
- private static final String ACCESSKEY_ID = "aliyun.message.accessKeyId";
|
|
|
- private static final String ACCESSKEY_SECRET = "aliyun.message.accessKeySecret";
|
|
|
- private static final String SIGN_NAME = "aliyun.message.signName";
|
|
|
- /**
|
|
|
- * 发送短信验证码
|
|
|
- * @param mobileNo 手机号
|
|
|
- * @param templateCode 手机模板
|
|
|
- * @param param 参数
|
|
|
- * @param random 验证码 验证类短息需要传
|
|
|
- */
|
|
|
- @Override
|
|
|
- public void sendMessageCode(String mobileNo, String templateCode, Map<String, String> param, String random)
|
|
|
- {
|
|
|
- // 发送短信验证码
|
|
|
- if (StringUtils.isNotEmpty(random)) {
|
|
|
- if (CommonUtil.isEmpty(mobileNo)) {
|
|
|
- throw new BaseException("手机号不能为空!");
|
|
|
- } else {
|
|
|
- // 校验手机号
|
|
|
- Pattern pattern = Pattern.compile(MOBILE);
|
|
|
- Matcher matcher = pattern.matcher(mobileNo);
|
|
|
- boolean rs = matcher.matches();
|
|
|
- if (!rs) {
|
|
|
- throw new BaseException("手机号格式错误");
|
|
|
- }
|
|
|
- }
|
|
|
- //查询验证码
|
|
|
- LambdaQueryWrapper<PubVerifyCode> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
- queryWrapper.eq(PubVerifyCode::getPvcPhone,mobileNo);
|
|
|
- queryWrapper.eq(PubVerifyCode::getPvcState,NO_CHECK);
|
|
|
- queryWrapper.orderByDesc(PubVerifyCode::getPvcLastTime);
|
|
|
- List<PubVerifyCode> staffInfList = iPubVerifyCodeService.findPubVerifyCodes(queryWrapper);
|
|
|
- // 如果已经发送过短`信验证码,并且在60S以内,稍后再发送
|
|
|
- if(staffInfList.size()>0){
|
|
|
- //取得指定时间间隔后的系统时间
|
|
|
- GregorianCalendar calendar = (GregorianCalendar) Calendar.getInstance();
|
|
|
- calendar.add( Calendar.MINUTE, -1);
|
|
|
- SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
- System.out.println("比较时间"+formatter.format(calendar.getTime()));
|
|
|
- System.out.println("最后时间"+formatter.format(staffInfList.get(0).getPvcLastTime()));
|
|
|
- if(formatter.format(calendar.getTime()).compareTo(formatter.format(staffInfList.get(0).getPvcLastTime()))<0){
|
|
|
- System.out.println("请勿频繁发送验证码");
|
|
|
- throw new BaseException("请勿频繁发送验证码!");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- PubVerifyCode pubVerifyCode = new PubVerifyCode();
|
|
|
- pubVerifyCode.setPvcId(IdUtils.fastSimpleUUID());
|
|
|
- pubVerifyCode.setPvcCode(random);
|
|
|
- pubVerifyCode.setPvcPhone(mobileNo);
|
|
|
- pubVerifyCode.setPvcLastTime(DateUtils.getNowDate());
|
|
|
- pubVerifyCode.setPvcState("00");
|
|
|
- iPubVerifyCodeService.createPubVerifyCode(pubVerifyCode);
|
|
|
- }
|
|
|
-
|
|
|
- //调试模式
|
|
|
- boolean isTest = true;
|
|
|
- String regVal = RedisUtils.getDictValue("pub_verification_code", templateCode);
|
|
|
-
|
|
|
- if(!isTest) {
|
|
|
- try {
|
|
|
- String accessKeyId = configService.selectConfigByKey(ACCESSKEY_ID);
|
|
|
- String accessKeySecret = configService.selectConfigByKey(ACCESSKEY_SECRET);
|
|
|
- String signName = configService.selectConfigByKey(SIGN_NAME);
|
|
|
- SendSms.sendMessage(accessKeyId, accessKeySecret, mobileNo, signName, regVal, JSONObject.toJSONString(param));
|
|
|
- } catch (Exception ex) {
|
|
|
- System.out.println("发送短信验证码失败");
|
|
|
- ex.printStackTrace();
|
|
|
- throw new BaseException("发送短信验证码失败");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String genRandomNum(int card_len){
|
|
|
- //35是因为数组是从0开始的,26个字母+10个数字
|
|
|
- final int maxNum = 36;
|
|
|
- //生成的随机数
|
|
|
- int i;
|
|
|
- //生成的密码的长度
|
|
|
- int count = 0;
|
|
|
- char[] str = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
|
|
|
- StringBuffer pwd = new StringBuffer("");
|
|
|
- Random r = new Random();
|
|
|
- while(count < card_len){
|
|
|
- //生成随机数,取绝对值,防止生成负数 生成的数最大为36-1
|
|
|
- i = Math.abs(r.nextInt(maxNum));
|
|
|
- if (i >= 0 && i < str.length) {
|
|
|
- pwd.append(str[i]);
|
|
|
- count ++;
|
|
|
- }
|
|
|
- }
|
|
|
- return pwd.toString();
|
|
|
- }
|
|
|
-}
|