重修前端

微信公众号——服务器端

2019-11-24  本文已影响0人  LM林慕

此文项目代码:https://github.com/bei-yang/I-want-to-be-an-architect
码字不易,辛苦点个star,感谢!

引言


此篇文章主要涉及以下内容:

  1. 开发环境搭建
  2. 客服消息 - (你问我答)完成
  3. 消息接口源码讲解
  4. 服务器端API调用
  5. 多进程下保存token全局票据

参考资料及目标


参考资料

目标

开发环境搭建


image.png
# weixin_lesson目录
./ngrok.sh

客服消息 - (你问我答)完成

// conf.js
module.exports = {
appid: 'wxfc60e88fa8622c69',
appsecret: '23c57e17b4073db7d03cca2ebac525ae',
token: 'kaikeba',
};
// index.js
const conf = require('./conf')

消息接口源码讲解


// 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接⼝口,这样用起来方便,运行速度也快。

安全哈希算法(Secure Hash Algorithm)主要用于数字签名标准 (Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signature Algorithm DSA)。对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。当接收到消息的时候,这个消息摘要可以用来验证数据的完整性。在传输的过程中,数据很可能会发生变化,那么这时候就会产生不不同的消息摘要。SHA1有如下特性:不可以从消息摘要中复原信息;两个不同的消息不会产生同样的消息摘要,(但会有1x10 ^ 48分之一的机率出现相同的消息摘要,一般使用时忽略)。
哈希: 不可变长 -> 摘要固定长度

归纳

// 接受信息
// 将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;
});
// 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全局票据

你的赞是我前进的动力

求赞,求评论,求分享...

上一篇 下一篇

猜你喜欢

热点阅读