浏览代码

阿里短信发送

tudc 3 年之前
父节点
当前提交
d19d8988ad

+ 6 - 3
sc-service/src/main/java/com/huyi/service/common/ShortMessageController.java

@@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
+import java.util.HashMap;
 import java.util.Map;
 
 @RestController
@@ -24,10 +25,12 @@ public class ShortMessageController {
     @Log(title = "发送短信", businessType = BusinessType.INSERT)
     @Transactional(rollbackFor=Exception.class)
     @PostMapping("/sendShortMessage")
-    public AjaxResult sendShortMessage(@RequestBody Map<String, String> requestBody,
-                                       HttpServletRequest request) {
+    public AjaxResult sendShortMessage(@RequestBody Map<String, String> requestBody) {
         validateCodeService.checkCapcha(requestBody.get("imgCode"), requestBody.get("uuid"));
-        messageUtilsService.sendMessageCode(requestBody.get("phone"), requestBody.get("verificationCode"), request);
+        String random = messageUtilsService.genRandomNum(6);
+        Map<String, String> param = new HashMap<>();
+        param.put("code", random);
+        messageUtilsService.sendMessageCode(requestBody.get("phone"), requestBody.get("verificationCode"), param,  random);
         return AjaxResult.success();
     }
 }

+ 4 - 1
sc-service/src/main/java/com/huyi/service/util/IMessageUtilsService.java

@@ -1,7 +1,10 @@
 package com.huyi.service.util;
 
 import javax.servlet.http.HttpServletRequest;
+import java.util.Map;
 
 public interface IMessageUtilsService {
-    void sendMessageCode(String mobileNo, String verification_code, HttpServletRequest request) throws Exception;
+    void sendMessageCode(String mobileNo, String templateCode, Map<String, String> param, String random) throws Exception;
+
+    public String genRandomNum(int card_len);
 }

+ 15 - 40
sc-service/src/main/java/com/huyi/service/util/impl/MessageUtilsServiceImpl.java

@@ -1,11 +1,11 @@
 package com.huyi.service.util.impl;
 
-import cn.hutool.http.HttpUtil;
+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.constant.MessageConstants;
 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;
@@ -14,13 +14,8 @@ import com.tianhu.common.redis.common.RedisUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpSession;
 import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.GregorianCalendar;
-import java.util.List;
-import java.util.Random;
+import java.util.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -39,16 +34,18 @@ public class MessageUtilsServiceImpl implements IMessageUtilsService
     //验证码状态(未校验)
     static final String  NO_CHECK = "00";
 
+    private static final String accessKeyId = "";
+    private static final String accessKeySecret = "";
+    private static final String signName = "";
     /**
      * 发送短信验证码
-     * @param mobileNo 手机号码
-     * @param verificationCode 模板id
-     * @param request
-     * @return
-     * @throws Exception
+     * @param mobileNo         手机号
+     * @param templateCode     手机模板
+     * @param param            参数
+     * @param random           验证码 验证类短息需要传
      */
     @Override
-    public void sendMessageCode(String mobileNo, String verificationCode, HttpServletRequest request)
+    public void sendMessageCode(String mobileNo, String templateCode, Map<String, String> param, String random)
     {
         if (CommonUtil.isEmpty(mobileNo)) {
             throw new BaseException("手机号不能为空!");
@@ -61,9 +58,6 @@ public class MessageUtilsServiceImpl implements IMessageUtilsService
                 throw new BaseException("手机号格式错误");
             }
         }
-
-        // 验证是否已经发送验证码
-        HttpSession httpSession = request.getSession();
         //查询验证码
         LambdaQueryWrapper<PubVerifyCode> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(PubVerifyCode::getPvcPhone,mobileNo);
@@ -84,19 +78,9 @@ public class MessageUtilsServiceImpl implements IMessageUtilsService
             }
         }
 
-        // 获取随机数
-        String random = genRandomNum(6);
         //调试模式
         boolean isTest = true;
-        // 获取验证码时间
-        httpSession.setAttribute(MessageConstants.LAST_MOBILE_TIME, DateUtils.getNowDate());
-        // 验证码
-        httpSession.setAttribute(MessageConstants.MOBILE_RANDOM, random);
-        // 手机号
-        httpSession.setAttribute(MessageConstants.MOBILE_NO, mobileNo);
-        String regVal = RedisUtils.getDictValue("pub_verification_code", verificationCode);
-        // 发送短信
-        String msg = regVal.replaceAll("@", random);
+        String regVal = RedisUtils.getDictValue("pub_verification_code", templateCode);
 
         PubVerifyCode pubVerifyCode = new PubVerifyCode();
         pubVerifyCode.setPvcId(IdUtils.fastSimpleUUID());
@@ -106,18 +90,8 @@ public class MessageUtilsServiceImpl implements IMessageUtilsService
         pubVerifyCode.setPvcState("00");
         iPubVerifyCodeService.createPubVerifyCode(pubVerifyCode);
 		if(!isTest) {
-			System.out.println("mobileNo:"+mobileNo+"====ccc======msg:"+msg);
-			StringBuilder sb = new StringBuilder();
-			sb.append("mobile=");
-			sb.append(mobileNo);
-			sb.append("&content=");
-			sb.append(msg);
-			sb.append("&appkey=");
-            String appKey = RedisUtils.getDictValue("pub_jisu_app_key", "000000_app_key");
-			sb.append(appKey);
 			try {
-				System.out.println("======sb:"+ sb.toString());
-				HttpUtil.get("http://api.jisuapi.com/sms/send?" + sb.toString());
+                SendSms.sendMessage(accessKeyId, accessKeySecret, mobileNo, signName, regVal, JSONObject.toJSONString(param));
 			} catch (Exception ex) {
                 System.out.println("获取短信验证码失败");
 				throw new BaseException("获取短信验证码失败");
@@ -125,7 +99,8 @@ public class MessageUtilsServiceImpl implements IMessageUtilsService
 		}
     }
 
-    public static String genRandomNum(int card_len){
+    @Override
+    public String genRandomNum(int card_len){
         //35是因为数组是从0开始的,26个字母+10个数字
         final int maxNum = 36;
         //生成的随机数