构造函数失败报错:WeChat is not a constructor
来源:2-6 第二天 票据 access_token 打开新世界大门
web_zhang
2018-08-13
function WeChat(options) { this.appID = options.appID; this.appSecret = options.appSecret; this.getAccessToken = options.getAccessToken; this.saveAccessToken = options.saveAccessToken; var that = this; this.getAccessToken() .then(function (data) { try { data = JSON.parse(data); } catch (e) { return that.updateAccessToken(); } if (that.isValidAccessToken(data)) { return Promise.resolve(data); } else { return that.updateAccessToken(); } }) .then(function (data) { console.log(data); that.access_token = data.access_token; that.expires_in = data.expires_in; that.saveAccessToken(data) return Promise.resolve(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 () { var appID = this.appID; var appSecret = this.appSecret; var url = api.access_token + '&appid=' + appID + '&secret=' + appSecret; return new Promise(function (resolve,reject) { request({url: url,json: true}) .then(function (res) { var data = res.body; var now = new Date().getTime(); var expires_in = now + (data.expires_in - 20) * 1000; data.expires_in = expires_in; resolve(data) }) }) }; module.exports = function (options) { var WeChat = new WeChat(options); //这句报错 return function *(next) { console.log(this.query); var token = options.token; var signature = this.query.signature; var nonce = this.query.nonce; var timestamp = this.query.timestamp; var echostr = this.query.echostr; var str = [token,timestamp,nonce].sort().join(''); var sha = Sha1(str); if (sha === signature) { this.body = echostr + '' }else { this.body = 'wrong' } } }
写回答
1回答
-
web_zhang
提问者
2018-08-13
已解决,大写改小写
10
相似问题