Ver Fonte

阿里发送短信

tudc há 3 anos atrás
pai
commit
b1b49ad424

+ 9 - 0
tianhu-system/src/main/java/com/tianhu/system/common/IMessageUtilsService.java

@@ -0,0 +1,9 @@
+package com.tianhu.system.common;
+
+import java.util.Map;
+
+public interface IMessageUtilsService {
+    void sendMessageCode(String mobileNo, String templateCode, Map<String, String> param, String random) throws Exception;
+
+    public String genRandomNum(int card_len);
+}

+ 123 - 0
tianhu-system/src/main/java/com/tianhu/system/common/impl/MessageUtilsServiceImpl.java

@@ -0,0 +1,123 @@
+package com.tianhu.system.common.impl;
+
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+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.redis.common.RedisUtils;
+import com.tianhu.system.common.IMessageUtilsService;
+import com.tianhu.system.domain.PubVerifyCode;
+import com.tianhu.system.service.IPubVerifyCodeService;
+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;
+    //手机号格式校验
+    public static String MOBILE = "^1\\d{10}$";
+    //验证码状态(未校验)
+    static final String  NO_CHECK = "00";
+
+    private static final String accessKeyId = "";
+    private static final String accessKeySecret = "";
+    private static final String signName = "";
+    /**
+     * 发送短信验证码
+     * @param mobileNo         手机号
+     * @param templateCode     手机模板
+     * @param param            参数
+     * @param random           验证码 验证类短息需要传
+     */
+    @Override
+    public void sendMessageCode(String mobileNo, String templateCode, Map<String, String> param, String 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("请勿频繁发送验证码!");
+            }
+        }
+
+        //调试模式
+        boolean isTest = true;
+        String regVal = RedisUtils.getDictValue("pub_verification_code", templateCode);
+
+        PubVerifyCode pubVerifyCode = new PubVerifyCode();
+        pubVerifyCode.setPvcId(IdUtils.fastSimpleUUID());
+        pubVerifyCode.setPvcCode(random);
+        pubVerifyCode.setPvcPhone(mobileNo);
+        pubVerifyCode.setPvcLastTime(DateUtils.getNowDate());
+        pubVerifyCode.setPvcState("00");
+        iPubVerifyCodeService.createPubVerifyCode(pubVerifyCode);
+		if(!isTest) {
+			try {
+                SendSms.sendMessage(accessKeyId, accessKeySecret, mobileNo, signName, regVal, JSONObject.toJSONString(param));
+			} catch (Exception ex) {
+                System.out.println("获取短信验证码失败");
+				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();
+    }
+}

+ 7 - 1
tianhu-system/src/main/java/com/tianhu/system/controller/WxApiController.java

@@ -141,7 +141,9 @@ public class WxApiController {
                         LoginUser sysUserVo = new LoginUser();
                         sysUserVo.setSysUser(sysUser);
                         resMap.put("loginUser", sysUserVo);
-                        tokenService.createToken(sysUserVo);
+
+                        Map<String, Object> map = tokenService.createToken(sysUserVo);
+                        resMap.put("tokenInfo", map);
                         // 只绑定了一家企业
                     } else if (userCompanyRelList.size() == 1) {
                         resMap.put("code", "1");
@@ -178,12 +180,16 @@ public class WxApiController {
 
                         resMap.put("loginUser", sysUserVo);
                         tokenService.createToken(sysUserVo);
+                        Map<String, Object> map = tokenService.createToken(sysUserVo);
+                        resMap.put("tokenInfo", map);
                         // 绑定多加企业
                     } else {
                         LoginUser sysUserVo = new LoginUser();
                         sysUserVo.setSysUser(sysUser);
                         resMap.put("loginUser", sysUserVo);
                         tokenService.createToken(sysUserVo);
+                        Map<String, Object> map = tokenService.createToken(sysUserVo);
+                        resMap.put("tokenInfo", map);
 
                         List<String> companyIdList = new ArrayList<>();
                         for (SysUserCompanyRel companyRel : userCompanyRelList) {