此文项目代码:https://github.com/bei-yang/I-want-to-be-an-architect
码字不易,辛苦点个star,感谢!
引言
此篇文章主要涉及以下内容:
- 开发环境搭建
- 客服消息 - (你问我答)完成
- 消息接口源码讲解
- 服务器端
API
调用 - 多进程下保存
token
全局票据
参考资料及目标
参考资料
目标
- 本地环境可以调试公众号接口
- 能够通过程序对公众号消息进行应答
- 能够调用服务器端接口对接 - 比如:查看关注者列表
开发环境搭建
- 注册微信订阅号
- 注册小程序测试号
- 复制
koa+vue
的种子项目 - 安装sunny-ngrok实现外网的映射

# weixin_lesson目录
./ngrok.sh
客服消息 - (你问我答)完成

- npm库:https://github.com/node-webot/co-wechat
- 开通公众号测试账号
- https://mp.weixin.qq.com
- 选择[开发者工具]->[公众号平台测试账号]

- 编写配置文件
// conf.js
module.exports = {
appid: 'wxfc60e88fa8622c69',
appsecret: '23c57e17b4073db7d03cca2ebac525ae',
token: 'kaikeba',
};
// index.js
const conf = require('./conf')
消息接口源码讲解
- co-wechat实现
// server.js
// npm i co-wechat
const wechat = require('co-wechat')
router.all('/wechat', wechat(conf).middleware(
async message => {
console.log('wechart', message)
return 'Hello world! '+message.Content;
}
))
扫描二维码测试,微信发送信息测试
- 验证部分
crypto类
https://www.liaoxuefeng.com/wiki/1022910821149312/1023025778520640
crypto模块的目的是为了提供通用的加密和哈希算法。用纯JavaScript代码实现这些功能不是不可能,但速度会非常慢。Nodejs用C/C++实现这些算法后,通过cypto这个模块暴露为JavaScript接⼝口,这样用起来方便,运行速度也快。
- 验证过程
SHA1算法
安全哈希算法(Secure Hash Algorithm)主要用于数字签名标准 (Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signature Algorithm DSA)。对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。当接收到消息的时候,这个消息摘要可以用来验证数据的完整性。在传输的过程中,数据很可能会发生变化,那么这时候就会产生不不同的消息摘要。SHA1有如下特性:不可以从消息摘要中复原信息;两个不同的消息不会产生同样的消息摘要,(但会有1x10 ^ 48分之一的机率出现相同的消息摘要,一般使用时忽略)。
哈希: 不可变长 -> 摘要固定长度
归纳
- 摘要
- 雪崩效应
- 类似MD5 SHA256
- 收发消息
// 接受信息
// 将bodyparser更更换xmlParser
const xmlParser = require("koa-xml-body");
app.use(xmlParser());
const xml2js = require("xml2js");
// 接受信息
router.post("/wechat", ctx => {
const { xml: msg } = ctx.request.body;
console.log("Receive:", msg);
const builder = new xml2js.Builder();
const result = builder.buildObject({
xml: {
ToUserName: msg.FromUserName,
FromUserName: msg.ToUserName,
CreateTime: Date.now(),
MsgType: msg.MsgType,
Content: "Hello " + msg.Content
}
});
console.log("xml:", result);
ctx.body = result;
});
服务器端API调用
官方文档:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
npn库:https://github.com/node-webot/co-wechat
- 功能
- 用户管理
- 用户分组
- 备注姓名
- 获取用户基本资料
- 获取用户列表
- 获取用户地理位置
- 界面设置-定制菜单
- 素材管理
- 推广支持 生成二维码
- 用户管理
- 源码实现
// index.html
// 获取关注着列列表
getTokens: async function () {
let res = await axios.get(`/getTokens`)
console.log('res',res)
},
// index.js
const tokenCache = {
access_token: "",
updateTime: Date.now(),
expires_in: 7200
};
router.get("/getToken", async ctx => {
const wxDomain = `https://api.weixin.qq.com`;
const path = `/cgi-bin/token`;
const params = `?
grant_type=client_credential&appid=${conf.appid}&secret=${conf.appsecret}`;
const url = `${wxDomain}${path}${params}`;
const res = await axios.get(url);
Object.assign(tokenCache, res.data, {
updateTime: Date.now()
});
ctx.body = res.data;
});
router.get("/getFollowers", async ctx => {
const url = `https://api.weixin.qq.com/cgi-bin/user/get?
access_token=${tokenCache.access_token}`;
const res = await axios.get(url);
console.log("getFollowers", res.data);
ctx.body = res.data;
});
- co-wechat-api库实现
// index.js
const WechatAPI = require('co-wechat-api');
const api = new WechatAPI(conf.appid, conf.appsecret);
// 获取关注者列列表
router.get('/getFollowers', async ctx => {
var res = await api.getFollowers();
ctx.body = res
})
// index.html
// 获取关注着列列表
getFollowers: async function () {
let res = await axios.get(`/getFollowers`)
console.log('res',res)
},
// 获取关注者列列表
router.get('/getFollowers', async ctx => {
var res = await api.getFollowers();
console.log('res', res)
res = await api.batchGetUsers(res.data.openid, 'zh_CN');
console.log('res', res)
ctx.body = res
})
多进程下保存token全局票据
- docker-compose提供MongoDB服务
-
当多进程时,token需要全局维护,以下为保存token的接口
你的赞是我前进的动力
求赞,求评论,求分享...
网友评论