|
|
@@ -52,6 +52,14 @@ public class AliSms {
|
|
|
@Value("${ali.sms.codeTimeOut}")
|
|
|
private Long codeTimeOut;
|
|
|
|
|
|
+ @Value("${ali.sms.templateAccount}")
|
|
|
+ private String templateAccount;
|
|
|
+
|
|
|
+ @Value("${ali.sms.templateAccountCode:}")
|
|
|
+ private String templateAccountCode;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 发送验证码
|
|
|
*
|
|
|
@@ -236,4 +244,105 @@ public class AliSms {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * JSON字符串转义,防止特殊字符破坏JSON格式
|
|
|
+ */
|
|
|
+ private String escapeJsonString(String str) {
|
|
|
+ if (str == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return str.replace("\\", "\\\\")
|
|
|
+ .replace("\"", "\\\"")
|
|
|
+ .replace("\n", "\\n")
|
|
|
+ .replace("\r", "\\r")
|
|
|
+ .replace("\t", "\\t");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送律师短信信息(账号密码通知)
|
|
|
+ * @param phone 手机号
|
|
|
+ * @param userName 用户名
|
|
|
+ * @param password 密码
|
|
|
+ * @return 1-发送成功, null-发送失败
|
|
|
+ */
|
|
|
+ public Integer sendLawyerSms(String phone, String userName, String password) {
|
|
|
+ log.info("AliSmsConfig.sendLawyerSms?phone={}&userName={}", phone, userName);
|
|
|
+ try {
|
|
|
+ Config config = new Config()
|
|
|
+ .setEndpoint(endPoint)
|
|
|
+ .setAccessKeyId(accessKeyId)
|
|
|
+ .setAccessKeySecret(accessKeySecret);
|
|
|
+
|
|
|
+ // 构建模板参数JSON,必须符合阿里云短信模板参数格式
|
|
|
+ // 模板内容:您在律所管理平台的账号已创建成功,账号名称为${userName},登录密码为${password}.
|
|
|
+ // 模板CODE:SMS_499205254
|
|
|
+ // 变量:userName(用户昵称)、password(其他号码)
|
|
|
+ // 参数名必须与模板中的变量名一致(去掉${}),格式必须是有效的JSON
|
|
|
+ String templateParam = String.format("{\"userName\":\"%s\",\"password\":\"%s\"}",
|
|
|
+ escapeJsonString(userName),
|
|
|
+ escapeJsonString(password));
|
|
|
+
|
|
|
+ // 使用账号密码短信模板代码
|
|
|
+ // 优先级:templateAccountCode > templateAccount(如果以SMS_开头)> templateCode
|
|
|
+ String accountTemplateCode;
|
|
|
+ if (templateAccountCode != null && !templateAccountCode.trim().isEmpty()) {
|
|
|
+ accountTemplateCode = templateAccountCode;
|
|
|
+ } else if (templateAccount != null && templateAccount.startsWith("SMS_")) {
|
|
|
+ // 如果templateAccount配置的是模板代码(以SMS_开头),则使用它
|
|
|
+ accountTemplateCode = templateAccount;
|
|
|
+ } else {
|
|
|
+ accountTemplateCode = templateCode;
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("AliSmsConfig.sendLawyerSms 准备发送短信,phone={}, userName={}, templateCode={}, templateParam={}",
|
|
|
+ phone, userName, accountTemplateCode, templateParam);
|
|
|
+
|
|
|
+ // 构建发送请求
|
|
|
+ SendSmsRequest sendSmsRequest = new SendSmsRequest()
|
|
|
+ // 设置签名
|
|
|
+ .setSignName(signName)
|
|
|
+ // 设置模板代码(账号密码短信模板)
|
|
|
+ .setTemplateCode(accountTemplateCode)
|
|
|
+ // 设置手机号
|
|
|
+ .setPhoneNumbers(phone)
|
|
|
+ // 设置模板参数(必须是有效的JSON格式)
|
|
|
+ .setTemplateParam(templateParam);
|
|
|
+
|
|
|
+ // 运行时选择,可以设置不同的属性来配置运行时环境的参数。
|
|
|
+ RuntimeOptions runtime = new RuntimeOptions();
|
|
|
+ Client client = new Client(config);
|
|
|
+ // 调用阿里云短信API
|
|
|
+ SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
|
|
|
+
|
|
|
+ String responseCode = sendSmsResponse.getBody().getCode();
|
|
|
+ String responseMessage = sendSmsResponse.getBody().getMessage();
|
|
|
+ String bizId = sendSmsResponse.getBody().getBizId(); // 业务ID,用于查询发送状态
|
|
|
+
|
|
|
+ // 记录完整的响应信息
|
|
|
+ log.info("AliSmsConfig.sendLawyerSms API响应详情,phone={}, responseCode={}, responseMessage={}, bizId={}",
|
|
|
+ phone, responseCode, responseMessage, bizId);
|
|
|
+
|
|
|
+ if (!"OK".equals(responseCode)) {
|
|
|
+ log.error("AliSmsConfig.sendLawyerSms 短信发送失败,phone={}, userName={}, templateCode={}, templateParam={}, responseCode={}, responseMessage={}, bizId={}",
|
|
|
+ phone, userName, accountTemplateCode, templateParam, responseCode, responseMessage, bizId);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 即使返回OK,也可能存在其他问题,记录详细信息便于排查
|
|
|
+ log.info("AliSmsConfig.sendLawyerSms 短信发送成功(API返回OK),phone={}, userName={}, bizId={}, templateCode={}, templateParam={}",
|
|
|
+ phone, userName, bizId, accountTemplateCode, templateParam);
|
|
|
+
|
|
|
+ // 如果bizId为空,可能是异常情况
|
|
|
+ if (bizId == null || bizId.trim().isEmpty()) {
|
|
|
+ log.warn("AliSmsConfig.sendLawyerSms 警告:API返回OK但bizId为空,可能存在问题,phone={}, responseMessage={}",
|
|
|
+ phone, responseMessage);
|
|
|
+ }
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("AliSmsConfig.sendLawyerSms ERROR phone={}, userName={}, Msg={}", phone, userName, e.getMessage(), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|