微信开放平台的第三方平台、全网发布流程、组件API、返回普通文本消息

时间:2025-03-21 21:13:39

        微信开放平台的第三方平台全网发布流程

1、在这里先吐槽一下微信的开放平台的全网发布流程文档。说实话:这个文档写的是真的不咋地!

 (1)公众号消息校验Token公众号消息加解密Key、公众号消息与事件接收URL、网页开发域名. 这些信息都需要在微信开放平台设置

     (2) 、 加密、解密的 代码在微信开放平台文档里面都有,自己下载即可.

2、啥也不说了,直接上代码:

 

import ;
import ;

import ;
import ;

import ;
import ;

import ;
import ;
import ;
import ;
import ;
import org.;
import org.;
import org.;
import org.;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

/**
 * <p>ClassName: 测试微信 全网检测 </p> 
 * <p>Description: 检测通过后 才能进行全网发布 </p>
 * @author Andy  2015年8月31日
 */
@Controller
@RequestMapping(value = "/weixinOpenCheck")
public class WeixinOpenCheckController {
	
	private static final Log log = ();
	
	@Autowired
	private WxConfigStorageService wxConfigStorageService;
	
	@Autowired
	private PlatformParamService platformParamService;
	
	@Autowired
	private MemcachedClient memcachedClient;
	
	
	@ResponseBody
	@RequestMapping(value = "{appid}/callback", method = )
    public void acceptMessageAndEvent(HttpServletRequest request, HttpServletResponse response
    		,@PathVariable(value = "appid") String appId)//springMVC 获取地址里面的参数信息
    				throws IOException, AesException, DocumentException {
		
        ("进入全网发布流程=================================================================================");
        String nonce = ("nonce");
        String timestamp = ("timestamp");
        String msgSignature = ("msg_signature");
        
        ("读取数据为:"+"msg_signature="+msgSignature+",  timestamp="+timestamp+",  nonce="+nonce+",  app读取的XML为:"+xml);
 
        if (("wx570bc396a51b8ff8")){// 微信自动化测试的专用测试公众账号
        	
        	PlatformParam component = ();//获取 平台ID
        	
            WXBizMsgCrypt pc = new WXBizMsgCrypt((),(),
            												());
            ("加解密======================================");
            try {
            	xml = (msgSignature, timestamp, nonce, xml);//将xml进行加密后,和sign签名码进行对比,如果正确则返回xml
            	("解密后:"+xml);
            	Document doc = (xml);
                Element rootElt = ();
                String msgType = ("MsgType");
                String toUserName = ("ToUserName");
                String fromUserName = ("FromUserName");
                
                if(("event")){// 返回类型值,做一下区分
                	String event = ("Event");
                	//返回时, 将发送人和接收人 调换一下即可 
                	replyEventMessage(request,response,event,fromUserName,toUserName);
                }
                
                if(("text")){ //标示文本消息,
                	String content = ("Content");
                	//返回时, 将发送人和接收人 调换一下即可 
                	processTextMessage(request,response,content,fromUserName,toUserName);//用文本消息去拼接字符串。微信规定
                }
			} catch (AesException e) {
				("错误码为: "+());
				("错误信息为: "+());
				//应该做容错处理
			}
        }else{
        	("app,正确的值为:wx570bc396a51b8ff8");
        	("检测不是微信开放平台测试账号,发布程序终止.");
        }
        
    }
	
	/**
	 * 方法描述: 类型为enevt的时候,拼接
	 * @param request
	 * @param response
	 * @param event
	 * @param toUserName  发送接收人
	 * @param fromUserName  发送人
	 * @author Andy 2015年9月1日  下午2:16:26
	 */
	public void replyEventMessage(HttpServletRequest request, HttpServletResponse response, 
					String event, String toUserName, String fromUserName) 
							throws DocumentException, IOException {
        String content = event + "from_callback";
        replyTextMessage(request,response,content,toUserName,fromUserName);
    }
	
	
	/**
	 * 方法描述: 立马回应文本消息并最终触达粉丝
	 * @param content  文本
	 * @param toUserName  发送接收人
	 * @param fromUserName  发送人
	 * @author Andy 2015年8月31日  下午6:24:38
	 */
	public void processTextMessage(HttpServletRequest request, HttpServletResponse response,
					String content,String toUserName, String fromUserName) 
							throws IOException, DocumentException{
        if("TESTCOMPONENT_MSG_TYPE_TEXT".equals(content)){
            String returnContent = content+"_callback";
            replyTextMessage(request,response,returnContent,toUserName,fromUserName);
        }else if((content, "QUERY_AUTH_CODE")){
        	().print("");//需在5秒内返回空串表明暂时不回复,然后再立即使用客服消息接口发送消息回复粉丝
        	("content:"+content+" content[1]:"+(":")[1]+" fromUserName:"+fromUserName+" toUserName:"+toUserName);
            //接下来客服API再回复一次消息
        	//此时 content字符的内容为是 QUERY_AUTH_CODE:adsg5qe4q35
            replyApiTextMessage((":")[1],toUserName);
        }
    }
	
	
	
	/**
	 * 方法描述: 直接返回给微信开放平台
	 * @param request
	 * @param response
	 * @param content  文本
	 * @param toUserName  发送接收人
	 * @param fromUserName  发送人
	 * @author Andy 2015年9月1日  下午2:15:40
	 */
	public void replyTextMessage(HttpServletRequest request, HttpServletResponse response, 
							String content,String toUserName, String fromUserName) 
												throws DocumentException, IOException {
        Long createTime = () / 1000;
        StringBuffer sb = new StringBuffer(512);
        ("<xml>");
        ("<ToUserName><![CDATA["+toUserName+"]]></ToUserName>");
        ("<FromUserName><![CDATA["+fromUserName+"]]></FromUserName>");
        ("<CreateTime>"+()+"</CreateTime>");
        ("<MsgType><![CDATA[text]]></MsgType>");
        ("<Content><![CDATA["+content+"]]></Content>");
        ("</xml>");
        String replyMsg = ();
        ("确定发送的XML为:"+replyMsg);//千万别加密
        returnJSON(replyMsg,response);
    }
	
	/**
	 * 方法描述: 调用客服回复消息给粉丝
	 * @param auth_code
	 * @param fromUserName
	 * @throws DocumentException
	 * @throws IOException
	 * @return void
	 * @author Andy 2015年9月7日  上午9:48:01
	 */
	public void replyApiTextMessage(String auth_code, String fromUserName) throws DocumentException, IOException {
	        // 得到微信授权成功的消息后,应该立刻进行处理!!相关信息只会在首次授权的时候推送过来
	        String componentAccessToken= ();//本人平台缓存的token
	        PlatformParam component = ();//获取 平台ID
	        ///cgi-bin/component/api_query_auth  到这个微信的接口去获取数据
	        MpAuthorization m=().mpAuthorization(componentAccessToken, (),auth_code);
	        AuthorizationInfo info=m.getAuthorization_info();
	        String authorizer_access_token = info.getAuthorizer_access_token();
	        
	        String url = "/cgi-bin/message/custom/send?access_token="+authorizer_access_token;
			JSONObject json = new JSONObject();
			("touser",fromUserName);
			("msgtype", "text");
			("text", "{\"content\":\""+auth_code+"_from_api"+"\"}");
	        
			String result = (url, ());
	        ("客服发送接口返回值:"+result);
    }   
	
	
	/**
	 * 方法描述: 返回数据到请求方
	 * @param data 数据
	 * @param response
	 * @author Andy 2015年9月1日  下午1:06:54
	 */
	public void returnJSON(Object data,HttpServletResponse response) {
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			JsonEncoding encoding = JsonEncoding.UTF8;
			("application/json");
			 generator = ().
									createJsonGenerator((), encoding);
			(generator, data);
		} catch (IOException e) {
			();
		}
	}
	
	
	/**
	 * 方法描述:
	 * @param args
	 * @return void
	 * @author Andy 2015年8月31日  下午5:31:07
	 */
	public static void main(String[] args) {
		JSONObject j=new JSONObject();
		("content", "aaa"+"_from_api");
		(());
		("{\"content\":\"好的_from_api\"}");
	}
	
}

3、容易出错的点:

      (1)、组件API: 记住这地方,要去重新获取authorizer_access_token值,发送消息的人,必须是自己,别弄错了,不然返回错误码4***3

      (2)、返回普通文本消息的时候,千万别加密那个XML的字符串,加密就报错.

      (3)、还有一个就是,全网发布的时候,要将自己的外网IP填写在 白名单里面,不然获取token会失败报错 61004

      (4)、如果几次的测试都通过的情况下,突然有一天或者一段时间  客服发送接口返回值 出现48001,提示api没有权限,那估计就是腾讯的服务器八成挂了,耐心的等待吧!

4、本文档截稿日期为 2015-09-07, 请距此时间过久的朋友阅读时,具体以微信开放平台官方文档为主! 此处仅供参考