Skip to content

3 得到微信请求参数

vcdemon edited this page Jun 1, 2016 · 1 revision

微信平台会在请求的post数据中带有一些参数,例如用户的openid之类的信息,当你使用了wechat-framework之后,得到这些信息是非常方便的。如果微信公众平台设置成安全模式,则接收消息之后的解密过程已经由wechat-framework自动完成,得到的请求消息对象WechatRequest依然是明文。

import com.itfvck.wechatframework.core.support.WechatSupport;
/**
 * 用户自定义消息处理中心,若要该类起作用,则需配置在wem.xml中
 *
 * @author
 *
 */
public class MyService extends WechatSupport {
    static Logger logger = LoggerFactory.getLogger(MyService.class);

    /**
     * 文本消息处理Msgtype=text
     *
     * @param wechatRequest
     *            请求对象
     * @return
     * @throws Exception
     */
    @Override
    protected String onText(WechatRequest wechatRequest) throws Exception {
        String xml = null;
        if (wechatRequest.getContent().indexOf("收到不支持的消息类型") > 0) {
            xml = onUnknown(wechatRequest);
        } else {
            logger.info("wechatRequest:", wechatRequest.toString());
            String content = wechatRequest.getContent().trim();
            // TODO 这里写具体的业务

            // 返回的消息对象,任何返回消息都构造为WechatResponse
            WechatResponse wechatResponse = new WechatResponse();
            wechatResponse.setContent("文本回复:" + content);
            wechatResponse.setMsgType(MsgType.text.name());
            // 消息对象构造完成之后,调用formatWechatResponse方法即可
            // formatWechatResponse方法会自动对ToUserName,FromUserName,CreateTime赋值
            xml = formatWechatResponse(wechatRequest, wechatResponse);

            logger.info("WechatResponse:", wechatResponse.toString());
            logger.info("onText Xml:", xml);
        }
        return xml;
    }
}

上面代码中的protected String onText(WechatRequest wechatRequest) throws Exception 的参数列表的wechatRequest即是微信post数据中xml数据转变成的请求消息对象.

WechatRequest可以得到的post数据

WechatRequest成员方法中get的方法,名称同微信开发文档中xml形式的post数据的节点。

  • getFromUserName()
  • getContent()
  • getCreateTime()
  • getDescription()
  • getLabel()
  • getMsgId
  • getEvent()
  • getFormat()
  • getLabel()
  • getLocation_X()
  • getLocation_X()
  • getMediaId()
  • getMsgId()
  • getMsgType()
  • getPicUrl()
  • getScale()
  • getTitle()
  • getToUserName()
  • getUrl()

wechat-framework

技术支持

欢迎有志之士一起加入。

Clone this wiki locally