Giter VIP home page Giter VIP logo

Comments (4)

nick-ma avatar nick-ma commented on September 17, 2024

首先,这东西(包括其他的api)是不能跨进程复用的。
其次,跨进程部署的时候,需要把accessToken放在数据库、文件、redis之类的共享的地方。
第三,如果你的前台和后台是2个独立的进程(即2个独立的,互不干扰的express工程),那么各自初始化自己的api就好了,无非他们对accessToken的读取和存储是一个位置就好。

from wechat-api.

keyiis avatar keyiis commented on September 17, 2024

我是第三种情况,在2个单独的进程汇总都使用如下代码初始化api
var api = new API(AppID, AppSecret, function (callback) {
// 传入一个获取全局token的方法
Weixin.findOne({'AppID':AppID},function(err,weixin){
callback(null, weixin.access_token);//weixin.access_token包含accessToken和expireTime两个字段。
});
}, function (token, callback) {
// 请将token存储到全局,跨进程、跨机器级别的全局,比如写到数据库、redis等
// 这样才能在cluster模式及多机情况下使用,以下为写入到文件的示例
Weixin.update({'AppID':AppID},{$set:{access_token:token}},function(err){
if (err) return callback(err);
});
});

from wechat-api.

keyiis avatar keyiis commented on September 17, 2024

Weixin的schema结构:
name:String,//微信名称
number:{type:String,index:{unique: true}},//微信号
AppID:{type:String,index:{unique: true}},
AppSecret:String,
access_token:{}

from wechat-api.

keyiis avatar keyiis commented on September 17, 2024

问题找到了
Weixin.update({'AppID':AppID},{$set:{access_token:token}},function(err){
if (err) return callback(err);
这里没有将token回传,加上callback(token);就正常了
});

from wechat-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.