|
@@ -5,7 +5,12 @@ import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
|
|
import com.aliyun.tea.TeaException;
|
|
|
import com.aliyun.teautil.models.RuntimeOptions;
|
|
|
import com.rongwei.bscommon.sys.service.AliyunSmsService;
|
|
|
+import com.rongwei.bscommon.sys.service.SendRecordService;
|
|
|
+import com.rongwei.bscommon.sys.utils.QHSEUtils;
|
|
|
+import com.rongwei.bsentity.domain.SendRecordDo;
|
|
|
+import com.rongwei.rwadmincommon.system.vo.SysUserVo;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
@@ -24,6 +29,9 @@ public class AliyunSmsServiceImpl implements AliyunSmsService {
|
|
|
@Value("${aliyun-sms.sign-name:#{null}}")
|
|
|
private String signName;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SendRecordService sendRecordService;
|
|
|
+
|
|
|
private com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
|
|
|
// 工程代码建议使用更安全的无AK方式,凭据配置方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
|
|
com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
|
|
@@ -49,6 +57,7 @@ public class AliyunSmsServiceImpl implements AliyunSmsService {
|
|
|
Map<String, String> templateParamMap, String phone) throws Exception {
|
|
|
String templateParam = com.aliyun.teautil.Common.toJSONString(templateParamMap);
|
|
|
com.aliyun.dysmsapi20170525.Client client = createClient();
|
|
|
+ // 创建请求
|
|
|
SendSmsRequest sendSmsRequest = new SendSmsRequest();
|
|
|
sendSmsRequest.setSignName(signName);
|
|
|
sendSmsRequest.setTemplateCode(templateId);
|
|
@@ -59,6 +68,8 @@ public class AliyunSmsServiceImpl implements AliyunSmsService {
|
|
|
try {
|
|
|
resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
|
|
|
log.info("发送短信结果:{}", com.aliyun.teautil.Common.toJSONString(resp));
|
|
|
+ // 保存发送记录
|
|
|
+ saveSendRecord(resp, sendSmsRequest);
|
|
|
} catch (TeaException error) {
|
|
|
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
|
// 错误 message
|
|
@@ -77,4 +88,23 @@ public class AliyunSmsServiceImpl implements AliyunSmsService {
|
|
|
}
|
|
|
return resp;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存发送记录
|
|
|
+ *
|
|
|
+ * @param resp 发送结果
|
|
|
+ * @param sendSmsRequest 发送请求
|
|
|
+ */
|
|
|
+ private void saveSendRecord(SendSmsResponse resp, SendSmsRequest sendSmsRequest) {
|
|
|
+ SendRecordDo sendRecordDo = new SendRecordDo();
|
|
|
+ sendRecordDo.setResult(com.aliyun.teautil.Common.toJSONString(resp));
|
|
|
+ sendRecordDo.setTemplateCode(sendSmsRequest.getTemplateCode());
|
|
|
+ sendRecordDo.setTemplateParam(sendSmsRequest.getTemplateParam());
|
|
|
+ sendRecordDo.setPhoneNumber(sendSmsRequest.getPhoneNumbers());
|
|
|
+ sendRecordDo.setSignName(sendSmsRequest.getSignName());
|
|
|
+ SysUserVo currentUser = QHSEUtils.getCurrentUser();
|
|
|
+ QHSEUtils.initModelGeneralParameters(sendRecordDo, currentUser);
|
|
|
+ sendRecordService.save(sendRecordDo);
|
|
|
+ }
|
|
|
+
|
|
|
}
|