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

wechater

v1.0.11

Published

微信生态SDK

Downloads

51

Readme

Wechater 微信SDK

使用typescript, 结合微信提供的API,提供到一个完善到SDK,支持智能代码提醒、功能拓展。

导航🧭

Install

npm install wechater

QuickStart

koa 接入wechat

const KoaApplication = require('koa');
const Wechater = require('wechater');

const koa = new KoaApplication();

const wechat = new Wechater({
  appId: 'wxAppId',
  appSecret: 'wxAppSecret',
  token: 'wxToken',
  encodingAesKey: 'wxEncodingAesKey',
  isDebug: true,
});

koa.use(wechat.accessWxKoa((reply, ctx, next) => {
  reply.replyText('hello koa wechater');
}));
koa.listen(1718,()=>{console.info('Listen 1718')});

express 接入wechat

const Express = require('express');
const Wechater = require('wechater');

const wechat = new Wechater({
  appId: 'wxAppId',
  appSecret: 'wxAppSecret',
  token: 'wxToken',
  encodingAesKey: 'wxEncodingAesKey',
  isDebug: true,
});

const app = new Express();
app.all('/', wechat.accessWxExpress((reply) => {
  reply.replyText('hello express wechater');
}));

app.listen(1728, ()=>{console.info('Listen 1728')});

获取AccessToken 创建菜单接口

const Express = require('express');
const Wechater = require('wechater');
const bodyParser = require('body-parser')

let accessToken = '';
let expireTimestamp = 0;

const wechat = new Wechater({
  appId: 'wxAppId',
  appSecret: 'wxAppSecret',
  accessTokenFunc: async()=>{
    const nowTime = Math.floor((new Date().getTime())/1000);
    if(nowTime >= expireTimestamp){
      const data = await wechat.getAccessToken();
      if(data.errcode){
        throw new Error(`Get AccessToken Error, ${data.errmsg}`)
        }
      accessToken = data.access_token;
      expireTimestamp = nowTime + data.expires_in - 120;
    }
    return accessToken;
  }
});

const app = new Express();
app.use(bodyParser.json());
app.post('/menuCreate', async (req, res)=>{
  const result = await wechat.menuCreate(req.body);
  res.send(result)
});

app.listen(1728, ()=>{console.info('Listen 1728')});

模拟请求

POST ip:1728/menuCreate, 公众号菜单会设置成功

{
     "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"
            }]
       }]
 }

Expand 自定义拓展SDK

由于SDK与微信存在一段时间更新期,我们建议用户自定义拓展SDK, 以新建菜单接口为例

const Wechater = require('wechater');

class NewWechater extends Wechater{
  constructor(options){
    super(options);
  }
  menuCreate(data){
    return this.request('cgi-bin/menu/create',{
      method: 'post',
      data,
    })
  }
}

module.exports = NewWechater;

Config

interface Options {
  appId?: string;
  appSecret?: string;
  token?: string;
  encodingAesKey?: string;
  host?: string;
  isDebug?: boolean;
  accessTokenFunc?: () => Promise<string>;
}

常用功能

  1. 登录
const result = await wechater.jscode2session(js_code)
  1. 获取access_token
const result = await wechater.getAccessToken()
  1. 解析数据
const resuult = wechater.decryptData(sessionKey, encryptedData, iv)
  1. 请求数据,根据access_token
const result = await wechater.request('url', AxiosRequestConfig)  //url 为除去baseUrl路由部分

公众号SDK

微信官方文档 - 公众号

用户管理

  1. user 查看用户详情

消息管理-接收与被动回复

使用 wechater.accessWxKoawechater.accessWxExpress 中间件,获取到hanler, 使用handler进行消息接收与被动回复信息`

  1. replyText 回复文本
  2. replyImage 回复图片
  3. replyVoice 回复语音
  4. replyVideo 回复视频
  5. replyMusic 回复音乐
  6. replyToCustomer 转发到客服系统
  7. replyNews 发送图文信息

小程序SDK

微信官方文档 - 小程序

微信支付SDK

微信官方文档 - 微信支付

企业微信SDK

微信官方文档 - 企业微信