教你3分鐘快速開發微信公衆號[訂閱號][服務號]

Wx-tools是基於微信公衆平臺API的輕量級框架。 基於Wx-tools你可以開速開發一個訂閱號/服務號的web應用後臺。
博主最近終於有空!!已經更新到2.0.0啦!!
GitHub倉庫
下載wx-tools-2.0.0.jar
wx-tools開發文檔

大三做過幾個基於微信公衆平臺API的項目,感覺操作太繁瑣,有時微信官方開發文檔還有錯!!

所以!wx-tools誕生了!

看過很多優秀的開源代碼,特別是chanjarster的代碼,優秀的設計思想讓我醍醐灌頂。

不多說,讓我帶你快速有效可拓展的開發一個訂閱號/服務號的後臺。


到底有多簡單?只需要3步即可~
1. 創建web工程,添加jar包及依賴
2. 配置wx.properties
3. 調用WxService來實現業務邏輯即可

如果有過開發過基於微信公衆平臺API項目的小夥伴們,應該很容易上手哦!沒有也沒關係,戳這裏看看官方文檔入門


開始

一、創建web工程,添加jar包及依賴

我這裏使用的是maven構建項目,可以在eclipse創建web項目。

maven創建項目指令

mvn archetype:generate -DgroupId=wxtools.demo -DartifactId=demo -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeCatalog=local
  • 注意:此指令創建的web工程版本是2.3的,比較低。可以修改web.xml,變成3.0
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>demo</display-name>
</web-app>

創建完畢後,導入wx-tools-1.0.1.jar

  • 注意,導入後記得修改jar包的編碼。window下默認讀GBK,而框架本身是UTF-8。如果需要看源代碼的務必手動修改編碼。
  • 修改方式:在eclipse的工程下,對着jar包右鍵 –> Properties –> Encoding –> UTF-8

建立基本的package,如圖。

package

添加wx-tools依賴的jar包。

  • 如果使用maven構建項目,可以直接添加如下座標下載依賴jar。

maven pom.xml

<!-- 補全依賴 -->
    <!-- 1:日誌; java日誌:slf4j,log4j,logback,common-logging
        slf4j接口/規範
        log4j,logback,common-logging,實現
        此處使用slf4j+logback -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.6</version>
    </dependency>

    <!-- 實現slf4j日誌 -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.1</version>
    </dependency> 

    <!-- HttpClient -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.6</version>
    </dependency>
    <!-- http://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.3.6</version>
    </dependency>

    <!-- JSON -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    <!-- XML -->
    <!-- http://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
    <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>1.4.7</version>
    </dependency>
    <!-- IO -->
    <!-- http://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

如果不是maven構建的項目,可以自行下載相應的jar包,放在WEB-INF/lib文件夾下

如果與項目中的jar包衝突或重複,只留一個就可以了~


二、配置wx.properties

搭好項目基本框架後,在src/main/resources或者src/main/java下新建wx.properties,如上圖

#配置如下
wx.appId=wxb1bff1627d37417b
wx.appSecret=dd037d9b9b4eea00fba14167a9f3c75d
wx.token=antgan
wx.aesKey=f82PVzQsKG5d8en3DwnZ8VVEoGInkmsWz3X3HsreEqe
wx.mchId=

不要填錯了哦!注意大小寫。


三、實現業務邏輯

  • 我們模擬一個業務場景:某公衆平臺進行一項活動,有獎競猜一個單詞,只有關注本公衆號大於3天的用戶才能參與。回答正確的用戶會接收到平臺回覆“恭喜你中獎了”。正確答案是:smart

這裏有必要介紹一下wx-tools的四大組件。

  • WxMessageRouter 消息路由器
  • WxMessageMatcher(接口) 消息匹配器
  • WxMessageInterceptor (接口) 消息攔截器
  • WxMessageHandler (接口) 消息處理器

這四大組件構成了對微信服務器發送過來的消息進行拓展性的處理。
其中,以上三個是接口,開發者可實現並構建自己的匹配器,攔截器,處理器。

此外,還有幾個類需要注意一下

  • WxConsts 類
    封裝所有微信公衆平臺API的常量類型,包括接口請求路徑,事件等。
  • WxConfig 類
    基本配置庫。裏面包含了AppId,AppSecret等信息。wx-tools已經提供了個基於內存管理的配置庫。暫不支持自行拓展,如有需要持久化到數據庫,需要自己實現。注意:配置庫對於整個程序是單例的。
  • WxService 類
    微信統一的API Service入口,繼承IService接口,所有接口都從這裏調用。
  • WxErrorException 類
    微信異常
  • WxErrorExceptionHandler 接口
    開發者可自行實現該接口,處理微信異常。

理清了關係後,開始創建自定義匹配器Matcher

1. 創建DemoMatcher.java 實現 WxMessageMatcher接口
任務:識別是是否是smart的消息,如果是,接收;否則,不接受。

/**
* Demo 簡單的匹配器,可以用於更加複雜的消息驗證操作
* @author antgan
*/
public class DemoMatcher implements WxMessageMatcher{
  //答案是smart,如果匹配smart返回true;反之,false。
  public boolean match(WxXmlMessage message) {
      if(message.getContent().equals("smart")){
        return true;
      }
      return false;
  }
}

2. 創建DemoInterceptor.java 實現 WxMessageInterceptor接口
任務:攔截消息,驗證用戶條件,需要滿足關注3天以上。

/**
 * Demo 攔截器,可以做身份驗證,權限驗證等操作。
 * @author antgan
 */
public class DemoInterceptor implements WxMessageInterceptor{
    public boolean intercept(WxXmlMessage wxMessage, Map<String, Object> context, IService wxService)
            throws WxErrorException {
        //可以使用wxService的微信API方法
        //可以在Handler和Interceptor傳遞消息,使用context上下文
        //可以實現自己的業務邏輯

        //這裏就不編寫驗證關注三天以上的用戶了
        if(/*用戶關注時長大於3天*/){
            return true;
        }
        return false;
    }
}

3. 創建類DemoHandler.java 實現 WxMessageHandler接口
任務:如果滿足以上條件的用戶消息,返回字符串“恭喜你中獎了”

/**
 * Demo 處理微信消息Handler
 * @author antgan
 */
public class DemoHandler implements WxMessageHandler {
    //wxMessage 消息 | context 上下文 | WxService API對象
    public WxXmlOutMessage handle(WxXmlMessage wxMessage, Map<String, Object> context, IServicewxService) throws WxErrorException {
        WxXmlOutMessage xmlOutMsg = null;
        //必須以build()作爲結尾,否則不生效。
        if(wxMessage.getMsgType().equals(WxConsts.XML_MSG_TEXT)){
            xmlOutMsg = WxXmlOutMessage.TEXT().content("恭喜你中獎了").toUser(wxMessage.getFromUserName()).fromUser(wxMessage.getToUserName()).build();
        }
        return xmlOutMsg;
    }
}

4. 創建servlet,名爲DemoServlet.java
任務:接收微信服務器發來的消息,並傳入路由器中。

/**
 * <pre>
 * Demo Servlet
 * 如果使用的是SpringMVC,也是類似這樣的寫法~
 * 
 * 注意:WxConfig請調用getInstance()
 * 因爲對於全局是唯一的。採用單例模式。
 * </pre>
 * 
 * @author antgan
 * @date 2016/12/15
 *
 */
@WebServlet("/wx")
public class DemoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    // 實例化 統一業務API入口
    private IService iService = new WxService();

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 驗證服務器的有效性
        PrintWriter out = response.getWriter();
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        if (iService.checkSignature(signature, timestamp, nonce, echostr)) {
            out.print(echostr);
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        // 返回消息給微信服務器
        PrintWriter out = response.getWriter();

        // 創建一個路由器
        WxMessageRouter router = new WxMessageRouter(iService);
        try {
            // 微信服務器推送過來的是XML格式。
            WxXmlMessage wx = XStreamTransformer.fromXml(WxXmlMessage.class, request.getInputStream());
            System.out.println("消息:\n " + wx.toString());
            // 添加規則;這裏的規則是所有消息都交給DemoMatcher處理,交給DemoInterceptor處理,交給DemoHandler處理
            // 注意!!每一個規則,必須由end()或者next()結束。不然不會生效。
            // end()是指消息進入該規則後不再進入其他規則。 而next()是指消息進入了一個規則後,如果滿足其他規則也能進入,處理。
            router.rule().matcher(new DemoMatcher()).interceptor(new DemoInterceptor()).handler(new DemoHandler())
                    .end();
            // 把消息傳遞給路由器進行處理
            WxXmlOutMessage xmlOutMsg = router.route(wx);
            if (xmlOutMsg != null)
                out.print(xmlOutMsg.toXml());// 因爲是明文,所以不用加密,直接返回給用戶。

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }
}

就是這麼簡單。
完成了這個活動的業務。


這裏實例講得很粗糙,可以看wx-tools的開發文檔。有詳細的實例和使用講解。

GitHub持續更新!喜歡的點個star哦~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章