当前位置:网站首页>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证书进行解析,我是亲自排查了很久这个原因才找到了
边栏推荐
- PE file infrastructure sorting
- Centos7.3 modifying MySQL default password_ Explain centos7 modifying the password of the specified user in MySQL
- Once beego failed to find bee after passing the go get command Exe's pit
- doak-cms 文章管理系统 推荐
- DSPACE设置斑马线和道路箭头
- New solution of 202112-2 sequence query
- 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?
- F - spices (linear basis)
- Using qdomdocument to manipulate XML files in QT
猜你喜欢
随机推荐
高速缓存Cache详解(西电考研向)
打新债100%中签的方法 开户是安全的吗
Expressing the transformation of two coordinate systems with vectors
C#实现水晶报表绑定数据并实现打印
36岁前亚马逊变性黑客,窃取超1亿人数据被判20年监禁!
AI clothing generation helps you complete the last step of clothing design
F - Spices(线性基)
Enlightenment of using shadergraph to make edge fusion particle shader
Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
14 BS object Node name Name attrs string get node name attribute content
怎么开户打新债 开户是安全的吗
CMakeLists中的add_definitions()函数
Processon producer process (customized)
Detailed explanation of cache (for the postgraduate entrance examination of XD)
Network planning | [four network layers] knowledge points and examples
It's 2022, and you still don't know what performance testing is?
Pytorch learning notes (VII) ------------------ vision transformer
Advanced usage of groovy
Distributed transaction solutions and code implementation
Computer wechat user picture decoded into picture in DAT format (TK version)