
Security News
RubyGems Adds Cooldown Feature to Bundler for Newly Published Gems
RubyGems and Bundler 4.0.13 introduced an opt-in cooldown feature that delays newly published gems during dependency resolution.
koa-easywechat
Advanced tools
koa-easywechat是一个基于koa2的微信公众号开发中间件
由于使用的是koa2,异步开发使用async/await的写法,所以请保证你的node版本>=7.6
点击查看更新日志
$ npm install koa-easywechat -S
注意:koa-easywechat中间件要写在最前面,也就是要第一个use,因为我在ctx上挂载了一个wechat对象,这个对象实现了大部分的微信接口,这样才能保证开发者在自己的写路由里,获取到ctx.wechat进行自己的业务开发
var Koa=require("koa");
var WeChat=require("koa-easywechat");
var app=new Koa();
app.use(WeChat({
appID:"",
appsecret:"",
token:"",
isSafeModel:false,
encodingAESKey:""
},async function (next){
this.reply={
type:"text",
content:"回复一段文字吧"
}
}
}));
app.listen(3000,()=>{
console.log("server is running")
});
上面这段代码用于回复一段文字
async replyHandler(next){
var message=this.message;
var wechat=this.wechat;
this.reply={
type:"text",
content:"回复一段文字吧"
}
}
如果没有回复的业务逻辑,可以调用next()将控制权交给之后的中间件处理
async replyHandler(next){
await next();
}
在这个函数里,this就是koa框架中的上下文(ctx)的引用,我在ctx对象上挂载了两个对象wechat和message
功能:获取accessToken。因为调用accessToken的接口每天的调用频率是有上限的,不能频繁调用,所以这里实现了accessToken的自管理,开发者不必关心accessToken是否过期,过期之后会自动去微信服务器请求并更新最新的accessToken。
var wechat=ctx.wechat;
var token=await ctx.getAccessToken();
功能:获取jsapi_ticketj,sapi_ticket是公众号用于调用微信JS接口的临时票据。jsapi_ticket与token类似,在微信网页开发中需要用到,也需要全局缓存。
var wechat=ctx.wechat;
var jsApiTicket=await ctx.getJsApiTicket();
功能:上传临时素材
参数
| 属性 | 类型 | 说明 |
|---|---|---|
| type | String | 支持的值有image, voice, video, thumb |
| filePath | String | 多媒体文件的路径 |
返回值(json)
| 属性 | 说明 |
|---|---|
| type | 媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb,主要用于视频与音乐格式的缩略图) |
| media_id | 媒体文件上传后,获取标识 |
| created_at | 媒体文件上传时间戳 |
var wechat=ctx.wechat;
var media=await ctx.uploadTemporaryMaterial("image","文件的路径");
功能:创建自定义菜单
参数:
| 属性 | 类型 | 说明 |
|---|---|---|
| menuObj | Object | 菜单对象,格式要符合规定的格式 |
var menu = {
button: [
{
type: "click",
name: "今日歌曲",
key: "V1001_TODAY_MUSIC"
},
{
name: "菜单",
sub_button: [
{
type: "view",
name: "搜索",
url: "http://www.soso.com/"
},
{
type: "click",
name: "赞一下我们",
key: "V1001_GOOD"
}]
}]
};
var wechat=ctx.wechat;
await wechat.createMenu(menu);
功能:获取json格式的自定义菜单
var wechat=ctx.wechat;
var menu=await wechat.getMenu();
功能:删除自定义菜单
var wechat=ctx.wechat;
var menu=await wechat.deleteMenu();
完结,撒花~~~~~~~~~~~~~~~
FAQs
使用koa开发微信公众号
The npm package koa-easywechat receives a total of 8 weekly downloads. As such, koa-easywechat popularity was classified as not popular.
We found that koa-easywechat demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
RubyGems and Bundler 4.0.13 introduced an opt-in cooldown feature that delays newly published gems during dependency resolution.

Security News
pnpm 11.5 now recognizes npm staged publish approvals in release metadata, preventing those releases from being mistaken for lower-trust package publishes.

Security News
Federal audit finds NIST lacked a plan to clear the NVD backlog, wasted funds on duplicate work, and delayed use of CISA data.