Skip to content

Commit

Permalink
Merge 0dc37d4 into 002f031
Browse files Browse the repository at this point in the history
  • Loading branch information
befinal committed Apr 4, 2018
2 parents 002f031 + 0dc37d4 commit 0e61905
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/api_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class API {
* @param {AsyncFunction} getToken 可选的。获取全局token对象的方法,多进程模式部署时需在意
* @param {AsyncFunction} saveToken 可选的。保存全局token对象的方法,多进程模式部署时需在意
*/
constructor(appid, appsecret, getToken, saveToken) {
constructor(appid, appsecret, getToken, saveToken, tokenFromCustom) {
this.appid = appid;
this.appsecret = appsecret;
this.getToken = getToken || async function () {
Expand All @@ -76,6 +76,7 @@ class API {
this.merchantPrefix = 'https://api.weixin.qq.com/merchant/';
this.customservicePrefix = 'https://api.weixin.qq.com/customservice/';
this.defaults = {};
this.tokenFromCustom = tokenFromCustom;
// set default js ticket handle
this.registerTicketHandle();
}
Expand Down Expand Up @@ -140,7 +141,7 @@ class API {
err.name = 'WeChatAPIError';
err.code = data.errcode;

if (err.code === 40001 && retry > 0) {
if (err.code === 40001 && retry > 0 && !this.tokenFromCustom) {
// 销毁已过期的token
await this.saveToken(null);
let token = await this.getAccessToken();
Expand Down Expand Up @@ -202,6 +203,11 @@ class API {
var accessToken;
if (token && (accessToken = new AccessToken(token.accessToken, token.expireTime)).isValid()) {
return accessToken;
} else if (this.tokenFromCustom) {
let err = new Error('accessToken Error');
err.name = 'WeChatAPIError';
err.code = 40001;
throw err;
}
return this.getAccessToken();
}
Expand Down

0 comments on commit 0e61905

Please sign in to comment.