package com.platform.common.utils;@b@@b@import com.alibaba.fastjson.JSONObject;@b@import com.aliyun.dysmsapi20170525.Client;@b@import com.aliyun.dysmsapi20170525.models.SendSmsRequest;@b@import com.aliyun.dysmsapi20170525.models.SendSmsResponse;@b@import com.aliyun.teaopenapi.models.Config;@b@import com.aliyun.teautil.models.RuntimeOptions;@b@import com.github.qcloudsms.SmsMultiSender;@b@import com.github.qcloudsms.SmsMultiSenderResult;@b@import com.github.qcloudsms.SmsSingleSender;@b@import com.github.qcloudsms.SmsSingleSenderResult;@b@@b@import java.util.Map;@b@@b@/**@b@ * https://next.api.aliyun.com/api/Dysmsapi/2017-05-25/SendSms?params={}&lang=JAVA@b@ *@b@ * @author lipengjun@b@ */@b@public class SmsUtil {@b@ /**@b@ * 使用AK&SK初始化阿里云SMS账号Client@b@ *@b@ * @param accessKeyId accessKeyId@b@ * @param accessKeySecret accessKeySecret@b@ * @return Client@b@ * @throws Exception@b@ */@b@ public static Client createAliSmsClient(String accessKeyId, String accessKeySecret) throws Exception {@b@ Config config = new Config()@b@ .setAccessKeyId(accessKeyId)@b@ .setAccessKeySecret(accessKeySecret)@b@ .setEndpoint("dysmsapi.aliyuncs.com");@b@ return new Client(config);@b@ }@b@@b@ /**@b@ * 指定模板ID单发短信@b@ * 签名参数未提供或者为空时,会使用默认签名发送短信@b@ *@b@ * @param accessKeyId accessKeyId@b@ * @param accessKeySecret accessKeySecret@b@ * @param phoneNumber 手机号@b@ * @param templateCode 模板CODE@b@ * @param params 参数@b@ * @param smsSign 签名@b@ * @return SmsSingleSenderResult@b@ */@b@ public static SendSmsResponse aliSendSms(String accessKeyId, String accessKeySecret, String phoneNumber, String templateCode, Map<String, Object> params, String smsSign) throws Exception {@b@ Client client = SmsUtil.createAliSmsClient(accessKeyId, accessKeySecret);@b@ SendSmsRequest sendSmsRequest = new SendSmsRequest();@b@ RuntimeOptions runtime = new RuntimeOptions();@b@ sendSmsRequest.setSignName(smsSign);@b@ sendSmsRequest.setPhoneNumbers(phoneNumber);@b@ sendSmsRequest.setTemplateCode(templateCode);@b@ sendSmsRequest.setTemplateParam(JSONObject.toJSONString(params));@b@@b@ return client.sendSmsWithOptions(sendSmsRequest, runtime);@b@ }@b@@b@ /**@b@ * 指定模板ID群发@b@ * 签名参数未提供或者为空时,会使用默认签名发送短信@b@ *@b@ * @param appid appid@b@ * @param appkey appkey@b@ * @param nationCode 国家码,如 86 为中国@b@ * @param phoneNumbers 手机号@b@ * @param templateId 模板ID@b@ * @param params 参数@b@ * @param sign 签名@b@ * @return SmsMultiSenderResult@b@ */@b@ public static SmsMultiSenderResult crSendSms(int appid, String appkey, String nationCode, String[] phoneNumbers, int templateId, String[] params, String sign) {@b@ SmsMultiSenderResult result = new SmsMultiSenderResult();@b@ try {@b@ SmsMultiSender msender = new SmsMultiSender(appid, appkey);@b@ result = msender.sendWithParam(nationCode, phoneNumbers, templateId, params, sign, "", "");@b@ } catch (Exception e) {@b@ // 网络IO错误@b@ e.printStackTrace();@b@ }@b@ return result;@b@ }@b@@b@ /**@b@ * 指定模板ID单发短信@b@ * 签名参数未提供或者为空时,会使用默认签名发送短信@b@ *@b@ * @param appid appid@b@ * @param appkey appkey@b@ * @param nationCode 国家码,如 86 为中国@b@ * @param phoneNumber 手机号@b@ * @param templateId 模板ID@b@ * @param params 参数@b@ * @param smsSign 签名@b@ * @return SmsSingleSenderResult@b@ */@b@ public static SmsSingleSenderResult crSendSms(int appid, String appkey, String nationCode, String phoneNumber, int templateId, String[] params, String smsSign) {@b@ SmsSingleSenderResult result = new SmsSingleSenderResult();@b@ try {@b@ SmsSingleSender ssender = new SmsSingleSender(appid, appkey);@b@ result = ssender.sendWithParam(nationCode, phoneNumber, templateId, params, smsSign, "", "");@b@ } catch (Exception e) {@b@ // 网络IO错误@b@ e.printStackTrace();@b@ }@b@ return result;@b@ }@b@@b@ /**@b@ * 单发短信@b@ *@b@ * @param appid appid@b@ * @param appkey appkey@b@ * @param type 短信类型,0 为普通短信,1 营销短信@b@ * @param nationCode 国家码,如 86 为中国@b@ * @param phoneNumber 不带国家码的手机号列表@b@ * @param msg 短信内容,必须与申请的模板格式一致,否则将返回错误@b@ * @return SmsSingleSenderResult@b@ */@b@ public static SmsSingleSenderResult crSendSms(int appid, String appkey, int type, String nationCode, String phoneNumber, String msg) {@b@ SmsSingleSenderResult result = new SmsSingleSenderResult();@b@ try {@b@ SmsSingleSender ssender = new SmsSingleSender(appid, appkey);@b@ result = ssender.send(type, nationCode, phoneNumber, msg, "", "");@b@ } catch (Exception e) {@b@ // 网络IO错误@b@ e.printStackTrace();@b@ }@b@ return result;@b@ }@b@@b@ /**@b@ * 群发短信@b@ *@b@ * @param appid appid@b@ * @param appkey appkey@b@ * @param type 短信类型,0 为普通短信,1 营销短信@b@ * @param nationCode 国家码,如 86 为中国@b@ * @param phoneNumbers 不带国家码的手机号列表@b@ * @param msg 短信内容,必须与申请的模板格式一致,否则将返回错误@b@ * @return SmsMultiSenderResult@b@ */@b@ public static SmsMultiSenderResult crSendSms(int appid, String appkey, int type, String nationCode, String[] phoneNumbers, String msg) {@b@ SmsMultiSenderResult result = new SmsMultiSenderResult();@b@ try {@b@ SmsMultiSender msender = new SmsMultiSender(appid, appkey);@b@ result = msender.send(type, nationCode, phoneNumbers, msg, "", "");@b@ } catch (Exception e) {@b@ // 网络IO错误@b@ e.printStackTrace();@b@ }@b@ return result;@b@ }@b@}