|
|
@@ -1,17 +1,14 @@
|
|
|
package com.inspur.customer.service.authcode;
|
|
|
|
|
|
import com.alibaba.cola.dto.Response;
|
|
|
-import com.google.common.collect.Lists;
|
|
|
-import com.google.common.collect.Maps;
|
|
|
import com.inspur.customer.client.authcode.AuthCodeService;
|
|
|
import com.inspur.customer.object.user.authcode.AuthCodeCheckDTO;
|
|
|
import com.inspur.customer.object.user.authcode.AuthCodeCheckResponse;
|
|
|
import com.inspur.customer.object.user.authcode.AuthCodeDTO;
|
|
|
-import com.inspur.inform.client.email.SmsbEmailService;
|
|
|
-import com.inspur.inform.client.sms.SmsbSmsService;
|
|
|
-import com.inspur.inform.object.message.EmailMessage;
|
|
|
-import com.inspur.inform.object.message.SmsMessage;
|
|
|
-import com.inspur.inform.constants.SmsbSmsTemplateType;
|
|
|
+import com.inspur.inform.client.CommonInformService;
|
|
|
+import com.inspur.inform.object.message.AbstractBaseInformMessage;
|
|
|
+import com.inspur.inform.object.message.EmailInformMessage;
|
|
|
+import com.inspur.inform.object.message.SmsInformMessage;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
@@ -19,6 +16,8 @@ import org.apache.dubbo.config.annotation.DubboService;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@@ -33,19 +32,18 @@ import java.util.concurrent.TimeUnit;
|
|
|
@DubboService(interfaceClass = AuthCodeService.class)
|
|
|
public class AuthCodeServiceImpl implements AuthCodeService {
|
|
|
|
|
|
- @DubboReference
|
|
|
- SmsbSmsService smsbSmsService;
|
|
|
+ private static final String AUTH_CODE_MESSAGE_TEMPLATE = "auth_code";
|
|
|
|
|
|
- @DubboReference
|
|
|
- SmsbEmailService smsbEmailService;
|
|
|
+ private static final String AUTH_CODE_CACHE_KEY = "msr:authCode:";
|
|
|
|
|
|
+ @DubboReference
|
|
|
+ private CommonInformService commonInformService;
|
|
|
@Resource
|
|
|
private RedisTemplate<String, String> redisTemplate;
|
|
|
|
|
|
- /**@Description: 电话、邮件发送验证码
|
|
|
- * @param[com.inspur.customer.service.dto.AuthCodeDTO]
|
|
|
+ /**
|
|
|
+ * 电话、邮件发送验证码
|
|
|
* @return com.alibaba.cola.dto.Response
|
|
|
- * @version v1.0
|
|
|
* @author joe wan
|
|
|
* @date 2022/6/29 17:20
|
|
|
*/
|
|
|
@@ -54,43 +52,42 @@ public class AuthCodeServiceImpl implements AuthCodeService {
|
|
|
try{
|
|
|
|
|
|
//生成一个6位验证码
|
|
|
- int authcode = (int)((Math.random() * 9 + 1) * 100000);
|
|
|
- //发送邮件
|
|
|
- if(StringUtils.isNotBlank(authCodeDTO.getEmail())){
|
|
|
- EmailMessage emailMessage = new EmailMessage();
|
|
|
- emailMessage.setSubject("浪潮安播云");
|
|
|
- emailMessage.setEmailContent("【浪潮安播云】您的验证码"+ authcode +",该验证码5分钟内有效,请勿泄漏于他人!");
|
|
|
- emailMessage.setAddressees(Lists.newArrayList(authCodeDTO.getEmail()));
|
|
|
- Boolean aBoolean = smsbEmailService.sendEmail(emailMessage);
|
|
|
- if(aBoolean) {
|
|
|
- redisTemplate.opsForValue().set("smsb:authcode:email:" + authCodeDTO.getEmail(), authcode + "", 5, TimeUnit.MINUTES);
|
|
|
- }
|
|
|
- }
|
|
|
- //发送手机短信
|
|
|
- if(StringUtils.isNotBlank(authCodeDTO.getPhoneNum())){
|
|
|
- SmsMessage smsMessage = new SmsMessage();
|
|
|
- Map<String, String> maps = Maps.newHashMap();
|
|
|
- maps.put("MSRcode",""+authcode);
|
|
|
- smsMessage.setTemplateParam(maps);
|
|
|
- smsMessage.setPhones(Lists.newArrayList(authCodeDTO.getPhoneNum()));
|
|
|
- smsMessage.setSignName("浪潮安播云");
|
|
|
- smsMessage.setTemplateType(SmsbSmsTemplateType.AUTH_CODE);
|
|
|
- Response sendResponse = smsbSmsService.sendWarningMessage(smsMessage);
|
|
|
- if (sendResponse.isSuccess()) {
|
|
|
- redisTemplate.opsForValue().set("smsb:authcode:phoneNum:" + authCodeDTO.getPhoneNum(), authcode + "", 5, TimeUnit.MINUTES);
|
|
|
- }
|
|
|
+ String authCode = String.valueOf(((Math.random() * 9 + 1) * 100000));
|
|
|
+ Boolean sendResult = commonInformService.sendMessage(AUTH_CODE_MESSAGE_TEMPLATE, getMessage(authCode, authCodeDTO));
|
|
|
+ if (Boolean.TRUE.equals(sendResult)) {
|
|
|
+ redisTemplate.opsForValue().set(AUTH_CODE_CACHE_KEY + authCodeDTO.getEmail(), authCode, 5, TimeUnit.MINUTES);
|
|
|
+ return Response.buildSuccess();
|
|
|
+ } else {
|
|
|
+ return Response.buildFailure("500", "send auth code fail");
|
|
|
}
|
|
|
} catch (Exception e){
|
|
|
- log.error("发送验证码报错 --》 {}", authCodeDTO, e);
|
|
|
- return Response.buildFailure("999","发送失败!");
|
|
|
+ log.error("auth code message: {}", authCodeDTO);
|
|
|
+ log.error("fail message: {}", e.getMessage(), e);
|
|
|
+ return Response.buildFailure("500","send auth code fail");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private AbstractBaseInformMessage getMessage(String authCode, AuthCodeDTO authCodeDTO) {
|
|
|
+ if (StringUtils.isNotBlank(authCodeDTO.getEmail())) {
|
|
|
+ EmailInformMessage emailInformMessage = new EmailInformMessage();
|
|
|
+ Map<String, String> params = new HashMap<>(1);
|
|
|
+ params.put("\\{MSR_AUTH_CODE}", String.valueOf(authCode));
|
|
|
+ emailInformMessage.setContentParams(params);
|
|
|
+ emailInformMessage.setAddressees(Collections.singletonList(authCodeDTO.getEmail()));
|
|
|
+ return emailInformMessage;
|
|
|
+ } else {
|
|
|
+ SmsInformMessage smsInformMessage = new SmsInformMessage();
|
|
|
+ Map<String, String> params = new HashMap<>(1);
|
|
|
+ params.put("MSRcode", String.valueOf(authCode));
|
|
|
+ smsInformMessage.setContentParams(params);
|
|
|
+ smsInformMessage.setAddressees(Collections.singletonList(authCodeDTO.getPhoneNum()));
|
|
|
+ return smsInformMessage;
|
|
|
}
|
|
|
- return Response.buildSuccess();
|
|
|
}
|
|
|
|
|
|
- /**@Description: 电话、邮件验证码check
|
|
|
- * @param[com.inspur.customer.service.dto.AuthCodeCheckDTO]
|
|
|
+ /**
|
|
|
+ * 电话、邮件验证码check
|
|
|
* @return com.inspur.customer.object.user.authcode.AuthCodeCheckResponse
|
|
|
- * @version v1.0
|
|
|
* @author joe wan
|
|
|
* @date 2022/6/30 17:56
|
|
|
*/
|
|
|
@@ -99,13 +96,13 @@ public class AuthCodeServiceImpl implements AuthCodeService {
|
|
|
|
|
|
AuthCodeCheckResponse result = new AuthCodeCheckResponse();
|
|
|
if (StringUtils.isNotBlank(authCodeCheckDTO.getPhoneNumAuthCode())) {
|
|
|
- String authcodePhone = redisTemplate.opsForValue().get("smsb:authcode:phoneNum:" + authCodeCheckDTO.getPhoneNum());
|
|
|
+ String authcodePhone = redisTemplate.opsForValue().get(AUTH_CODE_CACHE_KEY + authCodeCheckDTO.getPhoneNum());
|
|
|
if (authCodeCheckDTO.getPhoneNumAuthCode().equals(authcodePhone)) {
|
|
|
result.setCheckPhoneNum(true);
|
|
|
}
|
|
|
}
|
|
|
if (StringUtils.isNotBlank(authCodeCheckDTO.getEmailAuthCode())) {
|
|
|
- String authcodeEmail = redisTemplate.opsForValue().get("smsb:authcode:email:" + authCodeCheckDTO.getEmail());
|
|
|
+ String authcodeEmail = redisTemplate.opsForValue().get(AUTH_CODE_CACHE_KEY + authCodeCheckDTO.getEmail());
|
|
|
if (authCodeCheckDTO.getEmailAuthCode().equals(authcodeEmail)) {
|
|
|
result.setCheckEmail(true);
|
|
|
}
|