提交 73b616fd 编写于 作者: rushui.chen's avatar rushui.chen

20191014 整理代码,合并发送短信验证码逻辑

上级 cb1674e0
流水线 #15747 已失败 于阶段
in 0 second
...@@ -48,8 +48,7 @@ public class AccountController extends AccountBaseController { ...@@ -48,8 +48,7 @@ public class AccountController extends AccountBaseController {
private Logger logger = LoggerFactory.getLogger(AccountController.class); private Logger logger = LoggerFactory.getLogger(AccountController.class);
@Autowired @Autowired
private AccountService accountService; private AccountService accountService;
@Autowired
private CaptchaService captchaService;
@Autowired @Autowired
@Qualifier("cacheMigrateClient") @Qualifier("cacheMigrateClient")
private ICacheClient redisClient; private ICacheClient redisClient;
...@@ -59,30 +58,6 @@ public class AccountController extends AccountBaseController { ...@@ -59,30 +58,6 @@ public class AccountController extends AccountBaseController {
return "test"; return "test";
} }
@ApiOperation("H5端和PC端获取短信验证码")
@PostMapping("/authCode")
public PicaResponse<String> getAuthCode(@RequestBody EncryptEntity entity) throws Exception {
BaseRequest request = CryptoUtil.decrypt(entity, BaseRequest.class);
String mobilePhone = request.getMobile();
String flag = request.getFlag()+"";
String captchaToken = request.getCaptchaToken();
String captchaAnswer = request.getCaptchaAnswer();
//校验图形验证码
if (!captchaService.acknowledge(captchaToken, captchaAnswer)) {
return PicaResponse.toResponse(null, PicaResultCode.PARAM_IS_INVALID.code(), "图形验证码错误");
}
this.checkMobilePhone(mobilePhone);
String authCode = CommonUtil.createValidateCode(); //随机生成验证码
String message = "您的验证码是" + authCode + ",在10分钟内有效。如非本人操作,请忽略本短信!";
//判断账号是否已经存在
Account account = accountService.getByMobilePhone(mobilePhone);
long senderId = account == null ? 0L : account.getId();
//验证码保存到redis,失效时间10分钟
redisClient.set(this.getAuthCodeKey(mobilePhone, flag), authCode, 600);
//发送短信
super.sendMobileMessage(mobilePhone, message, senderId);
return PicaResponse.toResponse(StringUtils.EMPTY);
}
@ApiOperation("微信登录") @ApiOperation("微信登录")
@PostMapping("/login/wechat") @PostMapping("/login/wechat")
......
package com.pica.cloud.account.account.server.controller; package com.pica.cloud.account.account.server.controller;
import com.pica.cloud.account.account.server.entity.Account;
import com.pica.cloud.account.account.server.entity.AccountInfoEntity; import com.pica.cloud.account.account.server.entity.AccountInfoEntity;
import com.pica.cloud.account.account.server.entity.AccountUnionEntity; import com.pica.cloud.account.account.server.entity.AccountUnionEntity;
import com.pica.cloud.account.account.server.entity.EncryptEntity; import com.pica.cloud.account.account.server.entity.EncryptEntity;
...@@ -8,15 +9,21 @@ import com.pica.cloud.account.account.server.enums.AccountTypeEnum; ...@@ -8,15 +9,21 @@ import com.pica.cloud.account.account.server.enums.AccountTypeEnum;
import com.pica.cloud.account.account.server.exception.AccountException; import com.pica.cloud.account.account.server.exception.AccountException;
import com.pica.cloud.account.account.server.mapper.AccountInfoDetailMapper; import com.pica.cloud.account.account.server.mapper.AccountInfoDetailMapper;
import com.pica.cloud.account.account.server.req.BaseRequest; import com.pica.cloud.account.account.server.req.BaseRequest;
import com.pica.cloud.account.account.server.service.AccountService;
import com.pica.cloud.account.account.server.service.AccountUnionService; import com.pica.cloud.account.account.server.service.AccountUnionService;
import com.pica.cloud.account.account.server.service.CaptchaService;
import com.pica.cloud.account.account.server.util.AESUtil; import com.pica.cloud.account.account.server.util.AESUtil;
import com.pica.cloud.account.account.server.util.AccountUtils; import com.pica.cloud.account.account.server.util.AccountUtils;
import com.pica.cloud.account.account.server.util.CryptoUtil; import com.pica.cloud.account.account.server.util.CryptoUtil;
import com.pica.cloud.foundation.entity.PicaException;
import com.pica.cloud.foundation.entity.PicaResponse; import com.pica.cloud.foundation.entity.PicaResponse;
import com.pica.cloud.foundation.entity.PicaResultCode;
import com.pica.cloud.foundation.redis.ICacheClient; import com.pica.cloud.foundation.redis.ICacheClient;
import com.pica.cloud.foundation.utils.utils.CommonUtil; import com.pica.cloud.foundation.utils.utils.CommonUtil;
import com.pica.cloud.foundation.utils.utils.ValidateUtils;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -26,7 +33,6 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -26,7 +33,6 @@ import org.springframework.web.bind.annotation.RestController;
@Api(description = "短信验证码资源") @Api(description = "短信验证码资源")
@RestController @RestController
@RequestMapping(value = "/authCode")
public class AutoCodeController extends AccountBaseController { public class AutoCodeController extends AccountBaseController {
private final String AUTH_CODE_PREFIX = "authCode-"; private final String AUTH_CODE_PREFIX = "authCode-";
...@@ -37,19 +43,19 @@ public class AutoCodeController extends AccountBaseController { ...@@ -37,19 +43,19 @@ public class AutoCodeController extends AccountBaseController {
@Autowired @Autowired
private AccountUnionService accountUnionService; private AccountUnionService accountUnionService;
@Autowired
private CaptchaService captchaService;
@Autowired
private AccountService accountService;
@Autowired @Autowired
@Qualifier("cacheMigrateClient") @Qualifier("cacheMigrateClient")
private ICacheClient cacheClient; private ICacheClient cacheClient;
/**
* 必须要传递flag类型 @ApiOperation("获取短信验证码,无需图形验证码,如app端")
* @PostMapping(value = "/authCode")
* @param entity
* @return
* @throws Exception
*/
@ApiOperation("App端获取短信验证码")
@PostMapping(value = "")
public PicaResponse getAuthCode(@RequestBody EncryptEntity entity) throws Exception { public PicaResponse getAuthCode(@RequestBody EncryptEntity entity) throws Exception {
BaseRequest request = CryptoUtil.decrypt(entity, BaseRequest.class); BaseRequest request = CryptoUtil.decrypt(entity, BaseRequest.class);
AccountUtils.checkMobilePhone(request.getMobile()); AccountUtils.checkMobilePhone(request.getMobile());
...@@ -57,6 +63,31 @@ public class AutoCodeController extends AccountBaseController { ...@@ -57,6 +63,31 @@ public class AutoCodeController extends AccountBaseController {
return PicaResponse.toResponse(); return PicaResponse.toResponse();
} }
@ApiOperation("获取短信验证码,需要图形验证码,如H5端和PC端;验证码类型 0默认 1注册 2微信登录绑定手机 3修改手机 4重置密码 5忘记密码 7患者招募提交问卷(效验)")
@PostMapping("/account/authCode")
public PicaResponse<String> getAuthCodeWithCaptcha(@RequestBody EncryptEntity entity) throws Exception {
BaseRequest request = CryptoUtil.decrypt(entity, BaseRequest.class);
String mobilePhone = request.getMobile();
String flag = request.getFlag() + "";
String captchaToken = request.getCaptchaToken();
String captchaAnswer = request.getCaptchaAnswer();
//校验图形验证码
if (!captchaService.acknowledge(captchaToken, captchaAnswer)) {
return PicaResponse.toResponse(null, PicaResultCode.PARAM_IS_INVALID.code(), "图形验证码错误");
}
this.checkMobilePhone(mobilePhone);
String authCode = CommonUtil.createValidateCode(); //随机生成验证码
String message = "您的验证码是" + authCode + ",在10分钟内有效。如非本人操作,请忽略本短信!";
//判断账号是否已经存在
Account account = accountService.getByMobilePhone(mobilePhone);
long senderId = account == null ? 0L : account.getId();
//验证码保存到redis,失效时间10分钟
cacheClient.set(this.getAuthCodeKey(mobilePhone, flag), authCode, 600);
//发送短信
super.sendMobileMessage(mobilePhone, message, senderId);
return PicaResponse.toResponse(StringUtils.EMPTY);
}
@ApiOperation("微信获取验证码") @ApiOperation("微信获取验证码")
@PostMapping(value = "/authCode/wechat") @PostMapping(value = "/authCode/wechat")
public PicaResponse getWChatSysCode(@RequestBody EncryptEntity entity) throws Exception { public PicaResponse getWChatSysCode(@RequestBody EncryptEntity entity) throws Exception {
...@@ -89,7 +120,7 @@ public class AutoCodeController extends AccountBaseController { ...@@ -89,7 +120,7 @@ public class AutoCodeController extends AccountBaseController {
Long time = cacheClient.get(this.getAuthCodeKey(mobilePhone, flag.toString()) + "-secure", Long.class); Long time = cacheClient.get(this.getAuthCodeKey(mobilePhone, flag.toString()) + "-secure", Long.class);
if (time == null) { if (time == null) {
processSendAuthCode(mobilePhone, flag, authCodeKeySecure); processSendAuthCode(mobilePhone, flag, authCodeKeySecure);
}else{ } else {
int remainTime = 59 - (int) (System.currentTimeMillis() - time) / 1000; int remainTime = 59 - (int) (System.currentTimeMillis() - time) / 1000;
if (remainTime > 0) { if (remainTime > 0) {
throw new AccountException(AccountExceptionEnum.PICA_SYSCODE_RETRY.getCode(), throw new AccountException(AccountExceptionEnum.PICA_SYSCODE_RETRY.getCode(),
...@@ -124,4 +155,11 @@ public class AutoCodeController extends AccountBaseController { ...@@ -124,4 +155,11 @@ public class AutoCodeController extends AccountBaseController {
private String getAuthCodeKey(String mobilePhone, String flag) { private String getAuthCodeKey(String mobilePhone, String flag) {
return AUTH_CODE_PREFIX + flag + "-" + AESUtil.encryptV0(mobilePhone); return AUTH_CODE_PREFIX + flag + "-" + AESUtil.encryptV0(mobilePhone);
} }
//手机格式校验
private void checkMobilePhone(String mobilePhone) {
if (StringUtils.isBlank(mobilePhone) || !ValidateUtils.isMobile(mobilePhone)) {
throw new PicaException(PicaResultCode.PARAM_IS_INVALID.code(), "请输入正确的手机号");
}
}
} }
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册