微信第三方全网发布,个人分享 写的不好勿喷
先判断是否微信全网发布的测试号进入
//全网发布微信检测
if("gh_3c884a361561".equals(toUserName)){
checkWeixinAllNetworkCheck(request, response, content, requestMap, pc, timestamp, nonce);
}
/**
* 全网发布检测
* @throws Exception
*/
public void checkWeixinAllNetworkCheck(HttpServletRequest request,HttpServletResponse response,
String content,Map<String,String> requestMap,WXBizMsgCrypt pc,String timestamp,String nonce) throws Exception{
String msgType = requestMap.get("MsgType");
String fromUserName = requestMap.get("FromUserName");
String toUserName = requestMap.get("ToUserName");
if(msgType.equals("event")){// 返回类型值,做一下区分
String event = requestMap.get("Event");
System.out.println("------------------------event------------------------");
replyEventMessage(request,response,event,fromUserName,toUserName,pc,timestamp, nonce);
}
if(msgType.equals("text")){ //标示文本消息,
String content2 = requestMap.get("Content");
processTextMessage(request,response,content2,fromUserName,toUserName,pc,timestamp, nonce);//用文本消息去拼接字符串。微信规定
}
}
public void replyEventMessage(HttpServletRequest request, HttpServletResponse response,
String event, String fromUserName, String toUserName,WXBizMsgCrypt pc,String timeStamp, String nonce)
throws Exception {
String content = event + "from_callback";
replyTextMessage(request,response,content,fromUserName,toUserName,pc,timeStamp,nonce);
}
public void processTextMessage(HttpServletRequest request, HttpServletResponse response,
String content,String fromUserName, String toUserName,WXBizMsgCrypt pc,String timeStamp, String nonce)
throws Exception{
if("TESTCOMPONENT_MSG_TYPE_TEXT".equals(content)){
String returnContent = content+"_callback";
System.out.println("------------------------text------------------------");
replyTextMessage(request,response,returnContent,fromUserName,toUserName,pc, timeStamp, nonce);
}else if(StringUtils.startsWithIgnoreCase(content, "QUERY_AUTH_CODE")){
System.out.println("------------------------code------------------------");
response.getWriter().print("");//需在5秒内返回空串表明暂时不回复,然后再立即使用客服消息接口发送消息回复粉丝
log.info("-----------------QUERY_AUTH_CODE类型------------------------------------");
//接下来客服API再回复一次消息
//此时 content字符的内容为是 QUERY_AUTH_CODE
replyApiTextMessage(content.split(":")[1],fromUserName,toUserName);
}
}
public void replyTextMessage(HttpServletRequest request, HttpServletResponse response,
String content,String fromUserName, String toUserName,WXBizMsgCrypt pc,String timeStamp, String nonce)
throws Exception {
Long createTime = System.currentTimeMillis() / 1000;
StringBuffer sb = new StringBuffer(512);
sb.append("<xml>");
sb.append("<ToUserName><![CDATA["+fromUserName+"]]></ToUserName>");
sb.append("<FromUserName><![CDATA["+toUserName+"]]></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("确定发送的XML为:"+replyMsg);
output(response, pc.encryptMsg(replyMsg, timeStamp, nonce));
//return replyMsg;
}
public void replyApiTextMessage(String auth_code, String fromUserName,String toUserName) throws Exception {
// 得到微信授权成功的消息后,应该立刻进行处理!!相关信息只会在首次授权的时候推送过来
String accessToken = wxAuthClient.getAuthAccessToken(wxAuthService.getCommonAccessToken(), auth_code).getAuthorizerAccessToken();
System.out.println("---------------处理api消息----------------------------");
sendKfMsgCheck(fromUserName, "text", auth_code+"_from_api",accessToken);
//log.info("客服发送接口返回值:"+result);
}
/**
* 发送客服消息测试的
* @throws Exception
* @data 2016年7月7日
*/
public String sendKfMsgCheck(String touser ,String msgtype,String value,String accessToken) throws Exception{
Map<String,Object> bigMap = new HashMap<>();
Map<String,Object> smallMap = new HashMap<>();
bigMap.put("touser", touser);
bigMap.put("msgtype", "text");
smallMap.put("content", value);
bigMap.put("text", smallMap);
System.out.println("----------------进入调用客服接口--------------------");
System.out.println("toUser="+touser+".................................................");
String sss = wxClient.sendWeixinMsg(bigMap, accessToken);
return sss;
}
/**
* 工具类:回复微信服务器"文本消息"
* @param response
* @param returnvaleue
*/
public void output(HttpServletResponse response,String returnvaleue){
try {
PrintWriter pw = response.getWriter();
pw.write(returnvaleue);
// System.out.println("****************returnvaleue***************="+returnvaleue);
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}