TNWX-開啓公衆號開發者模式

TNW-開啓公衆號開發者模式

簡介

**TNWX:TypeScript + Node.js + WeiXin 微信系開發腳手架,支持微信公衆號、微信支付、微信小遊戲、微信小程序、企業號/企業微信、企業微信開發平臺。最最最重要的是能快速的集成至任何 Node.js 框架(Express、Nest、Egg、Koa 等)
**

安裝

NPM 依賴方式

1、下載

$ npm i tnw

2、Express 示例

3、Nest 示例

源碼方式

1、下載項目並安裝依賴

$ git clone https://github.com/Javen205/TNW.git 
或者 
$ git clone https://gitee.com/Javen205/TNW.git 
$ cd TNW
$ npm install 

2、編譯並運行

$ npm run build 
$ npm run dev

3、完整示例

配置公衆號參考

  // 亦可以讀取配置文件
  let apiConfig = new ApiConfig("Javen", "wx614c453e0d1dcd12", "19a02e4927d346484fc70327970457f9");
  // 支持多公衆號
  ApiConfigKit.putApiConfig(apiConfig);
  // 開啓開發模式,方便調試
  ApiConfigKit.devMode = true;

特別說明

  1. 支持多公衆號配置,如需要可以多實例化 ApiConfig 然後調用 ApiConfigKit.putApiConfig(apiConfig) 進行設置。

  2. ApiConfig 參數說明

    第一個參數:令牌 Token 可以任意填寫

    第二個參數:開發者ID appId

    第三個參數:開發者密碼 appScrect

    第四個參數:是否開啓加密 encryptMessage 默認值爲 false

    第五個參數:消息加解密密鑰 encodingAesKey 非必須

  3. 設置多個公衆號配置時默認使用第一個 ApiConfig

  4. 切換公衆號配置可以調用 ApiConfigKit.setCurrentAppId(appId)

公衆號開啓開發者模式

TNW 中驗證簽名的關鍵接口如下:

WeChat.checkSignature(signature, timestamp,nonce, echostr)

Express 示例如下:

app.get('/msg', (req: any, res: any) => {
    console.log('get query...', req.query);

    let appId: string = req.query.appId;
    if (appId) {
        ApiConfigKit.setCurrentAppId(appId);
    }

    let signature = req.query.signature,//微信加密簽名
        timestamp = req.query.timestamp,//時間戳
        nonce = req.query.nonce,//隨機數
        echostr = req.query.echostr;//隨機字符串
    res.send(WeChat.checkSignature(signature, timestamp,
        nonce, echostr));
});

Nest 示例如下:

@Get('/msg')
getMsg(@Req() request: Request, @Res() response: Response) {
  let appId: string = request.query.appId;
  if (appId) {
    ApiConfigKit.setCurrentAppId(appId);
  }

  let signature = request.query.signature,//微信加密簽名
      timestamp = request.query.timestamp,//時間戳
      nonce = request.query.nonce,//隨機數
      echostr = request.query.echostr;//隨機字符串
  response.send(WeChat.checkSignature(signature, timestamp,nonce, echostr));
}

特別說明:

  1. 開發者URL爲:http/https://域名/msg 或者 http/https://域名/msg?appId=xxxxx
  2. 測試號的加密模式必須設置爲 false (上文提到的 ApiConfig 第四個參數)
  3. 正式號推薦開啓加密模式並設置爲 安全模式

本地端口映射工具

推薦使用 FRP 目前Github Start 已超越 2.2w。如有更好的工具歡迎推薦(留言區見)

開源推薦

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