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;
特別說明
-
支持多公衆號配置,如需要可以多實例化
ApiConfig
然後調用ApiConfigKit.putApiConfig(apiConfig)
進行設置。 -
ApiConfig
參數說明第一個參數:令牌 Token 可以任意填寫
第二個參數:開發者ID appId
第三個參數:開發者密碼 appScrect
第四個參數:是否開啓加密 encryptMessage 默認值爲 false
第五個參數:消息加解密密鑰 encodingAesKey 非必須
-
設置多個公衆號配置時默認使用第一個
ApiConfig
-
切換公衆號配置可以調用
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));
}
特別說明:
- 開發者URL爲:
http/https://域名/msg
或者http/https://域名/msg?appId=xxxxx
- 測試號的加密模式必須設置爲
false
(上文提到的ApiConfig
第四個參數) - 正式號推薦開啓加密模式並設置爲
安全模式
本地端口映射工具
推薦使用 FRP 目前Github Start 已超越 2.2w。如有更好的工具歡迎推薦(留言區見)
開源推薦
TNW
微信公衆號開發腳手架:https://gitee.com/javen205/TNWIJPay
讓支付觸手可及:https://gitee.com/javen205/IJPay- SpringBoot 微服務高效開發
mica
工具集:https://gitee.com/596392912/mica Avue
一款基於 vue 可配置化的神奇框架:https://gitee.com/smallweigit/avuepig
宇宙最強微服務(架構師必備):https://gitee.com/log4j/pigSpringBlade
完整的線上解決方案(企業開發必備):https://gitee.com/smallc/SpringBlade