微信公衆號快速開發(二)項目搭建與被動回覆

工具:idea2018,jdk1.8,框架:springboot+thymeleaf前端

基礎搭建

說明,本項目着力於快速開發,前端頁面只作最基本的頁面跳轉和參數校驗。如果須要先後端分離,可參考一些開源的項目,如givebest/node.js-wechat-js-sdkjava

準備

1、測試/正式帳號公衆平臺測試帳號,根據上一篇註冊便可node

2、web開發者工具,拖至文末,選擇適配版本下載便可。(由於微信部分URL須要權限的校驗,只能在微信瀏覽器內打開,而相似postman的第三方工具沒法使用)mysql

3、公網可訪問地址,如阿里雲,或內網穿透工具,可參考上一篇中【接口配置信息修改】。(個人是http://chety.mynatapp.cc -> 127.0.0.1:8080,已打開狀態)git

4、訂閱測試號,掃描二維碼便可github

開始開發——被動回覆

描述

向公衆號發送消息,公衆號原樣返回咱們的內容,如發送【你好】,公衆號回覆【你好】web

實現思路

1、參考微信公衆平臺技術文檔,選擇【消息管理】模塊中的【接收普通消息】。spring

2、根據接收普通消息的介紹,可知:sql

  • 消息類型分爲:文本消息,圖片消息,語音消息,視頻消息等
  • 消息的數據包請求格式爲xml,請求方式爲post
  • 消息請求有重試和加密機制
  • 接收消息的URL與接入驗證的路徑一致,用請求方式來區分

3、參數介紹數據庫

如文本消息,有消息的發送的接收方,消息類型和內容等。可以使用bean對象來封裝該消息格式

  • ToUserName:開發者微信號,即目前我正在使用的測試公衆號
  • FromUserName:發送方帳號,描述爲一個openid,即一個手機微信用戶在一公衆號下的惟一標識,不管何種客戶端設備訪問,該id都是統一且惟一的。

注意:消息的收發方是相對的,客戶端與服務端相反

代碼開發

1、新建springboot項目,須要的核心依賴以下:

<dependencies>
    <!-- web項目 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.1</version>
    </dependency>
    <!-- 引入thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- alibaba druid鏈接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.10</version>
    </dependency>
    <!-- lombok JavaBean工具 -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.11</version>
    </dependency>
    <!-- http客戶端工具 -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.9</version>
    </dependency>
    <!-- json轉換工具 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.58</version>
    </dependency>
    <!-- 數據庫可選,如項目須要本地存儲支付交易流水 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.26</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>
複製代碼

2、我這裏都使用默認配置,所以暫時不配置application.yml,各位根據本身的須要配置

發送消息

3、根據輸入消息的字段,封裝爲bean對象

注意,xml中參數的鍵都是大寫,所以定義java參數時,須要使用註解或直接大寫命名

@Data // lombok,包含setter,getter,tostring
@XmlRootElement(name = "xml") // 根節點
@XmlAccessorType(XmlAccessType.FIELD) // 映射類中的全部字段到XML
public class MsgSendEntity {
    /** * 公有部分 */
    // 開發者微信號
    @XmlElement(name = "ToUserName") // 指定名稱映射
    private String toUserName;

    // 發送方賬號(一個OpenID)
    @XmlElement(name = "FromUserName")
    private String fromUserName;

    // 消息建立時間 (整型)
    @XmlElement(name = "CreateTime")
    private Long createTime;

    // 消息類型
    @XmlElement(name = "MsgType")
    private String msgType;

    // 消息id,64位整型
    @XmlElement(name = "MsgId")
    private Long msgId;
    
    // 文本消息內容
    @XmlElement(name = "Content")
    private String content;
}
複製代碼

4、URL接入校驗接口,需公網可見或打開內網穿透工具。詳細內容參考上一篇文章

@Controller
@RequestMapping("/api/v1/wechat1")
public class WeChatController {

    /** * url接入校驗 * @param signature 微信加密簽名,signature結合了開發者填寫的token參數和請求中的timestamp參數、nonce參數。 * @param timestamp 時間戳 * @param nonce 隨機數 * @param echostr 隨機字符串 * @return 若校驗成功,原樣返回echostr參數內容 */
    @GetMapping("/gzh")
    @ResponseBody
    public String validate(String signature,String timestamp,String nonce,String echostr){
        if (!WeChatUtil.checkSignature(signature, timestamp, nonce)) {
            WeChatUtil.getLogger().info("WeChatController.validate -- 公衆號接入失敗");
            return null;
        }
        WeChatUtil.getLogger().info("WeChatController.validate -- 公衆號接入成功,echostr:{}"+echostr);
        return echostr;
    }
}    
複製代碼

測試號管理配置如圖:

5、接收消息接口(路徑與接入驗證URL的一致)

/** * 公衆號消息處理 * @param inMsg 客戶端輸入的消息信息 * @return 服務端返回信息 */
@PostMapping("gzh")
@ResponseBody
public Object handleMessage(@RequestBody InMsgEntity inMsg) {
    return null;
}    
複製代碼

6、斷點調試

微信在測試公衆號(已訂閱)發送文本消息

查看斷點信息,能夠看到接收到文本消息的字段信息

放行斷點,能夠看到接收消息時的重試機制

接收消息

參考技術文檔,這裏選擇【被動回覆消息】

7、封裝【回覆文本消息】的實體類

@Data
@XmlRootElement(name="xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class MsgReplyEntity {
    // 用戶的OpenID
    private String ToUserName;
    
    // 測試號的微信號
    private String FromUserName;
    
    // 消息建立時間 (整型)
    private Long CreateTime;
    
    // 消息類型
    private String MsgType;

    // 文本消息內容
    private String Content;
}
複製代碼

8、完善消息處理接口的方法

@PostMapping("gzh")
@ResponseBody
public Object handleMessage(@RequestBody MsgSendEntity msgSend) {
    // 服務端消息回覆的實體類
    MsgReplyEntity msgReply = new MsgReplyEntity();
    // 根據接收的信息回覆,接收和發送方相反
    msgReply.setFromUserName(msgSend.getToUserName());
    msgReply.setToUserName(msgSend.getFromUserName());
    msgReply.setCreateTime(new Date().getTime());    
    msgReply.setMsgType(msgSend.getMsgType());
    // 消息內容原樣返回
    msgReply.setContent(msgSend.getContent());
    return msgReply;
}    
複製代碼

測試樣例

1、客戶端發送消息測試


ok,公衆號的簡單被動文本消息回覆就完成了。固然,其餘的消息類型也相似處理,只需簡單的判斷便可。

相關文章
相關標籤/搜索