当前位置:网站首页>Egg 服务搭建微信公众号的基础服务
Egg 服务搭建微信公众号的基础服务
2022-06-24 23:52:00 【前端Baymax】
Egg 服务搭建微信公众号的基础服务
| 标题 | 内容 |
|---|---|
| 引入 | 服务搭建(http |
| 配置 | Config配置 |
| 代码实现 | 控制层、路由 |
安装依赖
npm i co-wechat -s
# or
yarn add co-wechat
# or
cnpm i co-wechat -s
Config配置代码
/* eslint valid-jsdoc: "off" */
'use strict';
/** * 微信公众号对接配置内容 * * config/config.default.js * 当然这里面也有一些其他的配置,这里只展示 */
module.exports = appInfo => {
config.wehcat = {
token: 'token', // 基础配置的token信息
appid: 'appid', // 微信公众号的appid信息
encodingAESKey: 'encodingAESKey', // 微信公众号的encodingAESKey信息
}
}
路由代码
'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);
}
控制层代码
'use strict';
/** * 微信公众号基础配置,接口接入 * // 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', // 基础配置的token信息
appid: 'wx8a709a03e1584e6b', // 微信公众号的appid信息
encodingAESKey: 'H6xJLje9n5JjgAFiCsmk0TfIkcUpiGrURAUxtnRui0G', // 微信公众号的encodingAESKey信息
}).middleware(async (message, ctx) => {
console.log(message);
return {
type: 'text', content: 'Hello world!' };
})
module.exports = WechatController;
注意点
- 阿里云默认的ssl或者tsl证书比较缓慢,如果使用之后是URL请求超时或者服务根本没有走到egg服务里面,可以替换成http进行测试,或者使用付费的ssl或者tsl证书进行解析,我是亲自排查了很久这个原因才找到了
边栏推荐
- @PostConstruct
- F - Spices(线性基)
- Summary of knowledge points of computer level III (database) test preparation topics
- npm包发布详细教程
- Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
- Using qdomdocument to manipulate XML files in QT
- Enlightenment of using shadergraph to make edge fusion particle shader
- Processon producer process (customized)
- qt打包exe文件,解决“无法定位程序输入点_ZdaPvj于动态链接库Qt5Cored.dll”
- ERROR日志格式与注意点
猜你喜欢

MATLAB主窗口与编辑器窗口分开为两个界面的解决办法
![[live review] battle code pioneer phase 7: how third-party application developers contribute to open source](/img/ad/26a302ca724177e37fe123f8b75e4e.png)
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source

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

Are programmers from Huawei, Alibaba and other large manufacturers really easy to find?

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"

It's 2022, and you still don't know what performance testing is?

Solution of separating matlab main window and editor window into two interfaces

left join on和 join on的区别

李宏毅《机器学习》丨6. Convolutional Neural Network(卷积神经网络)

Advanced mathematics | proficient in mean value theorem problem solving routines summary
随机推荐
如何卸载cuda
計網 | 【四 網絡層】知識點及例題
VSCode中如何实现点击DOM自动定位到相应代码行
ProcessOn制作ER过程(自定义)
Call system function security scheme
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source
MySQL command backup
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (1) -- migrate data to node 1
AI服装生成,帮你完成服装设计的最后一步
GO同步等待组
Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
商城项目 pc----商品详情页
Pytorch learning notes (VII) ------------------ vision transformer
使用XXL-JOB自定义任务并调度
Detailed explanation of cache (for the postgraduate entrance examination of XD)
New solution of 202112-2 sequence query
It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets
[i.mx6ul] u-boot migration (VI) network driver modification lan8720a
202112-2 序列查询新解
高数 | 精通中值定理 解题套路汇总