TypeError: Wechat is not a constructor

来源:2-7 第二天 先撸一个自动回复出来

辞久

2017-06-13

http://szimg.mukewang.com/593f856d0001affb07170283.jpg


'use strict'


var Promise = require('bluebird')

var request = Promise.promisify(require('request'))

var prefix = 'https://api.weixin.qq.com/cgi-bin/'

var api = {

accessToken:'token?grant_type=client_credential'

}

function Wechat (opts) {

var that = this;

this.appID = opts.appID;

this.appSecret = opts.appSecret;

this.getAccessToken = opts.getAccessToken;

this.saveAccessToken = opts.saveAccessToken;

this.getAccessToken()

.then(function  (data) {

try{

data = JSON.parse(data)

}

catch(e){

return that.updateAccessToken()

}

if (that.isValidAccessToken(data)) {

Promise.resolve(data)

}

else {

return that.updateAccessToken()

}

})

.then(function  (data) {

that.access_token = data.access_token

that.expires_in = data.expires_in

that.saveAccessToken(data)

})

}


Wechat.prototype.isValidAccessToken = function  (data) {

if (!data || !data.access_token || !data.expires_in) {

return false;

}

var access_token = data.access_token;

var expires_in = data.expires_in;

var now = (new Date().getTime())

if (now < expires_in) {

return true;

}

else {

return false;

}

}


Wechat.prototype.updateAccessToken = function  (data) {

var appID = this.appID

var appSecret = this.appSecret

var url = prefix + api.accessToken + '&appid=' + appID + '&secret=' + appSecret

return new Promise(function  (resolve,reject) {

request({url:url,json:true}).then(function  (response) {

var data = response.body

var now = (new Date().getTime())

var expires_in = now + (data.expires_in -20)*1000

data.expires_in = expires_in

resolve(data)

})

})

}


写回答

1回答

Scott

2017-06-13

是不是没有通过 module.exports = Wechat 把构造函数暴露出去

0
1
辞久
确实是
2017-06-13
共1条回复

7天搞定Node.js微信公众号

Koa框架、ES2015新特性、MongoDB,开发微信公众号

1742 学习 · 787 问题

查看课程