3,143
社区成员
发帖
与我相关
我的任务
分享
//System.out.println("=进入微信全网发布测试==");
String text=map.get("Encrypt");
String token = "********";
String encodingAesKey = "*********************";
String appId = "*********************";
try {
WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
String xmlMsg=pc.decrypt(text);
Map<String, Object> msgMap=MessageUtil.xml2map(xmlMsg, false);
msgType = (String) msgMap.get("MsgType");
fromUserName=(String) msgMap.get("FromUserName");//这是检测方公众号的粉丝
toUserName = (String) msgMap.get("ToUserName");
String Content=(String) msgMap.get("Content");
if(Content==null)Content="";
JSONObject jsonObject = null;
String url=null;
String json =null;
if("event".equals(msgType)){
String event=(String) msgMap.get("Event");
content = event + "from_callback";
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[event]]></MsgType>");
sb.append("<Content><![CDATA["+content+"]]></Content>");
sb.append("</xml>");
message = sb.toString();
System.out.println("确定发送的XML为:"+message);
out.write(message);
out.flush();
}else{
if("TESTCOMPONENT_MSG_TYPE_TEXT".equals(Content)){
String returnContent = Content+"_callback";
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["+returnContent+"]]></Content>");
sb.append("</xml>");
message = sb.toString();
System.out.println("确定发送的XML为:"+message);
out.write(message);
out.flush();
}else if(Content.startsWith("QUERY_AUTH_CODE")){
Content=Content.replace("QUERY_AUTH_CODE:", "");
String component_access_token=MessageUtil.getComponentAccessToken();
url="https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token="+component_access_token;
String componentAppid = "wxc6d54a94a02cecf0";
json = "{\"component_appid\":\""+componentAppid+"\" ,\"authorization_code\": \""+Content+"\"}";
jsonObject = WeChatUtil.doPostStr(url, json);
if(jsonObject!=null){
message="";
out.print(message);//需在5秒内返回空串表明暂时不回复,然后再立即使用客服消息接口发送消息回复粉丝
out.flush();
//接下来客服API再回复一次消息
String xml=jsonObject.getString("authorization_info");
Map<String, Object> authorizeerMap= JSONObject.fromObject(xml);
//客服发送接口返回值
url="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+authorizeerMap.get("authorizer_access_token");
json = "{\"touser\":\""+fromUserName+"\" ,\"msgtype\": \"text\",\"text\":{\"content\":\""+Content+"_from_api\"}}";
jsonObject = WeChatUtil.doPostStr(url, json);
System.out.println("客服发送接口返回值=="+jsonObject);
}
}
}