| | |
| | | package com.yeshi.fanli.util;
|
| | |
|
| | | import com.aliyuncs.CommonRequest;
|
| | | import com.aliyuncs.CommonResponse;
|
| | | import com.aliyuncs.DefaultAcsClient;
|
| | | import com.aliyuncs.IAcsClient;
|
| | | import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
|
| | | import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
| | | import com.aliyuncs.exceptions.ClientException;
|
| | | import com.aliyuncs.exceptions.ServerException;
|
| | | import com.aliyuncs.http.MethodType;
|
| | | import com.aliyuncs.profile.DefaultProfile;
|
| | | import com.yeshi.fanli.exception.SMSException;
|
| | |
|
| | | import net.sf.json.JSONArray;
|
| | | import com.aliyuncs.profile.IClientProfile;
|
| | | import com.yeshi.fanli.exception.config.SMSException;
|
| | | import com.yeshi.fanli.log.LogHelper;
|
| | |
|
| | | //腾讯短信发送
|
| | | public class AliyunSMSUtil {
|
| | |
| | | * @param msg
|
| | | */
|
| | | public static void sendSingleMsg(String phone, String templateCode, String templateParamJson) throws SMSException {
|
| | | DefaultProfile profile = DefaultProfile.getProfile("default", Constant.smsConfig.getAliyunAppId(),
|
| | | IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", Constant.smsConfig.getAliyunAppId(),
|
| | | Constant.smsConfig.getAliyunAppSecret());
|
| | | IAcsClient client = new DefaultAcsClient(profile);
|
| | |
|
| | | CommonRequest request = new CommonRequest();
|
| | | // request.setProtocol(ProtocolType.HTTPS);
|
| | | request.setMethod(MethodType.POST);
|
| | | request.setDomain("dysmsapi.aliyuncs.com");
|
| | | request.setVersion("2017-05-25");
|
| | | request.setAction("SendBatchSms");
|
| | | JSONArray array = new JSONArray();
|
| | | array.add(phone);
|
| | | request.putQueryParameter("PhoneNumberJson", array.toString());
|
| | | request.putQueryParameter("SignNameJson", Constant.smsConfig.getSmsSign());
|
| | | request.putQueryParameter("TemplateCode", templateCode);
|
| | | request.putQueryParameter("TemplateParamJson", templateParamJson);
|
| | | try {
|
| | | CommonResponse response = client.getCommonResponse(request);
|
| | | System.out.println(response.getData());
|
| | | DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
|
| | | } catch (ClientException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | IAcsClient acsClient = new DefaultAcsClient(profile);
|
| | | // 组装请求对象
|
| | | SendSmsRequest request = new SendSmsRequest();
|
| | | // 使用post提交
|
| | | request.setMethod(MethodType.POST);
|
| | | request.setPhoneNumbers(phone);
|
| | | request.setSignName("小影记科技");
|
| | | request.setTemplateCode(templateCode);
|
| | | request.setTemplateParam(templateParamJson);
|
| | | SendSmsResponse sendSmsResponse;
|
| | | try {
|
| | | sendSmsResponse = acsClient.getAcsResponse(request);
|
| | | if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
|
| | | // 请求成功
|
| | | LogHelper.smsInfo("阿里云短信发送成功:" + phone);
|
| | | } else {
|
| | | LogHelper.smsInfo("阿里云短信发送失败:" + phone + "-" + sendSmsResponse.getMessage());
|
| | | throw new SMSException(3, sendSmsResponse.getMessage());
|
| | | }
|
| | | } catch (ServerException e) {
|
| | | e.printStackTrace();
|
| | | } catch (ClientException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | }
|