微信第三方平台工具模块
$ npm install wechat-platform --save
-
- 获取微信推送给第三方平台的component_verify_ticket(十分钟推送一次)
- component_verify_ticket换取第三方平台component_access_token
- component_access_token换取预授权码pre_auth_code。
- 构建跳转URL,用户授权后获取授权码authorization_code
- authorization_code换取授权公众号的授权信息
-
- 使用authorizer_access_token访问公众号API
var config = {};
config.wechat = {
appid: 'appid',
appsecret: 'appsecret',
msg_token: 'token',
msg_aeskey: 'aeskey'
};
config.redis = {
host: '192.168.0.111',
port: 6379,
options: {
connect_timeout: 30000
}
};
module.exports = config;
/**
* Module dependencies.
*/
var OAuth = require('wechat-platform').OAuth;
var ComponentToken = require('wechat-platform').ComponentToken;
var RedisCo = require('./redisCo');
var config = require('./config');
var redisClient = RedisCo(config.redis).client;
var oAuth = OAuth(config.wechat);
/**
* redis key
*/
const PLATFORM_TICKET_KEY = 'qianmi.wechat.platform.ticket';
const PLATFORM_TOKEN_KEY = 'qianmi.wechat.platform.token';
/**
* 获取第三方平台token
*/
exports.getComponentToken = function *() {
var tokenData = JSON.parse(yield redisClient.get(PLATFORM_TOKEN_KEY));
if(tokenData) {
let componentToken = ComponentToken(tokenData);
if (componentToken.isValid()) {
return componentToken.data.access_token;
}
}
var ticket = yield redisClient.get(PLATFORM_TICKET_KEY);
tokenData = yield oAuth.getComponentToken(ticket);
var result = ComponentToken(tokenData);
redisClient.set(PLATFORM_TOKEN_KEY, JSON.stringify(result.data));
return result.data.access_token;
}
var Event = require('wechat-platform').Message;
exports.eventProxy = Event(config.wechat).proxy(function *(){
var info = this.weixin;
if(info.InfoType = 'component_verify_ticket') {
//存入缓存
redisClient.set(PLATFORM_TICKET_KEY, info.ComponentVerifyTicket);
}
this.body = 'success';
})
其他功能点使用于此类似
有任何问题欢迎email作者:[email protected]
或者 在此提交
如果wechat-platform帮助到您了,欢迎请作者一杯咖啡。