如何进行微信快速开发

 时间:2024-10-12 03:15:02

1、第一步:将WTF的4个jar包拷贝到项目的lib目录下;下载地址:http://115.159.67.204/Tutorial/Download/wtf-1.0.rar

2、第二步:在项目的web.xml文件中增加以下配置:<!-- Spring上下文配置 颍骈城茇--><context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/halo/wechat/config/applicationContext-wechat.xml</param-value></context-param><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- Spring MVC框架配置 --><servlet> <servlet-name>mvcFramework</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/halo/wechat/config/applicationContext-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet><servlet-mapping> <servlet-name>mvcFramework</servlet-name> <url-pattern>*.do</url-pattern></servlet-mapping>熟悉Spring框架的开发者对以上的配置并不陌生,这实际上是在配置Spring的监听器和MVC框架的DispatcherServlet,只不过指明了要加载WTF的com/halo/wechat/config/applicationContext-wechat.xml和com/halo/wechat/config/applicationContext-servlet.xml这两个上下文(Context)配置文件(见红色字体标明部分)。这两个配置文件已经包含在WTF的jar包里。开发者也可以同时加载自己的上下文(Context)配置文件,只需将红色字体标明部分改为以下形式:<param-value>self_aplicationContext.xml,classpath:com/halo/wechat/config/applicationContext-wechat.xml</param-value><param-value> self_aplicationContext_servlet.xml,classpath:com/halo/wechat/config/applicationContext-servlet.xml</param-value>例子中假设self_aplicationContext.xml和self_aplicationContext_servlet.xml是开发者自己的上下文配置,多个配置文件之间用“,”(半角逗号)隔开,当然,实际开发时你还需要在配置文件名前加上相对或绝对路径。

3、第三步:在项目的classpath目录下加入两个配置文件“log4j2.xml”和“wechat.properties”:“log4j2.xml”是log4j(日志记录工具)的配置文件,内容不需要修改。“wechat.properties”中需要配置开发者自己的微信公众号的app_id、app_secret和token,开发者在微信公众平台上可以找到这三个参数的配置。以上两个文件在wtf-1.0.rar中都有。

4、第四步:实现WTF框架中的com.halo.wechat.mvc.commands.Command接口,开发者在这个实现类中粕盘镱嘧编写消息和事件处理的代码,如下例子:// 注意:Command接口的实现类一定要定义在名称形式如“com.*.wechat.mvc.commands”的包中,因为在applicationContext-servlet.xml配置文件中已经指明了要对这种形式的包进行扫描以发现可以自动装配的Beanpackage com.user.wechat.mvc.commands;import java.sql.Timestamp;import java.util.Random;import org.springframework.stereotype.Component;import com.halo.wechat.messages.ClickEvent;import com.halo.wechat.messages.Event;import com.halo.wechat.messages.LocationEvent;import com.halo.wechat.messages.LocationMessage;import com.halo.wechat.messages.Message;import com.halo.wechat.messages.MsgType;import com.halo.wechat.messages.SubscribeEvent;import com.halo.wechat.messages.TextMessage;import com.halo.wechat.mvc.commands.Command;import com.halo.wechat.mvc.commands.CommandException;/*** @file UserCommand.java* @author Junior* @date 2015年8月9日 上午11:21:44* @version 1.0*/// 注意:此处一定要加上@Component注解,这样Spring MVC框架才可以扫描到开发者自定义的实现类并自动装配到WTF中,这样无需编写任何代码即可让WTF自动调用开发者自己的消息和事件处理方法。@Componentpublic class UserCommand implements Command { // 消息处理方法,在接收到微信公众平台发送的消息时,WTF会自动调用该方法。 @Override public Message processMessage(Message receiveMessage) throws CommandException { Message responseMessage = null; System.out.println("Receive a " + receiveMessage.getClass().getSimpleName() + " type " + receiveMessage.getMsgType()); Random rand = new Random(); long msgId = rand.nextLong(); String responseText = "你好啊!"; // 此处可根据receiveMessage.getMsgType()判断接收到的微信公众平台消息类型,消息类型常量定义在MsgType类中,例如MsgType.TEXT是文本消息,详见MsgType类的说明。 if (MsgType.LOCATION.equals(receiveMessage.getMsgType())) { LocationMessage locationMsg = (LocationMessage) receiveMessage; responseText = "你目前位于东经" + String.valueOf(locationMsg.getLocation_Y()) + "度,北纬" + String.valueOf(locationMsg.getLocation_X()) + "度。地点:" + locationMsg.getLabel(); } // 开发者可构建自己的回复消息,如果不需要回复任何消息,可直接retrun null responseMessage = new TextMessage(receiveMessage.getFromUserName(), receiveMessage.getToUserName(), new Timestamp(System.currentTimeMillis()), MsgType.TEXT, responseText, msgId); return responseMessage; } // 事件处理方法,在接收到的微信公众平台消息是事件类型时,WTF会自动调用该方法。 @Override public Message processEvent(Event receiveEvent) throws CommandException { Message responseMessage = null; String responseText = "建设中..."; // 以下方法可以判断事件类型。 if (receiveEvent instanceof ClickEvent) { ClickEvent clickEvent = (ClickEvent) receiveEvent; // 以下方法可以判断菜单点击事件中,用户具体点击的是哪一个菜单。 if ("tip".equals(clickEvent.getEventKey())) { responseText = "你想要回复的内容……"; } } else if (receiveEvent instanceof SubscribeEvent) { responseText = "你想要回复的内容……"; } else if (receiveEvent instanceof LocationEvent) { LocationEvent lctnEvnt = (LocationEvent) receiveEvent; responseText = "你目前位于东经" + String.valueOf(lctnEvnt.getLongitude()) + "度,北纬" + String.valueOf(lctnEvnt.getLatitude()) + "度。"; } // 开发者可构建自己的回复消息,如果不需要回复任何消息,可直接retrun null Random rand = new Random(); long msgId = rand.nextLong(); responseMessage = new TextMessage(receiveEvent.getFromUserName(), receiveEvent.getToUserName(), new Timestamp(System.currentTimeMillis()), MsgType.TEXT, responseText, msgId); return responseMessage; }}至此,你已经可以开始开发自己的微信公众号了!在开发中你还需要创建菜单、向微信公众平台请求二维码等更多功能,WTF以Capability的形式封装了这些能力接口,以更简单方便的形式提供给开发者调用,更多高级的接口功能请参考以下的教程。

5、WTF利用了Spring、Log4J等诸多框架,因此你需要将以下包也加入到项目中,方法同加入WTF的jar包一样。依赖包:httpcomponent-4.5json-2.4log4j-2.3spring-3.2.9spring-related依赖包下载地址:http://115.159.67.204/Tutorial/Download/wtf-dependencies.rar

6、更多开发指南请参考:http://115.159.67.204/Tutorial/tutorial.htm

  • 怎么关闭游戏人生
  • 雾霾生活小知识?
  • 红枣桂圆小米粥
  • 滴滴出行app为什么被下架
  • 3种不能直接睡觉的情况
  • 热门搜索
    关于法制的手抄报 争做新时代好队员手抄报 数学手抄报模板 春天的手抄报图片 汉字手抄报图片 禁毒手抄报内容写什么 化学手抄报 关于读书的手抄报图片 国庆节的手抄报 节约粮食手抄报简单又漂亮