silencehvk / wechatbynode Goto Github PK
View Code? Open in Web Editor NEWUse the Node.js development WeChat(使用Node.js 开发微信公众号)【 博客地址:https://cnodejs.org/user/SilenceHVK 】
Home Page: https://cnodejs.org/user/SilenceHVK
License: MIT License
Use the Node.js development WeChat(使用Node.js 开发微信公众号)【 博客地址:https://cnodejs.org/user/SilenceHVK 】
Home Page: https://cnodejs.org/user/SilenceHVK
License: MIT License
CommonJS 在导出时都是值拷贝,就算导出的值变了,导入的值也不会改变,所以如果想更新值,必须重新导入一次。所以即使更新access_token.json
的内容,只要服务器没有重启,都是首次require
时的access_token
的值。这里应该改为fs.readFileSync / fs.writeFileSync
。
示例:
async handleAccesstoken() {
const rawAccess = fs.readFileSync(__dirname + '/accessToken.json', 'utf8');
const accessTokenJson = JSON.parse(rawAccess);
return new Promise((resolve, reject) => {
const currentTime = new Date().getTime();
if (accessTokenJson.access_token === "" || accessTokenJson.expires_time < currentTime) {
const url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${config.wechat.appid}&secret=${config.wechat.appsecret}`;
rp(url).then(res => {
const file = {};
// //更新本地存储的
const result = JSON.parse(res);
if(res.indexOf("errcode") < 0){
file.access_token = result.access_token;
file.expires_time = new Date().getTime() + (parseInt(result.expires_in) - 200) * 1000;
//更新本地存储的
fs.writeFile(__dirname + '/accessToken.json', JSON.stringify(file), (error) => {
reject(error);
});
//将获取后的 access_token 返回
resolve(file.access_token);
}else{
//将错误返回
resolve(result);
}
}).catch(err => {
reject(err);
});
} else {
//将本地存储的 access_token 返回
resolve(accessTokenJson.access_token);
}
})
}
用的是Koa
是nodeV8.9 不支持这样的模块引入,还是您引入的方法有问题,我是新手,谢谢~
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.