Ver Fonte

手机验证码

chao há 4 anos atrás
pai
commit
526e06f07b

+ 10 - 10
src/main/java/com/caimei365/user/controller/BaseApi.java

@@ -5,10 +5,7 @@ import com.caimei365.user.service.BaseService;
 import io.swagger.annotations.*;
 import lombok.RequiredArgsConstructor;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.web.bind.annotation.CrossOrigin;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import java.util.Map;
 
@@ -57,21 +54,24 @@ public class BaseApi {
      *
      * @param mobile           手机号
      * @param activateCodeType 1:找回密码,2:手机号注册机构,3:供应商注册
+     * @param platformType     0:www,1:crm/h5,2:小程序
+     * @param isCheckCaptcha   是否检查图片验证码,0:检查,1:不检查
      * @param imgCode          图片验证码
      * @param token            图片验证码token
-     * @param platformType     0:www,1:crm/h5,2:小程序
      */
     @ApiOperation("获取短信验证码")
     @ApiImplicitParams({
         @ApiImplicitParam(required = true, name = "mobile", value = "手机号"),
         @ApiImplicitParam(required = true, name = "activateCodeType", value = "1:找回密码,2:手机号注册机构,3:供应商注册"),
-        @ApiImplicitParam(required = true, name = "imgCode", value = "图片验证码"),
-        @ApiImplicitParam(required = true, name = "token", value = "图片token"),
-        @ApiImplicitParam(required = true, name = "platformType", value = "0:www,1:crm/h5,2:小程序")
+        @ApiImplicitParam(required = false, name = "platformType", value = "0:www,1:crm/h5,2:小程序"),
+        @ApiImplicitParam(required = false, name = "isCheckCaptcha", value = "是否检查图片验证码,0:检查,1:不检查"),
+        @ApiImplicitParam(required = false, name = "imgCode", value = "图片验证码"),
+        @ApiImplicitParam(required = false, name = "token", value = "图片token")
+
     })
     @GetMapping("/sms/code")
-    public ResponseJson getSmsCode(String mobile, String activateCodeType, String imgCode, String token, Integer platformType) {
-        return baseService.getSmsCode(mobile, activateCodeType, imgCode, token, platformType);
+    public ResponseJson getSmsCode(String mobile, Integer activateCodeType, Integer platformType, Integer isCheckCaptcha, String imgCode, String token) {
+        return baseService.getSmsCode(mobile, activateCodeType, platformType, isCheckCaptcha, imgCode, token);
     }
 
 

+ 3 - 2
src/main/java/com/caimei365/user/service/BaseService.java

@@ -34,10 +34,11 @@ public interface BaseService {
      *
      * @param mobile           手机号
      * @param activateCodeType 1:找回密码,2:手机号注册机构,3:供应商注册
+     * @param platformType     0:www,1:crm/h5,2:小程序
+     * @param isCheckCaptcha   是否检查图片验证码,0:检查,1:不检查
      * @param imgCode          图片验证码
      * @param token            图片验证码token
-     * @param platformType     0:www,1:crm/h5,2:小程序
      * @return void
      */
-    ResponseJson getSmsCode(String mobile, String activateCodeType, String imgCode, String token, Integer platformType);
+    ResponseJson getSmsCode(String mobile, Integer activateCodeType, Integer platformType, Integer isCheckCaptcha, String imgCode, String token);
 }

+ 41 - 38
src/main/java/com/caimei365/user/service/impl/BaseServiceImpl.java

@@ -93,53 +93,56 @@ public class BaseServiceImpl implements BaseService {
      *
      * @param mobile           手机号
      * @param activateCodeType 1:找回密码,2:手机号注册机构,3:供应商注册
+     * @param platformType     0:www,1:crm/h5,2:小程序
+     * @param isCheckCaptcha   是否检查图片验证码,0:检查,1:不检查
      * @param imgCode          图片验证码
      * @param token            图片验证码token
-     * @param platformType     0:www,1:crm/h5,2:小程序
      */
     @Override
-    public ResponseJson getSmsCode(String mobile, String activateCodeType, String imgCode, String token, Integer platformType) {
-        Boolean checkFlag = checkCaptchaImage(token, imgCode, platformType);
-        if (!checkFlag) {
-            return ResponseJson.error("图片验证码错误");
-        } else {
-            // 验证手机号
-            ResponseJson mobileModel = ValidateUtil.validateMobile(mobile);
-            if (mobileModel.getCode() == -1) {
-                return mobileModel;
+    public ResponseJson getSmsCode(String mobile, Integer activateCodeType, Integer platformType, Integer isCheckCaptcha, String imgCode, String token) {
+        if (null != isCheckCaptcha && isCheckCaptcha == 1){
+            Boolean checkFlag = checkCaptchaImage(token, imgCode, platformType);
+            if (!checkFlag) {
+                return ResponseJson.error("图片验证码错误");
             }
-            boolean sendFlag = false;
-            // 生成六位验证码
-            String randomCode = CodeUtil.generateCodeInt(6);
-            // 根据手机号查询用户Id
-            Integer userId = baseMapper.getUserIdByMobile(mobile);
-            if ("1".equals(activateCodeType)) {
-                if (null == userId || userId < 1) {
-                    return ResponseJson.error("该手机号尚未注册");
-                }
-                // 找回密码
-                sendFlag = AliyunSmsUtil.sendSms(mobile, 1, "{code:" + randomCode + "}");
-            } else {
-                if (null != userId && userId > 0) {
-                    return ResponseJson.error("该手机号已被使用");
-                }
-                if ("2".equals(activateCodeType)) {
-                    // 机构用户(自主)注册
-                    sendFlag = AliyunSmsUtil.sendSms(mobile, 8, "{code:"+ randomCode +"}");
-                } else if ("3".equals(activateCodeType)) {
-                    // 供应商(自主)注册
-                    sendFlag = AliyunSmsUtil.sendSms(mobile, 9, "{code:"+ randomCode +"}");
-                } else {
-                    return ResponseJson.error("参数错误:activateCodeType");
-                }
+        }
+        // 验证手机号
+        ResponseJson mobileModel = ValidateUtil.validateMobile(mobile);
+        if (mobileModel.getCode() == -1) {
+            return mobileModel;
+        }
+        boolean sendFlag = false;
+        // 生成六位验证码
+        String randomCode = CodeUtil.generateCodeInt(6);
+        // 根据手机号查询用户Id
+        Integer userId = baseMapper.getUserIdByMobile(mobile);
+        if (1 == activateCodeType) {
+            if (null == userId || userId < 1) {
+                return ResponseJson.error("该手机号尚未注册");
+            }
+            // 找回密码
+            sendFlag = AliyunSmsUtil.sendSms(mobile, 1, "{code:" + randomCode + "}");
+        } else {
+            if (null != userId && userId > 0) {
+                return ResponseJson.error("该手机号已被使用");
             }
-            if (sendFlag) {
-                redisService.set("code:"+mobile, randomCode, 1800L);
-                log.info("发送到:" + mobile + "的短信验证码为: " + randomCode);
+            if (2 == activateCodeType) {
+                // 机构用户(自主)注册
+                sendFlag = AliyunSmsUtil.sendSms(mobile, 8, "{code:"+ randomCode +"}");
+            } else if (3 == activateCodeType) {
+                // 供应商(自主)注册
+                sendFlag = AliyunSmsUtil.sendSms(mobile, 9, "{code:"+ randomCode +"}");
             } else {
-                return ResponseJson.error("验证码发送失败!请稍后重试");
+                return ResponseJson.error("参数错误:activateCodeType");
             }
         }
+        if (sendFlag) {
+            redisService.set("code:"+mobile, randomCode, 1800L);
+            String codeType = activateCodeType == 1 ? "找回密码" : (activateCodeType == 2 ? "注册机构" : "注册供应商");
+            log.info(codeType + ",发送到:" + mobile + "的短信验证码为: " + randomCode);
+        } else {
+            return ResponseJson.error("验证码发送失败!请稍后重试");
+        }
         return ResponseJson.success("发送验证码成功");
     }
 }

+ 2 - 3
src/main/java/com/caimei365/user/utils/AliyunSmsUtil.java

@@ -101,9 +101,8 @@ public class AliyunSmsUtil {
             SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
             if(sendSmsResponse.getCode() != null && "OK".equals(sendSmsResponse.getCode())) {
                 //请求成功
-                log.info(">----阿里云短信接口返回的数据: "
-                          +",Code="+ sendSmsResponse.getCode()
-                          +",Code=" + sendSmsResponse.getCode()
+                log.info(">阿里云短信接口返回的数据: "
+                          +"Code="+ sendSmsResponse.getCode()
                           +",Message=" + sendSmsResponse.getMessage()
                           +",RequestId=" + sendSmsResponse.getRequestId()
                           +",BizId=" + sendSmsResponse.getBizId());