@@ -57,7 +57,7 @@ class API {
5757 * @param {AsyncFunction } getToken 可选的。获取全局token对象的方法,多进程模式部署时需在意
5858 * @param {AsyncFunction } saveToken 可选的。保存全局token对象的方法,多进程模式部署时需在意
5959 */
60- constructor ( appid , appsecret , getToken , saveToken ) {
60+ constructor ( appid , appsecret , getToken , saveToken , tokenFromCustom ) {
6161 this . appid = appid ;
6262 this . appsecret = appsecret ;
6363 this . getToken = getToken || async function ( ) {
@@ -76,6 +76,7 @@ class API {
7676 this . merchantPrefix = 'https://api.weixin.qq.com/merchant/' ;
7777 this . customservicePrefix = 'https://api.weixin.qq.com/customservice/' ;
7878 this . defaults = { } ;
79+ this . tokenFromCustom = tokenFromCustom ;
7980 // set default js ticket handle
8081 this . registerTicketHandle ( ) ;
8182 }
@@ -140,7 +141,7 @@ class API {
140141 err . name = 'WeChatAPIError' ;
141142 err . code = data . errcode ;
142143
143- if ( err . code === 40001 && retry > 0 ) {
144+ if ( err . code === 40001 && retry > 0 && ! this . tokenFromCustom ) {
144145 // 销毁已过期的token
145146 await this . saveToken ( null ) ;
146147 let token = await this . getAccessToken ( ) ;
@@ -202,6 +203,11 @@ class API {
202203 var accessToken ;
203204 if ( token && ( accessToken = new AccessToken ( token . accessToken , token . expireTime ) ) . isValid ( ) ) {
204205 return accessToken ;
206+ } else if ( this . tokenFromCustom ) {
207+ let err = new Error ( 'accessToken Error' ) ;
208+ err . name = 'WeChatAPIError' ;
209+ err . code = 40001 ;
210+ throw err ;
205211 }
206212 return this . getAccessToken ( ) ;
207213 }
0 commit comments