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-open-service

v0.2.9

Published

apis set for wechat open platform 3rd-party service 微信开放平台的第三方平台接口

Downloads

37

Readme

wechat openflatform 3rd-party service

微信开放平台第三方开放平台版SDK-主动调用接口

功能说明

微信开放平台第三方开放平台接口主动调用,以及提供应用授权接口。

安装方法

$ npm install wechat-open-service

使用方法

var _route = function(message, req, res, next) {

      if (message.InfoType == 'component_verify_ticket') { //微信服务器发过来的票,每10分钟发一次
        ParamConfigService.set('component_verify_ticket',message.ComponentVerifyTicket);
        sails.log.error("ticket:"+message.ComponentVerifyTicket);
        res.reply('success');
      }else if (message.InfoType == 'unauthorized') { //取消授权的通知
        //更新到数据库
        sails.log.error("取消授权的appid:"+message.AuthorizerAppid);
        res.reply('success');
      } else {
        res.reply('success');
      };
    }

    wechat_cs(_config, _route)(req, res, next);

前提

  • 首先,你要有一个开放平台号(open.weixin.qq.com)。
  • 然后,你要通过开发者认证以及申请第三方开放平台。
  • 接下来才可以创建第三方平台应用,并且设置第三方平台应用。
  • 基于本SDK开发具体的第三方平台应用。

用法

其中的token,encodingAESKey,suite_id可以在套件的信息配置界面获取。

var WechatOpenAPI = require('wechat-open-service');

var get_token = function(cb) {
  var self = this;
  SuiteConfig
    .findOne({
      suite_id: self.suiteId,
    })
    .exec(function(err, result) {
      if (result && (new Date().getTime()) < (new Date(result.suite_access_token_expire)).getTime()) {
        // 有效期内,直接返回
        cb(null, {
          suite_access_token: result.suite_access_token,
          expires_in: result.suite_access_token_expire
        });
      } else {
        cb(null, null);
      }
    });
}

var save_token = function(token, cb) {
  var self = this;
  async.waterfall([
    function(cb) {
      SuiteConfig
        .findOne({
          suite_id: self.suiteId,
        }).exec(cb);
    },
    function(sc, cb) {
      if (sc) {
        //已经存在了,就更新一下token,否则创建一条新的
        sc.suite_access_token = token.suite_access_token;
        sc.suite_access_token_expire = new Date((new Date()).getTime() + 7190000);
        sc.save(cb);
      } else {
        cb(null, null);
      }
    }
  ], cb);
}

var openApi = new WechatOpenAPI(component_appid,component_appsecret,ticket,get_token,save_token);
// 授权完成后跳转的URL,一般返回套件开发商自己的页面,并且获取用户授权的信息。
redirect_uri = 'http://xxx.xxx.xxx/auth_callback_url',
auth_url = '';

// 获取临时授权码,生成授权页面(带一个授权的按钮)
openApi.getPreAuthCode(appid, function(err, result) {
  auth_url = openApi.generateAuthUrl(result.pre_auth_code, encodeURIComponent(redirect_uri));
});

// 授权后,跳转回来的URL,可以获取auth_code,然后换取authorizer_refresh_token。得到永久授权之后就能知道是哪个微信公众号appid了(authorizer_appid)。
var auth_code = req.query.auth_code;
openApi.getAuthorizerRefreshToken(auth_code, cb);

代替授权公众号调用接口用法, 基于前面的方法获得authorizer_refresh_token后, 可以这样调用


openApi.getLatestToken(function(err, token){
	var component_access_token = token.component_access_token;
	var apiAgent = new WechatOpenAPI.Agent(component_appid,component_access_token,authorizer_appid,authorizer_refresh_token,get_token,save_token);
		//举例,获取客户公众号的素材数量
	apiAgent.getMaterialCount(function(err, result){...});	
});

通过代理服务器访问

场景

对于大规模的集群部署模式,为了安全和速度,会有一些负载均衡的节点放在内网的服务器上(即负载均衡的节点与主结点通过内网连接,并且内网服务器上没有外网的IP)。这时,就需要配置代理服务器来使内网的机器可以有限度的访问外网的资源。例如:微信套件中的各种主动调用接口。

如何架设代理服务器在这里不做赘述,一般推荐使用squid 3,免费、快速、配置简单。

技术原理

由于需要访问的微信API服务器是https协议,所以普通的http代理模式不能使用。 而一般都是http协议的代理服务器。 我们要实现的就是通过http代理通道来走https的请求。

基本的步骤是2步:

  • 连接到代理服务器,发送CONNECT命令,打开一个TCP连接。
  • 使用上一步打开的TCP连接,发送https的请求。

实现步骤

一、下载node-tunnel 注意:npm上的版本较老,不支持node v0.10以上的版本。

二、使用 httpsOverHttp 这个agent。

三、将agent配置给urllib,通过urllib的beforeRequest这个方法。

var tunnel = require('tunnel');
var APICorp = require('wechat-corp-service');

var agent = tunnel.httpsOverHttp({
  proxy: {
    host: 'proxy_host_ip',
    port: 3128
  }
});

var apicorp = new APICorp(sc.suite_id, sc.suite_secert, sc.suite_ticket, get_token, save_token);

apicorp.setOpts({
    beforeRequest:function(options){
        options.agent = agent;
    }
});

相关文档

License

The MIT license.

交流群

QQ群:157964097,使用疑问,开发,贡献代码请加群。

感谢

感谢以下贡献者:

 project  : wechat-corp-service
 repo age : 10 months
 active   : 7 days
 commits  : 16
 files    : 13
 authors  :
     6  Jackson Tian  37.5%
     6  Nick Ma       37.5%
     3  hezedu        18.8%
     1  马剑          6.2%

捐赠

如果您觉得Wechat企业号版本对您有帮助,欢迎请作者一杯咖啡

捐赠wechat