npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wechat-nodejs

v1.0.1

Published

wechat media platform sdk for nodejs

Downloads

7

Readme

微信公众平台开发NodeJs SDK

本SDK要求NodeJs >= 4.x,欢迎大家在issues提问。

##安装 npm install wechat-nodejs

##说明 使用所有sdk功能前需要初始化wechat句柄,初始化代码如下:

const Wechat = require('wechat-nodejs').Wechat;
const wechat = new Wechat(appId,appSecret);

SDK扩展了JS原始错误类,包含message,code属性,SDK中Promise抛出的错误code为微信返回的errcode,抛出一个自定义错误代码如下:

const AppError = require('wechat-nodejs').AppError;
throw new AppError('参数错误',1);

##功能列表 ###分组

  • 初始化Group
const Group = require('wechat-nodejs').Group;
const group = new Group(wechat.getInstance());
  • 创建分组
group.create('测试分组').then((data)=>{
	console.log(data);
}).catch((e)=>{
	console.error(e.message,e.code);
});
  • 获取所有分组
group.getAll().then((data)=>{
	console.log(data);
}).catch((e)=>{
	console.error(e.message,e.code);
});
  • 获取用户所在分组
group.getIdByOpenid(openid).then((groupId)=>{
	console.log(groupId);
}).catch((e)=>{
	console.error(e.message,e.code);
});
  • 修改分组名称
group.update(100,'测试分组01').then((data)=>{
	console.log(data);
}).catch((e)=>{
	console.error(e.message,e.code);
});
  • 移动用户分组
group.moveUserToGroup('oA-yljrYgywqN3SCXS_3jZnIP6Yw',100).then((data)=>{
	console.log(data);
}).catch((e)=>{
	console.error(e.message,e.code);
});
  • 批量移动用户分组
group.moveUsersToGroup(['oA-yljrYgywqN3SCXS_3jZnIP6Yw'],100).then((data)=>{
	console.log(data);
}).catch((e)=>{
	console.error(e.message,e.code);
});
  • 删除分组
group.remove(100).then((data)=>{
	console.log(data);
}).catch((e)=>{
	console.error(e.message,e.code);
});

JSSDK

  • 初始化JSSDK
const JSSDK = require('wechat-nodejs').JSSDK;
const jssdk = new JSSDK(wechat.getInstance());
  • 获取jsticket
jssdk.getTicket().then((ticket)=>{
	console.log(ticket);
}).catch((e)=>{
	console.error(e.message,e.code);
});
  • 获取jssdk配置参数
jssdk.getConfig('http://www.baidu.com',['onMenuShareTimeline'],false).then((config)=>{
	console.log(config);
}).catch((e)=>{
	console.error(e.message,e.code);
});

###自定义菜单

  • 初始化Menu
const Menu = require('wechat-nodejs').Menu;
const menu = new Menu(wechat.getInstance());
  • 创建自定义菜单
const button = [
  {
    "type": "click",
    "name": "今日歌曲",
    "key": "V1001_TODAY_MUSIC"
  },
  {
    "name": "菜单",
    "sub_button": [
    {
      "type": "view",
      "name": "搜索",
      "url": "http://www.soso.com/"
    },
    {
      "type": "view",
      "name": "视频",
      "url": "http://v.qq.com/"
    },
    {
      "type": "click",
      "name": "赞一下我们",
      "key": "V1001_GOOD"
    }]
  }
];
menu.create(button).then((data)=>{
	console.log(data);
}).catch((e)=>{
	console.error(e.message,e.code);
});
  • 查询自定义菜单
menu.get().then((data)=>{
	console.log(data);
}).catch((e)=>{
	console.error(e.message,e.code);
});
  • 删除自定义菜单
menu.remove().then((data)=>{
	console.log(data);
}).catch((e)=>{
	console.error(e.message,e.code);
});
  • 获取公众平台后台设置的自定义菜单
menu.getByWeb().then((data)=>{
	console.log(data);
}).catch((e)=>{
	console.error(e.message,e.code);
});

消息

  • 初始化Message
const Message = require('wechat-nodejs').Message;
const message = new Message(wechat.getInstance());
  • 发送模板消息
const data = {
  orderId: {
    value: '20160101'
  },
  status: {
    value: '已发货'
  }
};
const openid = 'oA-yljj5cBGSvnwFodHT1iqis7X8';
const templateId = 'G4C9rNCejbhyYzh7xsOh46pieLelrmj_bLQtRhdOqkY';
const url = 'https://github.com';
message.sendTemplate(openid,templateId,url,data).then((data)=> {
  data.should.have.property('errcode', 0);
}).catch((e)=>{
  	console.error(e.message,e.code);
});

用户

  • 初始化User
const User = require('wechat-nodejs').User;
const user = new User(wechat.getInstance());
  • 设置用户备注名
user.setRemark('oA-yljj5cBGSvnwFodHT1iqis7X8','重要客户').then((data)=>{
	console.log(data);
}).catch((e)=>{
	console.error(e.message,e.code);
});
  • 获取用户信息
user.getInfo('oA-yljj5cBGSvnwFodHT1iqis7X8').then((data)=>{
	console.log(data);
}).catch((e)=>{
	console.error(e.message,e.code);
});
  • 批量获取用户信息
user.batchGetInfo(['oA-yljj5cBGSvnwFodHT1iqis7X8']).then((data)=>{
	console.log(data);
}).catch((e)=>{
	console.error(e.message,e.code);
});
  • 检测关注
user.isSubscribe('oA-yljj5cBGSvnwFodHT1iqis7X8').then((isSubscribe)=>{
	console.log(isSubscribe);
}).catch((e)=>{
	console.error(e.message,e.code);
});
  • 获取关注用户openid列表
user.getList('oA-yljj5cBGSvnwFodHT1iqis7X8').then((data)=>{
	console.log(data);
}).catch((e)=>{
	console.error(e.message,e.code);
});

##单元测试 npm install mocha -g
在本sdk根目录新建config.json,内容如下:

{
  "wechat": {
    "appId": "微信公众号appId",
    "appSecret": "微信公众号appSecret"
  }
}

npm run test ##授权协议 MIT License ##我的博客 每天进步一点点