Commit c1ddbc56 by 刘红梅

发送短信代码22

parent 337d1896
...@@ -17,9 +17,9 @@ public interface SmsService { ...@@ -17,9 +17,9 @@ public interface SmsService {
* @param phone 手机号 * @param phone 手机号
* @return 是否发送成功 * @return 是否发送成功
*/ */
boolean sendSmsCode(String phone); boolean sendSmsCode(String phone,int smsTemplateType);
boolean sendGlySmsCode(String phone); boolean sendGlySmsCode(String phone,int smsTemplateType);
boolean sendGlySmsContent(String phone, String content, String code); boolean sendGlySmsContent(String phone, String content, String code);
......
...@@ -102,7 +102,7 @@ public class LoginServiceImpl implements LoginService { ...@@ -102,7 +102,7 @@ public class LoginServiceImpl implements LoginService {
if(!flag){ if(!flag){
throw new BusinessException(GlobalErrorCodeConstants.CAPTCHA_EXPIRED.getCode(),GlobalErrorCodeConstants.CAPTCHA_EXPIRED.getMsg()); throw new BusinessException(GlobalErrorCodeConstants.CAPTCHA_EXPIRED.getCode(),GlobalErrorCodeConstants.CAPTCHA_EXPIRED.getMsg());
}else{*/ }else{*/
boolean smsfalg = smsService.sendSmsCode(vo.getPhoneNumber()); boolean smsfalg = smsService.sendSmsCode(vo.getPhoneNumber(),vo.getSmsTemplateType());
if(!smsfalg) { if(!smsfalg) {
throw new BusinessException(GlobalErrorCodeConstants.SEND_SMS_ERROR.getCode(),GlobalErrorCodeConstants.SEND_SMS_ERROR.getMsg()); throw new BusinessException(GlobalErrorCodeConstants.SEND_SMS_ERROR.getCode(),GlobalErrorCodeConstants.SEND_SMS_ERROR.getMsg());
} else { } else {
......
...@@ -22,6 +22,8 @@ import java.util.concurrent.CompletableFuture; ...@@ -22,6 +22,8 @@ import java.util.concurrent.CompletableFuture;
@Slf4j @Slf4j
public class SmsServiceImpl implements SmsService { public class SmsServiceImpl implements SmsService {
public static final String SMS_CODE_CONTENT = "您的验证码是:%s(有效期为2分钟),请勿泄露给他人,如非本人操作,请忽略此消息。"; public static final String SMS_CODE_CONTENT = "您的验证码是:%s(有效期为2分钟),请勿泄露给他人,如非本人操作,请忽略此消息。";
public static final String SMS_LOGIN_CODE_CONTENT = "【专家小程序】您正在登录专家小程序,请确认是本人操作。验证码为:%s,10分钟内有效,请勿告知他人。‌";
public static final String SMS_CHANGEPWD_CODE_CONTENT = "【专家小程序】您正在修改登录密码,请确认是本人操作。验证码为:%s,10分钟内有效,请勿告知他人。";
@Autowired @Autowired
private StringRedisTemplate redisTemplate; private StringRedisTemplate redisTemplate;
private final SmsConfig smsConfig; private final SmsConfig smsConfig;
...@@ -51,20 +53,20 @@ public class SmsServiceImpl implements SmsService { ...@@ -51,20 +53,20 @@ public class SmsServiceImpl implements SmsService {
* @param phone 手机号 * @param phone 手机号
* @return 是否发送成功 * @return 是否发送成功
*/ */
public boolean sendSmsCode(String phone) { public boolean sendSmsCode(String phone,int smsTemplateType) {
// 生成6位验证码 // 生成6位验证码
//String code = RandomUtil.randomNumbers(6); //String code = RandomUtil.randomNumbers(6);
String code = "666666"; String code = "666666";
String key = "sms:code:" + phone; String key = "sms:code:" + phone;
redisTemplate.opsForValue().set(key, code, 5,TimeUnit.MINUTES);// 存入 Redis,设置过期时间为5分钟 redisTemplate.opsForValue().set(key, code, 10,TimeUnit.MINUTES);// 存入 Redis,设置过期时间为10分钟
//String storedCode = (String)redisTemplate.opsForValue().get(key); //String storedCode = (String)redisTemplate.opsForValue().get(key);
System.out.println("发送短信验证码:" + phone + " -> " + code);// 模拟发送短信,实际应调用第三方短信服务 System.out.println("发送短信验证码:" + phone + " -> " + code);// 模拟发送短信,实际应调用第三方短信服务
return true; return true;
//return sendGlySmsCode(phone); //return sendGlySmsCode(phone,smsTemplateType);
} }
@Override @Override
public boolean sendGlySmsCode(String phone) { public boolean sendGlySmsCode(String phone,int smsTemplateType) {
// 生成验证码并构建缓存键 // 生成验证码并构建缓存键
String code = SmsUtil.generateVerificationCode(); String code = SmsUtil.generateVerificationCode();
System.out.println("发送短信验证码:" + phone + " -> " + code); System.out.println("发送短信验证码:" + phone + " -> " + code);
...@@ -72,14 +74,18 @@ public class SmsServiceImpl implements SmsService { ...@@ -72,14 +74,18 @@ public class SmsServiceImpl implements SmsService {
try { try {
// 构建短信内容 // 构建短信内容
String content = String.format(SMS_CODE_CONTENT, code); String content = String.format(SMS_LOGIN_CODE_CONTENT, code);
if(smsTemplateType == 2){
//修改密码
content = String.format(SMS_CHANGEPWD_CODE_CONTENT, code);
}
System.out.println("发送短信验证码:" + phone + " -> " + code); System.out.println("发送短信验证码:" + phone + " -> " + code);
log.info("云MAS业务平台 发送手机号: {},短信验证码:{}", phone, code); log.info("云MAS业务平台 发送手机号: {},短信验证码:{}", phone, code);
System.out.println("云MAS业务平台 短信内容: " + content); System.out.println("云MAS业务平台 短信内容: " + content);
boolean result = sendGlySmsContent(phone, content, code); boolean result = sendGlySmsContent(phone, content, code);
if(result){ if(result){
redisTemplate.opsForValue().set(key, code, 2 * 60); // 存入 Redis,设置过期时间为2分钟 redisTemplate.opsForValue().set(key, code, 10 * 60); // 存入 Redis,设置过期时间为10分钟
} }
return result; return result;
} catch (Exception e) { } catch (Exception e) {
...@@ -134,10 +140,10 @@ public class SmsServiceImpl implements SmsService { ...@@ -134,10 +140,10 @@ public class SmsServiceImpl implements SmsService {
*/ */
private Map<String, String> buildRequestBody(String phone, String content, String code) { private Map<String, String> buildRequestBody(String phone, String content, String code) {
Map<String, Object> sms = new HashMap<>(); Map<String, Object> sms = new HashMap<>();
Map<String, String> dataItems = new HashMap<>(); //Map<String, String> dataItems = new HashMap<>();
dataItems.put("code", code); //dataItems.put("code", code);
sms.put("template", "短信验证码"); // sms.put("template", "短信验证码");
sms.put("dataItems", JsonUtils.toJsonString((dataItems))); //sms.put("dataItems", JsonUtils.toJsonString((dataItems)));
sms.put("type", "1"); sms.put("type", "1");
String[] recvNumArray = new String[1]; String[] recvNumArray = new String[1];
recvNumArray[0] = phone; recvNumArray[0] = phone;
......
...@@ -28,6 +28,10 @@ public class LoginVO { ...@@ -28,6 +28,10 @@ public class LoginVO {
@Min(value = 1) @Min(value = 1)
private Integer loginType; private Integer loginType;
@Max(value = 2, message = "短信模版类型只能是1,登录时候发送验证码的短信模版 2.修改密码时候发送验证码的短信模版")
@Min(value = 1)
private Integer smsTemplateType;
//图形验证码的key //图形验证码的key
private String key; private String key;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment