|
|
@ -1,18 +1,29 @@ |
|
|
|
package com.epmet.service.impl; |
|
|
|
|
|
|
|
import cn.hutool.json.JSONObject; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.nacos.client.config.utils.IOUtils; |
|
|
|
import com.epmet.commons.tools.constant.NumConstant; |
|
|
|
import com.epmet.commons.tools.utils.ConvertUtils; |
|
|
|
import com.epmet.commons.tools.utils.HttpClientManager; |
|
|
|
import com.epmet.constant.ModuleConstant; |
|
|
|
import com.epmet.constant.ThirdRedisKeyConstant; |
|
|
|
import com.epmet.constant.ThirdRunTimeInfoConstant; |
|
|
|
import com.epmet.dao.*; |
|
|
|
import com.epmet.dto.form.CodeAuditRecordFormDTO; |
|
|
|
import com.epmet.dto.form.MessagePushTextFormDTO; |
|
|
|
import com.epmet.dto.result.AuthorizationInfoResultDTO; |
|
|
|
import com.epmet.dto.result.CustomerIdAndClientResultDTO; |
|
|
|
import com.epmet.dto.result.TemplateAndAppIdResultDTO; |
|
|
|
import com.epmet.mpaes.AesException; |
|
|
|
import com.epmet.mpaes.WXBizMsgCrypt; |
|
|
|
import com.epmet.mpaes.WXXmlToMapUtil; |
|
|
|
import com.epmet.redis.RedisThird; |
|
|
|
import com.epmet.service.WarrantService; |
|
|
|
import com.epmet.wxapi.constant.WxMaCodeConstant; |
|
|
|
import com.fasterxml.jackson.core.JsonEncoding; |
|
|
|
import com.fasterxml.jackson.core.JsonGenerator; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.dom4j.DocumentException; |
|
|
@ -26,6 +37,7 @@ import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
/** |
|
|
@ -56,6 +68,8 @@ public class WarrantServiceImpl implements WarrantService { |
|
|
|
private ComponentVerifyTicketServiceImpl componentVerifyTicketServiceImpl; |
|
|
|
@Autowired |
|
|
|
private MessagePushTextDao messagePushTextDao; |
|
|
|
@Autowired |
|
|
|
private RedisThird redisThird; |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 1.保存代码审核结果 2.更新代码上传结果 |
|
|
@ -134,8 +148,140 @@ public class WarrantServiceImpl implements WarrantService { |
|
|
|
Date date = componentVerifyTicketServiceImpl.sToDate(createTime.toString()); |
|
|
|
messagePushTextFormDTO.setWeChatCreateTime(date); |
|
|
|
messagePushTextDao.insertMessageText(messagePushTextFormDTO); |
|
|
|
// switch (messagePushTextFormDTO.getContent()){
|
|
|
|
// //测试公众号处理用户消息
|
|
|
|
// case ModuleConstant.TESTCOMPONENT_MSG_TYPE_TEXT:
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// if (messagePushTextFormDTO.getContent().startsWith(ModuleConstant.QUERY_AUTH_CODE)){
|
|
|
|
// // 测试公众号使用客服消息接口处理用户消息
|
|
|
|
// String content = messagePushTextFormDTO.getContent();
|
|
|
|
// String contentBefore = content.substring(NumConstant.ZERO, content.indexOf(ThirdRedisKeyConstant.COLON));
|
|
|
|
// //auth_code
|
|
|
|
// String queryAuthCode = content.substring(contentBefore.length() + NumConstant.ONE, content.length());
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
} |
|
|
|
log.info("消息与事件接收URL【代码审核结果】结束......"); |
|
|
|
return ModuleConstant.SUCCESS; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 方法描述: 类型为enevt的时候,拼接 |
|
|
|
* @param request |
|
|
|
* @param response |
|
|
|
* @param event |
|
|
|
* @param toUserName 发送接收人 |
|
|
|
* @param fromUserName 发送人 |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
public void replyEventMessage(HttpServletRequest request, HttpServletResponse response, |
|
|
|
String event, String toUserName, String fromUserName) |
|
|
|
throws DocumentException, IOException { |
|
|
|
String content = event + ModuleConstant.FROM_CALLBACK; |
|
|
|
replyTextMessage(request,response,content,toUserName,fromUserName); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 立马回应文本消息并最终触达粉丝 |
|
|
|
* @param content 文本 |
|
|
|
* @param toUserName 发送接收人 |
|
|
|
* @param fromUserName 发送人 |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
public void processTextMessage(HttpServletRequest request, HttpServletResponse response, |
|
|
|
String content,String toUserName, String fromUserName) |
|
|
|
throws IOException, DocumentException{ |
|
|
|
if(ModuleConstant.TESTCOMPONENT_MSG_TYPE_TEXT.equals(content)){ |
|
|
|
String returnContent = content+ModuleConstant._CALL_BACK; |
|
|
|
replyTextMessage(request,response,returnContent,toUserName,fromUserName); |
|
|
|
}else if(StringUtils.startsWithIgnoreCase(content, ModuleConstant.QUERY_AUTH_CODE)){ |
|
|
|
response.getWriter().print("");//需在5秒内返回空串表明暂时不回复,然后再立即使用客服消息接口发送消息回复粉丝
|
|
|
|
log.info(String.format(ThirdRunTimeInfoConstant.TEXT_MESSAGE_LOG_INFO,content,content.split(ThirdRedisKeyConstant.COLON)[NumConstant.ONE],fromUserName,toUserName)); |
|
|
|
//接下来客服API再回复一次消息
|
|
|
|
replyApiTextMessage(content.split(ThirdRedisKeyConstant.COLON)[NumConstant.ONE],toUserName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 方法描述: 直接返回给微信开放平台 |
|
|
|
* @param request |
|
|
|
* @param response |
|
|
|
* @param content 文本 |
|
|
|
* @param toUserName 发送接收人 |
|
|
|
* @param fromUserName 发送人 |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
public void replyTextMessage(HttpServletRequest request, HttpServletResponse response, |
|
|
|
String content,String toUserName, String fromUserName) |
|
|
|
throws DocumentException, IOException { |
|
|
|
Long createTime = System.currentTimeMillis() / 1000; |
|
|
|
StringBuffer sb = new StringBuffer(512); |
|
|
|
sb.append("<xml>"); |
|
|
|
sb.append("<ToUserName><![CDATA["+toUserName+"]]></ToUserName>"); |
|
|
|
sb.append("<FromUserName><![CDATA["+fromUserName+"]]></FromUserName>"); |
|
|
|
sb.append("<CreateTime>"+createTime.toString()+"</CreateTime>"); |
|
|
|
sb.append("<MsgType><![CDATA[text]]></MsgType>"); |
|
|
|
sb.append("<Content><![CDATA["+content+"]]></Content>"); |
|
|
|
sb.append("</xml>"); |
|
|
|
String replyMsg = sb.toString(); |
|
|
|
log.info(String.format(ThirdRunTimeInfoConstant.SEND_MESSAGE_XML,replyMsg)); |
|
|
|
returnJSON(replyMsg,response); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 方法描述: 调用客服回复消息给粉丝 |
|
|
|
* @param auth_code |
|
|
|
* @param fromUserName |
|
|
|
* @throws DocumentException |
|
|
|
* @throws IOException |
|
|
|
* @return void |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
public void replyApiTextMessage(String auth_code, String fromUserName) throws DocumentException, IOException { |
|
|
|
// 得到微信授权成功的消息后,应该立刻进行处理!!相关信息只会在首次授权的时候推送过来
|
|
|
|
//第三方平台【component_access_token】
|
|
|
|
String componentAccessToken = redisThird.getComponentAccessToken(); |
|
|
|
com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject(); |
|
|
|
jsonObject.put(ModuleConstant.COMPONENT_APPID, componentAppId); |
|
|
|
jsonObject.put(ModuleConstant.AUTHORIZATION_CODE, auth_code); |
|
|
|
String authInfo = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.API_QUERY_AUTH_URL + componentAccessToken, JSON.toJSONString(jsonObject)).getData(); |
|
|
|
HashMap<String,Map> hashMap = JSON.parseObject(authInfo, HashMap.class); |
|
|
|
Map map = hashMap.get(ModuleConstant.AUTHORIZATION_INFO); |
|
|
|
AuthorizationInfoResultDTO authorizationInfoResultDTO = ConvertUtils.mapToEntity(map, AuthorizationInfoResultDTO.class); |
|
|
|
String authorizer_access_token = authorizationInfoResultDTO.getAuthorizer_access_token(); |
|
|
|
|
|
|
|
JSONObject json = new JSONObject(); |
|
|
|
json.put("touser",fromUserName); |
|
|
|
json.put("msgtype", "text"); |
|
|
|
json.put("text", "{\"content\":\""+auth_code+ModuleConstant._FROM_API+"\"}"); |
|
|
|
|
|
|
|
String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.SEND_MESSAGE_CUSTOM + authorizer_access_token, JSON.toJSONString(json)).getData(); |
|
|
|
log.info("客服发送接口返回值:"+data); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 方法描述: 返回数据到请求方 |
|
|
|
* @param data 数据 |
|
|
|
* @param response |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
public void returnJSON(Object data,HttpServletResponse response) { |
|
|
|
try { |
|
|
|
ObjectMapper objectMapper = new ObjectMapper(); |
|
|
|
JsonEncoding encoding = JsonEncoding.UTF8; |
|
|
|
response.setContentType(ModuleConstant.APPLICATION_JSON); |
|
|
|
JsonGenerator generator = objectMapper.getJsonFactory(). |
|
|
|
createJsonGenerator(response.getOutputStream(), encoding); |
|
|
|
objectMapper.writeValue(generator, data); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|