Commit c1ddbc56 by 刘红梅

发送短信代码22

parent 337d1896
......@@ -17,9 +17,9 @@ public interface SmsService {
* @param phone 手机号
* @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);
......
......@@ -102,7 +102,7 @@ public class LoginServiceImpl implements LoginService {
if(!flag){
throw new BusinessException(GlobalErrorCodeConstants.CAPTCHA_EXPIRED.getCode(),GlobalErrorCodeConstants.CAPTCHA_EXPIRED.getMsg());
}else{*/
boolean smsfalg = smsService.sendSmsCode(vo.getPhoneNumber());
boolean smsfalg = smsService.sendSmsCode(vo.getPhoneNumber(),vo.getSmsTemplateType());
if(!smsfalg) {
throw new BusinessException(GlobalErrorCodeConstants.SEND_SMS_ERROR.getCode(),GlobalErrorCodeConstants.SEND_SMS_ERROR.getMsg());
} else {
......
......@@ -22,6 +22,8 @@ import java.util.concurrent.CompletableFuture;
@Slf4j
public class SmsServiceImpl implements SmsService {
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
private StringRedisTemplate redisTemplate;
private final SmsConfig smsConfig;
......@@ -51,20 +53,20 @@ public class SmsServiceImpl implements SmsService {
* @param phone 手机号
* @return 是否发送成功
*/
public boolean sendSmsCode(String phone) {
public boolean sendSmsCode(String phone,int smsTemplateType) {
// 生成6位验证码
//String code = RandomUtil.randomNumbers(6);
String code = "666666";
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);
System.out.println("发送短信验证码:" + phone + " -> " + code);// 模拟发送短信,实际应调用第三方短信服务
return true;
//return sendGlySmsCode(phone);
//return sendGlySmsCode(phone,smsTemplateType);
}
@Override
public boolean sendGlySmsCode(String phone) {
public boolean sendGlySmsCode(String phone,int smsTemplateType) {
// 生成验证码并构建缓存键
String code = SmsUtil.generateVerificationCode();
System.out.println("发送短信验证码:" + phone + " -> " + code);
......@@ -72,14 +74,18 @@ public class SmsServiceImpl implements SmsService {
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);
log.info("云MAS业务平台 发送手机号: {},短信验证码:{}", phone, code);
System.out.println("云MAS业务平台 短信内容: " + content);
boolean result = sendGlySmsContent(phone, content, code);
if(result){
redisTemplate.opsForValue().set(key, code, 2 * 60); // 存入 Redis,设置过期时间为2分钟
redisTemplate.opsForValue().set(key, code, 10 * 60); // 存入 Redis,设置过期时间为10分钟
}
return result;
} catch (Exception e) {
......@@ -134,10 +140,10 @@ public class SmsServiceImpl implements SmsService {
*/
private Map<String, String> buildRequestBody(String phone, String content, String code) {
Map<String, Object> sms = new HashMap<>();
Map<String, String> dataItems = new HashMap<>();
dataItems.put("code", code);
sms.put("template", "短信验证码");
sms.put("dataItems", JsonUtils.toJsonString((dataItems)));
//Map<String, String> dataItems = new HashMap<>();
//dataItems.put("code", code);
// sms.put("template", "短信验证码");
//sms.put("dataItems", JsonUtils.toJsonString((dataItems)));
sms.put("type", "1");
String[] recvNumArray = new String[1];
recvNumArray[0] = phone;
......
......@@ -28,6 +28,10 @@ public class LoginVO {
@Min(value = 1)
private Integer loginType;
@Max(value = 2, message = "短信模版类型只能是1,登录时候发送验证码的短信模版 2.修改密码时候发送验证码的短信模版")
@Min(value = 1)
private Integer smsTemplateType;
//图形验证码的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