fix: 微信登录验证码及配置修改。
This commit is contained in:
@@ -17,6 +17,7 @@ import com.intc.common.mail.utils.MailUtils;
|
||||
import com.intc.common.ratelimiter.annotation.RateLimiter;
|
||||
import com.intc.common.ratelimiter.enums.LimitType;
|
||||
import com.intc.common.redis.utils.RedisUtils;
|
||||
import com.intc.common.sms.config.properties.SmsProperties;
|
||||
import com.intc.common.web.config.properties.CaptchaProperties;
|
||||
import com.intc.common.web.enums.CaptchaType;
|
||||
import com.intc.web.domain.vo.CaptchaVo;
|
||||
@@ -50,6 +51,7 @@ public class CaptchaController {
|
||||
|
||||
private final CaptchaProperties captchaProperties;
|
||||
private final MailProperties mailProperties;
|
||||
private final SmsProperties smsProperties;
|
||||
|
||||
/**
|
||||
* 短信验证码
|
||||
@@ -60,17 +62,45 @@ public class CaptchaController {
|
||||
@GetMapping("/resource/sms/code")
|
||||
public R<Void> smsCode(@NotBlank(message = "{user.phonenumber.not.blank}") String phonenumber) {
|
||||
String key = GlobalConstants.CAPTCHA_CODE_KEY + phonenumber;
|
||||
String code = RandomUtil.randomNumbers(4);
|
||||
String code = RandomUtil.randomNumbers(6);
|
||||
RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
|
||||
// 验证码模板id 自行处理 (查数据库或写死均可)
|
||||
String templateId = "";
|
||||
// 验证码模板id 从配置文件读取
|
||||
String templateId = smsProperties.getCodeTemplateId();
|
||||
if (StringUtils.isBlank(templateId)) {
|
||||
log.error("短信验证码模板ID未配置,请在配置文件中设置 sms.plus.code-template-id");
|
||||
return R.fail("短信服务配置错误,请联系管理员");
|
||||
}
|
||||
LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
|
||||
map.put("code", code);
|
||||
SmsBlend smsBlend = SmsFactory.getSmsBlend("config1");
|
||||
SmsResponse smsResponse = smsBlend.sendMessage(phonenumber, templateId, map);
|
||||
if (!smsResponse.isSuccess()) {
|
||||
log.error("验证码短信发送异常 => {}", smsResponse);
|
||||
return R.fail(smsResponse.getData().toString());
|
||||
// 解析错误信息
|
||||
String errorMsg = "短信发送失败";
|
||||
if (smsResponse.getData() != null) {
|
||||
String data = smsResponse.getData().toString();
|
||||
if (data.contains("SMS_TEMPLATE_ILLEGAL")) {
|
||||
errorMsg = "短信模板不存在,请联系管理员配置正确的模板ID";
|
||||
} else if (data.contains("SMS_SIGNATURE_ILLEGAL")) {
|
||||
errorMsg = "短信签名不存在,请联系管理员配置正确的签名";
|
||||
} else if (data.contains("Message")) {
|
||||
// 尝试提取Message字段
|
||||
try {
|
||||
int msgStart = data.indexOf("Message\":\"") + 10;
|
||||
int msgEnd = data.indexOf("\"", msgStart);
|
||||
if (msgStart > 10 && msgEnd > msgStart) {
|
||||
String message = data.substring(msgStart, msgEnd);
|
||||
errorMsg = "短信发送失败:" + message;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("解析短信错误信息失败", e);
|
||||
}
|
||||
} else if (data.contains("isv.")) {
|
||||
errorMsg = "短信服务配置错误,请联系管理员";
|
||||
}
|
||||
}
|
||||
return R.fail(errorMsg);
|
||||
}
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user