|
@@ -23,101 +23,101 @@ import java.time.format.DateTimeFormatter;
|
|
|
@Component
|
|
|
public class MsgClient {
|
|
|
|
|
|
- @Autowired
|
|
|
- private RestTemplate restTemplate;
|
|
|
-
|
|
|
- @NotBlank
|
|
|
- @Value("${lnxx.sms.tencent.secretId}")
|
|
|
- private String secretId;
|
|
|
-
|
|
|
- @NotBlank
|
|
|
- @Value("${lnxx.sms.tencent.secretKey}")
|
|
|
- private String secretKey;
|
|
|
-
|
|
|
- @NotBlank
|
|
|
- @Value("${lnxx.sms.tencent.loginId}")
|
|
|
- private String loginId;
|
|
|
-
|
|
|
- @NotBlank
|
|
|
- @Value("${lnxx.sms.tencent.registerId}")
|
|
|
- private String registerId;
|
|
|
-
|
|
|
- @NotBlank
|
|
|
- @Value("${lnxx.sms.tencent.notifyId}")
|
|
|
- private String notifyId;
|
|
|
-
|
|
|
- @NotBlank
|
|
|
- @Value("${lnxx.sms.tencent.sdkAppId}")
|
|
|
- private String sdkAppId;
|
|
|
-
|
|
|
- @NotBlank
|
|
|
- @Value("${lnxx.sms.tencent.region}")
|
|
|
- private String region;
|
|
|
-
|
|
|
- @NotBlank
|
|
|
- @Value("${lnxx.sms.tencent.signName}")
|
|
|
- private String signName;
|
|
|
-
|
|
|
- public String sendMsg(String mobile, String content, String signName) {
|
|
|
- if (isValidMobile(mobile)) {
|
|
|
- return "手机号格式不正确";
|
|
|
- }
|
|
|
- if (StringUtils.isEmpty(signName)) {
|
|
|
- signName = "安徽柒零玖";
|
|
|
- }
|
|
|
- try {
|
|
|
- String encodedContent = URLEncoder.encode(content + "【" + signName + "】", "GB2312");
|
|
|
- String url = String.format(
|
|
|
- "https://mb345.com/ws/BatchSend2.aspx?CorpID=HFLKJ0006642&Pwd=zh9527@&Mobile=%s&Content=%s&Cell=&SendTime=",
|
|
|
- mobile, encodedContent);
|
|
|
-
|
|
|
- log.info("发送短信内容: {}", content);
|
|
|
- return restTemplate.getForObject(url, String.class);
|
|
|
-
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
- log.error("短信内容编码失败", e);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("调用短信接口异常", e);
|
|
|
- }
|
|
|
- return "发送失败";
|
|
|
- }
|
|
|
-
|
|
|
- public String sendLoginMsg(String mobile, String captcha) {
|
|
|
- return sendTencentMsg(mobile, loginId, new String[]{captcha, "5"});
|
|
|
- }
|
|
|
-
|
|
|
- public String sendRegisterMsg(String mobile, String captcha) {
|
|
|
- return sendTencentMsg(mobile, registerId, new String[]{captcha});
|
|
|
- }
|
|
|
-
|
|
|
- public String sendNotifyMsg(String mobile, String devName) {
|
|
|
- String nowTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
- String[] timeParts = nowTime.split("[- :]");
|
|
|
- return sendTencentMsg(mobile, notifyId, new String[]{devName, timeParts[0], timeParts[1], timeParts[2], timeParts[3], timeParts[4], timeParts[5]});
|
|
|
- }
|
|
|
-
|
|
|
- private String sendTencentMsg(String mobile, String templateId, String[] templateParams) {
|
|
|
- if (isValidMobile(mobile)) {
|
|
|
- throw new BizException("手机号码格式有误");
|
|
|
- }
|
|
|
- try {
|
|
|
- SmsClient client = new SmsClient(new Credential(secretId, secretKey), region, new ClientProfile());
|
|
|
- SendSmsRequest req = new SendSmsRequest();
|
|
|
- req.setPhoneNumberSet(new String[]{mobile});
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+ @NotBlank
|
|
|
+ @Value("${lnxx.sms.tencent.secretId}")
|
|
|
+ private String secretId;
|
|
|
+
|
|
|
+ @NotBlank
|
|
|
+ @Value("${lnxx.sms.tencent.secretKey}")
|
|
|
+ private String secretKey;
|
|
|
+
|
|
|
+ @NotBlank
|
|
|
+ @Value("${lnxx.sms.tencent.loginId}")
|
|
|
+ private String loginId;
|
|
|
+
|
|
|
+ @NotBlank
|
|
|
+ @Value("${lnxx.sms.tencent.registerId}")
|
|
|
+ private String registerId;
|
|
|
+
|
|
|
+ @NotBlank
|
|
|
+ @Value("${lnxx.sms.tencent.notifyId}")
|
|
|
+ private String notifyId;
|
|
|
+
|
|
|
+ @NotBlank
|
|
|
+ @Value("${lnxx.sms.tencent.sdkAppId}")
|
|
|
+ private String sdkAppId;
|
|
|
+
|
|
|
+ @NotBlank
|
|
|
+ @Value("${lnxx.sms.tencent.region}")
|
|
|
+ private String region;
|
|
|
+
|
|
|
+ @NotBlank
|
|
|
+ @Value("${lnxx.sms.tencent.signName}")
|
|
|
+ private String signName;
|
|
|
+
|
|
|
+ public String sendMsg(String mobile, String content, String signName) {
|
|
|
+ if (isValidMobile(mobile)) {
|
|
|
+ return "手机号格式不正确";
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(signName)) {
|
|
|
+ signName = "安徽柒零玖";
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String encodedContent = URLEncoder.encode(content + "【" + signName + "】", "GB2312");
|
|
|
+ String url = String.format(
|
|
|
+ "https://mb345.com/ws/BatchSend2.aspx?CorpID=HFLKJ0006642&Pwd=zh9527@&Mobile=%s&Content=%s&Cell=&SendTime=",
|
|
|
+ mobile, encodedContent);
|
|
|
+
|
|
|
+ log.info("发送短信内容: {}", content);
|
|
|
+ return restTemplate.getForObject(url, String.class);
|
|
|
+
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ log.error("短信内容编码失败", e);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("调用短信接口异常", e);
|
|
|
+ }
|
|
|
+ return "发送失败";
|
|
|
+ }
|
|
|
+
|
|
|
+ public String sendLoginMsg(String mobile, String captcha) {
|
|
|
+ return sendTencentMsg(mobile, loginId, new String[]{captcha, "5"});
|
|
|
+ }
|
|
|
+
|
|
|
+ public String sendRegisterMsg(String mobile, String captcha) {
|
|
|
+ return sendTencentMsg(mobile, registerId, new String[]{captcha});
|
|
|
+ }
|
|
|
+
|
|
|
+ public String sendNotifyMsg(String mobile, String devName) {
|
|
|
+ String nowTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
+ String[] timeParts = nowTime.split("[- :]");
|
|
|
+ return sendTencentMsg(mobile, notifyId, new String[]{devName, timeParts[0], timeParts[1], timeParts[2], timeParts[3], timeParts[4], timeParts[5]});
|
|
|
+ }
|
|
|
+
|
|
|
+ private String sendTencentMsg(String mobile, String templateId, String[] templateParams) {
|
|
|
+ if (isValidMobile(mobile)) {
|
|
|
+ throw new BizException("手机号码格式有误");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ SmsClient client = new SmsClient(new Credential(secretId, secretKey), region, new ClientProfile());
|
|
|
+ SendSmsRequest req = new SendSmsRequest();
|
|
|
+ req.setPhoneNumberSet(new String[]{mobile});
|
|
|
// req.setSignName("雷能守护小程序");
|
|
|
- req.setSignName(signName);
|
|
|
- req.setTemplateId(templateId);
|
|
|
- req.setTemplateParamSet(templateParams);
|
|
|
- req.setSmsSdkAppId(sdkAppId);
|
|
|
- SendSmsResponse resp = client.SendSms(req);
|
|
|
- return AbstractModel.toJsonString(resp);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("发送腾讯云短信失败", e);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- private boolean isValidMobile(String mobile) {
|
|
|
- return mobile == null || !mobile.matches("^1[3-9]\\d{9}$");
|
|
|
- }
|
|
|
+ req.setSignName(signName);
|
|
|
+ req.setTemplateId(templateId);
|
|
|
+ req.setTemplateParamSet(templateParams);
|
|
|
+ req.setSmsSdkAppId(sdkAppId);
|
|
|
+ SendSmsResponse resp = client.SendSms(req);
|
|
|
+ return AbstractModel.toJsonString(resp);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("发送腾讯云短信失败", e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isValidMobile(String mobile) {
|
|
|
+ return mobile == null || !mobile.matches("^1[3-9]\\d{9}$");
|
|
|
+ }
|
|
|
}
|