Commit 3af407cb authored by mlchun's avatar mlchun

1.添加短信推送

parent 4f9f73d8
......@@ -89,7 +89,8 @@ public class PensionOrderServiceImpl extends CrudServiceImpl<PensionOrderDao, Pe
save(pensionOrderDTO);
PensionOrderServiceDTO orderServiceDTO = new PensionOrderServiceDTO().assemblePensionOrderService(pensionOrderDTO);
pensionOrderServiceService.save(orderServiceDTO);
pushMessage.sendMsg(pensionOrderDTO, userService.getUserByUserId(pensionOrderDTO.getCreator()).getOpenid());
// pushMessage.sendWeChatMsg(pensionOrderDTO, userService.getUserByUserId(pensionOrderDTO.getCreator()).getOpenid());
// pushMessage.sendSMSMsg(pensionOrderDTO, userService.getUserByUserId(pensionOrderDTO.getCreator()).getOpenid());
}
@Override
......
......@@ -4,6 +4,8 @@ import io.hmit.modules.serviceOrder.dto.PensionOrderDTO;
public interface PushMessage {
void sendMsg(PensionOrderDTO pensionOrderDTO, String openId);
void sendWeChatMsg(PensionOrderDTO pensionOrderDTO, String openId);
void sendSMSMsg(PensionOrderDTO pensionOrderDTO, String receiver);
}
package io.hmit.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.NameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import java.util.*;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
@Service
public class SmsService {
// public static void main(String[] args) throws Exception {
// SmsService s = new SmsService();
// s.sendSms();
// }
//无需修改,用于格式化鉴权头域,给"X-WSSE"参数赋值
private static final String WSSE_HEADER_FORMAT = "UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\"";
//无需修改,用于格式化鉴权头域,给"Authorization"参数赋值
private static final String AUTH_HEADER_VALUE = "WSSE realm=\"SDP\",profile=\"UsernameToken\",type=\"Appkey\"";
public void sendSms(Map<String,Object> map) throws Exception {
String url = "https://rtcsms.cn-north-1.myhuaweicloud.com:10743/sms/batchSendSms/v1"; //APP接入地址+接口访问URI
String appKey = "l5mVbx22mcBH6ZNJ1362T6eI1Xp5"; //APP_Key
String appSecret = "6CTVWB8EYzOWvvyD9eiWzYo2dWiE"; //APP_Secret
String sender = "8820052030944"; //国内短信签名通道号或国际/港澳台短信通道号
String templateId = map.get("tempId").toString(); //模板ID
String signature = "浙江汇民网络技术"; //签名名称
String receiver = map.get("num").toString();
String statusCallBack = "";
System.out.println(map.get("tempParams").toString());
String templateParas = map.get("tempParams").toString();
//请求Body,不携带签名名称时,signature请填null
String body = buildRequestBody(sender, receiver, templateId, templateParas, statusCallBack, signature);
if (null == body || body.isEmpty()) {
System.out.println("body is null.");
return;
}
//请求Headers中的X-WSSE参数值
String wsseHeader = buildWsseHeader(appKey, appSecret);
if (null == wsseHeader || wsseHeader.isEmpty()) {
System.out.println("wsse header is null.");
return;
}
//如果JDK版本低于1.8,可使用如下代码
//为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
//CloseableHttpClient client = HttpClients.custom()
// .setSSLContext(new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
// @Override
// public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
// return true;
// }
// }).build()).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
//如果JDK版本是1.8,可使用如下代码
//为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
CloseableHttpClient client = HttpClients.custom()
.setSSLContext(new SSLContextBuilder().loadTrustMaterial(null,
(x509CertChain, authType) -> true).build())
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
HttpResponse response = client.execute(RequestBuilder.create("POST")//请求方法POST
.setUri(url)
.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded")
.addHeader(HttpHeaders.AUTHORIZATION, AUTH_HEADER_VALUE)
.addHeader("X-WSSE", wsseHeader)
.setEntity(new StringEntity(body)).build());
System.out.println(response.toString()); //打印响应头域信息
System.out.println(EntityUtils.toString(response.getEntity())); //打印响应消息实体
}
public String buildRequestBody(String sender, String receiver, String templateId, String templateParas,
String statusCallbackUrl, String signature){
if (null == sender || null == receiver || null == templateId || sender.isEmpty() || receiver.isEmpty()
|| templateId.isEmpty()) {
System.out.println("buildRequestBody(): sender, receiver or templateId is null.");
return null;
}
List<NameValuePair> keyValues = new ArrayList<NameValuePair>();
keyValues.add(new BasicNameValuePair("from", sender));
keyValues.add(new BasicNameValuePair("to", receiver));
keyValues.add(new BasicNameValuePair("templateId", templateId));
if (null != templateParas && !templateParas.isEmpty()) {
keyValues.add(new BasicNameValuePair("templateParas", templateParas));
}
if (null != statusCallbackUrl && !statusCallbackUrl.isEmpty()) {
keyValues.add(new BasicNameValuePair("statusCallback", statusCallbackUrl));
}
if (null != signature && !signature.isEmpty()) {
keyValues.add(new BasicNameValuePair("signature", signature));
}
return URLEncodedUtils.format(keyValues, Charset.forName("UTF-8"));
}
public String buildWsseHeader(String appKey, String appSecret) {
if (null == appKey || null == appSecret || appKey.isEmpty() || appSecret.isEmpty()) {
System.out.println("buildWsseHeader(): appKey or appSecret is null.");
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String time = sdf.format(new Date()); //Created
String nonce = UUID.randomUUID().toString().replace("-", ""); //Nonce
byte[] passwordDigest = DigestUtils.sha256(nonce + time + appSecret);
String hexDigest = Hex.encodeHexString(passwordDigest);
//如果JDK版本是1.8,请加载原生Base64类,并使用如下代码
String passwordDigestBase64Str = Base64.getEncoder().encodeToString(hexDigest.getBytes()); //PasswordDigest
//如果JDK版本低于1.8,请加载三方库提供Base64类,并使用如下代码
//String passwordDigestBase64Str = Base64.encodeBase64String(hexDigest.getBytes(Charset.forName("utf-8"))); //PasswordDigest
//若passwordDigestBase64Str中包含换行符,请执行如下代码进行修正
//passwordDigestBase64Str = passwordDigestBase64Str.replaceAll("[\\s*\t\n\r]", "");
return String.format(WSSE_HEADER_FORMAT, appKey, passwordDigestBase64Str, nonce, time);
}
}
......@@ -4,6 +4,7 @@ import io.hmit.config.WeChatMpConfig;
import io.hmit.config.WechatAccountConfig;
import io.hmit.modules.serviceOrder.dto.PensionOrderDTO;
import io.hmit.service.PushMessage;
import io.hmit.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
......@@ -13,8 +14,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Description :
......@@ -32,8 +36,11 @@ public class PushMessageImpl implements PushMessage {
@Autowired
private WechatAccountConfig wechatAccountConfig;
@Autowired
private SmsService smsService;
@Override
public void sendMsg(PensionOrderDTO pensionOrderDTO, String openId) {
public void sendWeChatMsg(PensionOrderDTO pensionOrderDTO, String openId) {
WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
templateMessage.setTemplateId(wechatAccountConfig.getTemplateId());
templateMessage.setToUser(openId);
......@@ -51,4 +58,30 @@ public class PushMessageImpl implements PushMessage {
log.error("【微信模板消息】发送失败,{}", e);
}
}
@Override
public void sendSMSMsg(PensionOrderDTO pensionOrderDTO, String receiver) {
try {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfYMD = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfHms = new SimpleDateFormat("HH:mm:ss");
String ymd = sdfYMD.format(pensionOrderDTO.getAppointmentTime());
String hms = sdfHms.format(pensionOrderDTO.getAppointmentTime());
Map<String,Object> params = new HashMap<>();
params.put("tempId","9ba5e502b15d4fa2b52d287da00b7735");
params.put("num", "18758806780");
StringBuffer sb = new StringBuffer();
sb.append("[");
sb.append("\""+"8"+"\",");
sb.append("\""+ymd+"\",");
sb.append("\""+hms+"\",");
sb.append("\""+pensionOrderDTO.getServiceName()+"\",");
sb.append("\""+pensionOrderDTO.getAppointmentPhone()+"\"]");
params.put("tempParams",sb.toString());
smsService.sendSms(params);
}catch (Exception ignored){}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment