package com.project.system.sms;
|
|
import com.project.common.constant.Constants;
|
import com.project.common.utils.SslUtils;
|
import com.project.system.service.ISysConfigService;
|
import lombok.RequiredArgsConstructor;
|
import org.apache.http.HttpEntity;
|
import org.apache.http.NameValuePair;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.util.EntityUtils;
|
import org.springframework.stereotype.Service;
|
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import static com.project.common.utils.WxPayUtils.createIgnoreSSLHttpClient;
|
|
/**
|
* 短信http接口的java代码调用示例
|
* 基于Apache HttpClient 4.3
|
* @author Mr.Zhao
|
*/
|
|
@Service
|
@RequiredArgsConstructor
|
public class YPSmsApi {
|
|
|
private final ISysConfigService configService;
|
|
|
/**
|
* 智能匹配模板发送地址
|
*/
|
private static final String YP_SMS_URI = "https://sms.yunpian.com/v2/sms/single_send.json";
|
|
/**
|
* 模板发送地址
|
*/
|
private static final String YP_SMS_TMP_URI = "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
|
|
/**
|
* KEY
|
*/
|
private static final String API_KEY = "faf531146ca1e38abacd3862fb3fc32b";
|
|
/**
|
* 签名
|
*/
|
private static final String SIGN = "【沈丘惠企执法】";
|
|
/**
|
* 执法单申请模板
|
*/
|
public static final String APPLY_TMP = SIGN + "{} 提交了新的申请单,请您及时审批!";
|
|
/**
|
* 执法单审核后模板
|
*/
|
public static final String CHECK_TMP = SIGN + "您提交的申请单已被审批人 {}, 请您及时查看!";
|
|
/**
|
* 企业通知模板
|
*/
|
public static final String COMPANY_TMP = SIGN + "{} 单位预计将于 {} 到贵单位进行检查,请提前知晓";
|
|
/**
|
* 企业审核结果通知模板
|
*/
|
public static final String COMPANY_CHECK_SUCCESS_TMP = SIGN + "您的企业注册信息审核已通过,请用申请时填写的联系人手机号登录小程序。";
|
public static final String COMPANY_CHECK_FILE_TMP = SIGN + "您的企业注册信息审核未通过,请重新在小程序提交信息进行审核。";
|
|
/**
|
* 验证码模板
|
*/
|
public static final String CODE_TMP = SIGN + "您的验证码是{}";
|
|
|
|
|
|
/**
|
* 简单发送短信
|
* @param mobile 接受的手机号
|
* @param msg 短信内容
|
*/
|
public String sendSms(String mobile, String msg)
|
{
|
String smsFlag = configService.selectConfigByKey("sys.sms.flag");
|
if ("1".equals(smsFlag)) {
|
Map<String, String> params = new HashMap<>(3);
|
params.put("apikey", API_KEY);
|
params.put("text", msg);
|
params.put("mobile", mobile);
|
return post(YP_SMS_URI, params);
|
}
|
return "";
|
}
|
|
/**
|
* 基于HttpClient 4.3的通用POST方法
|
*
|
* @param url 提交的URL
|
* @param paramsMap 提交<参数,值>Map
|
* @return 提交响应
|
*/
|
public static String post(String url, Map<String, String> paramsMap)
|
{
|
CloseableHttpClient client = createIgnoreSSLHttpClient();
|
|
String responseText = "";
|
CloseableHttpResponse response = null;
|
try {
|
|
HttpPost method = new HttpPost(url);
|
if(method.getURI().getPath().contains("https")){
|
SslUtils.ignoreSsl();
|
}
|
if (paramsMap != null) {
|
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
|
for (Map.Entry<String, String> param : paramsMap.entrySet()) {
|
NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
|
paramList.add(pair);
|
}
|
method.setEntity(new UrlEncodedFormEntity(paramList, Constants.UTF8));
|
}
|
if (client != null) {
|
response = client.execute(method);
|
}
|
HttpEntity entity = null;
|
if (response != null) {
|
entity = response.getEntity();
|
}
|
if (entity != null) {
|
responseText = EntityUtils.toString(entity);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
if (response != null) {
|
response.close();
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
System.out.println(responseText);//此处打印在console后,会给出一个IP地址
|
return responseText;
|
}
|
}
|