| | |
| | | @Slf4j |
| | | public class MsgUtils { |
| | | |
| | | @Value("${code.config.accessKeyId}") |
| | | private String accessKeyId; |
| | | @Value("${code.config.accessKeySecret}") |
| | | private String accessKeySecret; |
| | | @Value("${code.config.signName}") |
| | | private String signName; |
| | | @Value("${code.config.templateCode}") |
| | | private String templateCode; |
| | | @Value("${code.config.signNameTest}") |
| | | private String signNameTest; |
| | | @Value("${code.config.templateCodeTest}") |
| | | private String templateCodeTest; |
| | | |
| | | /** |
| | | * 使用AK&SK初始化账号Client |
| | | * @param accessKeyId |
| | | * @param accessKeySecret |
| | | * @return Client |
| | | * @throws Exception |
| | | */ |
| | | public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception { |
| | | Config config = new Config() |
| | | // 您的 AccessKey ID |
| | | .setAccessKeyId(accessKeyId) |
| | | // 您的 AccessKey Secret |
| | | .setAccessKeySecret(accessKeySecret); |
| | | // 访问的域名 |
| | | config.endpoint = "dysmsapi.aliyuncs.com"; |
| | | return new com.aliyun.dysmsapi20170525.Client(config); |
| | | } |
| | | |
| | | public void sendMsg(String phone,String code) throws Exception { |
| | | com.aliyun.dysmsapi20170525.Client client = MsgUtils.createClient(accessKeyId,accessKeySecret); |
| | | SendSmsRequest sendSmsRequest = new SendSmsRequest() |
| | | .setSignName(signName) |
| | | .setTemplateCode(templateCode) |
| | | .setPhoneNumbers(phone) |
| | | .setTemplateParam("{\"code\":\""+code+"\"}"); |
| | | RuntimeOptions runtime = new RuntimeOptions(); |
| | | try { |
| | | // 复制代码运行请自行打印 API 的返回值 |
| | | SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime); |
| | | log.info("短信发送成功:{},{}",sendSmsResponse.getBody().getMessage(),sendSmsResponse.getStatusCode()); |
| | | } catch (TeaException error) { |
| | | // 如有需要,请打印 error |
| | | com.aliyun.teautil.Common.assertAsString(error.message); |
| | | log.info("短信发送失败:{}",error.message); |
| | | } catch (Exception _error) { |
| | | TeaException error = new TeaException(_error.getMessage(), _error); |
| | | // 如有需要,请打印 error |
| | | com.aliyun.teautil.Common.assertAsString(error.message); |
| | | log.info("短信发送失败:{}",error.message); |
| | | } |
| | | } |
| | | // @Value("${code.config.accessKeyId}") |
| | | // private String accessKeyId; |
| | | // @Value("${code.config.accessKeySecret}") |
| | | // private String accessKeySecret; |
| | | // @Value("${code.config.signName}") |
| | | // private String signName; |
| | | // @Value("${code.config.templateCode}") |
| | | // private String templateCode; |
| | | // @Value("${code.config.signNameTest}") |
| | | // private String signNameTest; |
| | | // @Value("${code.config.templateCodeTest}") |
| | | // private String templateCodeTest; |
| | | // |
| | | // /** |
| | | // * 使用AK&SK初始化账号Client |
| | | // * @param accessKeyId |
| | | // * @param accessKeySecret |
| | | // * @return Client |
| | | // * @throws Exception |
| | | // */ |
| | | // public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception { |
| | | // Config config = new Config() |
| | | // // 您的 AccessKey ID |
| | | // .setAccessKeyId(accessKeyId) |
| | | // // 您的 AccessKey Secret |
| | | // .setAccessKeySecret(accessKeySecret); |
| | | // // 访问的域名 |
| | | // config.endpoint = "dysmsapi.aliyuncs.com"; |
| | | // return new com.aliyun.dysmsapi20170525.Client(config); |
| | | // } |
| | | // |
| | | // public void sendMsg(String phone,String code) throws Exception { |
| | | // com.aliyun.dysmsapi20170525.Client client = MsgUtils.createClient(accessKeyId,accessKeySecret); |
| | | // SendSmsRequest sendSmsRequest = new SendSmsRequest() |
| | | // .setSignName(signName) |
| | | // .setTemplateCode(templateCode) |
| | | // .setPhoneNumbers(phone) |
| | | // .setTemplateParam("{\"code\":\""+code+"\"}"); |
| | | // RuntimeOptions runtime = new RuntimeOptions(); |
| | | // try { |
| | | // // 复制代码运行请自行打印 API 的返回值 |
| | | // SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime); |
| | | // log.info("短信发送成功:{},{}",sendSmsResponse.getBody().getMessage(),sendSmsResponse.getStatusCode()); |
| | | // } catch (TeaException error) { |
| | | // // 如有需要,请打印 error |
| | | // com.aliyun.teautil.Common.assertAsString(error.message); |
| | | // log.info("短信发送失败:{}",error.message); |
| | | // } catch (Exception _error) { |
| | | // TeaException error = new TeaException(_error.getMessage(), _error); |
| | | // // 如有需要,请打印 error |
| | | // com.aliyun.teautil.Common.assertAsString(error.message); |
| | | // log.info("短信发送失败:{}",error.message); |
| | | // } |
| | | // } |
| | | } |