node-webot / co-wechat Goto Github PK
View Code? Open in Web Editor NEWWechat for Koa
Wechat for Koa
lib/wechat.js:
182行: var encrypted =! !(query.encrypt_type && query.encrypt_type === 'aes' && query.msg_signature);
应 为:var encrypted = !(query.encrypt_type && query.encrypt_type === 'aes' && query.msg_signature);
283行:if (!query.encrypt_type || query.encrypt_type === 'raw')
应 为:if (!!query.encrypt_type || query.encrypt_type === 'raw')
都是感叹号,一行多了,一行少了
还有当在app.js页面(主页面)使用的时候没问题,但在其它页面做中间件使用的时候,必须要跟上路由器的get和post方法,co-wechat才能起作用,这可能是一个bug。我用的是koa-router
您好。
这边有个需求是这样的,收到微信的消息后,需要把消息存到数据库中。
我使用的是egg框架。
但看您的文档,middleware中,现在的上下文不再是原始的 请求上下文,而仅仅是 message 对象。
那我需要怎样才能获取到原始的请求上下文,然后调用service或者model中的方法呢?谢谢。
Template Literal is fastest, smallest and simplest template engine, because it use JS's literal template feature.
It's 55 times faster than EJS, and it also use less CPU and RAM ressources, so it may be a good idea to use it instead of EJS 😀
公众号发消息时,后端日志报下面的错误,导致后续代码未执行!
版本:2.4.0
koa版本:2.8.2
报错信息
Error: Non-whitespace before first tag.
Line: 0
Column: 1
Char:
at error (/home/github/repo/node_modules/co-wechat/node_modules/sax/lib/sax.js:642:8)
at strictFail (/home/github/repo/node_modules/co-wechat/node_modules/sax/lib/sax.js:662:22)
at SAXParser.write (/home/github/repo/node_modules/co-wechat/node_modules/sax/lib/sax.js:935:11)
at Parser.exports.Parser.Parser.parseString (/home/github/repo/node_modules/co-wechat/node_modules/xml2js/lib/xml2js.js:403:31)
at Parser.parseString (/home/github/repo/node_modules/co-wechat/node_modules/xml2js/lib/xml2js.js:6:61)
at Object.exports.parseString (/home/github/repo/node_modules/co-wechat/node_modules/xml2js/lib/xml2js.js:433:19)
at Promise (/home/github/repo/node_modules/co-wechat/lib/wechat.js:19:12)
at new Promise (<anonymous>)
at parseXML (/home/github/repo/node_modules/co-wechat/lib/wechat.js:18:10)
at /home/github/repo/node_modules/co-wechat/lib/wechat.js:259:34
at process._tickCallback (internal/process/next_tick.js:68:7)
代码:
import originWechat from 'co-wechat';
const middleware = () => {
const { appid, aes, token } = wechatAccount;
const wechat = originWechat({ appid, encodingAESKey: aes, token });
return wechat.middleware(bot);
};
export const register = ({ router, wechat, open }) => {
open.get('/wechat', middleware());
open.post('/wechat', middleware());
};
Unclosed root tag 解析xml数据时为何会报这个错
WechatController.prototype.autokey = wechat(config).middleware(async(message, ctx)=> {
console.log(message);
});
请指点迷津,谢谢诶
非常感谢你开源这个插件,我有一个问题如下
router.use('/aaa',wechat(config).middleware(async (message, ctx) => {
return '啊啊啊啊';
}))
为什么,这个路由不走呢?
wechat("some token").middleware()中的配置的some token是Token(令牌)还是EncodingAESKey?
使用令牌的话报400错误,
使用EncodingAESKey的话是401
跟踪代码到 await getRawBody(ctx.req这里,await一直没有返回。。
新增的处理xml提前处理的方法,可能需要加个判断body是不是字符串
我用的koa2+koa-bodyparser4.2.x,导致ctx.request.body
是个{}
,所以就一直报错了。。
搞了一下午,刚刚突然发现是新增了那个判断。
if (ctx.request.body && typeof ctx.request.body === 'string') {
xml = ctx.request.body;
} else {
// 取原始数据
xml = await getRawBody(ctx.req, {
length: ctx.request.length,
limit: '1mb',
encoding: ctx.request.charset || 'utf-8'
});
}
在koa2.0下用的时候
ctx.request.body === 'string'
这个不是string
而是 object
Lines 224 to 228 in 6dbb669
var xml = await getRawBody(ctx.req, {
length: ctx.length,
limit: '1mb',
encoding: ctx.charset
});
此处的 ctx.length
是委托给 ctx.response.length
的,所以值是 undefined
ctx.charset
应该写成 ctx.request.charset
才对,并且按照 raw-body
文档,encoding: true
时默认为 'utf-8'。
参见 https://github.com/koajs/koa/blob/aaac09af1a6aa02161fead1422fac460fbdcce3e/lib/context.js#L154-L208
所以我认为这里应该写成:
var xml = await getRawBody(ctx.req, {
length: ctx.request.length,
limit: '1mb',
encoding: ctx.request.charset || 'utf-8'
});
作者你好,感谢提供这个封装库,这使得在做微信回复功能从原来数小时缩短至数分钟,由于我是刚学习nodejs不久,也是刚使用koa2,在我原有项目中使用该库里面本身的路由访问全部提示Invalid signature,请问作者如何解决啊
2017-09-28 17:27:06,846 ERROR 1014 nodejs.unhandledRejectionError: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:489:11)
at ServerResponse.setHeader (_http_outgoing.js:496:3)
at Object.set (/usr/src/app/node_modules/koa/lib/response.js:436:16)
at Object.set length [as length] (/usr/src/app/node_modules/egg/app/extend/response.js:11:10)
at Object.set body [as body] (/usr/src/app/node_modules/koa/lib/response.js:152:19)
at Object.body (/usr/src/app/node_modules/delegates/index.js:92:31)
at /usr/src/app/node_modules/co-wechat/lib/wechat.js:278:20
at
at process._tickCallback (internal/process/next_tick.js:188:7)
name: 'unhandledRejectionError'
pid: 1014
hostname: 6f6a5b08ca67
问题由来:
我们公司环境是node6.x使用框架是koa2,集成co-wechat2.1运行是提示语法有问题,查看了co-wechat2.1源码发现里面使用了node7.x + 才支持的await等,所以 想要使用co-wechat 2.x是不是只能升级node7.x+ ,我们不能调整koa至1因为框架很多有依赖,所以想问下 是不是如此?假如是 请 作者在页面标注下,防止其他用户出现类似问题,谢谢啦!
使用koa2 。 当MsgType:event,Event:SCAN 即已关注的用户扫带参二维码进来之后,return "111"。那么会先收到111, 跟随着会出现 "该公众号提供的服务出现故障,请稍后再试" 。 测试号没问题, 其他事件也不会出现这个情况
1.X版本报 nodejs.TypeError: Cannot read property 'encrypt_type' of undefined
Template Literal is fastest, smallest and simplest template engine, because it use JS's literal template feature.
It's 55 times faster than EJS, and it also use less CPU and RAM ressources, so it may be a good idea to use it instead of EJS 😀
'use strict';
const wechat = require('co-wechat');
module.exports = app => {
class HomeController extends app.Controller { }
// 因为 Egg 需要用类的形式来组织,而 wechat 是通过 middleware 方法来生成中间件
HomeController.prototype.wechat = wechat({
token: '',
appid: '',
encodingAESKey: ''
}).middleware(async (message, ctx) => {
// TODO
var content = message.Content;
var reply = '无法回复您的内容: ' + content;
if (message.MsgType === 'event') {
if (message.Event === 'subscribe') {
if (message.EventKey) {
//扫描二维码事件
console.log('扫描二维码进入...');
}
reply = 'Thanks to subscribe. \r\n';
} else if (message.Event === 'unsubscribe') {
// 取消关注
console.log('取消关注。。。');
reply = '';
}
} else if (message.MsgType === 'text') {
// 用户输入文本选项
switch (content) {
case '1':
reply = '您当前选项为 1。'
break;
case '2':
reply = '您当前选项为 2。'
break;
case '3':
reply = '您当前选项为 3。'
break;
default:
break;
}
return reply;
}
await next();
});
return HomeController;
};
我用了微信公众平台接口调试工具进行调试,基础支持返回access_token,消息接口测试返回的是请求失败,手机上也是返回服务出现故障,刚刚上手egg.js,请问是哪里出现了问题?
重新修改了下代码
当前逻辑是提示签名异常就直接返回错误信息了,然而有些情况是希望捕获这个异常并且自定义返回结果的。
babel默认配置会把node_modules过滤,所以编译时node_modules里的es6语法并未转换
我查看了一下babel插件的源码,都是经过编译后再上传到npm上的,所以我希望你们也可以把编译结果文件传到npm上,而不要把源码直接上传,这样对小白使用起来不太方便
看了下接收设备绑定解绑事件的MsgType好像目前没有支持(是device_event)
想使用最新的开发
这样的形式是否部署后会占用项目根目录,假如想要支持多个目录,例如:
sorry, my mistake. 看错了。
作者你好,最近遇到一个问题:和egg一起使用在处理不同的消息时调用不同的service?
module.exports = app => {
class HomeController extends app.Controller {}
// 因为 Egg 需要用类的形式来组织,而 wechat 是通过 middleware 方法来生成中间件
HomeController.prototype.wechat = wechat({
token: 'token',
appid: 'appid',
encodingAESKey: ''
}).middleware(async (message, ctx) => {
// 此处能否调用不同的service?
});
return HomeController;
};
reply
方法下,content
可能为 null
,而且 typeof null === 'object'
,这段代码(144行)可能会出错
if (Array.isArray(content)) {
type = 'news';
} else if (typeof content === 'object') { // content 可能为 null
if (content.hasOwnProperty('type')) { // 如果 content 为 null,会报错
if (content.type === 'customerService') {
return reply2CustomerService(fromUsername, toUsername, content.kfAccount);
}
type = content.type;
info.content = content.content;
} else {
type = 'music';
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.