Browse Source

全网发布

feature/evaluate
zxc 5 years ago
parent
commit
81967ea068
  1. 4
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/WarrantController.java
  2. 4
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/mpaes/WXBizMsgCrypt.java
  3. 2
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/WarrantService.java
  4. 1
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/AppLetAuthorizationServiceImpl.java
  5. 94
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/WarrantServiceImpl.java

4
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/WarrantController.java

@ -27,9 +27,9 @@ public class WarrantController {
private WarrantService warrantService; private WarrantService warrantService;
@PostMapping(value ="/{APPID}/callback") @PostMapping(value ="/{APPID}/callback")
public String acceptMessageAndEvent(HttpServletRequest request, @PathVariable("APPID") String appid, public void acceptMessageAndEvent(HttpServletRequest request, @PathVariable("APPID") String appid,
HttpServletResponse response)throws IOException, DocumentException, AesException { HttpServletResponse response)throws IOException, DocumentException, AesException {
return warrantService.acceptMessageAndEvent(request, appid, response); warrantService.acceptMessageAndEvent(request, appid, response);
} }
} }

4
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/mpaes/WXBizMsgCrypt.java

@ -85,7 +85,7 @@ public class WXBizMsgCrypt {
} }
// 随机生成16位字符串 // 随机生成16位字符串
String getRandomStr() { public String getRandomStr() {
String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random(); Random random = new Random();
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
@ -103,7 +103,7 @@ public class WXBizMsgCrypt {
* @return 加密后base64编码的字符串 * @return 加密后base64编码的字符串
* @throws AesException aes加密失败 * @throws AesException aes加密失败
*/ */
String encrypt(String randomStr, String text) throws AesException { public String encrypt(String randomStr, String text) throws AesException {
ByteGroup byteCollector = new ByteGroup(); ByteGroup byteCollector = new ByteGroup();
byte[] randomStrBytes = randomStr.getBytes(CHARSET); byte[] randomStrBytes = randomStr.getBytes(CHARSET);
byte[] textBytes = text.getBytes(CHARSET); byte[] textBytes = text.getBytes(CHARSET);

2
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/WarrantService.java

@ -13,6 +13,6 @@ import java.io.IOException;
*/ */
public interface WarrantService { public interface WarrantService {
String acceptMessageAndEvent(HttpServletRequest request,String appid,HttpServletResponse response)throws IOException, DocumentException, AesException; void acceptMessageAndEvent(HttpServletRequest request,String appid,HttpServletResponse response)throws IOException, DocumentException, AesException;
} }

1
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/AppLetAuthorizationServiceImpl.java

@ -60,7 +60,6 @@ public class AppLetAuthorizationServiceImpl implements AppLetAuthorizationServic
String userId = tokenDto.getUserId(); String userId = tokenDto.getUserId();
//获取预授权码 //获取预授权码
String preAuthCode = componentVerifyTicketService.preAuthCode(); String preAuthCode = componentVerifyTicketService.preAuthCode();
// String redirectUrl = WxMaCodeConstant.WEB_URL + formDTO.getClientType();
String redirectUrl = String.format(WxMaCodeConstant.WEB_URL,source,formDTO.getClientType()); String redirectUrl = String.format(WxMaCodeConstant.WEB_URL,source,formDTO.getClientType());
String authUrl = String.format(WxMaCodeConstant.API_AUTH_REGISTER_URL, componentAppId, preAuthCode, redirectUrl); String authUrl = String.format(WxMaCodeConstant.API_AUTH_REGISTER_URL, componentAppId, preAuthCode, redirectUrl);
result.setUrl(authUrl); result.setUrl(authUrl);

94
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/WarrantServiceImpl.java

@ -8,6 +8,7 @@ import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest; import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse; import com.dingtalk.api.response.OapiRobotSendResponse;
import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.commons.tools.utils.ConvertUtils;
import com.epmet.commons.tools.utils.HttpClientManager; import com.epmet.commons.tools.utils.HttpClientManager;
import com.epmet.constant.ModuleConstant; import com.epmet.constant.ModuleConstant;
@ -22,6 +23,7 @@ import com.epmet.dto.result.TemplateAndAppIdResultDTO;
import com.epmet.mpaes.AesException; import com.epmet.mpaes.AesException;
import com.epmet.mpaes.WXBizMsgCrypt; import com.epmet.mpaes.WXBizMsgCrypt;
import com.epmet.mpaes.WXXmlToMapUtil; import com.epmet.mpaes.WXXmlToMapUtil;
import com.epmet.mpaes.XMLParse;
import com.epmet.redis.RedisThird; import com.epmet.redis.RedisThird;
import com.epmet.service.WarrantService; import com.epmet.service.WarrantService;
import com.epmet.wxapi.constant.WxMaCodeConstant; import com.epmet.wxapi.constant.WxMaCodeConstant;
@ -37,10 +39,12 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -87,15 +91,16 @@ public class WarrantServiceImpl implements WarrantService {
*/ */
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@Override @Override
public String acceptMessageAndEvent(HttpServletRequest request, String appId, HttpServletResponse response)throws IOException, DocumentException, AesException { public void acceptMessageAndEvent(HttpServletRequest request, String appId, HttpServletResponse response)throws IOException, DocumentException, AesException {
log.info("消息与事件接收URL【代码审核结果】开始执行......");
log.info("appId:"+ appId);
request.setCharacterEncoding(ModuleConstant.UTF8); request.setCharacterEncoding(ModuleConstant.UTF8);
String msgSignature = request.getParameter(ModuleConstant.MSG_SIGNATURE); String msgSignature = request.getParameter(ModuleConstant.MSG_SIGNATURE);
String timeStamp = request.getParameter(ModuleConstant.TIMESTAMP); String timeStamp = request.getParameter(ModuleConstant.TIMESTAMP);
String nonce = request.getParameter(ModuleConstant.NONCE); String nonce = request.getParameter(ModuleConstant.NONCE);
if (!StringUtils.isNotBlank(msgSignature)) { if (!StringUtils.isNotBlank(msgSignature)) {
return ModuleConstant.SUCCESS;// 微信推送给第三方开放平台的消息一定是加过密的,无消息加密无法解密消息 // 微信推送给第三方开放平台的消息一定是加过密的,无消息加密无法解密消息
PrintWriter pw = response.getWriter();
pw.write(ModuleConstant.SUCCESS);
pw.flush();
} }
InputStream inputStream; InputStream inputStream;
String postData = null; String postData = null;
@ -109,7 +114,6 @@ public class WarrantServiceImpl implements WarrantService {
} catch (AesException e) { } catch (AesException e) {
e.printStackTrace(); e.printStackTrace();
} }
log.info(String.format(ThirdRunTimeInfoConstant.MSG,msg));
// 将xml转为map // 将xml转为map
Map<String, Object> result = WXXmlToMapUtil.multilayerXmlToMap(msg); Map<String, Object> result = WXXmlToMapUtil.multilayerXmlToMap(msg);
Map<String,Object> xml = (Map<String, Object>) result.get(ModuleConstant.XML); Map<String,Object> xml = (Map<String, Object>) result.get(ModuleConstant.XML);
@ -117,20 +121,22 @@ public class WarrantServiceImpl implements WarrantService {
log.info(String.format(ThirdRunTimeInfoConstant.MSG_TYPE,msgType)); log.info(String.format(ThirdRunTimeInfoConstant.MSG_TYPE,msgType));
String toUserName = null; String toUserName = null;
String fromUserName = null; String fromUserName = null;
if (msgType.equals(ModuleConstant.EVENT_LOW)) {
String event = xml.get(ModuleConstant.EVENT).toString();
if (xml.containsKey(ModuleConstant.TO_USER_NAME)){ if (xml.containsKey(ModuleConstant.TO_USER_NAME)){
toUserName = xml.get(ModuleConstant.TO_USER_NAME).toString(); toUserName = xml.get(ModuleConstant.TO_USER_NAME).toString();
log.info("toUserName为:" + toUserName);
} }
if (xml.containsKey(ModuleConstant.FROM_USER_NAME)){ if (xml.containsKey(ModuleConstant.FROM_USER_NAME)){
fromUserName = xml.get(ModuleConstant.FROM_USER_NAME).toString(); fromUserName = xml.get(ModuleConstant.FROM_USER_NAME).toString();
log.info("fromUserName为:"+fromUserName);
} }
if (msgType.equals(ModuleConstant.EVENT_LOW)) {
String event = xml.get(ModuleConstant.EVENT).toString();
if (event.startsWith(ModuleConstant.WE_APP_AUDIT)) { if (event.startsWith(ModuleConstant.WE_APP_AUDIT)) {
log.info("消息与事件接收URL【代码审核结果】开始执行......");
// TODO 目前来看,msgType = ‘event’ 的是代码审核结果 // TODO 目前来看,msgType = ‘event’ 的是代码审核结果
Long createTime = Long.valueOf(xml.get(ModuleConstant.CREATE_TIME).toString()); Long createTime = Long.valueOf(xml.get(ModuleConstant.CREATE_TIME).toString());
CodeAuditRecordFormDTO codeAuditRecord = ConvertUtils.mapToEntity(xml, CodeAuditRecordFormDTO.class); CodeAuditRecordFormDTO codeAuditRecord = ConvertUtils.mapToEntity(xml, CodeAuditRecordFormDTO.class);
codeAuditRecord.setWechatCreateTime(componentVerifyTicketServiceImpl.sToDate(createTime.toString())); codeAuditRecord.setWechatCreateTime(componentVerifyTicketServiceImpl.sToDate(createTime.toString()));
// String toUserName = codeAuditRecord.getToUserName();//小程序原始ID
CustomerIdAndClientResultDTO customerIdAndClientResultDTO = miniInfoDao.selectCustomerIdAndClientByToUserName(toUserName); CustomerIdAndClientResultDTO customerIdAndClientResultDTO = miniInfoDao.selectCustomerIdAndClientByToUserName(toUserName);
String clientType = customerIdAndClientResultDTO.getClientType(); String clientType = customerIdAndClientResultDTO.getClientType();
String customerId = customerIdAndClientResultDTO.getCustomerId(); String customerId = customerIdAndClientResultDTO.getCustomerId();
@ -147,7 +153,6 @@ public class WarrantServiceImpl implements WarrantService {
log.info(String.format(ThirdRunTimeInfoConstant.CODE_AUDIT_RESULT, xml)); log.info(String.format(ThirdRunTimeInfoConstant.CODE_AUDIT_RESULT, xml));
codeAuditRecordDao.insertCodeAuditRecord(codeAuditRecord); codeAuditRecordDao.insertCodeAuditRecord(codeAuditRecord);
// 修改 code_audit_result 中的代码审核结果 // 修改 code_audit_result 中的代码审核结果
// String event = codeAuditRecord.getEvent();
String reason = codeAuditRecord.getReason(); String reason = codeAuditRecord.getReason();
String codeResult = null; String codeResult = null;
switch (event) { switch (event) {
@ -167,9 +172,13 @@ public class WarrantServiceImpl implements WarrantService {
String codeCustomerId = codeCustomerDao.selectCodeCustomerId(codeAuditRecord); String codeCustomerId = codeCustomerDao.selectCodeCustomerId(codeAuditRecord);
codeAuditResultDao.updateAuditResult(customerId, codeCustomerId, codeResult); codeAuditResultDao.updateAuditResult(customerId, codeCustomerId, codeResult);
log.info("消息与事件接收URL【代码审核结果】结束......"); log.info("消息与事件接收URL【代码审核结果】结束......");
}else { PrintWriter pw = response.getWriter();
pw.write(ModuleConstant.SUCCESS);
pw.flush();
}/*else {
log.info("==================== ==== event message ========= =");
this.replyEventMessage(request,response,event,toUserName,fromUserName); this.replyEventMessage(request,response,event,toUserName,fromUserName);
} }*/
}else if (msgType.equals(ModuleConstant.TEXT)){ }else if (msgType.equals(ModuleConstant.TEXT)){
// TODO 公众号回复消息 // TODO 公众号回复消息
MessagePushTextFormDTO messagePushTextFormDTO = ConvertUtils.mapToEntity(xml, MessagePushTextFormDTO.class); MessagePushTextFormDTO messagePushTextFormDTO = ConvertUtils.mapToEntity(xml, MessagePushTextFormDTO.class);
@ -179,18 +188,15 @@ public class WarrantServiceImpl implements WarrantService {
// messagePushTextDao.insertMessageText(messagePushTextFormDTO); // messagePushTextDao.insertMessageText(messagePushTextFormDTO);
String content = messagePushTextFormDTO.getContent(); String content = messagePushTextFormDTO.getContent();
log.info(String.format(ThirdRunTimeInfoConstant.CONTENT,content)); log.info(String.format(ThirdRunTimeInfoConstant.CONTENT,content));
/**
* 测试公众号处理用户消息
* 模拟粉丝发送文本消息给专用测试公众号第三方平台方需根据文本消息的内容进行相应的响应
* 1微信模推送给第三方平台方文本消息其中 Content 字段的内容固定为TESTCOMPONENT_MSG_TYPE_TEXT
* 2第三方平台方立马回应文本消息并最终触达粉丝Content 必须固定为TESTCOMPONENT_MSG_TYPE_TEXT_callback
*/
this.processTextMessage(request, response, content, toUserName, fromUserName); this.processTextMessage(request, response, content, toUserName, fromUserName);
} }
return ModuleConstant.SUCCESS;
} }
/*public static void main(String[] args) {
WarrantServiceImpl w = new WarrantServiceImpl();
w.dingDingRobot("haha","weapp_audit_fail","haha","jumian");
}*/
/** /**
* @Description 钉钉机器人报警 * @Description 钉钉机器人报警
* @author zxc * @author zxc
@ -236,7 +242,7 @@ public class WarrantServiceImpl implements WarrantService {
} }
/** /**
* 方法描述: 类型为enevt的时候拼接 * 方法描述: 类型为event的时候拼接
* @param request * @param request
* @param response * @param response
* @param event * @param event
@ -247,6 +253,7 @@ public class WarrantServiceImpl implements WarrantService {
public void replyEventMessage(HttpServletRequest request, HttpServletResponse response, public void replyEventMessage(HttpServletRequest request, HttpServletResponse response,
String event, String toUserName, String fromUserName) String event, String toUserName, String fromUserName)
throws DocumentException, IOException { throws DocumentException, IOException {
log.info("================ event + from_callback...................");
String content = event + ModuleConstant.FROM_CALLBACK; String content = event + ModuleConstant.FROM_CALLBACK;
replyTextMessage(request,response,content,toUserName,fromUserName); replyTextMessage(request,response,content,toUserName,fromUserName);
} }
@ -263,13 +270,24 @@ public class WarrantServiceImpl implements WarrantService {
String content,String toUserName, String fromUserName) String content,String toUserName, String fromUserName)
throws IOException, DocumentException{ throws IOException, DocumentException{
if(ModuleConstant.TESTCOMPONENT_MSG_TYPE_TEXT.equals(content)){ if(ModuleConstant.TESTCOMPONENT_MSG_TYPE_TEXT.equals(content)){
String returnContent = content+ModuleConstant._CALL_BACK; log.info("收到消息,要回复了......");
replyTextMessage(request,response,returnContent,toUserName,fromUserName); // String returnContent = content+ModuleConstant._CALL_BACK;
String returnContent = "TESTCOMPONENT_MSG_TYPE_TEXT_callback";
log.info("拼接的回复内容【普通】:"+returnContent);
String result = replyTextMessage(request, response, returnContent, toUserName, fromUserName);
log.info("加密的回复内容为:"+result);
PrintWriter pw = response.getWriter();
pw.write(result);
pw.flush();
}else if(StringUtils.startsWithIgnoreCase(content, ModuleConstant.QUERY_AUTH_CODE)){ }else if(StringUtils.startsWithIgnoreCase(content, ModuleConstant.QUERY_AUTH_CODE)){
response.getWriter().print("");//需在5秒内返回空串表明暂时不回复,然后再立即使用客服消息接口发送消息回复粉丝 PrintWriter pw = response.getWriter();//需在5秒内返回空串表明暂时不回复,然后再立即使用客服消息接口发送消息回复粉丝
pw.write("");
pw.flush();
log.info(String.format(ThirdRunTimeInfoConstant.TEXT_MESSAGE_LOG_INFO,content,content.split(ThirdRedisKeyConstant.COLON)[NumConstant.ONE],fromUserName,toUserName)); log.info(String.format(ThirdRunTimeInfoConstant.TEXT_MESSAGE_LOG_INFO,content,content.split(ThirdRedisKeyConstant.COLON)[NumConstant.ONE],fromUserName,toUserName));
//接下来客服API再回复一次消息 //接下来客服API再回复一次消息
replyApiTextMessage(content.split(ThirdRedisKeyConstant.COLON)[NumConstant.ONE],toUserName); String[] split = content.split(ThirdRedisKeyConstant.COLON);
replyApiTextMessage(split[NumConstant.ONE],fromUserName);
} }
} }
@ -284,21 +302,30 @@ public class WarrantServiceImpl implements WarrantService {
* @param fromUserName 发送人 * @param fromUserName 发送人
* @author zxc * @author zxc
*/ */
public void replyTextMessage(HttpServletRequest request, HttpServletResponse response, public String replyTextMessage(HttpServletRequest request, HttpServletResponse response,
String content,String toUserName, String fromUserName) String content,String toUserName, String fromUserName)
throws DocumentException, IOException { throws DocumentException, IOException {
Long createTime = System.currentTimeMillis() / NumConstant.ONE_THOUSAND; Long createTime = System.currentTimeMillis() / NumConstant.ONE_THOUSAND;
StringBuffer sb = new StringBuffer(512); StringBuffer sb = new StringBuffer(512);
log.info("********************** encryption start..............");
sb.append("<xml>"); sb.append("<xml>");
sb.append("<ToUserName><![CDATA["+toUserName+"]]></ToUserName>"); sb.append("<ToUserName><![CDATA["+toUserName+"]]></ToUserName>");
sb.append("<FromUserName><![CDATA["+fromUserName+"]]></FromUserName>"); sb.append("<FromUserName><![CDATA["+fromUserName+"]]></FromUserName>");
sb.append("<CreateTime>"+createTime.toString()+"</CreateTime>"); sb.append("<CreateTime>"+createTime+"</CreateTime>");
sb.append("<MsgType><![CDATA[text]]></MsgType>");
sb.append("<Content><![CDATA["+content+"]]></Content>"); sb.append("<Content><![CDATA["+content+"]]></Content>");
sb.append("<MsgType><![CDATA[text]]></MsgType>");
sb.append("</xml>"); sb.append("</xml>");
String replyMsg = sb.toString(); String replyMsg = sb.toString();
log.info(String.format(ThirdRunTimeInfoConstant.SEND_MESSAGE_XML,replyMsg)); log.info(String.format(ThirdRunTimeInfoConstant.SEND_MESSAGE_XML,replyMsg));
returnJSON(replyMsg,response); WXBizMsgCrypt wxBizMsgCrypt;
String generate = null;
try {
wxBizMsgCrypt = new WXBizMsgCrypt(token,aesKey,componentAppId);
generate = wxBizMsgCrypt.encryptMsg(replyMsg, String.valueOf(System.currentTimeMillis()/1000), wxBizMsgCrypt.getRandomStr());
} catch (AesException e) {
e.printStackTrace();
}
return generate;
} }
/** /**
@ -319,15 +346,20 @@ public class WarrantServiceImpl implements WarrantService {
jsonObject.put(ModuleConstant.AUTHORIZATION_CODE, auth_code); jsonObject.put(ModuleConstant.AUTHORIZATION_CODE, auth_code);
String authInfo = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.API_QUERY_AUTH_URL + componentAccessToken, JSON.toJSONString(jsonObject)).getData(); String authInfo = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.API_QUERY_AUTH_URL + componentAccessToken, JSON.toJSONString(jsonObject)).getData();
HashMap<String,Map> hashMap = JSON.parseObject(authInfo, HashMap.class); HashMap<String,Map> hashMap = JSON.parseObject(authInfo, HashMap.class);
if (hashMap.containsKey("errcode")){
throw new RenException("全网发布接入检测【API文本消息回复失败】");
}
Map map = hashMap.get(ModuleConstant.AUTHORIZATION_INFO); Map map = hashMap.get(ModuleConstant.AUTHORIZATION_INFO);
AuthorizationInfoResultDTO authorizationInfoResultDTO = ConvertUtils.mapToEntity(map, AuthorizationInfoResultDTO.class); AuthorizationInfoResultDTO authorizationInfoResultDTO = ConvertUtils.mapToEntity(map, AuthorizationInfoResultDTO.class);
String authorizer_access_token = authorizationInfoResultDTO.getAuthorizer_access_token(); String authorizer_access_token = authorizationInfoResultDTO.getAuthorizer_access_token();
Map<String,Object> msgMap = new HashMap<String,Object>();
String msg = auth_code + "_from_api";
msgMap.put("content", msg);
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("touser",fromUserName); json.put("touser",fromUserName);
json.put("msgtype","text"); json.put("msgtype","text");
json.put("text", "{\"content\":\""+auth_code+ModuleConstant._FROM_API+"\"}"); json.put("text",msgMap);
String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.SEND_MESSAGE_CUSTOM + authorizer_access_token, JSON.toJSONString(json)).getData(); String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.SEND_MESSAGE_CUSTOM + authorizer_access_token, JSON.toJSONString(json)).getData();
log.info("客服发送接口返回值:"+data); log.info("客服发送接口返回值:"+data);
} }
@ -347,7 +379,7 @@ public class WarrantServiceImpl implements WarrantService {
JsonGenerator generator = objectMapper.getJsonFactory(). JsonGenerator generator = objectMapper.getJsonFactory().
createJsonGenerator(response.getOutputStream(), encoding); createJsonGenerator(response.getOutputStream(), encoding);
objectMapper.writeValue(generator, data); objectMapper.writeValue(generator, data);
} catch (IOException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }

Loading…
Cancel
Save