当前位置:网站首页>Eggservice builds the basic service of wechat official account
Eggservice builds the basic service of wechat official account
2022-06-25 02:59:00 【Front end baymax】
Egg Services build the basic services of wechat official account
| title | Content |
|---|---|
| introduce | Service establishment (http |
| To configure | Config To configure |
| Code implementation | Control layer 、 route |
Installation dependency
npm i co-wechat -s
# or
yarn add co-wechat
# or
cnpm i co-wechat -s
Config Configuration code
/* eslint valid-jsdoc: "off" */
'use strict';
/** * Wechat official account docking configuration content * * config/config.default.js * Of course, there are some other configurations , I'm just showing you */
module.exports = appInfo => {
config.wehcat = {
token: 'token', // Basic configuration token Information
appid: 'appid', // WeChat public account appid Information
encodingAESKey: 'encodingAESKey', // WeChat public account encodingAESKey Information
}
}
Routing code
'use strict';
/** * @param {Egg.Application} app - egg application */
module.exports = app => {
router.post('/wechat', controller.wechat.wechat.wechat);
router.get('/wechat', controller.wechat.wechat.index);
}
Control layer code
'use strict';
/** * Basic configuration of wechat official account , Interface access * // app/controller/wehcat/wechat.js */
const wechat = require('co-wechat');
const crypto = require('crypto');
const Controller = require('egg').Controller;
class WechatController extends Controller {
async index() {
const {
ctx } = this;
const query = ctx.request.query;
const signature = query.signature;
const timestamp = query.timestamp;
const nonce = query.nonce;
const echostr = query.echostr;
if (await this.check(timestamp, nonce, signature, 'weixin')) {
this.ctx.body = echostr;
} else {
this.ctx.body = 'It is not from weixin';
}
}
async check(timestamp, nonce, signature, token) {
const tmp = [token, timestamp, nonce].sort().join('');
const currSign = crypto.createHash('sha1').update(tmp).digest('hex');
return (currSign === signature);
}
}
WechatController.prototype.wechat = wechat({
token: 'weixin', // Basic configuration token Information
appid: 'wx8a709a03e1584e6b', // WeChat public account appid Information
encodingAESKey: 'H6xJLje9n5JjgAFiCsmk0TfIkcUpiGrURAUxtnRui0G', // WeChat public account encodingAESKey Information
}).middleware(async (message, ctx) => {
console.log(message);
return {
type: 'text', content: 'Hello world!' };
})
module.exports = WechatController;
Be careful
- Alibaba cloud default ssl perhaps tsl Certificates are slow , If after use URL The request timed out or the service did not arrive at all egg In the service , Can be substituted with http To test , Or use paid ssl perhaps tsl The certificate is parsed , It took me a long time to find out the reason
Basic service docking instructions for wechat official account
Wechat official account interface commissioning
Source location
边栏推荐
- 运行时修改Universal Render Data
- Is it safe to open an account in the way of winning 100% of the new bonds
- Insurance can also be bought together? Four risks that individuals can pool enough people to buy Medical Insurance in groups
- F - Spices(线性基)
- Yarn: unable to load file c:\users\xxx\appdata\roaming\npm\yarn PS1 because running scripts is prohibited on this system
- Expressing the transformation of two coordinate systems with vectors
- QT package the EXE file to solve the problem that "the program input point \u zdapvj cannot be located in the dynamic link library qt5cored.dll"
- ACM. Hj70 matrix multiplication calculation amount estimation ●●
- 记一次beego通过go get命令后找不到bee.exe的坑
- npm包发布详细教程
猜你喜欢

自动化测试

20 years ICPC Macau station L - random permutation

Once beego failed to find bee after passing the go get command Exe's pit

AI自己写代码让智能体进化!OpenAI的大模型有“人类思想”那味了

Overview of AOSP ~ WiFi architecture
![[STL source code analysis] configurator (to be supplemented)](/img/87/0ed1895e9cdb5327411c0c9cb0197f.png)
[STL source code analysis] configurator (to be supplemented)

高数 | 精通中值定理 解题套路汇总

Xiaomi routing R4A Gigabit version installation feed+openwrt tutorial (the full script does not need to be hard modified)

MySQL learning notes -- addition, deletion, modification and query on a single table

什么是SSL证书,拥有一个SSL证书有什么好处?
随机推荐
Centos7.3 modifying MySQL default password_ Explain centos7 modifying the password of the specified user in MySQL
nacos实践记录
电脑端微信用户图片DAT格式解码为图片(TK版)
Computer wechat user picture decoded into picture in DAT format (TK version)
调用系统函数安全方案
Call system function security scheme
202112-2 序列查询新解
Insurance can also be bought together? Four risks that individuals can pool enough people to buy Medical Insurance in groups
Copilot免费时代结束!学生党和热门开源项目维护者可白嫖
left join on和 join on的区别
Difference between left join on and join on
PyTorch学习笔记(七)------------------ Vision Transformer
DSPACE的性能渲染问题
Lihongyi, machine learning 6 Convolutional neural network
QT package the EXE file to solve the problem that "the program input point \u zdapvj cannot be located in the dynamic link library qt5cored.dll"
Solution of separating matlab main window and editor window into two interfaces
Using qdomdocument to manipulate XML files in QT
Array - fast and slow pointer in one breath
Refresh mechanism of vie
How to uninstall CUDA