TNW-开启公众号开发者模式

TNW-开启公众号开发者模式javascript

简介

TNW: TypeScript(The) + Node.js(Next) + WeChat 微信公众号开发脚手架,支持 http 模块扩展、支持任何 Node.js 的服务端框架(Express、NestJS 等)vue

安装

NPM 依赖方式

一、下载java

$ npm i tnw
复制代码

二、Express 示例git

三、Nest 示例github

源码方式

一、下载项目并安装依赖typescript

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

二、编译并运行npm

$ npm run build 
$ npm run dev
复制代码

三、完整示例api

配置公众号参考

// 亦能够读取配置文件
  let apiConfig = new ApiConfig("Javen", "wx614c453e0d1dcd12", "19a02e4927d346484fc70327970457f9");
  // 支持多公众号
  ApiConfigKit.putApiConfig(apiConfig);
  // 开启开发模式,方便调试
  ApiConfigKit.devMode = true;
复制代码

特别说明安全

  1. 支持多公众号配置,如须要能够多实例化 ApiConfig 而后调用 ApiConfigKit.putApiConfig(apiConfig) 进行设置。bash

  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。若有更好的工具欢迎推荐(留言区见)

开源推荐

相关文章
相关标签/搜索